Chromium Code Reviews| 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"; |
| 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 bool ParseContentScript(const ContentScriptDetails& script_value, | |
| 83 const extensions::Extension* extension, | |
| 84 const GURL& owner_base_url, | |
| 85 UserScript* script, | |
| 86 std::string* error) { | |
| 87 // matches (required): | |
| 88 const std::vector<std::string>& matches = script_value.matches; | |
| 89 if (matches.size() == 0) { | |
| 90 return false; | |
| 91 } | |
| 92 | |
| 93 for (const std::string& match : matches) { | |
| 94 URLPattern pattern(UserScript::ValidUserScriptSchemes( | |
| 95 true /* canExecuteScriptEverywhere */)); | |
| 96 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) { | |
| 97 *error = errors::kInvalidMatches; | |
| 98 return false; | |
| 99 } | |
| 100 script->add_url_pattern(pattern); | |
| 101 } | |
| 102 | |
| 103 // exclude_matches: | |
| 104 if (script_value.exclude_matches) { | |
| 105 const std::vector<std::string>& exclude_matches = | |
| 106 *(script_value.exclude_matches.get()); | |
| 107 for (const std::string& exclude_match : exclude_matches) { | |
| 108 URLPattern pattern(UserScript::ValidUserScriptSchemes( | |
| 109 true /* canExecuteScriptEverywhere */)); | |
| 110 | |
| 111 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) { | |
| 112 *error = errors::kInvalidExcludeMatches; | |
| 113 return false; | |
| 114 } | |
| 115 script->add_exclude_url_pattern(pattern); | |
| 116 } | |
| 117 } | |
| 118 // run_at: | |
| 119 if (script_value.run_at) { | |
| 120 UserScript::RunLocation run_at = UserScript::UNDEFINED; | |
| 121 switch (script_value.run_at) { | |
| 122 case extensions::core_api::extension_types::RUN_AT_NONE: | |
| 123 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_IDLE: | |
| 124 run_at = UserScript::DOCUMENT_IDLE; | |
| 125 break; | |
| 126 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_START: | |
| 127 run_at = UserScript::DOCUMENT_START; | |
| 128 break; | |
| 129 case extensions::core_api::extension_types::RUN_AT_DOCUMENT_END: | |
| 130 run_at = UserScript::DOCUMENT_END; | |
| 131 break; | |
| 132 } | |
| 133 script->set_run_location(run_at); | |
| 134 } | |
| 135 | |
| 136 // match_about_blank: | |
| 137 if (script_value.match_about_blank) | |
| 138 script->set_match_about_blank(*script_value.match_about_blank); | |
| 139 | |
| 140 // css: | |
| 141 if (script_value.css) { | |
| 142 const std::vector<std::string>& css_files = *(script_value.css.get()); | |
| 143 for (const std::string& relative : css_files) { | |
| 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 const std::vector<std::string>& js_files = *(script_value.js.get()); | |
| 159 for (const std::string& relative : js_files) { | |
| 160 GURL url = owner_base_url.Resolve(relative); | |
| 161 if (extension) { | |
| 162 ExtensionResource resource = extension->GetResource(relative); | |
| 163 script->js_scripts().push_back(UserScript::File( | |
| 164 resource.extension_root(), resource.relative_path(), url)); | |
| 165 } else { | |
| 166 script->js_scripts().push_back(extensions::UserScript::File( | |
| 167 base::FilePath(), base::FilePath(), url)); | |
| 168 } | |
| 169 } | |
| 170 } | |
| 171 | |
| 172 // all_frames: | |
| 173 if (script_value.all_frames) { | |
| 174 script->set_match_all_frames(*(script_value.all_frames)); | |
| 175 } | |
| 176 | |
| 177 // include_globs: | |
| 178 if (script_value.include_globs) { | |
| 179 const std::vector<std::string>& include_globs = | |
| 180 *(script_value.include_globs.get()); | |
| 181 for (const std::string& glob : include_globs) | |
| 182 script->add_glob(glob); | |
| 183 } | |
| 184 | |
| 185 // exclude_globs: | |
| 186 if (script_value.exclude_globs) { | |
| 187 const std::vector<std::string>& exclude_globs = | |
| 188 *(script_value.exclude_globs.get()); | |
| 189 for (const std::string& glob : exclude_globs) | |
| 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 GURL& owner_base_url, | |
| 200 std::map<std::string, UserScript>* result, | |
| 201 std::string* error) { | |
| 202 if (content_script_list.size() == 0) | |
| 203 return false; | |
| 204 for (size_t i = 0; i < content_script_list.size(); ++i) { | |
| 205 const ContentScriptDetails& script_value = *content_script_list[i]; | |
| 206 const std::string& name = script_value.name; | |
| 207 UserScript script; | |
| 208 if (!ParseContentScript(script_value, extension, owner_base_url, &script, | |
| 209 error)) | |
| 210 return false; | |
| 211 else | |
| 212 result->insert(std::pair<std::string, UserScript>(name, script)); | |
| 213 } | |
| 214 return true; | |
| 215 } | |
| 216 | |
| 217 // Initialize the user script. | |
| 218 void InitUserScript(UserScript* script, | |
| 219 std::string name, | |
| 220 bool is_incognito_enabled, | |
| 221 HostID host_id) { | |
| 222 if (!script) | |
| 223 return; | |
| 224 script->set_id(UserScript::GenerateUserScriptID()); | |
| 225 script->set_name(name); | |
| 226 script->set_incognito_enabled(is_incognito_enabled); | |
| 227 script->set_host_id(host_id); | |
| 228 script->set_consumer_instance_type(UserScript::WEBVIEW); | |
| 229 } | |
| 230 | |
| 59 } // namespace | 231 } // namespace |
| 60 | 232 |
| 61 namespace extensions { | 233 namespace extensions { |
| 62 | 234 |
| 63 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. | 235 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. |
| 64 // Each WebUIURLFetcher is associated with a given |render_process_id, | 236 // Each WebUIURLFetcher is associated with a given |render_process_id, |
| 65 // render_view_id| pair. | 237 // render_view_id| pair. |
| 66 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | 238 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher |
| 67 : public net::URLFetcherDelegate { | 239 : public net::URLFetcherDelegate { |
| 68 public: | 240 public: |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 249 error, on_url, result); | 421 error, on_url, result); |
| 250 } | 422 } |
| 251 | 423 |
| 252 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { | 424 WebViewInternalInsertCSSFunction::WebViewInternalInsertCSSFunction() { |
| 253 } | 425 } |
| 254 | 426 |
| 255 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { | 427 bool WebViewInternalInsertCSSFunction::ShouldInsertCSS() const { |
| 256 return true; | 428 return true; |
| 257 } | 429 } |
| 258 | 430 |
| 431 WebViewInternalAddContentScriptsFunction:: | |
| 432 WebViewInternalAddContentScriptsFunction() { | |
| 433 } | |
| 434 | |
| 435 WebViewInternalAddContentScriptsFunction:: | |
| 436 ~WebViewInternalAddContentScriptsFunction() { | |
| 437 } | |
| 438 | |
| 439 ExecuteCodeFunction::ResponseAction | |
| 440 WebViewInternalAddContentScriptsFunction::Run() { | |
| 441 scoped_ptr<web_view_internal::AddContentScripts::Params> params( | |
| 442 web_view_internal::AddContentScripts::Params::Create(*args_)); | |
| 443 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 444 | |
| 445 int view_instance_id = 0; | |
| 446 if (!args_->GetInteger(0, &view_instance_id) || !view_instance_id) { | |
| 447 error_ = kViewInstanceIdError; | |
|
Devlin
2015/04/10 16:04:31
RespondNow(Error(x)) sets error_ to x.
Xi Han
2015/04/10 18:27:06
Done.
| |
| 448 return RespondNow(Error(error_)); | |
| 449 } | |
| 450 | |
| 451 GURL owner_base_url( | |
| 452 render_view_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath()); | |
| 453 std::map<std::string, UserScript> result; | |
| 454 if (!ParseContentScripts(params->content_script_list, extension(), | |
| 455 owner_base_url, &result, &error_)) | |
| 456 return RespondNow(Error(error_)); | |
| 457 | |
| 458 WebViewContentScriptManager* manager = | |
| 459 WebViewContentScriptManager::Get(browser_context()); | |
| 460 | |
| 461 int embedder_process_id = | |
| 462 GetSenderWebContents()->GetRenderProcessHost()->GetID(); | |
| 463 HostID host_id = | |
| 464 GenerateHostIDFromEmbedder(extension(), GetSenderWebContents()); | |
| 465 const bool is_incognito_enabled = browser_context()->IsOffTheRecord(); | |
| 466 | |
| 467 for (auto& iter : result) | |
| 468 InitUserScript(&iter.second, iter.first, is_incognito_enabled, host_id); | |
| 469 | |
| 470 manager->AddContentScripts(embedder_process_id, view_instance_id, host_id, | |
|
Fady Samuel
2015/04/08 22:40:04
Maybe pass in GetSenderWebContents() instead? That
Xi Han
2015/04/10 18:27:06
Sorry, forget to update here.
| |
| 471 result); | |
| 472 | |
| 473 return RespondNow(NoArguments()); | |
| 474 } | |
| 475 | |
| 476 WebViewInternalRemoveContentScriptsFunction:: | |
| 477 WebViewInternalRemoveContentScriptsFunction() { | |
| 478 } | |
| 479 | |
| 480 WebViewInternalRemoveContentScriptsFunction:: | |
| 481 ~WebViewInternalRemoveContentScriptsFunction() { | |
| 482 } | |
| 483 | |
| 484 ExecuteCodeFunction::ResponseAction | |
| 485 WebViewInternalRemoveContentScriptsFunction::Run() { | |
| 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 view_instance_id = 0; | |
| 491 if (!args_->GetInteger(0, &view_instance_id) || !view_instance_id) { | |
| 492 error_ = kViewInstanceIdError; | |
| 493 return RespondNow(Error(error_)); | |
| 494 } | |
| 495 | |
| 496 WebViewContentScriptManager* manager = | |
| 497 WebViewContentScriptManager::Get(browser_context()); | |
| 498 DCHECK(manager); | |
| 499 | |
| 500 int embedder_process_id = | |
| 501 GetSenderWebContents()->GetRenderProcessHost()->GetID(); | |
| 502 HostID host_id = | |
| 503 GenerateHostIDFromEmbedder(extension(), GetSenderWebContents()); | |
| 504 if (!params->script_name_list) { | |
| 505 manager->RemoveContentScripts(embedder_process_id, view_instance_id, | |
|
Fady Samuel
2015/04/08 22:40:03
Take in the embedder WebContents here.
| |
| 506 host_id, std::vector<std::string>()); | |
| 507 } else { | |
| 508 manager->RemoveContentScripts(embedder_process_id, view_instance_id, | |
| 509 host_id, *(params->script_name_list)); | |
| 510 } | |
| 511 return RespondNow(NoArguments()); | |
| 512 } | |
| 513 | |
| 259 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { | 514 WebViewInternalSetNameFunction::WebViewInternalSetNameFunction() { |
| 260 } | 515 } |
| 261 | 516 |
| 262 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { | 517 WebViewInternalSetNameFunction::~WebViewInternalSetNameFunction() { |
| 263 } | 518 } |
| 264 | 519 |
| 265 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { | 520 bool WebViewInternalSetNameFunction::RunAsyncSafe(WebViewGuest* guest) { |
| 266 scoped_ptr<web_view_internal::SetName::Params> params( | 521 scoped_ptr<web_view_internal::SetName::Params> params( |
| 267 web_view_internal::SetName::Params::Create(*args_)); | 522 web_view_internal::SetName::Params::Create(*args_)); |
| 268 EXTENSION_FUNCTION_VALIDATE(params.get()); | 523 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| (...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 677 // Will finish asynchronously. | 932 // Will finish asynchronously. |
| 678 return true; | 933 return true; |
| 679 } | 934 } |
| 680 | 935 |
| 681 void WebViewInternalClearDataFunction::ClearDataDone() { | 936 void WebViewInternalClearDataFunction::ClearDataDone() { |
| 682 Release(); // Balanced in RunAsync(). | 937 Release(); // Balanced in RunAsync(). |
| 683 SendResponse(true); | 938 SendResponse(true); |
| 684 } | 939 } |
| 685 | 940 |
| 686 } // namespace extensions | 941 } // namespace extensions |
| OLD | NEW |