OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 |
| 5 #include "ppapi/shared_impl/url_util_impl.h" |
| 6 |
| 7 #include "googleurl/src/gurl.h" |
| 8 |
| 9 namespace pp { |
| 10 namespace shared_impl { |
| 11 |
| 12 namespace { |
| 13 |
| 14 void ConvertComponent(const url_parse::Component& input, |
| 15 PP_URLComponent_Dev* output) { |
| 16 output->begin = input.begin; |
| 17 output->len = input.len; |
| 18 } |
| 19 |
| 20 // Converts components from a GoogleUrl parsed to a PPAPI parsed structure. |
| 21 // Output can be NULL to specify "do nothing." This rule is followed by all |
| 22 // the url util functions, so we implement it once here. |
| 23 // |
| 24 // Output can be NULL to specify "do nothing." This rule is followed by all the |
| 25 // url util functions, so we implement it once here. |
| 26 void ConvertComponents(const url_parse::Parsed& input, |
| 27 PP_URLComponents_Dev* output) { |
| 28 if (!output) |
| 29 return; |
| 30 |
| 31 ConvertComponent(input.scheme, &output->scheme); |
| 32 ConvertComponent(input.username, &output->username); |
| 33 ConvertComponent(input.password, &output->password); |
| 34 ConvertComponent(input.host, &output->host); |
| 35 ConvertComponent(input.port, &output->port); |
| 36 ConvertComponent(input.path, &output->path); |
| 37 ConvertComponent(input.query, &output->query); |
| 38 ConvertComponent(input.ref, &output->ref); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 // static |
| 44 PP_Var URLUtilImpl::Canonicalize(StringFromVar string_from_var, |
| 45 VarFromUtf8 var_from_utf8, |
| 46 PP_Module pp_module, |
| 47 PP_Var url, |
| 48 PP_URLComponents_Dev* components) { |
| 49 const std::string* url_string = string_from_var(url); |
| 50 if (!url_string) |
| 51 return PP_MakeNull(); |
| 52 return GenerateURLReturn(var_from_utf8, pp_module, |
| 53 GURL(*url_string), components); |
| 54 } |
| 55 |
| 56 // static |
| 57 PP_Var URLUtilImpl::ResolveRelativeToURL(StringFromVar string_from_var, |
| 58 VarFromUtf8 var_from_utf8, |
| 59 PP_Module pp_module, |
| 60 PP_Var base_url, |
| 61 PP_Var relative, |
| 62 PP_URLComponents_Dev* components) { |
| 63 const std::string* base_url_string = string_from_var(base_url); |
| 64 const std::string* relative_string = string_from_var(relative); |
| 65 if (!base_url_string || !relative_string) |
| 66 return PP_MakeNull(); |
| 67 |
| 68 GURL base_gurl(*base_url_string); |
| 69 if (!base_gurl.is_valid()) |
| 70 return PP_MakeNull(); |
| 71 return GenerateURLReturn(var_from_utf8, pp_module, |
| 72 base_gurl.Resolve(*relative_string), |
| 73 components); |
| 74 } |
| 75 |
| 76 // static |
| 77 PP_Bool URLUtilImpl::IsSameSecurityOrigin(StringFromVar string_from_var, |
| 78 PP_Var url_a, PP_Var url_b) { |
| 79 const std::string* url_a_string = string_from_var(url_a); |
| 80 const std::string* url_b_string = string_from_var(url_b); |
| 81 if (!url_a_string || !url_b_string) |
| 82 return PP_FALSE; |
| 83 |
| 84 GURL gurl_a(*url_a_string); |
| 85 GURL gurl_b(*url_b_string); |
| 86 if (!gurl_a.is_valid() || !gurl_b.is_valid()) |
| 87 return PP_FALSE; |
| 88 |
| 89 return gurl_a.GetOrigin() == gurl_b.GetOrigin() ? PP_TRUE : PP_FALSE; |
| 90 } |
| 91 |
| 92 // Used for returning the given GURL from a PPAPI function, with an optional |
| 93 // out param indicating the components. |
| 94 PP_Var URLUtilImpl::GenerateURLReturn(VarFromUtf8 var_from_utf8, |
| 95 PP_Module module, |
| 96 const GURL& url, |
| 97 PP_URLComponents_Dev* components) { |
| 98 if (!url.is_valid()) |
| 99 return PP_MakeNull(); |
| 100 ConvertComponents(url.parsed_for_possibly_invalid_spec(), components); |
| 101 return var_from_utf8(module, url.possibly_invalid_spec().c_str(), |
| 102 static_cast<uint32_t>(url.possibly_invalid_spec().size())); |
| 103 } |
| 104 |
| 105 } // namespace shared_impl |
| 106 } // namespace pp |
OLD | NEW |