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