| 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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 if (web_contents && web_contents->GetWebUI()) { | 79 if (web_contents && web_contents->GetWebUI()) { |
| 80 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL(); | 80 const GURL& url = web_contents->GetSiteInstance()->GetSiteURL(); |
| 81 return HostID(HostID::WEBUI, url.spec()); | 81 return HostID(HostID::WEBUI, url.spec()); |
| 82 } | 82 } |
| 83 NOTREACHED(); | 83 NOTREACHED(); |
| 84 return HostID(); | 84 return HostID(); |
| 85 } | 85 } |
| 86 | 86 |
| 87 // Creates content script files when parsing InjectionItems of "js" or "css" | 87 // Creates content script files when parsing InjectionItems of "js" or "css" |
| 88 // proterties, and stores them in the |result|. | 88 // proterties, and stores them in the |result|. |
| 89 void AddScriptFiles(const GURL& owner_base_url, | 89 void ParseScriptFiles(const GURL& owner_base_url, |
| 90 const extensions::Extension* extension, | 90 const extensions::Extension* extension, |
| 91 const InjectionItems& items, | 91 const InjectionItems& items, |
| 92 UserScript::FileList* result) { | 92 UserScript::FileList* list) { |
| 93 DCHECK(list->empty()); |
| 94 list->reserve((items.files ? items.files->size() : 0) + (items.code ? 1 : 0)); |
| 93 // files: | 95 // files: |
| 94 if (items.files) { | 96 if (items.files) { |
| 95 for (const std::string& relative : *items.files) { | 97 for (const std::string& relative : *items.files) { |
| 96 GURL url = owner_base_url.Resolve(relative); | 98 GURL url = owner_base_url.Resolve(relative); |
| 97 if (extension) { | 99 if (extension) { |
| 98 ExtensionResource resource = extension->GetResource(relative); | 100 ExtensionResource resource = extension->GetResource(relative); |
| 99 result->push_back(UserScript::File(resource.extension_root(), | 101 |
| 100 resource.relative_path(), url)); | 102 list->push_back(base::MakeUnique<extensions::UserScript::File>( |
| 103 resource.extension_root(), resource.relative_path(), url)); |
| 101 } else { | 104 } else { |
| 102 result->push_back(extensions::UserScript::File(base::FilePath(), | 105 list->push_back(base::MakeUnique<extensions::UserScript::File>( |
| 103 base::FilePath(), url)); | 106 base::FilePath(), base::FilePath(), url)); |
| 104 } | 107 } |
| 105 } | 108 } |
| 106 } | 109 } |
| 107 // code: | 110 // code: |
| 108 if (items.code) { | 111 if (items.code) { |
| 109 extensions::UserScript::File file((base::FilePath()), (base::FilePath()), | 112 std::unique_ptr<extensions::UserScript::File> file( |
| 110 GURL()); | 113 new extensions::UserScript::File(base::FilePath(), base::FilePath(), |
| 111 file.set_content(*items.code); | 114 GURL())); |
| 112 result->push_back(file); | 115 file->set_content(*items.code); |
| 116 list->push_back(std::move(file)); |
| 113 } | 117 } |
| 114 } | 118 } |
| 115 | 119 |
| 116 // Parses the values stored in ContentScriptDetails, and constructs a | 120 // Parses the values stored in ContentScriptDetails, and constructs a |
| 117 // UserScript. | 121 // user script. |
| 118 bool ParseContentScript(const ContentScriptDetails& script_value, | 122 std::unique_ptr<extensions::UserScript> ParseContentScript( |
| 119 const extensions::Extension* extension, | 123 const ContentScriptDetails& script_value, |
| 120 const GURL& owner_base_url, | 124 const extensions::Extension* extension, |
| 121 UserScript* script, | 125 const GURL& owner_base_url, |
| 122 std::string* error) { | 126 std::string* error) { |
| 123 // matches (required): | 127 // matches (required): |
| 124 if (script_value.matches.empty()) | 128 if (script_value.matches.empty()) |
| 125 return false; | 129 return std::unique_ptr<extensions::UserScript>(); |
| 130 |
| 131 std::unique_ptr<extensions::UserScript> script(new extensions::UserScript()); |
| 126 | 132 |
| 127 // The default for WebUI is not having special access, but we can change that | 133 // The default for WebUI is not having special access, but we can change that |
| 128 // if needed. | 134 // if needed. |
| 129 bool allowed_everywhere = false; | 135 bool allowed_everywhere = false; |
| 130 if (extension && | 136 if (extension && |
| 131 extensions::PermissionsData::CanExecuteScriptEverywhere(extension)) | 137 extensions::PermissionsData::CanExecuteScriptEverywhere(extension)) |
| 132 allowed_everywhere = true; | 138 allowed_everywhere = true; |
| 133 | 139 |
| 134 for (const std::string& match : script_value.matches) { | 140 for (const std::string& match : script_value.matches) { |
| 135 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere)); | 141 URLPattern pattern(UserScript::ValidUserScriptSchemes(allowed_everywhere)); |
| 136 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) { | 142 if (pattern.Parse(match) != URLPattern::PARSE_SUCCESS) { |
| 137 *error = errors::kInvalidMatches; | 143 *error = errors::kInvalidMatches; |
| 138 return false; | 144 return std::unique_ptr<extensions::UserScript>(); |
| 139 } | 145 } |
| 140 script->add_url_pattern(pattern); | 146 script->add_url_pattern(pattern); |
| 141 } | 147 } |
| 142 | 148 |
| 143 // exclude_matches: | 149 // exclude_matches: |
| 144 if (script_value.exclude_matches) { | 150 if (script_value.exclude_matches) { |
| 145 const std::vector<std::string>& exclude_matches = | 151 const std::vector<std::string>& exclude_matches = |
| 146 *(script_value.exclude_matches.get()); | 152 *(script_value.exclude_matches.get()); |
| 147 for (const std::string& exclude_match : exclude_matches) { | 153 for (const std::string& exclude_match : exclude_matches) { |
| 148 URLPattern pattern( | 154 URLPattern pattern( |
| 149 UserScript::ValidUserScriptSchemes(allowed_everywhere)); | 155 UserScript::ValidUserScriptSchemes(allowed_everywhere)); |
| 150 | 156 |
| 151 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) { | 157 if (pattern.Parse(exclude_match) != URLPattern::PARSE_SUCCESS) { |
| 152 *error = errors::kInvalidExcludeMatches; | 158 *error = errors::kInvalidExcludeMatches; |
| 153 return false; | 159 return std::unique_ptr<extensions::UserScript>(); |
| 154 } | 160 } |
| 155 script->add_exclude_url_pattern(pattern); | 161 script->add_exclude_url_pattern(pattern); |
| 156 } | 162 } |
| 157 } | 163 } |
| 158 // run_at: | 164 // run_at: |
| 159 if (script_value.run_at) { | 165 if (script_value.run_at) { |
| 160 UserScript::RunLocation run_at = UserScript::UNDEFINED; | 166 UserScript::RunLocation run_at = UserScript::UNDEFINED; |
| 161 switch (script_value.run_at) { | 167 switch (script_value.run_at) { |
| 162 case extensions::api::extension_types::RUN_AT_NONE: | 168 case extensions::api::extension_types::RUN_AT_NONE: |
| 163 case extensions::api::extension_types::RUN_AT_DOCUMENT_IDLE: | 169 case extensions::api::extension_types::RUN_AT_DOCUMENT_IDLE: |
| 164 run_at = UserScript::DOCUMENT_IDLE; | 170 run_at = UserScript::DOCUMENT_IDLE; |
| 165 break; | 171 break; |
| 166 case extensions::api::extension_types::RUN_AT_DOCUMENT_START: | 172 case extensions::api::extension_types::RUN_AT_DOCUMENT_START: |
| 167 run_at = UserScript::DOCUMENT_START; | 173 run_at = UserScript::DOCUMENT_START; |
| 168 break; | 174 break; |
| 169 case extensions::api::extension_types::RUN_AT_DOCUMENT_END: | 175 case extensions::api::extension_types::RUN_AT_DOCUMENT_END: |
| 170 run_at = UserScript::DOCUMENT_END; | 176 run_at = UserScript::DOCUMENT_END; |
| 171 break; | 177 break; |
| 172 } | 178 } |
| 173 // The default for run_at is RUN_AT_DOCUMENT_IDLE. | 179 // The default for run_at is RUN_AT_DOCUMENT_IDLE. |
| 174 script->set_run_location(run_at); | 180 script->set_run_location(run_at); |
| 175 } | 181 } |
| 176 | 182 |
| 177 // match_about_blank: | 183 // match_about_blank: |
| 178 if (script_value.match_about_blank) | 184 if (script_value.match_about_blank) |
| 179 script->set_match_about_blank(*script_value.match_about_blank); | 185 script->set_match_about_blank(*script_value.match_about_blank); |
| 180 | 186 |
| 181 // css: | 187 // css: |
| 182 if (script_value.css) { | 188 if (script_value.css) { |
| 183 AddScriptFiles(owner_base_url, extension, *script_value.css, | 189 ParseScriptFiles(owner_base_url, extension, *script_value.css, |
| 184 &script->css_scripts()); | 190 &script->css_scripts()); |
| 185 } | 191 } |
| 186 | 192 |
| 187 // js: | 193 // js: |
| 188 if (script_value.js) { | 194 if (script_value.js) { |
| 189 AddScriptFiles(owner_base_url, extension, *script_value.js, | 195 ParseScriptFiles(owner_base_url, extension, *script_value.js, |
| 190 &script->js_scripts()); | 196 &script->js_scripts()); |
| 191 } | 197 } |
| 192 | 198 |
| 193 // all_frames: | 199 // all_frames: |
| 194 if (script_value.all_frames) | 200 if (script_value.all_frames) |
| 195 script->set_match_all_frames(*script_value.all_frames); | 201 script->set_match_all_frames(*script_value.all_frames); |
| 196 | 202 |
| 197 // include_globs: | 203 // include_globs: |
| 198 if (script_value.include_globs) { | 204 if (script_value.include_globs) { |
| 199 for (const std::string& glob : *script_value.include_globs) | 205 for (const std::string& glob : *script_value.include_globs) |
| 200 script->add_glob(glob); | 206 script->add_glob(glob); |
| 201 } | 207 } |
| 202 | 208 |
| 203 // exclude_globs: | 209 // exclude_globs: |
| 204 if (script_value.exclude_globs) { | 210 if (script_value.exclude_globs) { |
| 205 for (const std::string& glob : *script_value.exclude_globs) | 211 for (const std::string& glob : *script_value.exclude_globs) |
| 206 script->add_exclude_glob(glob); | 212 script->add_exclude_glob(glob); |
| 207 } | 213 } |
| 208 | 214 |
| 209 return true; | 215 return script; |
| 210 } | 216 } |
| 211 | 217 |
| 212 bool ParseContentScripts( | 218 std::unique_ptr<extensions::UserScriptList> ParseContentScripts( |
| 213 const std::vector<ContentScriptDetails>& content_script_list, | 219 const std::vector<ContentScriptDetails>& content_script_list, |
| 214 const extensions::Extension* extension, | 220 const extensions::Extension* extension, |
| 215 const HostID& host_id, | 221 const HostID& host_id, |
| 216 bool incognito_enabled, | 222 bool incognito_enabled, |
| 217 const GURL& owner_base_url, | 223 const GURL& owner_base_url, |
| 218 extensions::UserScriptList* result, | |
| 219 std::string* error) { | 224 std::string* error) { |
| 220 if (content_script_list.empty()) | 225 if (content_script_list.empty()) |
| 221 return false; | 226 return std::unique_ptr<extensions::UserScriptList>(); |
| 222 | 227 |
| 228 std::unique_ptr<extensions::UserScriptList> result( |
| 229 new extensions::UserScriptList()); |
| 223 std::set<std::string> names; | 230 std::set<std::string> names; |
| 224 for (const ContentScriptDetails& script_value : content_script_list) { | 231 for (const ContentScriptDetails& script_value : content_script_list) { |
| 225 const std::string& name = script_value.name; | 232 const std::string& name = script_value.name; |
| 226 if (!names.insert(name).second) { | 233 if (!names.insert(name).second) { |
| 227 // The name was already in the list. | 234 // The name was already in the list. |
| 228 *error = kDuplicatedContentScriptNamesError; | 235 *error = kDuplicatedContentScriptNamesError; |
| 229 return false; | 236 return std::unique_ptr<extensions::UserScriptList>(); |
| 230 } | 237 } |
| 231 | 238 |
| 232 UserScript script; | 239 std::unique_ptr<extensions::UserScript> script = |
| 233 if (!ParseContentScript(script_value, extension, owner_base_url, &script, | 240 ParseContentScript(script_value, extension, owner_base_url, error); |
| 234 error)) | 241 if (!script) |
| 235 return false; | 242 return std::unique_ptr<extensions::UserScriptList>(); |
| 236 | 243 script->set_id(UserScript::GenerateUserScriptID()); |
| 237 script.set_id(UserScript::GenerateUserScriptID()); | 244 script->set_name(name); |
| 238 script.set_name(name); | 245 script->set_incognito_enabled(incognito_enabled); |
| 239 script.set_incognito_enabled(incognito_enabled); | 246 script->set_host_id(host_id); |
| 240 script.set_host_id(host_id); | 247 script->set_consumer_instance_type(extensions::UserScript::WEBVIEW); |
| 241 script.set_consumer_instance_type(UserScript::WEBVIEW); | 248 result->push_back(std::move(script)); |
| 242 result->push_back(script); | |
| 243 } | 249 } |
| 244 return true; | 250 return result; |
| 245 } | 251 } |
| 246 | 252 |
| 247 } // namespace | 253 } // namespace |
| 248 | 254 |
| 249 namespace extensions { | 255 namespace extensions { |
| 250 | 256 |
| 251 bool LegacyWebViewInternalExtensionFunction::RunAsync() { | 257 bool LegacyWebViewInternalExtensionFunction::RunAsync() { |
| 252 int instance_id = 0; | 258 int instance_id = 0; |
| 253 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 259 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
| 254 WebViewGuest* guest = WebViewGuest::From( | 260 WebViewGuest* guest = WebViewGuest::From( |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 | 503 |
| 498 if (!params->instance_id) | 504 if (!params->instance_id) |
| 499 return RespondNow(Error(kViewInstanceIdError)); | 505 return RespondNow(Error(kViewInstanceIdError)); |
| 500 | 506 |
| 501 GURL owner_base_url( | 507 GURL owner_base_url( |
| 502 render_frame_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath()); | 508 render_frame_host()->GetSiteInstance()->GetSiteURL().GetWithEmptyPath()); |
| 503 content::WebContents* sender_web_contents = GetSenderWebContents(); | 509 content::WebContents* sender_web_contents = GetSenderWebContents(); |
| 504 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents); | 510 HostID host_id = GenerateHostIDFromEmbedder(extension(), sender_web_contents); |
| 505 bool incognito_enabled = browser_context()->IsOffTheRecord(); | 511 bool incognito_enabled = browser_context()->IsOffTheRecord(); |
| 506 | 512 |
| 507 UserScriptList result; | 513 std::unique_ptr<UserScriptList> result = |
| 508 if (!ParseContentScripts(params->content_script_list, extension(), host_id, | 514 ParseContentScripts(params->content_script_list, extension(), host_id, |
| 509 incognito_enabled, owner_base_url, &result, &error_)) | 515 incognito_enabled, owner_base_url, &error_); |
| 516 if (!result) |
| 510 return RespondNow(Error(error_)); | 517 return RespondNow(Error(error_)); |
| 511 | 518 |
| 512 WebViewContentScriptManager* manager = | 519 WebViewContentScriptManager* manager = |
| 513 WebViewContentScriptManager::Get(browser_context()); | 520 WebViewContentScriptManager::Get(browser_context()); |
| 514 DCHECK(manager); | 521 DCHECK(manager); |
| 515 | 522 |
| 516 manager->AddContentScripts( | 523 manager->AddContentScripts( |
| 517 sender_web_contents->GetRenderProcessHost()->GetID(), render_frame_host(), | 524 sender_web_contents->GetRenderProcessHost()->GetID(), render_frame_host(), |
| 518 params->instance_id, host_id, result); | 525 params->instance_id, host_id, std::move(result)); |
| 519 | 526 |
| 520 return RespondNow(NoArguments()); | 527 return RespondNow(NoArguments()); |
| 521 } | 528 } |
| 522 | 529 |
| 523 WebViewInternalRemoveContentScriptsFunction:: | 530 WebViewInternalRemoveContentScriptsFunction:: |
| 524 WebViewInternalRemoveContentScriptsFunction() { | 531 WebViewInternalRemoveContentScriptsFunction() { |
| 525 } | 532 } |
| 526 | 533 |
| 527 WebViewInternalRemoveContentScriptsFunction:: | 534 WebViewInternalRemoveContentScriptsFunction:: |
| 528 ~WebViewInternalRemoveContentScriptsFunction() { | 535 ~WebViewInternalRemoveContentScriptsFunction() { |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 964 // Will finish asynchronously. | 971 // Will finish asynchronously. |
| 965 return true; | 972 return true; |
| 966 } | 973 } |
| 967 | 974 |
| 968 void WebViewInternalClearDataFunction::ClearDataDone() { | 975 void WebViewInternalClearDataFunction::ClearDataDone() { |
| 969 Release(); // Balanced in RunAsync(). | 976 Release(); // Balanced in RunAsync(). |
| 970 SendResponse(true); | 977 SendResponse(true); |
| 971 } | 978 } |
| 972 | 979 |
| 973 } // namespace extensions | 980 } // namespace extensions |
| OLD | NEW |