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

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

Powered by Google App Engine
This is Rietveld 408576698