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 bool allowed_everywhere = false; | |
Devlin
2015/04/15 20:58:05
Might be worth making a note here that we default
Xi Han
2015/04/16 15:12:22
Added.
| |
96 if (extension && | |
97 extensions::PermissionsData::CanExecuteScriptEverywhere(extension)) | |
98 allowed_everywhere = true; | |
99 | |
100 for (const std::string& match : matches) { | |
101 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere)); | |
102 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) { | |
103 *error = errors::kInvalidMatches; | |
104 return false; | |
105 } | |
106 script->add_url_pattern(pattern); | |
107 } | |
108 | |
109 // exclude_matches: | |
110 if (script_value.exclude_matches) { | |
111 const std::vector<std::string>& exclude_matches = | |
112 *(script_value.exclude_matches.get()); | |
113 for (const std::string& exclude_match : exclude_matches) { | |
114 URLPattern pattern( | |
115 UserScript::ValidUserScriptSchemes(allowed_everywhere)); | |
116 | |
117 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) { | |
118 *error = errors::kInvalidExcludeMatches; | |
119 return false; | |
120 } | |
121 script->add_exclude_url_pattern(pattern); | |
122 } | |
123 } | |
124 // run_at: | |
125 if (script_value.run_at) { | |
126 UserScript::RunLocation run_at = UserScript::UNDEFINED; | |
127 switch (script_value.run_at) { | |
128 case extensions::core_api::extension_types::RUN_AT_NONE: | |
129 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_IDLE: | |
130 run_at = UserScript::DOCUMENT_IDLE; | |
131 break; | |
132 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_START: | |
133 run_at = UserScript::DOCUMENT_START; | |
134 break; | |
135 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_END: | |
136 run_at = UserScript::DOCUMENT_END; | |
137 break; | |
138 } | |
139 script->set_run_location(run_at); | |
140 } | |
141 | |
142 // match_about_blank: | |
143 if (script_value.match_about_blank) | |
144 script->set_match_about_blank(*script_value.match_about_blank); | |
145 | |
146 // css: | |
147 if (script_value.css) { | |
148 for (const std::string& relative : *(script_value.css.get())) { | |
149 GURL url = owner_base_url.Resolve(relative); | |
150 if (extension) { | |
151 ExtensionResource resource = extension->GetResource(relative); | |
152 script->css_scripts().push_back(UserScript::File( | |
153 resource.extension_root(), resource.relative_path(), url)); | |
154 } else { | |
155 script->css_scripts().push_back(extensions::UserScript::File( | |
156 base::FilePath(), base::FilePath(), url)); | |
157 } | |
158 } | |
159 } | |
160 | |
161 // js: | |
162 if (script_value.js) { | |
163 for (const std::string& relative : *(script_value.js.get())) { | |
164 GURL url = owner_base_url.Resolve(relative); | |
165 if (extension) { | |
166 ExtensionResource resource = extension->GetResource(relative); | |
167 script->js_scripts().push_back(UserScript::File( | |
168 resource.extension_root(), resource.relative_path(), url)); | |
169 } else { | |
170 script->js_scripts().push_back(extensions::UserScript::File( | |
171 base::FilePath(), base::FilePath(), url)); | |
172 } | |
173 } | |
174 } | |
175 | |
176 // all_frames: | |
177 if (script_value.all_frames) | |
178 script->set_match_all_frames(*(script_value.all_frames)); | |
179 | |
180 // include_globs: | |
181 if (script_value.include_globs) { | |
182 for (const std::string& glob : *(script_value.include_globs.get())) | |
183 script->add_glob(glob); | |
184 } | |
185 | |
186 // exclude_globs: | |
187 if (script_value.exclude_globs) { | |
188 for (const std::string& glob : *(script_value.exclude_globs.get())) | |
189 script->add_exclude_glob(glob); | |
190 } | |
191 | |
192 return true; | |
193 } | |
194 | |
195 bool ParseContentScripts( | |
196 std::vector<linked_ptr<ContentScriptDetails>> content_script_list, | |
197 const extensions::Extension* extension, | |
198 const GURL& owner_base_url, | |
199 std::map<std::string, UserScript>* result, | |
200 std::string* error) { | |
201 if (content_script_list.empty()) | |
202 return false; | |
203 for (const linked_ptr<ContentScriptDetails> script_value : | |
204 content_script_list) { | |
205 const std::string& name = script_value->name; | |
206 UserScript script; | |
207 if (!ParseContentScript(*script_value, extension, owner_base_url, &script, | |
208 error)) | |
209 return false; | |
210 script.set_id(UserScript::GenerateUserScriptID()); | |
211 script.set_name(name); | |
212 (*result)[name] = script; | |
213 } | |
214 return true; | |
215 } | |
216 | |
59 } // namespace | 217 } // namespace |
60 | 218 |
61 namespace extensions { | 219 namespace extensions { |
62 | 220 |
63 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. | 221 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. |
64 // Each WebUIURLFetcher is associated with a given |render_process_id, | 222 // Each WebUIURLFetcher is associated with a given |render_process_id, |
65 // render_view_id| pair. | 223 // render_view_id| pair. |
66 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | 224 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher |
67 : public net::URLFetcherDelegate { | 225 : public net::URLFetcherDelegate { |
68 public: | 226 public: |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
249 error, on_url, result); | 407 error, on_url, result); |
250 } | 408 } |
251 | 409 |
252 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { | 410 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { |
253 } | 411 } |
254 | 412 |
255 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { | 413 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { |
256 return true; | 414 return true; |
257 } | 415 } |
258 | 416 |
417 WebViewInternalAddContentScriptsFunction:: | |
418 WebViewInternalAddContentScriptsFunction() { | |
419 } | |
420 | |
421 WebViewInternalAddContentScriptsFunction:: | |
422 ~WebViewInternalAddContentScriptsFunction() { | |
423 } | |
424 | |
425 ExecuteCodeFunction::ResponseAction | |
426 WebViewInternalAddContentScriptsFunction::Run() { | |
427 scoped_ptr<web_view_internal::AddContentScripts::Params> params( | |
428 web_view_internal::AddContentScripts::Params::Create(*args_)); | |
429 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
430 | |
431 int view_instance_id = params->instance_id; | |
432 if (!view_instance_id) | |
433 return RespondNow(Error(kViewInstanceIdError)); | |
434 | |
435 GURL owner_base_url( | |
436 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath()); | |
437 std::map<std::string, UserScript> scripts; | |
438 std::set<UserScript> result; | |
439 | |
440 if (!ParseContentScripts(params->content_script_list, extension(), | |
441 owner_base_url, &scripts, &error_)) | |
442 return RespondNow(Error(error_)); | |
443 | |
444 WebViewContentScriptManager* manager = | |
445 WebViewContentScriptManager::Get(browser_context()); | |
446 DCHECK(manager); | |
447 content::WebContents* sender_web_contents = GetSenderWebContents(); | |
448 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents); | |
449 | |
450 for (const auto& iter : scripts) { | |
451 UserScript script = iter.second; | |
452 script.set_incognito_enabled(browser_context()->IsOffTheRecord()); | |
453 script.set_host_id( | |
454 GenerateHostIDFromEmbedder(extension(), sender_web_contents)); | |
455 script.set_consumer_instance_type(UserScript::WEBVIEW); | |
456 result.insert(script); | |
457 } | |
458 | |
459 manager->AddContentScripts(sender_web_contents, view_instance_id, host_id, | |
460 result); | |
461 | |
462 return RespondNow(NoArguments()); | |
463 } | |
464 | |
465 WebViewInternalRemoveContentScriptsFunction:: | |
466 WebViewInternalRemoveContentScriptsFunction() { | |
467 } | |
468 | |
469 WebViewInternalRemoveContentScriptsFunction:: | |
470 ~WebViewInternalRemoveContentScriptsFunction() { | |
471 } | |
472 | |
473 ExecuteCodeFunction::ResponseAction | |
474 WebViewInternalRemoveContentScriptsFunction::Run() { | |
475 scoped_ptr<web_view_internal::RemoveContentScripts::Params> params( | |
476 web_view_internal::RemoveContentScripts::Params::Create(*args_)); | |
477 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
478 | |
479 int view_instance_id = params->instance_id; | |
480 if (!view_instance_id) | |
481 return RespondNow(Error(kViewInstanceIdError)); | |
482 | |
483 WebViewContentScriptManager* manager = | |
484 WebViewContentScriptManager::Get(browser_context()); | |
485 DCHECK(manager); | |
486 | |
487 content::WebContents* sender_web_contents = GetSenderWebContents(); | |
488 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents); | |
489 | |
490 std::vector<std::string> script_name_list; | |
491 if (params->script_name_list) | |
492 script_name_list.swap(*params->script_name_list); | |
493 manager->RemoveContentScripts(GetSenderWebContents(), view_instance_id, | |
494 host_id, script_name_list); | |
495 return RespondNow(NoArguments()); | |
496 } | |
497 | |
259 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { | 498 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { |
260 } | 499 } |
261 | 500 |
262 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { | 501 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { |
263 } | 502 } |
264 | 503 |
265 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { | 504 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { |
266 scoped_ptr<web_view_internal::SetName::Params> params( | 505 scoped_ptr<web_view_internal::SetName::Params> params( |
267 web_view_internal::SetName::Params::Create(*args_)); | 506 web_view_internal::SetName::Params::Create(*args_)); |
268 EXTENSION_FUNCTION_VALIDATE(params.get()); | 507 EXTENSION_FUNCTION_VALIDATE(params.get()); |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
677 // Will finish asynchronously. | 916 // Will finish asynchronously. |
678 return true; | 917 return true; |
679 } | 918 } |
680 | 919 |
681 void WebViewInternalClearDataFunction::ClearDataDone() { | 920 void WebViewInternalClearDataFunction::ClearDataDone() { |
682 Release(); // Balanced in RunAsync(). | 921 Release(); // Balanced in RunAsync(). |
683 SendResponse(true); | 922 SendResponse(true); |
684 } | 923 } |
685 | 924 |
686 } // namespace extensions | 925 } // namespace extensions |
OLD | NEW |