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/ppb_url_util_shared.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 PPB_URLUtil_Shared::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 PPB_URLUtil_Shared::ResolveRelativeToURL( | |
57 PP_Var base_url, | |
58 PP_Var relative, | |
59 PP_URLComponents_Dev* components) { | |
60 StringVar* base_url_string = StringVar::FromPPVar(base_url); | |
61 StringVar* relative_string = StringVar::FromPPVar(relative); | |
62 if (!base_url_string || !relative_string) | |
63 return PP_MakeNull(); | |
64 | |
65 GURL base_gurl(base_url_string->value()); | |
66 if (!base_gurl.is_valid()) | |
67 return PP_MakeNull(); | |
68 return GenerateURLReturn(base_url_string->pp_module(), | |
69 base_gurl.Resolve(relative_string->value()), | |
70 components); | |
71 } | |
72 | |
73 // static | |
74 PP_Bool PPB_URLUtil_Shared::IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) { | |
75 StringVar* url_a_string = StringVar::FromPPVar(url_a); | |
76 StringVar* url_b_string = StringVar::FromPPVar(url_b); | |
77 if (!url_a_string || !url_b_string) | |
78 return PP_FALSE; | |
79 | |
80 GURL gurl_a(url_a_string->value()); | |
81 GURL gurl_b(url_b_string->value()); | |
82 if (!gurl_a.is_valid() || !gurl_b.is_valid()) | |
83 return PP_FALSE; | |
84 | |
85 return gurl_a.GetOrigin() == gurl_b.GetOrigin() ? PP_TRUE : PP_FALSE; | |
86 } | |
87 | |
88 // Used for returning the given GURL from a PPAPI function, with an optional | |
89 // out param indicating the components. | |
90 PP_Var PPB_URLUtil_Shared::GenerateURLReturn( | |
91 PP_Module module, | |
92 const GURL& url, | |
93 PP_URLComponents_Dev* components) { | |
94 if (!url.is_valid()) | |
95 return PP_MakeNull(); | |
96 ConvertComponents(url.parsed_for_possibly_invalid_spec(), components); | |
97 return StringVar::StringToPPVar(module, url.possibly_invalid_spec()); | |
98 } | |
99 | |
100 PP_Var PPB_URLUtil_Shared::ConvertComponentsAndReturnURL( | |
101 const PP_Var& url, | |
102 PP_URLComponents_Dev* components) { | |
103 if (!components) | |
104 return url; // Common case - plugin doesn't care about parsing. | |
105 | |
106 StringVar* url_string = StringVar::FromPPVar(url); | |
107 if (!url_string) | |
108 return url; | |
109 | |
110 PP_Var result = Canonicalize(url, components); | |
111 PpapiGlobals::Get()->GetVarTracker()->ReleaseVar(url); | |
112 return result; | |
113 } | |
114 | |
115 } // namespace ppapi | |
OLD | NEW |