OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef URL_MOJO_URL_GURL_STRUCT_TRAITS_H_ | 5 #ifndef URL_MOJO_URL_GURL_STRUCT_TRAITS_H_ |
6 #define URL_MOJO_URL_GURL_STRUCT_TRAITS_H_ | 6 #define URL_MOJO_URL_GURL_STRUCT_TRAITS_H_ |
7 | 7 |
8 #include "base/strings/string_piece.h" | 8 #include "base/strings/string_piece.h" |
9 #include "url/gurl.h" | 9 #include "url/gurl.h" |
10 #include "url/mojo/url.mojom.h" | 10 #include "url/mojo/url.mojom.h" |
11 #include "url/url_constants.h" | 11 #include "url/url_constants.h" |
12 | 12 |
13 namespace mojo { | 13 namespace mojo { |
14 | 14 |
15 template <> | 15 template <> |
16 struct StructTraits<url::mojom::Url, GURL> { | 16 struct StructTraits<url::mojom::UrlDataView, GURL> { |
17 static base::StringPiece url(const GURL& r) { | 17 static base::StringPiece url(const GURL& r) { |
18 if (r.possibly_invalid_spec().length() > url::kMaxURLChars || | 18 if (r.possibly_invalid_spec().length() > url::kMaxURLChars || |
19 !r.is_valid()) { | 19 !r.is_valid()) { |
20 return base::StringPiece(); | 20 return base::StringPiece(); |
21 } | 21 } |
22 | 22 |
23 return base::StringPiece(r.possibly_invalid_spec().c_str(), | 23 return base::StringPiece(r.possibly_invalid_spec().c_str(), |
24 r.possibly_invalid_spec().length()); | 24 r.possibly_invalid_spec().length()); |
25 } | 25 } |
26 static bool Read(url::mojom::UrlDataView data, GURL* out) { | 26 static bool Read(url::mojom::UrlDataView data, GURL* out) { |
27 base::StringPiece url_string; | 27 base::StringPiece url_string; |
28 if (!data.ReadUrl(&url_string)) | 28 if (!data.ReadUrl(&url_string)) |
29 return false; | 29 return false; |
30 | 30 |
31 if (url_string.length() > url::kMaxURLChars) | 31 if (url_string.length() > url::kMaxURLChars) |
32 return false; | 32 return false; |
33 | 33 |
34 *out = GURL(url_string); | 34 *out = GURL(url_string); |
35 if (!url_string.empty() && !out->is_valid()) | 35 if (!url_string.empty() && !out->is_valid()) |
36 return false; | 36 return false; |
37 | 37 |
38 return true; | 38 return true; |
39 } | 39 } |
40 }; | 40 }; |
41 | 41 |
42 } | 42 } |
43 | 43 |
44 #endif // URL_MOJO_URL_GURL_STRUCT_TRAITS_H_ | 44 #endif // URL_MOJO_URL_GURL_STRUCT_TRAITS_H_ |
OLD | NEW |