url_pattern_set_mojom_traits.cc 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "extensions/common/mojom/url_pattern_set_mojom_traits.h"
  5. namespace mojo {
  6. bool StructTraits<extensions::mojom::URLPatternDataView, URLPattern>::Read(
  7. extensions::mojom::URLPatternDataView data,
  8. URLPattern* out) {
  9. std::string pattern;
  10. if (!data.ReadPattern(&pattern))
  11. return false;
  12. // TODO(jstritar): We don't want the URLPattern to fail parsing when the
  13. // scheme is invalid. Instead, the pattern should parse but it should not
  14. // match the invalid patterns. We get around this by setting the valid
  15. // schemes after parsing the pattern. Update these method calls once we can
  16. // ignore scheme validation with URLPattern parse options. crbug.com/90544
  17. out->SetValidSchemes(URLPattern::SCHEME_ALL);
  18. URLPattern::ParseResult result = out->Parse(pattern);
  19. out->SetValidSchemes(data.valid_schemes());
  20. return URLPattern::ParseResult::kSuccess == result;
  21. }
  22. bool StructTraits<extensions::mojom::URLPatternSetDataView,
  23. extensions::URLPatternSet>::
  24. Read(extensions::mojom::URLPatternSetDataView data,
  25. extensions::URLPatternSet* out) {
  26. std::vector<URLPattern> mojo_patterns;
  27. if (!data.ReadPatterns(&mojo_patterns))
  28. return false;
  29. for (const auto& pattern : mojo_patterns)
  30. out->AddPattern(pattern);
  31. if (mojo_patterns.size() != out->patterns().size())
  32. return false;
  33. return true;
  34. }
  35. } // namespace mojo