OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 #include "webkit/plugins/ppapi/ppb_url_util_impl.h" | 5 #include "webkit/plugins/ppapi/ppb_url_util_impl.h" |
6 | 6 |
7 #include "googleurl/src/gurl.h" | 7 #include "googleurl/src/gurl.h" |
8 #include "ppapi/c/dev/ppb_url_util_dev.h" | 8 #include "ppapi/c/dev/ppb_url_util_dev.h" |
| 9 #include "ppapi/c/ppb_var.h" |
| 10 #include "ppapi/shared_impl/url_util_impl.h" |
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" | 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" |
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" | 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebElement.h" |
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" | 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" |
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" | 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebNode.h" |
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" | 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebPluginContainer.h" |
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" | 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityOrigin.h" |
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" | 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURL.h" |
16 #include "webkit/plugins/ppapi/common.h" | 18 #include "webkit/plugins/ppapi/common.h" |
| 19 #include "webkit/plugins/ppapi/plugin_module.h" |
17 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" | 20 #include "webkit/plugins/ppapi/ppapi_plugin_instance.h" |
18 #include "webkit/plugins/ppapi/resource_tracker.h" | 21 #include "webkit/plugins/ppapi/resource_tracker.h" |
19 #include "webkit/plugins/ppapi/string.h" | 22 #include "webkit/plugins/ppapi/string.h" |
20 #include "webkit/plugins/ppapi/var.h" | 23 #include "webkit/plugins/ppapi/var.h" |
21 | 24 |
22 namespace webkit { | 25 namespace webkit { |
23 namespace ppapi { | 26 namespace ppapi { |
24 | 27 |
| 28 using pp::shared_impl::URLUtilImpl; |
| 29 |
25 namespace { | 30 namespace { |
26 | 31 |
27 void ConvertComponent(const url_parse::Component& input, | 32 // Returns the PP_Module associated with the given string, or 0 on failure. |
28 PP_URLComponent_Dev* output) { | 33 PP_Module GetModuleFromVar(PP_Var string_var) { |
29 output->begin = input.begin; | 34 scoped_refptr<StringVar> str(StringVar::FromPPVar(string_var)); |
30 output->len = input.len; | 35 if (!str) |
| 36 return 0; |
| 37 return str->module()->pp_module(); |
31 } | 38 } |
32 | 39 |
33 // Output can be NULL to specify "do nothing." This rule is followed by all the | 40 const std::string* StringFromVar(PP_Var var) { |
34 // url util functions, so we implement it once here. | 41 scoped_refptr<StringVar> string(StringVar::FromPPVar(var)); |
35 void ConvertComponents(const url_parse::Parsed& input, | 42 if (!string) |
36 PP_URLComponents_Dev* output) { | 43 return NULL; |
37 if (!output) | 44 return &string->value(); |
38 return; | |
39 | |
40 ConvertComponent(input.scheme, &output->scheme); | |
41 ConvertComponent(input.username, &output->username); | |
42 ConvertComponent(input.password, &output->password); | |
43 ConvertComponent(input.host, &output->host); | |
44 ConvertComponent(input.port, &output->port); | |
45 ConvertComponent(input.path, &output->path); | |
46 ConvertComponent(input.query, &output->query); | |
47 ConvertComponent(input.ref, &output->ref); | |
48 } | |
49 | |
50 // Used for returning the given GURL from a PPAPI function, with an optional | |
51 // out param indicating the components. | |
52 PP_Var GenerateURLReturn(PluginModule* module, const GURL& url, | |
53 PP_URLComponents_Dev* components) { | |
54 if (!url.is_valid()) | |
55 return PP_MakeNull(); | |
56 ConvertComponents(url.parsed_for_possibly_invalid_spec(), components); | |
57 return StringVar::StringToPPVar(module, url.possibly_invalid_spec()); | |
58 } | 45 } |
59 | 46 |
60 // Sets |*security_origin| to be the WebKit security origin associated with the | 47 // Sets |*security_origin| to be the WebKit security origin associated with the |
61 // document containing the given plugin instance. On success, returns true. If | 48 // document containing the given plugin instance. On success, returns true. If |
62 // the instance is invalid, returns false and |*security_origin| will be | 49 // the instance is invalid, returns false and |*security_origin| will be |
63 // unchanged. | 50 // unchanged. |
64 bool SecurityOriginForInstance(PP_Instance instance_id, | 51 bool SecurityOriginForInstance(PP_Instance instance_id, |
65 WebKit::WebSecurityOrigin* security_origin) { | 52 WebKit::WebSecurityOrigin* security_origin) { |
66 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | 53 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
67 if (!instance) | 54 if (!instance) |
68 return false; | 55 return false; |
69 | 56 |
70 WebKit::WebElement plugin_element = instance->container()->element(); | 57 WebKit::WebElement plugin_element = instance->container()->element(); |
71 WebKit::WebFrame* plugin_frame = plugin_element.document().frame(); | 58 WebKit::WebFrame* plugin_frame = plugin_element.document().frame(); |
72 if (!plugin_frame) | 59 if (!plugin_frame) |
73 return false; | 60 return false; |
74 | 61 |
75 *security_origin = plugin_frame->securityOrigin(); | 62 *security_origin = plugin_frame->securityOrigin(); |
76 return true; | 63 return true; |
77 } | 64 } |
78 | 65 |
79 PP_Var Canonicalize(PP_Var url, PP_URLComponents_Dev* components) { | 66 PP_Var Canonicalize(PP_Var url, PP_URLComponents_Dev* components) { |
80 scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url)); | 67 return URLUtilImpl::Canonicalize(&StringFromVar, |
81 if (!url_string) | 68 Var::GetInterface()->VarFromUtf8, |
82 return PP_MakeNull(); | 69 GetModuleFromVar(url), |
83 return GenerateURLReturn(url_string->module(), | 70 url, components); |
84 GURL(url_string->value()), components); | |
85 } | 71 } |
86 | 72 |
87 PP_Var ResolveRelativeToURL(PP_Var base_url, | 73 PP_Var ResolveRelativeToURL(PP_Var base_url, |
88 PP_Var relative, | 74 PP_Var relative, |
89 PP_URLComponents_Dev* components) { | 75 PP_URLComponents_Dev* components) { |
90 scoped_refptr<StringVar> base_url_string(StringVar::FromPPVar(base_url)); | 76 return URLUtilImpl::ResolveRelativeToURL(&StringFromVar, |
91 scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative)); | 77 Var::GetInterface()->VarFromUtf8, |
92 if (!base_url_string || !relative_string) | 78 GetModuleFromVar(base_url), |
93 return PP_MakeNull(); | 79 base_url, relative, components); |
94 | |
95 GURL base_gurl(base_url_string->value()); | |
96 if (!base_gurl.is_valid()) | |
97 return PP_MakeNull(); | |
98 return GenerateURLReturn(base_url_string->module(), | |
99 base_gurl.Resolve(relative_string->value()), | |
100 components); | |
101 } | 80 } |
102 | 81 |
103 PP_Var ResolveRelativeToDocument(PP_Instance instance_id, | 82 PP_Var ResolveRelativeToDocument(PP_Instance instance_id, |
104 PP_Var relative, | 83 PP_Var relative, |
105 PP_URLComponents_Dev* components) { | 84 PP_URLComponents_Dev* components) { |
106 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | 85 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
107 if (!instance) | 86 if (!instance) |
108 return PP_MakeNull(); | 87 return PP_MakeNull(); |
109 | 88 |
110 scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative)); | 89 scoped_refptr<StringVar> relative_string(StringVar::FromPPVar(relative)); |
111 if (!relative_string) | 90 if (!relative_string) |
112 return PP_MakeNull(); | 91 return PP_MakeNull(); |
113 | 92 |
114 WebKit::WebElement plugin_element = instance->container()->element(); | 93 WebKit::WebElement plugin_element = instance->container()->element(); |
115 GURL document_url = plugin_element.document().baseURL(); | 94 GURL document_url = plugin_element.document().baseURL(); |
116 return GenerateURLReturn(instance->module(), | 95 return URLUtilImpl::GenerateURLReturn( |
117 document_url.Resolve(relative_string->value()), | 96 Var::GetInterface()->VarFromUtf8, |
118 components); | 97 instance->module()->pp_module(), |
| 98 document_url.Resolve(relative_string->value()), |
| 99 components); |
119 } | 100 } |
120 | 101 |
121 PP_Bool IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) { | 102 PP_Bool IsSameSecurityOrigin(PP_Var url_a, PP_Var url_b) { |
122 scoped_refptr<StringVar> url_a_string(StringVar::FromPPVar(url_a)); | 103 return URLUtilImpl::IsSameSecurityOrigin(&StringFromVar, url_a, url_b); |
123 scoped_refptr<StringVar> url_b_string(StringVar::FromPPVar(url_b)); | |
124 if (!url_a_string || !url_b_string) | |
125 return PP_FALSE; | |
126 | |
127 GURL gurl_a(url_a_string->value()); | |
128 GURL gurl_b(url_b_string->value()); | |
129 if (!gurl_a.is_valid() || !gurl_b.is_valid()) | |
130 return PP_FALSE; | |
131 | |
132 return BoolToPPBool(gurl_a.GetOrigin() == gurl_b.GetOrigin()); | |
133 } | 104 } |
134 | 105 |
135 PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) { | 106 PP_Bool DocumentCanRequest(PP_Instance instance, PP_Var url) { |
136 scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url)); | 107 scoped_refptr<StringVar> url_string(StringVar::FromPPVar(url)); |
137 if (!url_string) | 108 if (!url_string) |
138 return PP_FALSE; | 109 return PP_FALSE; |
139 | 110 |
140 WebKit::WebSecurityOrigin security_origin; | 111 WebKit::WebSecurityOrigin security_origin; |
141 if (!SecurityOriginForInstance(instance, &security_origin)) | 112 if (!SecurityOriginForInstance(instance, &security_origin)) |
142 return PP_FALSE; | 113 return PP_FALSE; |
(...skipping 20 matching lines...) Expand all Loading... |
163 PP_Var GetDocumentURL(PP_Instance instance_id, | 134 PP_Var GetDocumentURL(PP_Instance instance_id, |
164 PP_URLComponents_Dev* components) { | 135 PP_URLComponents_Dev* components) { |
165 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); | 136 PluginInstance* instance = ResourceTracker::Get()->GetInstance(instance_id); |
166 if (!instance) | 137 if (!instance) |
167 return PP_MakeNull(); | 138 return PP_MakeNull(); |
168 | 139 |
169 WebKit::WebFrame* frame = instance->container()->element().document().frame(); | 140 WebKit::WebFrame* frame = instance->container()->element().document().frame(); |
170 if (!frame) | 141 if (!frame) |
171 return PP_MakeNull(); | 142 return PP_MakeNull(); |
172 | 143 |
173 return GenerateURLReturn(instance->module(), frame->url(), components); | 144 return URLUtilImpl::GenerateURLReturn(Var::GetInterface()->VarFromUtf8, |
| 145 instance->module()->pp_module(), |
| 146 frame->url(), components); |
174 } | 147 } |
175 | 148 |
176 const PPB_URLUtil_Dev ppb_url_util = { | 149 const PPB_URLUtil_Dev ppb_url_util = { |
177 &Canonicalize, | 150 &Canonicalize, |
178 &ResolveRelativeToURL, | 151 &ResolveRelativeToURL, |
179 &ResolveRelativeToDocument, | 152 &ResolveRelativeToDocument, |
180 &IsSameSecurityOrigin, | 153 &IsSameSecurityOrigin, |
181 &DocumentCanRequest, | 154 &DocumentCanRequest, |
182 &DocumentCanAccessDocument, | 155 &DocumentCanAccessDocument, |
183 &GetDocumentURL | 156 &GetDocumentURL |
184 }; | 157 }; |
185 | 158 |
186 } // namespace | 159 } // namespace |
187 | 160 |
188 // static | 161 // static |
189 const PPB_URLUtil_Dev* PPB_URLUtil_Impl::GetInterface() { | 162 const PPB_URLUtil_Dev* PPB_URLUtil_Impl::GetInterface() { |
190 return &ppb_url_util; | 163 return &ppb_url_util; |
191 } | 164 } |
192 | 165 |
193 } // namespace ppapi | 166 } // namespace ppapi |
194 } // namespace webkit | 167 } // namespace webkit |
195 | 168 |
OLD | NEW |