xref: /aosp_15_r20/external/cronet/url/mojom/url_gurl_mojom_traits.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "url/mojom/url_gurl_mojom_traits.h"
6 
7 #include "url/url_constants.h"
8 
9 namespace mojo {
10 
11 // static
url(const GURL & r)12 std::string_view StructTraits<url::mojom::UrlDataView, GURL>::url(
13     const GURL& r) {
14   if (r.possibly_invalid_spec().length() > url::kMaxURLChars || !r.is_valid()) {
15     return std::string_view();
16   }
17 
18   return r.possibly_invalid_spec();
19 }
20 
21 // static
Read(url::mojom::UrlDataView data,GURL * out)22 bool StructTraits<url::mojom::UrlDataView, GURL>::Read(
23     url::mojom::UrlDataView data,
24     GURL* out) {
25   std::string_view url_string;
26   if (!data.ReadUrl(&url_string)) {
27     return false;
28   }
29 
30   if (url_string.length() > url::kMaxURLChars)
31     return false;
32 
33   *out = GURL(url_string);
34   if (!url_string.empty() && !out->is_valid())
35     return false;
36 
37   return true;
38 }
39 
40 }  // namespace mojo
41