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

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: Delvin's 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/user_script.h"
18 #include "net/base/load_flags.h" 22 #include "net/base/load_flags.h"
19 #include "net/url_request/url_fetcher.h" 23 #include "net/url_request/url_fetcher.h"
20 #include "net/url_request/url_fetcher_delegate.h" 24 #include "net/url_request/url_fetcher_delegate.h"
21 #include "third_party/WebKit/public/web/WebFindOptions.h" 25 #include "third_party/WebKit/public/web/WebFindOptions.h"
22 26
23 using content::WebContents; 27 using content::WebContents;
28 using extensions::ExtensionResource;
29 using extensions::core_api::web_view_internal::ContentScriptDetails;
24 using extensions::core_api::web_view_internal::SetPermission::Params; 30 using extensions::core_api::web_view_internal::SetPermission::Params;
25 using extensions::core_api::extension_types::InjectDetails; 31 using extensions::core_api::extension_types::InjectDetails;
32 using extensions::UserScript;
26 using ui_zoom::ZoomController; 33 using ui_zoom::ZoomController;
34 // error messages for content scripts:
35 namespace errors = extensions::manifest_errors;
27 namespace web_view_internal = extensions::core_api::web_view_internal; 36 namespace web_view_internal = extensions::core_api::web_view_internal;
28 37
29 namespace { 38 namespace {
30 39
31 const char kAppCacheKey[] = "appcache"; 40 const char kAppCacheKey[] = "appcache";
32 const char kCacheKey[] = "cache"; 41 const char kCacheKey[] = "cache";
33 const char kCookiesKey[] = "cookies"; 42 const char kCookiesKey[] = "cookies";
34 const char kFileSystemsKey[] = "fileSystems"; 43 const char kFileSystemsKey[] = "fileSystems";
35 const char kIndexedDBKey[] = "indexedDB"; 44 const char kIndexedDBKey[] = "indexedDB";
36 const char kLocalStorageKey[] = "localStorage"; 45 const char kLocalStorageKey[] = "localStorage";
37 const char kWebSQLKey[] = "webSQL"; 46 const char kWebSQLKey[] = "webSQL";
38 const char kSinceKey[] = "since"; 47 const char kSinceKey[] = "since";
39 const char kLoadFileError[] = "Failed to load file: \"*\". "; 48 const char kLoadFileError[] = "Failed to load file: \"*\". ";
49 const char kViewInstanceIdError[] = "view_instance_id is missing.";
40 50
41 uint32 MaskForKey(const char* key) { 51 uint32 MaskForKey(const char* key) {
42 if (strcmp(key, kAppCacheKey) == 0) 52 if (strcmp(key, kAppCacheKey) == 0)
43 return webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE; 53 return webview::WEB_VIEW_REMOVE_DATA_MASK_APPCACHE;
44 if (strcmp(key, kCacheKey) == 0) 54 if (strcmp(key, kCacheKey) == 0)
45 return webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE; 55 return webview::WEB_VIEW_REMOVE_DATA_MASK_CACHE;
46 if (strcmp(key, kCookiesKey) == 0) 56 if (strcmp(key, kCookiesKey) == 0)
47 return webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES; 57 return webview::WEB_VIEW_REMOVE_DATA_MASK_COOKIES;
48 if (strcmp(key, kFileSystemsKey) == 0) 58 if (strcmp(key, kFileSystemsKey) == 0)
49 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; 59 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS;
50 if (strcmp(key, kIndexedDBKey) == 0) 60 if (strcmp(key, kIndexedDBKey) == 0)
51 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; 61 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB;
52 if (strcmp(key, kLocalStorageKey) == 0) 62 if (strcmp(key, kLocalStorageKey) == 0)
53 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; 63 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE;
54 if (strcmp(key, kWebSQLKey) == 0) 64 if (strcmp(key, kWebSQLKey) == 0)
55 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; 65 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL;
56 return 0; 66 return 0;
57 } 67 }
58 68
69 HostID GenerateHostIDFromEmbedder(const extensions::Extension* extension,
70 const content::WebContents* web_contents) {
71 if (extension)
72 return HostID(HostID::EXTENSIONS, extension->id());
73
74 if (web_contents && web_contents->GetWebUI()) {
75 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL();
76 return HostID(HostID::WEBUI, url.spec());
77 }
78 NOTREACHED();
79 return HostID();
80 }
81
82 // Parses the values stored in ContentScriptDetails, and constructs a
83 // UserScript.
84 bool ParseContentScript(const ContentScriptDetails& script_value,
85 const extensions::Extension* extension,
86 const GURL& owner_base_url,
87 UserScript* script,
88 std::string* error) {
89 // matches (required):
90 const std::vector<std::string>& matches = script_value.matches;
91 if (matches.size() == 0)
92 return false;
93
94 for (const std::string& match : matches) {
95 URLPattern pattern(UserScript::ValidUserScriptSchemes(
96 true /* canExecuteScriptEverywhere */));
97 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) {
98 *error = errors::kInvalidMatches;
99 return false;
100 }
101 script->add_url_pattern(pattern);
102 }
103
104 // exclude_matches:
105 if (script_value.exclude_matches) {
106 const std::vector<std::string>& exclude_matches =
107 *(script_value.exclude_matches.get());
108 for (const std::string& exclude_match : exclude_matches) {
109 URLPattern pattern(UserScript::ValidUserScriptSchemes(
110 true /* canExecuteScriptEverywhere */));
111
112 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) {
113 *error = errors::kInvalidExcludeMatches;
114 return false;
115 }
116 script->add_exclude_url_pattern(pattern);
117 }
118 }
119 // run_at:
120 if (script_value.run_at) {
121 UserScript::RunLocation run_at = UserScript::UNDEFINED;
122 switch (script_value.run_at) {
123 case extensions::core_api::extension_types::RUN_AT_NONE:
124 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_IDLE:
125 run_at = UserScript::DOCUMENT_IDLE;
126 break;
127 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_START:
128 run_at = UserScript::DOCUMENT_START;
129 break;
130 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_END:
131 run_at = UserScript::DOCUMENT_END;
132 break;
133 }
134 script->set_run_location(run_at);
135 }
136
137 // match_about_blank:
138 if (script_value.match_about_blank)
139 script->set_match_about_blank(*script_value.match_about_blank);
140
141 // css:
142 if (script_value.css) {
143 for (const std::string& relative : *(script_value.css.get())) {
144 GURL url = owner_base_url.Resolve(relative);
145 if (extension) {
146 ExtensionResource resource = extension->GetResource(relative);
147 script->css_scripts().push_back(UserScript::File(
148 resource.extension_root(), resource.relative_path(), url));
149 } else {
150 script->css_scripts().push_back(extensions::UserScript::File(
151 base::FilePath(), base::FilePath(), url));
152 }
153 }
154 }
155
156 // js:
157 if (script_value.js) {
158 for (const std::string& relative : *(script_value.js.get())) {
159 GURL url = owner_base_url.Resolve(relative);
160 if (extension) {
161 ExtensionResource resource = extension->GetResource(relative);
162 script->js_scripts().push_back(UserScript::File(
163 resource.extension_root(), resource.relative_path(), url));
164 } else {
165 script->js_scripts().push_back(extensions::UserScript::File(
166 base::FilePath(), base::FilePath(), url));
167 }
168 }
169 }
170
171 // all_frames:
172 if (script_value.all_frames)
173 script->set_match_all_frames(*(script_value.all_frames));
174
175 // include_globs:
176 if (script_value.include_globs) {
177 for (const std::string& glob : *(script_value.include_globs.get()))
178 script->add_glob(glob);
179 }
180
181 // exclude_globs:
182 if (script_value.exclude_globs) {
183 for (const std::string& glob : *(script_value.exclude_globs.get()))
184 script->add_exclude_glob(glob);
185 }
186
187 return true;
188 }
189
190 bool ParseContentScripts(
191 std::vector<linked_ptr<ContentScriptDetails>> content_script_list,
192 const extensions::Extension* extension,
193 const GURL& owner_base_url,
194 std::map<std::string, UserScript>* result,
195 std::string* error) {
196 if (content_script_list.empty())
197 return false;
198 for (const linked_ptr<ContentScriptDetails> script_value :
199 content_script_list) {
200 const std::string& name = script_value->name;
201 UserScript script;
202 if (!ParseContentScript(*script_value, extension, owner_base_url, &script,
203 error))
204 return false;
205 script.set_name(name);
206 (*result)[name] = script;
207 }
208 return true;
209 }
210
59 } // namespace 211 } // namespace
60 212
61 namespace extensions { 213 namespace extensions {
62 214
63 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. 215 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
64 // Each WebUIURLFetcher is associated with a given |render_process_id, 216 // Each WebUIURLFetcher is associated with a given |render_process_id,
65 // render_view_id| pair. 217 // render_view_id| pair.
66 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher 218 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher
67 : public net::URLFetcherDelegate { 219 : public net::URLFetcherDelegate {
68 public: 220 public:
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
249 error, on_url, result); 401 error, on_url, result);
250 } 402 }
251 403
252 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { 404 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() {
253 } 405 }
254 406
255 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { 407 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const {
256 return true; 408 return true;
257 } 409 }
258 410
411 WebViewInternalAddContentScriptsFunction::
412 WebViewInternalAddContentScriptsFunction() {
413 }
414
415 WebViewInternalAddContentScriptsFunction::
416 ~WebViewInternalAddContentScriptsFunction() {
417 }
418
419 ExecuteCodeFunction::ResponseAction
420 WebViewInternalAddContentScriptsFunction::Run() {
421 scoped_ptr<web_view_internal::AddContentScripts::Params> params(
422 web_view_internal::AddContentScripts::Params::Create(*args_));
423 EXTENSION_FUNCTION_VALIDATE(params.get());
424
425 int view_instance_id = params->instance_id;
Devlin 2015/04/14 23:04:57 why not just use params->instance_id?
Xi Han 2015/04/15 19:43:55 Since later we will use view_instance_id again (li
Devlin 2015/04/15 20:58:05 The compiler will definitely optimize this out (th
Xi Han 2015/04/16 15:12:22 That is fine, updated.
426 if (!view_instance_id)
427 return RespondNow(Error(kViewInstanceIdError));
428
429 GURL owner_base_url(
430 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath());
431 std::map<std::string, UserScript> scripts;
Devlin 2015/04/14 23:04:57 This map is still rather useless. Why not just ge
Xi Han 2015/04/15 19:43:55 This map is used to deal with the name duplication
Devlin 2015/04/15 20:58:05 I think if the user is trying to add two scripts w
Xi Han 2015/04/16 15:12:22 That is ok, I use a set to check the duplication o
432 std::set<UserScript> result;
433 if (!ParseContentScripts(params->content_script_list, extension(),
434 owner_base_url, &scripts, &error_))
435 return RespondNow(Error(error_));
436
437 WebViewContentScriptManager* manager =
438 WebViewContentScriptManager::Get(browser_context());
439
440 content::WebContents* sender_web_contents = GetSenderWebContents();
441 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents);
442
443 for (const auto& iter : scripts) {
444 UserScript script = iter.second;
445 script.set_id(UserScript::GenerateUserScriptID());
Devlin 2015/04/14 23:04:57 Why not do these in ParseContentScript(s)?
Xi Han 2015/04/15 19:43:55 It could, but I hope the ParseContentScript(s) onl
Devlin 2015/04/15 20:58:05 I think I'd like to have the script generation all
Xi Han 2015/04/16 15:12:22 Yes, as you suggested, I also move the initilizati
446 script.set_incognito_enabled(browser_context()->IsOffTheRecord());
447 script.set_host_id(
448 GenerateHostIDFromEmbedder(extension(), sender_web_contents));
449 script.set_consumer_instance_type(UserScript::WEBVIEW);
450 result.insert(script);
451 }
452
453 manager->AddContentScripts(sender_web_contents, view_instance_id, host_id,
454 result);
455
456 return RespondNow(NoArguments());
457 }
458
459 WebViewInternalRemoveContentScriptsFunction::
460 WebViewInternalRemoveContentScriptsFunction() {
461 }
462
463 WebViewInternalRemoveContentScriptsFunction::
464 ~WebViewInternalRemoveContentScriptsFunction() {
465 }
466
467 ExecuteCodeFunction::ResponseAction
468 WebViewInternalRemoveContentScriptsFunction::Run() {
469 scoped_ptr<web_view_internal::RemoveContentScripts::Params> params(
470 web_view_internal::RemoveContentScripts::Params::Create(*args_));
471 EXTENSION_FUNCTION_VALIDATE(params.get());
472
473 int view_instance_id = params->instance_id;
474 if (!view_instance_id)
Devlin 2015/04/14 23:04:57 just use params->instance_id
Xi Han 2015/04/15 19:43:55 Same as above. It is used in line 487.
Xi Han 2015/04/16 15:12:22 Done.
475 return RespondNow(Error(kViewInstanceIdError));
476
477 WebViewContentScriptManager* manager =
478 WebViewContentScriptManager::Get(browser_context());
479 DCHECK(manager);
480
481 content::WebContents* sender_web_contents = GetSenderWebContents();
482 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents);
483
484 std::vector<std::string> script_name_list;
485 if (params->script_name_list)
486 script_name_list.swap(*params->script_name_list);
487 manager->RemoveContentScripts(GetSenderWebContents(), view_instance_id,
488 host_id, script_name_list);
489 return RespondNow(NoArguments());
490 }
491
259 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { 492 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() {
260 } 493 }
261 494
262 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { 495 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
263 } 496 }
264 497
265 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { 498 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
266 scoped_ptr<web_view_internal::SetName::Params> params( 499 scoped_ptr<web_view_internal::SetName::Params> params(
267 web_view_internal::SetName::Params::Create(*args_)); 500 web_view_internal::SetName::Params::Create(*args_));
268 EXTENSION_FUNCTION_VALIDATE(params.get()); 501 EXTENSION_FUNCTION_VALIDATE(params.get());
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after
677 // Will finish asynchronously. 910 // Will finish asynchronously.
678 return true; 911 return true;
679 } 912 }
680 913
681 void WebViewInternalClearDataFunction::ClearDataDone() { 914 void WebViewInternalClearDataFunction::ClearDataDone() {
682 Release(); // Balanced in RunAsync(). 915 Release(); // Balanced in RunAsync().
683 SendResponse(true); 916 SendResponse(true);
684 } 917 }
685 918
686 } // namespace extensions 919 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698