OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" | 5 #include "extensions/browser/api/guest_view/web_view/web_view_internal_api.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | |
7 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
8 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
9 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
10 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
11 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
12 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
13 #include "content/public/common/stop_find_action.h" | 14 #include "content/public/common/stop_find_action.h" |
14 #include "content/public/common/url_fetcher.h" | 15 #include "content/public/common/url_fetcher.h" |
15 #include "extensions/browser/guest_view/web_view/web_view_constants.h" | 16 #include "extensions/browser/guest_view/web_view/web_view_constants.h" |
17 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager .h" | |
16 #include "extensions/common/api/web_view_internal.h" | 18 #include "extensions/common/api/web_view_internal.h" |
17 #include "extensions/common/error_utils.h" | 19 #include "extensions/common/error_utils.h" |
20 #include "extensions/common/manifest_constants.h" | |
21 #include "extensions/common/permissions/permissions_data.h" | |
22 #include "extensions/common/user_script.h" | |
18 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
19 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
20 #include "net/url_request/url_fetcher_delegate.h" | 25 #include "net/url_request/url_fetcher_delegate.h" |
21 #include "third_party/WebKit/public/web/WebFindOptions.h" | 26 #include "third_party/WebKit/public/web/WebFindOptions.h" |
22 | 27 |
23 using content::WebContents; | 28 using content::WebContents; |
29 using extensions::ExtensionResource; | |
30 using extensions::core_api::web_view_internal::ContentScriptDetails; | |
24 using extensions::core_api::web_view_internal::SetPermission::Params; | 31 using extensions::core_api::web_view_internal::SetPermission::Params; |
25 using extensions::core_api::extension_types::InjectDetails; | 32 using extensions::core_api::extension_types::InjectDetails; |
33 using extensions::UserScript; | |
26 using ui_zoom::ZoomController; | 34 using ui_zoom::ZoomController; |
35 // error messages for content scripts: | |
36 namespace errors = extensions::manifest_errors; | |
27 namespace web_view_internal = extensions::core_api::web_view_internal; | 37 namespace web_view_internal = extensions::core_api::web_view_internal; |
28 | 38 |
29 namespace { | 39 namespace { |
30 | 40 |
31 const char kAppCacheKey[] = "appcache"; | 41 const char kAppCacheKey[] = "appcache"; |
32 const char kCacheKey[] = "cache"; | 42 const char kCacheKey[] = "cache"; |
33 const char kCookiesKey[] = "cookies"; | 43 const char kCookiesKey[] = "cookies"; |
34 const char kFileSystemsKey[] = "fileSystems"; | 44 const char kFileSystemsKey[] = "fileSystems"; |
35 const char kIndexedDBKey[] = "indexedDB"; | 45 const char kIndexedDBKey[] = "indexedDB"; |
36 const char kLocalStorageKey[] = "localStorage"; | 46 const char kLocalStorageKey[] = "localStorage"; |
37 const char kWebSQLKey[] = "webSQL"; | 47 const char kWebSQLKey[] = "webSQL"; |
38 const char kSinceKey[] = "since"; | 48 const char kSinceKey[] = "since"; |
39 const char kLoadFileError[] = "Failed to load file: \"*\". "; | 49 const char kLoadFileError[] = "Failed to load file: \"*\". "; |
50 const char kViewInstanceIdError[] = "view_instance_id is missing."; | |
40 | 51 |
41 uint32 MaskForKey(const char* key) { | 52 uint32 MaskForKey(const char* key) { |
42 if (strcmp(key, kAppCacheKey) == 0) | 53 if (strcmp(key, kAppCacheKey) == 0) |
43 return webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE; | 54 return webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE; |
44 if (strcmp(key, kCacheKey) == 0) | 55 if (strcmp(key, kCacheKey) == 0) |
45 return webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE; | 56 return webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE; |
46 if (strcmp(key, kCookiesKey) == 0) | 57 if (strcmp(key, kCookiesKey) == 0) |
47 return webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES; | 58 return webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES; |
48 if (strcmp(key, kFileSystemsKey) == 0) | 59 if (strcmp(key, kFileSystemsKey) == 0) |
49 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; | 60 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; |
50 if (strcmp(key, kIndexedDBKey) == 0) | 61 if (strcmp(key, kIndexedDBKey) == 0) |
51 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; | 62 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; |
52 if (strcmp(key, kLocalStorageKey) == 0) | 63 if (strcmp(key, kLocalStorageKey) == 0) |
53 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; | 64 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; |
54 if (strcmp(key, kWebSQLKey) == 0) | 65 if (strcmp(key, kWebSQLKey) == 0) |
55 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; | 66 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; |
56 return 0; | 67 return 0; |
57 } | 68 } |
58 | 69 |
70 HostID GenerateHostIDFromEmbedder(const extensions::Extension* extension, | |
71 const content::WebContents* web_contents) { | |
72 if (extension) | |
73 return HostID(HostID::EXTENSIONS, extension->id()); | |
74 | |
75 if (web_contents && web_contents->GetWebUI()) { | |
76 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL(); | |
77 return HostID(HostID::WEBUI, url.spec()); | |
78 } | |
79 NOTREACHED(); | |
80 return HostID(); | |
81 } | |
82 | |
83 // Parses the values stored in ContentScriptDetails, and constructs a | |
84 // UserScript. | |
85 bool ParseContentScript(const ContentScriptDetails& script_value, | |
86 const extensions::Extension* extension, | |
87 const GURL& owner_base_url, | |
88 UserScript* script, | |
89 std::string* error) { | |
90 // matches (required): | |
91 const std::vector<std::string>& matches = script_value.matches; | |
92 if (matches.size() == 0) | |
93 return false; | |
94 | |
95 // The defaul for WebUI is not having special access, but we can change that | |
Fady Samuel
2015/04/16 15:24:55
nit: default
Xi Han
2015/04/16 15:38:11
Done.
| |
96 // if needed. | |
97 bool allowed_everywhere = false; | |
98 if (extension && | |
99 extensions::PermissionsData::CanExecuteScriptEverywhere(extension)) | |
100 allowed_everywhere = true; | |
101 | |
102 for (const std::string& match : matches) { | |
103 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere)); | |
104 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) { | |
105 *error = errors::kInvalidMatches; | |
106 return false; | |
107 } | |
108 script->add_url_pattern(pattern); | |
109 } | |
110 | |
111 // exclude_matches: | |
112 if (script_value.exclude_matches) { | |
113 const std::vector<std::string>& exclude_matches = | |
114 *(script_value.exclude_matches.get()); | |
115 for (const std::string& exclude_match : exclude_matches) { | |
116 URLPattern pattern( | |
117 UserScript::ValidUserScriptSchemes(allowed_everywhere)); | |
118 | |
119 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) { | |
120 *error = errors::kInvalidExcludeMatches; | |
121 return false; | |
122 } | |
123 script->add_exclude_url_pattern(pattern); | |
124 } | |
125 } | |
126 // run_at: | |
127 if (script_value.run_at) { | |
128 UserScript::RunLocation run_at = UserScript::UNDEFINED; | |
129 switch (script_value.run_at) { | |
130 case extensions::core_api::extension_types::RUN_AT_NONE: | |
131 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_IDLE: | |
132 run_at = UserScript::DOCUMENT_IDLE; | |
133 break; | |
134 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_START: | |
135 run_at = UserScript::DOCUMENT_START; | |
136 break; | |
137 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_END: | |
138 run_at = UserScript::DOCUMENT_END; | |
139 break; | |
140 } | |
141 script->set_run_location(run_at); | |
142 } | |
143 | |
144 // match_about_blank: | |
145 if (script_value.match_about_blank) | |
146 script->set_match_about_blank(*script_value.match_about_blank); | |
147 | |
148 // css: | |
149 if (script_value.css) { | |
150 for (const std::string& relative : *(script_value.css.get())) { | |
151 GURL url = owner_base_url.Resolve(relative); | |
152 if (extension) { | |
153 ExtensionResource resource = extension->GetResource(relative); | |
154 script->css_scripts().push_back(UserScript::File( | |
155 resource.extension_root(), resource.relative_path(), url)); | |
156 } else { | |
157 script->css_scripts().push_back(extensions::UserScript::File( | |
158 base::FilePath(), base::FilePath(), url)); | |
159 } | |
160 } | |
161 } | |
162 | |
163 // js: | |
164 if (script_value.js) { | |
165 for (const std::string& relative : *(script_value.js.get())) { | |
166 GURL url = owner_base_url.Resolve(relative); | |
167 if (extension) { | |
168 ExtensionResource resource = extension->GetResource(relative); | |
169 script->js_scripts().push_back(UserScript::File( | |
170 resource.extension_root(), resource.relative_path(), url)); | |
171 } else { | |
172 script->js_scripts().push_back(extensions::UserScript::File( | |
173 base::FilePath(), base::FilePath(), url)); | |
174 } | |
175 } | |
176 } | |
177 | |
178 // all_frames: | |
179 if (script_value.all_frames) | |
180 script->set_match_all_frames(*(script_value.all_frames)); | |
181 | |
182 // include_globs: | |
183 if (script_value.include_globs) { | |
184 for (const std::string& glob : *(script_value.include_globs.get())) | |
185 script->add_glob(glob); | |
186 } | |
187 | |
188 // exclude_globs: | |
189 if (script_value.exclude_globs) { | |
190 for (const std::string& glob : *(script_value.exclude_globs.get())) | |
191 script->add_exclude_glob(glob); | |
192 } | |
193 | |
194 return true; | |
195 } | |
196 | |
197 bool ParseContentScripts( | |
198 std::vector<linked_ptr<ContentScriptDetails>> content_script_list, | |
199 const extensions::Extension* extension, | |
200 const HostID& host_id, | |
201 bool incognito_enabled, | |
202 const GURL& owner_base_url, | |
203 std::set<UserScript>* result, | |
204 std::string* error) { | |
205 if (content_script_list.empty()) | |
206 return false; | |
207 | |
208 std::set<std::string> names; | |
209 for (const linked_ptr<ContentScriptDetails> script_value : | |
210 content_script_list) { | |
211 const std::string& name = script_value->name; | |
212 if (names.find(name) != names.end()) | |
213 return false; | |
214 names.insert(name); | |
215 | |
216 UserScript script; | |
217 if (!ParseContentScript(*script_value, extension, owner_base_url, &script, | |
218 error)) | |
219 return false; | |
220 | |
221 script.set_id(UserScript::GenerateUserScriptID()); | |
222 script.set_name(name); | |
223 script.set_incognito_enabled(incognito_enabled); | |
224 script.set_host_id(host_id); | |
225 script.set_consumer_instance_type(UserScript::WEBVIEW); | |
226 result->insert(script); | |
227 } | |
228 return true; | |
229 } | |
230 | |
59 } // namespace | 231 } // namespace |
60 | 232 |
61 namespace extensions { | 233 namespace extensions { |
62 | 234 |
63 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. | 235 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. |
64 // Each WebUIURLFetcher is associated with a given |render_process_id, | 236 // Each WebUIURLFetcher is associated with a given |render_process_id, |
65 // render_view_id| pair. | 237 // render_view_id| pair. |
66 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | 238 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher |
67 : public net::URLFetcherDelegate { | 239 : public net::URLFetcherDelegate { |
68 public: | 240 public: |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 error, on_url, result); | 421 error, on_url, result); |
250 } | 422 } |
251 | 423 |
252 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { | 424 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { |
253 } | 425 } |
254 | 426 |
255 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { | 427 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { |
256 return true; | 428 return true; |
257 } | 429 } |
258 | 430 |
431 WebViewInternalAddContentScriptsFunction:: | |
432 WebViewInternalAddContentScriptsFunction() { | |
433 } | |
434 | |
435 WebViewInternalAddContentScriptsFunction:: | |
436 ~WebViewInternalAddContentScriptsFunction() { | |
437 } | |
438 | |
439 ExecuteCodeFunction::ResponseAction | |
440 WebViewInternalAddContentScriptsFunction::Run() { | |
441 scoped_ptr<web_view_internal::AddContentScripts::Params> params( | |
442 web_view_internal::AddContentScripts::Params::Create(*args_)); | |
443 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
444 | |
445 if (!params->instance_id) | |
446 return RespondNow(Error(kViewInstanceIdError)); | |
447 | |
448 GURL owner_base_url( | |
449 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath()); | |
450 std::set<UserScript> result; | |
451 | |
452 content::WebContents* sender_web_contents = GetSenderWebContents(); | |
453 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents); | |
454 bool incognito_enabled = browser_context()->IsOffTheRecord(); | |
455 | |
456 if (!ParseContentScripts(params->content_script_list, extension(), host_id, | |
457 incognito_enabled, owner_base_url, &result, &error_)) | |
458 return RespondNow(Error(error_)); | |
459 | |
460 WebViewContentScriptManager* manager = | |
461 WebViewContentScriptManager::Get(browser_context()); | |
462 DCHECK(manager); | |
463 | |
464 manager->AddContentScripts(sender_web_contents, params->instance_id, host_id, | |
465 result); | |
466 | |
467 return RespondNow(NoArguments()); | |
468 } | |
469 | |
470 WebViewInternalRemoveContentScriptsFunction:: | |
471 WebViewInternalRemoveContentScriptsFunction() { | |
472 } | |
473 | |
474 WebViewInternalRemoveContentScriptsFunction:: | |
475 ~WebViewInternalRemoveContentScriptsFunction() { | |
476 } | |
477 | |
478 ExecuteCodeFunction::ResponseAction | |
479 WebViewInternalRemoveContentScriptsFunction::Run() { | |
480 scoped_ptr<web_view_internal::RemoveContentScripts::Params> params( | |
481 web_view_internal::RemoveContentScripts::Params::Create(*args_)); | |
482 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
483 | |
484 if (!params->instance_id) | |
485 return RespondNow(Error(kViewInstanceIdError)); | |
486 | |
487 WebViewContentScriptManager* manager = | |
488 WebViewContentScriptManager::Get(browser_context()); | |
489 DCHECK(manager); | |
490 | |
491 content::WebContents* sender_web_contents = GetSenderWebContents(); | |
492 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents); | |
493 | |
494 std::vector<std::string> script_name_list; | |
495 if (params->script_name_list) | |
496 script_name_list.swap(*params->script_name_list); | |
497 manager->RemoveContentScripts(GetSenderWebContents(), params->instance_id, | |
498 host_id, script_name_list); | |
499 return RespondNow(NoArguments()); | |
500 } | |
501 | |
259 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { | 502 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { |
260 } | 503 } |
261 | 504 |
262 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { | 505 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { |
263 } | 506 } |
264 | 507 |
265 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { | 508 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { |
266 scoped_ptr<web_view_internal::SetName::Params> params( | 509 scoped_ptr<web_view_internal::SetName::Params> params( |
267 web_view_internal::SetName::Params::Create(*args_)); | 510 web_view_internal::SetName::Params::Create(*args_)); |
268 EXTENSION_FUNCTION_VALIDATE(params.get()); | 511 EXTENSION_FUNCTION_VALIDATE(params.get()); |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
677 // Will finish asynchronously. | 920 // Will finish asynchronously. |
678 return true; | 921 return true; |
679 } | 922 } |
680 | 923 |
681 void WebViewInternalClearDataFunction::ClearDataDone() { | 924 void WebViewInternalClearDataFunction::ClearDataDone() { |
682 Release(); // Balanced in RunAsync(). | 925 Release(); // Balanced in RunAsync(). |
683 SendResponse(true); | 926 SendResponse(true); |
684 } | 927 } |
685 | 928 |
686 } // namespace extensions | 929 } // namespace extensions |
OLD | NEW |