Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(67)

Side by Side Diff: extensions/browser/api/guest_view/web_view/web_view_internal_api.cc

Issue 959413003: Implement <webview>.addContentScript/removeContentScript API [1] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.";
51 const char kDuplicatedContentScriptNamesError[] =
52 "The given content script name already exists.";
40 53
41 uint32 MaskForKey(const char* key) { 54 uint32 MaskForKey(const char* key) {
42 if (strcmp(key, kAppCacheKey) == 0) 55 if (strcmp(key, kAppCacheKey) == 0)
43 return webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE; 56 return webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE;
44 if (strcmp(key, kCacheKey) == 0) 57 if (strcmp(key, kCacheKey) == 0)
45 return webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE; 58 return webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE;
46 if (strcmp(key, kCookiesKey) == 0) 59 if (strcmp(key, kCookiesKey) == 0)
47 return webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES; 60 return webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES;
48 if (strcmp(key, kFileSystemsKey) == 0) 61 if (strcmp(key, kFileSystemsKey) == 0)
49 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; 62 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS;
50 if (strcmp(key, kIndexedDBKey) == 0) 63 if (strcmp(key, kIndexedDBKey) == 0)
51 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; 64 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB;
52 if (strcmp(key, kLocalStorageKey) == 0) 65 if (strcmp(key, kLocalStorageKey) == 0)
53 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; 66 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE;
54 if (strcmp(key, kWebSQLKey) == 0) 67 if (strcmp(key, kWebSQLKey) == 0)
55 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; 68 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL;
56 return 0; 69 return 0;
57 } 70 }
58 71
72 HostID GenerateHostIDFromEmbedder(const extensions::Extension* extension,
73 const content::WebContents* web_contents) {
74 if (extension)
75 return HostID(HostID::EXTENSIONS, extension->id());
76
77 if (web_contents && web_contents->GetWebUI()) {
78 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL();
79 return HostID(HostID::WEBUI, url.spec());
80 }
81 NOTREACHED();
82 return HostID();
83 }
84
85 // Parses the values stored in ContentScriptDetails, and constructs a
86 // UserScript.
87 bool ParseContentScript(const ContentScriptDetails& script_value,
88 const extensions::Extension* extension,
89 const GURL& owner_base_url,
90 UserScript* script,
91 std::string* error) {
92 // matches (required):
93 if (script_value.matches.empty())
94 return false;
95
96 // The default for WebUI is not having special access, but we can change that
97 // if needed.
98 bool allowed_everywhere = false;
99 if (extension &&
100 extensions::PermissionsData::CanExecuteScriptEverywhere(extension))
101 allowed_everywhere = true;
102
103 for (const std::string& match : script_value.matches) {
104 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere));
105 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) {
106 *error = errors::kInvalidMatches;
107 return false;
108 }
109 script->add_url_pattern(pattern);
110 }
111
112 // exclude_matches:
113 if (script_value.exclude_matches) {
114 const std::vector<std::string>& exclude_matches =
115 *(script_value.exclude_matches.get());
116 for (const std::string& exclude_match : exclude_matches) {
117 URLPattern pattern(
118 UserScript::ValidUserScriptSchemes(allowed_everywhere));
119
120 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) {
121 *error = errors::kInvalidExcludeMatches;
122 return false;
123 }
124 script->add_exclude_url_pattern(pattern);
125 }
126 }
127 // run_at:
128 if (script_value.run_at) {
129 UserScript::RunLocation run_at = UserScript::UNDEFINED;
130 switch (script_value.run_at) {
131 case extensions::core_api::extension_types::RUN_AT_NONE:
132 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_IDLE:
133 run_at = UserScript::DOCUMENT_IDLE;
134 break;
135 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_START:
136 run_at = UserScript::DOCUMENT_START;
137 break;
138 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_END:
139 run_at = UserScript::DOCUMENT_END;
140 break;
141 }
142 // The default for run_at is RUN_AT_DOCUMENT_IDLE.
143 script->set_run_location(run_at);
144 }
145
146 // match_about_blank:
147 if (script_value.match_about_blank)
148 script->set_match_about_blank(*script_value.match_about_blank);
149
150 // css:
151 if (script_value.css) {
152 for (const std::string& relative : *(script_value.css.get())) {
153 GURL url = owner_base_url.Resolve(relative);
154 if (extension) {
155 ExtensionResource resource = extension->GetResource(relative);
156 script->css_scripts().push_back(UserScript::File(
157 resource.extension_root(), resource.relative_path(), url));
158 } else {
159 script->css_scripts().push_back(extensions::UserScript::File(
160 base::FilePath(), base::FilePath(), url));
161 }
162 }
163 }
164
165 // js:
166 if (script_value.js) {
167 for (const std::string& relative : *(script_value.js.get())) {
168 GURL url = owner_base_url.Resolve(relative);
169 if (extension) {
170 ExtensionResource resource = extension->GetResource(relative);
171 script->js_scripts().push_back(UserScript::File(
172 resource.extension_root(), resource.relative_path(), url));
173 } else {
174 script->js_scripts().push_back(extensions::UserScript::File(
175 base::FilePath(), base::FilePath(), url));
176 }
177 }
178 }
179
180 // all_frames:
181 if (script_value.all_frames)
182 script->set_match_all_frames(*(script_value.all_frames));
183
184 // include_globs:
185 if (script_value.include_globs) {
186 for (const std::string& glob : *(script_value.include_globs.get()))
187 script->add_glob(glob);
188 }
189
190 // exclude_globs:
191 if (script_value.exclude_globs) {
192 for (const std::string& glob : *(script_value.exclude_globs.get()))
193 script->add_exclude_glob(glob);
194 }
195
196 return true;
197 }
198
199 bool ParseContentScripts(
200 std::vector<linked_ptr<ContentScriptDetails>> content_script_list,
201 const extensions::Extension* extension,
202 const HostID& host_id,
203 bool incognito_enabled,
204 const GURL& owner_base_url,
205 std::set<UserScript>* result,
206 std::string* error) {
207 if (content_script_list.empty())
208 return false;
209
210 std::set<std::string> names;
211 for (const linked_ptr<ContentScriptDetails> script_value :
212 content_script_list) {
213 const std::string& name = script_value->name;
214 if (!names.insert(name).second) {
215 // The name was already in the list.
216 *error = kDuplicatedContentScriptNamesError;
217 return false;
218 }
219
220 UserScript script;
221 if (!ParseContentScript(*script_value, extension, owner_base_url, &script,
222 error))
223 return false;
224
225 script.set_id(UserScript::GenerateUserScriptID());
226 script.set_name(name);
227 script.set_incognito_enabled(incognito_enabled);
228 script.set_host_id(host_id);
229 script.set_consumer_instance_type(UserScript::WEBVIEW);
230 result->insert(script);
231 }
232 return true;
233 }
234
59 } // namespace 235 } // namespace
60 236
61 namespace extensions { 237 namespace extensions {
62 238
63 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. 239 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
64 // Each WebUIURLFetcher is associated with a given |render_process_id, 240 // Each WebUIURLFetcher is associated with a given |render_process_id,
65 // render_view_id| pair. 241 // render_view_id| pair.
66 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher 242 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher
67 : public net::URLFetcherDelegate { 243 : public net::URLFetcherDelegate {
68 public: 244 public:
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 error, on_url, result); 425 error, on_url, result);
250 } 426 }
251 427
252 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { 428 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() {
253 } 429 }
254 430
255 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { 431 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const {
256 return true; 432 return true;
257 } 433 }
258 434
435 WebViewInternalAddContentScriptsFunction::
436 WebViewInternalAddContentScriptsFunction() {
437 }
438
439 WebViewInternalAddContentScriptsFunction::
440 ~WebViewInternalAddContentScriptsFunction() {
441 }
442
443 ExecuteCodeFunction::ResponseAction
444 WebViewInternalAddContentScriptsFunction::Run() {
445 scoped_ptr<web_view_internal::AddContentScripts::Params> params(
446 web_view_internal::AddContentScripts::Params::Create(*args_));
447 EXTENSION_FUNCTION_VALIDATE(params.get());
448
449 if (!params->instance_id)
450 return RespondNow(Error(kViewInstanceIdError));
451
452 GURL owner_base_url(
453 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath());
454 std::set<UserScript> result;
455
456 content::WebContents* sender_web_contents = GetSenderWebContents();
457 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents);
458 bool incognito_enabled = browser_context()->IsOffTheRecord();
459
460 if (!ParseContentScripts(params->content_script_list, extension(), host_id,
461 incognito_enabled, owner_base_url, &result, &error_))
462 return RespondNow(Error(error_));
463
464 WebViewContentScriptManager* manager =
465 WebViewContentScriptManager::Get(browser_context());
466 DCHECK(manager);
467
468 manager->AddContentScripts(sender_web_contents, params->instance_id, host_id,
469 result);
470
471 return RespondNow(NoArguments());
472 }
473
474 WebViewInternalRemoveContentScriptsFunction::
475 WebViewInternalRemoveContentScriptsFunction() {
476 }
477
478 WebViewInternalRemoveContentScriptsFunction::
479 ~WebViewInternalRemoveContentScriptsFunction() {
480 }
481
482 ExecuteCodeFunction::ResponseAction
483 WebViewInternalRemoveContentScriptsFunction::Run() {
484 scoped_ptr<web_view_internal::RemoveContentScripts::Params> params(
485 web_view_internal::RemoveContentScripts::Params::Create(*args_));
486 EXTENSION_FUNCTION_VALIDATE(params.get());
487
488 if (!params->instance_id)
489 return RespondNow(Error(kViewInstanceIdError));
490
491 WebViewContentScriptManager* manager =
492 WebViewContentScriptManager::Get(browser_context());
493 DCHECK(manager);
494
495 content::WebContents* sender_web_contents = GetSenderWebContents();
496 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents);
497
498 std::vector<std::string> script_name_list;
499 if (params->script_name_list)
500 script_name_list.swap(*params->script_name_list);
501 manager->RemoveContentScripts(GetSenderWebContents(), params->instance_id,
502 host_id, script_name_list);
503 return RespondNow(NoArguments());
504 }
505
259 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { 506 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() {
260 } 507 }
261 508
262 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { 509 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
263 } 510 }
264 511
265 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { 512 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
266 scoped_ptr<web_view_internal::SetName::Params> params( 513 scoped_ptr<web_view_internal::SetName::Params> params(
267 web_view_internal::SetName::Params::Create(*args_)); 514 web_view_internal::SetName::Params::Create(*args_));
268 EXTENSION_FUNCTION_VALIDATE(params.get()); 515 EXTENSION_FUNCTION_VALIDATE(params.get());
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 // Will finish asynchronously. 924 // Will finish asynchronously.
678 return true; 925 return true;
679 } 926 }
680 927
681 void WebViewInternalClearDataFunction::ClearDataDone() { 928 void WebViewInternalClearDataFunction::ClearDataDone() {
682 Release(); // Balanced in RunAsync(). 929 Release(); // Balanced in RunAsync().
683 SendResponse(true); 930 SendResponse(true);
684 } 931 }
685 932
686 } // namespace extensions 933 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698