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

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: Make the sync_IPC handled in IO thread. 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;
33 // error messages for content scripts:
34 namespace errors = extensions::manifest_errors;
26 namespace web_view_internal = extensions::core_api::web_view_internal; 35 namespace web_view_internal = extensions::core_api::web_view_internal;
27 36
28 namespace { 37 namespace {
29 38
30 const char kAppCacheKey[] = "appcache"; 39 const char kAppCacheKey[] = "appcache";
31 const char kCacheKey[] = "cache"; 40 const char kCacheKey[] = "cache";
32 const char kCookiesKey[] = "cookies"; 41 const char kCookiesKey[] = "cookies";
33 const char kFileSystemsKey[] = "fileSystems"; 42 const char kFileSystemsKey[] = "fileSystems";
34 const char kIndexedDBKey[] = "indexedDB"; 43 const char kIndexedDBKey[] = "indexedDB";
35 const char kLocalStorageKey[] = "localStorage"; 44 const char kLocalStorageKey[] = "localStorage";
(...skipping 12 matching lines...) Expand all
48 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; 57 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS;
49 if (strcmp(key, kIndexedDBKey) == 0) 58 if (strcmp(key, kIndexedDBKey) == 0)
50 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; 59 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB;
51 if (strcmp(key, kLocalStorageKey) == 0) 60 if (strcmp(key, kLocalStorageKey) == 0)
52 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; 61 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE;
53 if (strcmp(key, kWebSQLKey) == 0) 62 if (strcmp(key, kWebSQLKey) == 0)
54 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; 63 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL;
55 return 0; 64 return 0;
56 } 65 }
57 66
67 HostID GenerateHostID(const extensions::Extension* extension,
68 const content::WebContents* web_contents,
69 const content::RenderViewHost* rvh) {
Fady Samuel 2015/03/31 23:40:12 This is unnecessary. You can grab the SiteuRL from
Xi Han 2015/04/01 22:14:41 That is great. Updated.
70 if (extension)
71 return HostID(HostID::EXTENSIONS, extension->id());
72
73 if (web_contents && web_contents->GetWebUI()) {
74 const GURL& url = rvh->GetSiteInstance()->GetSiteURL();
Fady Samuel 2015/03/31 23:40:11 use web_contents->GetSiteInstance()
Xi Han 2015/04/01 22:14:40 Done.
75 return HostID(HostID::WEBUI, url.spec());
76 }
77 NOTREACHED();
78 return HostID();
79 }
80
81 bool Parse(const ContentScriptDetails& script_value,
82 const extensions::Extension* extension,
83 const GURL& owner_base_url,
84 UserScript* script,
85 std::string* error) {
86 // matches (required):
87 const std::vector<std::string>& matches = script_value.matches;
88 if (matches.size() == 0) {
89 return false;
90 }
91
92 for (const std::string& match : matches) {
93 URLPattern pattern(UserScript::ValidUserScriptSchemes(
94 true /* canExecuteScriptEverywhere */));
95 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) {
96 *error = errors::kInvalidMatches;
97 return false;
98 }
99 script->add_url_pattern(pattern);
100 }
101
102 // exclude_matches:
103 if (script_value.exclude_matches) {
104 const std::vector<std::string>& exclude_matches =
105 *(script_value.exclude_matches.get());
106 for (const std::string& exclude_match : exclude_matches) {
107 URLPattern pattern(UserScript::ValidUserScriptSchemes(
108 true /* canExecuteScriptEverywhere */));
109
110 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) {
111 *error = errors::kInvalidExcludeMatches;
112 return false;
113 }
114 script->add_exclude_url_pattern(pattern);
115 }
116 }
117 // run_at:
118 if (script_value.run_at) {
119 UserScript::RunLocation run_at = UserScript::UNDEFINED;
120 switch (script_value.run_at) {
121 case ContentScriptDetails::RUN_AT_NONE:
122 case ContentScriptDetails::RUN_AT_DOCUMENT_IDLE:
123 run_at = UserScript::DOCUMENT_IDLE;
124 break;
125 case ContentScriptDetails::RUN_AT_DOCUMENT_START:
126 run_at = UserScript::DOCUMENT_START;
127 break;
128 case ContentScriptDetails::RUN_AT_DOCUMENT_END:
129 run_at = UserScript::DOCUMENT_END;
130 break;
131 }
132 script->set_run_location(run_at);
133 }
134
135 // match_about_blank:
136 if (script_value.match_about_blank)
137 script->set_match_about_blank(*script_value.match_about_blank);
138
139 // css:
140 if (script_value.css) {
141 const std::vector<std::string>& css_files = *(script_value.css.get());
142 for (const std::string& relative : css_files) {
143 GURL url = owner_base_url.Resolve(relative);
144 if (extension) {
145 ExtensionResource resource = extension->GetResource(relative);
146 script->css_scripts().push_back(UserScript::File(
147 resource.extension_root(), resource.relative_path(), url));
148 } else {
149 script->css_scripts().push_back(extensions::UserScript::File(
150 base::FilePath(), base::FilePath(), url));
151 }
152 }
153 }
154
155 // js:
156 if (script_value.js) {
157 const std::vector<std::string>& js_files = *(script_value.js.get());
158 for (const std::string& relative : js_files) {
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
176 // include_globs:
177 if (script_value.include_globs) {
178 const std::vector<std::string>& include_globs =
179 *(script_value.include_globs.get());
180 for (const std::string& glob : include_globs)
181 script->add_glob(glob);
182 }
183
184 // exclude_globs:
185 if (script_value.exclude_globs) {
186 const std::vector<std::string>& exclude_globs =
187 *(script_value.exclude_globs.get());
188 for (const std::string& glob : exclude_globs)
189 script->add_exclude_glob(glob);
190 }
191
192 return true;
193 }
194
195 bool Parse(std::vector<linked_ptr<ContentScriptDetails>> content_script_list,
196 const extensions::Extension* extension,
197 const GURL& owner_base_url,
198 std::map<std::string, UserScript>* result,
199 std::string* error) {
200 if (content_script_list.size() == 0)
201 return false;
202 for (size_t i = 0; i < content_script_list.size(); ++i) {
203 const ContentScriptDetails& script_value = *content_script_list[i];
204 const std::string& name = script_value.name;
205 UserScript script;
206 if (!Parse(script_value, extension, owner_base_url, &script, error))
207 return false;
208 else
209 result->insert(std::pair<std::string, UserScript>(name, script));
210 }
211 return true;
212 }
213
214 // Initialize the user script.
215 void InitUserScript(UserScript* script, std::string name, HostID host_id) {
216 if (!script)
217 return;
218 script->set_id(UserScript::GenerateUserScriptID());
219 script->set_name(name);
220 script->set_host_id(host_id);
221 script->set_consumer_instance_type(UserScript::WEBVIEW);
222 }
223
58 } // namespace 224 } // namespace
59 225
60 namespace extensions { 226 namespace extensions {
61 227
62 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. 228 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI.
63 // Each WebUIURLFetcher is associated with a given |render_process_id, 229 // Each WebUIURLFetcher is associated with a given |render_process_id,
64 // render_view_id| pair. 230 // render_view_id| pair.
65 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher 231 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher
66 : public net::URLFetcherDelegate { 232 : public net::URLFetcherDelegate {
67 public: 233 public:
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 error, on_url, result); 414 error, on_url, result);
249 } 415 }
250 416
251 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { 417 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() {
252 } 418 }
253 419
254 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { 420 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const {
255 return true; 421 return true;
256 } 422 }
257 423
424 WebViewInternalAddContentScriptsFunction::
425 WebViewInternalAddContentScriptsFunction() {
426 }
427
428 WebViewInternalAddContentScriptsFunction::
429 ~WebViewInternalAddContentScriptsFunction() {
430 }
431
432 bool WebViewInternalAddContentScriptsFunction::RunAsync() {
433 scoped_ptr<web_view_internal::AddContentScripts::Params> params(
434 web_view_internal::AddContentScripts::Params::Create(*args_));
435 EXTENSION_FUNCTION_VALIDATE(params.get());
436
437 int guest_view_instance_id = 0;
Fady Samuel 2015/03/31 23:40:12 view_instance_id
Xi Han 2015/04/01 22:14:41 Done.
438 if (!args_->GetInteger(0, &guest_view_instance_id) ||
439 !guest_view_instance_id) {
440 SendResponse(false);
441 return false;
442 }
443
444 if (!render_view_host() || !render_view_host()->GetProcess()) {
Fady Samuel 2015/03/31 23:40:11 Is this even possible?
Xi Han 2015/04/01 22:14:41 Removed.
445 SendResponse(false);
446 return false;
447 }
448
449 GURL owner_base_url(
450 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath());
451 std::map<std::string, UserScript> result;
452 if (!Parse(params->content_script_list, extension(), owner_base_url, &result,
453 &error_)) {
454 SendResponse(false);
455 return false;
456 }
457
458 WebViewContentScriptManager* manager =
459 WebViewContentScriptManager::Get(browser_context());
460 if (!manager) {
461 SendResponse(false);
462 return false;
463 }
464
465 int embedder_process_id = render_view_host()->GetProcess()->GetID();
Fady Samuel 2015/03/31 23:40:11 Use GetSenderWebContents()->GetRenderProcessHost()
Xi Han 2015/04/01 22:14:40 Done.
466 HostID host_id =
467 GenerateHostID(extension(), GetSenderWebContents(), render_view_host());
468 for (auto& iter : result)
469 InitUserScript(&iter.second, iter.first, host_id);
470 manager->AddContentScripts(embedder_process_id, guest_view_instance_id,
471 host_id, result);
472
473 SendResponse(true);
474 return true;
475 }
476
477 WebViewInternalRemoveContentScriptsFunction::
478 WebViewInternalRemoveContentScriptsFunction() {
479 }
480
481 WebViewInternalRemoveContentScriptsFunction::
482 ~WebViewInternalRemoveContentScriptsFunction() {
483 }
484
485 bool WebViewInternalRemoveContentScriptsFunction::RunAsync() {
486 scoped_ptr<web_view_internal::RemoveContentScripts::Params> params(
487 web_view_internal::RemoveContentScripts::Params::Create(*args_));
488 EXTENSION_FUNCTION_VALIDATE(params.get());
489
490 int guest_view_instance_id = 0;
491 if (!args_->GetInteger(0, &guest_view_instance_id) ||
492 !guest_view_instance_id) {
493 SendResponse(false);
494 return false;
495 }
496
497 WebViewContentScriptManager* manager =
498 WebViewContentScriptManager::Get(browser_context());
499 if (!manager) {
Fady Samuel 2015/03/31 23:40:11 I don't think this is possible. I think this simpl
Xi Han 2015/04/01 22:14:41 Done.
500 SendResponse(false);
501 return false;
502 }
503
504 if (!render_view_host() || !render_view_host()->GetProcess()) {
Fady Samuel 2015/03/31 23:40:11 This check also seems unnecessary.
Xi Han 2015/04/01 22:14:41 Done.
505 SendResponse(false);
506 return false;
507 }
508
509 int embedder_process_id = render_view_host()->GetProcess()->GetID();
510 HostID host_id =
511 GenerateHostID(extension(), GetSenderWebContents(), render_view_host());
512 manager->RemoveContentScripts(embedder_process_id, guest_view_instance_id,
513 host_id, (params->script_name_list).get());
514
515 SendResponse(true);
516 return true;
517 }
518
258 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { 519 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() {
259 } 520 }
260 521
261 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { 522 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() {
262 } 523 }
263 524
264 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { 525 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) {
265 scoped_ptr<web_view_internal::SetName::Params> params( 526 scoped_ptr<web_view_internal::SetName::Params> params(
266 web_view_internal::SetName::Params::Create(*args_)); 527 web_view_internal::SetName::Params::Create(*args_));
267 EXTENSION_FUNCTION_VALIDATE(params.get()); 528 EXTENSION_FUNCTION_VALIDATE(params.get());
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after
613 // Will finish asynchronously. 874 // Will finish asynchronously.
614 return true; 875 return true;
615 } 876 }
616 877
617 void WebViewInternalClearDataFunction::ClearDataDone() { 878 void WebViewInternalClearDataFunction::ClearDataDone() {
618 Release(); // Balanced in RunAsync(). 879 Release(); // Balanced in RunAsync().
619 SendResponse(true); 880 SendResponse(true);
620 } 881 }
621 882
622 } // namespace extensions 883 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698