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/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"; |
(...skipping 12 matching lines...) Expand all Loading... |
49 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; | 58 return webview::WEB_VIEW_REMOVE_DATA_MASK_FILE_SYSTEMS; |
50 if (strcmp(key, kIndexedDBKey) == 0) | 59 if (strcmp(key, kIndexedDBKey) == 0) |
51 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; | 60 return webview::WEB_VIEW_REMOVE_DATA_MASK_INDEXEDDB; |
52 if (strcmp(key, kLocalStorageKey) == 0) | 61 if (strcmp(key, kLocalStorageKey) == 0) |
53 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; | 62 return webview::WEB_VIEW_REMOVE_DATA_MASK_LOCAL_STORAGE; |
54 if (strcmp(key, kWebSQLKey) == 0) | 63 if (strcmp(key, kWebSQLKey) == 0) |
55 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; | 64 return webview::WEB_VIEW_REMOVE_DATA_MASK_WEBSQL; |
56 return 0; | 65 return 0; |
57 } | 66 } |
58 | 67 |
| 68 HostID GenerateHostID(const extensions::Extension* extension, |
| 69 const content::WebContents* web_contents) { |
| 70 if (extension) |
| 71 return HostID(HostID::EXTENSIONS, extension->id()); |
| 72 |
| 73 if (web_contents && web_contents->GetWebUI()) { |
| 74 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL(); |
| 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 |
59 } // namespace | 224 } // namespace |
60 | 225 |
61 namespace extensions { | 226 namespace extensions { |
62 | 227 |
63 // 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. |
64 // Each WebUIURLFetcher is associated with a given |render_process_id, | 229 // Each WebUIURLFetcher is associated with a given |render_process_id, |
65 // render_view_id| pair. | 230 // render_view_id| pair. |
66 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | 231 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher |
67 : public net::URLFetcherDelegate { | 232 : public net::URLFetcherDelegate { |
68 public: | 233 public: |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 error, on_url, result); | 414 error, on_url, result); |
250 } | 415 } |
251 | 416 |
252 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { | 417 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { |
253 } | 418 } |
254 | 419 |
255 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { | 420 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { |
256 return true; | 421 return true; |
257 } | 422 } |
258 | 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 view_instance_id = 0; |
| 438 if (!args_->GetInteger(0, &view_instance_id) || !view_instance_id) { |
| 439 SendResponse(false); |
| 440 return false; |
| 441 } |
| 442 |
| 443 GURL owner_base_url( |
| 444 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath()); |
| 445 std::map<std::string, UserScript> result; |
| 446 if (!Parse(params->content_script_list, extension(), owner_base_url, &result, |
| 447 &error_)) { |
| 448 SendResponse(false); |
| 449 return false; |
| 450 } |
| 451 |
| 452 WebViewContentScriptManager* manager = |
| 453 WebViewContentScriptManager::Get(browser_context()); |
| 454 if (!manager) { |
| 455 SendResponse(false); |
| 456 return false; |
| 457 } |
| 458 |
| 459 int embedder_process_id = |
| 460 GetSenderWebContents()->GetRenderProcessHost()->GetID(); |
| 461 HostID host_id = GenerateHostID(extension(), GetSenderWebContents()); |
| 462 for (auto& iter : result) |
| 463 InitUserScript(&iter.second, iter.first, host_id); |
| 464 manager->AddContentScripts(embedder_process_id, view_instance_id, host_id, |
| 465 result); |
| 466 |
| 467 SendResponse(true); |
| 468 return true; |
| 469 } |
| 470 |
| 471 WebViewInternalRemoveContentScriptsFunction:: |
| 472 WebViewInternalRemoveContentScriptsFunction() { |
| 473 } |
| 474 |
| 475 WebViewInternalRemoveContentScriptsFunction:: |
| 476 ~WebViewInternalRemoveContentScriptsFunction() { |
| 477 } |
| 478 |
| 479 bool WebViewInternalRemoveContentScriptsFunction::RunAsync() { |
| 480 scoped_ptr<web_view_internal::RemoveContentScripts::Params> params( |
| 481 web_view_internal::RemoveContentScripts::Params::Create(*args_)); |
| 482 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 483 |
| 484 int view_instance_id = 0; |
| 485 if (!args_->GetInteger(0, &view_instance_id) || !view_instance_id) { |
| 486 SendResponse(false); |
| 487 return false; |
| 488 } |
| 489 |
| 490 WebViewContentScriptManager* manager = |
| 491 WebViewContentScriptManager::Get(browser_context()); |
| 492 DCHECK(manager); |
| 493 |
| 494 int embedder_process_id = |
| 495 GetSenderWebContents()->GetRenderProcessHost()->GetID(); |
| 496 HostID host_id = GenerateHostID(extension(), GetSenderWebContents()); |
| 497 if (!params->script_name_list) { |
| 498 manager->RemoveContentScripts(embedder_process_id, view_instance_id, |
| 499 host_id, std::vector<std::string>()); |
| 500 } else { |
| 501 manager->RemoveContentScripts(embedder_process_id, view_instance_id, |
| 502 host_id, *(params->script_name_list)); |
| 503 } |
| 504 SendResponse(true); |
| 505 return true; |
| 506 } |
| 507 |
259 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { | 508 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { |
260 } | 509 } |
261 | 510 |
262 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { | 511 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { |
263 } | 512 } |
264 | 513 |
265 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { | 514 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { |
266 scoped_ptr<web_view_internal::SetName::Params> params( | 515 scoped_ptr<web_view_internal::SetName::Params> params( |
267 web_view_internal::SetName::Params::Create(*args_)); | 516 web_view_internal::SetName::Params::Create(*args_)); |
268 EXTENSION_FUNCTION_VALIDATE(params.get()); | 517 EXTENSION_FUNCTION_VALIDATE(params.get()); |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
677 // Will finish asynchronously. | 926 // Will finish asynchronously. |
678 return true; | 927 return true; |
679 } | 928 } |
680 | 929 |
681 void WebViewInternalClearDataFunction::ClearDataDone() { | 930 void WebViewInternalClearDataFunction::ClearDataDone() { |
682 Release(); // Balanced in RunAsync(). | 931 Release(); // Balanced in RunAsync(). |
683 SendResponse(true); | 932 SendResponse(true); |
684 } | 933 } |
685 | 934 |
686 } // namespace extensions | 935 } // namespace extensions |
OLD | NEW |