| 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/string_number_conversions.h" |
| 8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/render_view_host.h" | 12 #include "content/public/browser/render_view_host.h" |
| 13 #include "content/public/browser/web_contents.h" | 13 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/common/stop_find_action.h" | 14 #include "content/public/common/stop_find_action.h" |
| 15 #include "content/public/common/url_fetcher.h" | 15 #include "content/public/common/url_fetcher.h" |
| 16 #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" | 17 #include "extensions/browser/guest_view/web_view/web_view_content_script_manager
.h" |
| 18 #include "extensions/common/api/web_view_internal.h" | 18 #include "extensions/common/api/web_view_internal.h" |
| 19 #include "extensions/common/error_utils.h" | 19 #include "extensions/common/error_utils.h" |
| 20 #include "extensions/common/manifest_constants.h" | 20 #include "extensions/common/manifest_constants.h" |
| 21 #include "extensions/common/permissions/permissions_data.h" | 21 #include "extensions/common/permissions/permissions_data.h" |
| 22 #include "extensions/common/user_script.h" | 22 #include "extensions/common/user_script.h" |
| 23 #include "net/base/load_flags.h" | |
| 24 #include "net/url_request/url_fetcher.h" | |
| 25 #include "net/url_request/url_fetcher_delegate.h" | |
| 26 #include "third_party/WebKit/public/web/WebFindOptions.h" | 23 #include "third_party/WebKit/public/web/WebFindOptions.h" |
| 27 | 24 |
| 28 using content::WebContents; | 25 using content::WebContents; |
| 29 using extensions::ExtensionResource; | 26 using extensions::ExtensionResource; |
| 30 using extensions::core_api::web_view_internal::ContentScriptDetails; | 27 using extensions::core_api::web_view_internal::ContentScriptDetails; |
| 31 using extensions::core_api::web_view_internal::SetPermission::Params; | 28 using extensions::core_api::web_view_internal::SetPermission::Params; |
| 32 using extensions::core_api::extension_types::InjectDetails; | 29 using extensions::core_api::extension_types::InjectDetails; |
| 33 using extensions::UserScript; | 30 using extensions::UserScript; |
| 34 using ui_zoom::ZoomController; | 31 using ui_zoom::ZoomController; |
| 35 // error messages for content scripts: | 32 // error messages for content scripts: |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 script.set_consumer_instance_type(UserScript::WEBVIEW); | 226 script.set_consumer_instance_type(UserScript::WEBVIEW); |
| 230 result->insert(script); | 227 result->insert(script); |
| 231 } | 228 } |
| 232 return true; | 229 return true; |
| 233 } | 230 } |
| 234 | 231 |
| 235 } // namespace | 232 } // namespace |
| 236 | 233 |
| 237 namespace extensions { | 234 namespace extensions { |
| 238 | 235 |
| 239 // WebUIURLFetcher downloads the content of a file by giving its |url| on WebUI. | |
| 240 // Each WebUIURLFetcher is associated with a given |render_process_id, | |
| 241 // render_view_id| pair. | |
| 242 class WebViewInternalExecuteCodeFunction::WebUIURLFetcher | |
| 243 : public net::URLFetcherDelegate { | |
| 244 public: | |
| 245 WebUIURLFetcher( | |
| 246 content::BrowserContext* context, | |
| 247 const WebViewInternalExecuteCodeFunction::WebUILoadFileCallback& callback) | |
| 248 : context_(context), callback_(callback) {} | |
| 249 ~WebUIURLFetcher() override {} | |
| 250 | |
| 251 void Start(int render_process_id, int render_view_id, const GURL& url) { | |
| 252 fetcher_.reset(net::URLFetcher::Create(url, net::URLFetcher::GET, this)); | |
| 253 fetcher_->SetRequestContext(context_->GetRequestContext()); | |
| 254 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | |
| 255 | |
| 256 content::AssociateURLFetcherWithRenderFrame( | |
| 257 fetcher_.get(), url, render_process_id, render_view_id); | |
| 258 fetcher_->Start(); | |
| 259 } | |
| 260 | |
| 261 private: | |
| 262 // net::URLFetcherDelegate: | |
| 263 void OnURLFetchComplete(const net::URLFetcher* source) override { | |
| 264 CHECK_EQ(fetcher_.get(), source); | |
| 265 | |
| 266 std::string data; | |
| 267 bool result = false; | |
| 268 if (fetcher_->GetStatus().status() == net::URLRequestStatus::SUCCESS) { | |
| 269 result = fetcher_->GetResponseAsString(&data); | |
| 270 DCHECK(result); | |
| 271 } | |
| 272 fetcher_.reset(); | |
| 273 callback_.Run(result, data); | |
| 274 callback_.Reset(); | |
| 275 } | |
| 276 | |
| 277 content::BrowserContext* context_; | |
| 278 WebViewInternalExecuteCodeFunction::WebUILoadFileCallback callback_; | |
| 279 scoped_ptr<net::URLFetcher> fetcher_; | |
| 280 | |
| 281 DISALLOW_COPY_AND_ASSIGN(WebUIURLFetcher); | |
| 282 }; | |
| 283 | |
| 284 bool WebViewInternalExtensionFunction::RunAsync() { | 236 bool WebViewInternalExtensionFunction::RunAsync() { |
| 285 int instance_id = 0; | 237 int instance_id = 0; |
| 286 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); | 238 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &instance_id)); |
| 287 WebViewGuest* guest = WebViewGuest::From( | 239 WebViewGuest* guest = WebViewGuest::From( |
| 288 render_view_host()->GetProcess()->GetID(), instance_id); | 240 render_view_host()->GetProcess()->GetID(), instance_id); |
| 289 if (!guest) | 241 if (!guest) |
| 290 return false; | 242 return false; |
| 291 | 243 |
| 292 return RunAsyncSafe(guest); | 244 return RunAsyncSafe(guest); |
| 293 } | 245 } |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 bool WebViewInternalExecuteCodeFunction::IsWebView() const { | 324 bool WebViewInternalExecuteCodeFunction::IsWebView() const { |
| 373 return true; | 325 return true; |
| 374 } | 326 } |
| 375 | 327 |
| 376 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { | 328 const GURL& WebViewInternalExecuteCodeFunction::GetWebViewSrc() const { |
| 377 return guest_src_; | 329 return guest_src_; |
| 378 } | 330 } |
| 379 | 331 |
| 380 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( | 332 bool WebViewInternalExecuteCodeFunction::LoadFileForWebUI( |
| 381 const std::string& file_src, | 333 const std::string& file_src, |
| 382 const WebUILoadFileCallback& callback) { | 334 const WebUIURLFetcher::WebUILoadFileCallback& callback) { |
| 383 if (!render_view_host() || !render_view_host()->GetProcess()) | 335 if (!render_view_host() || !render_view_host()->GetProcess()) |
| 384 return false; | 336 return false; |
| 385 WebViewGuest* guest = WebViewGuest::From( | 337 WebViewGuest* guest = WebViewGuest::From( |
| 386 render_view_host()->GetProcess()->GetID(), guest_instance_id_); | 338 render_view_host()->GetProcess()->GetID(), guest_instance_id_); |
| 387 if (!guest || host_id().type() != HostID::WEBUI) | 339 if (!guest || host_id().type() != HostID::WEBUI) |
| 388 return false; | 340 return false; |
| 389 | 341 |
| 390 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath()); | 342 GURL owner_base_url(guest->GetOwnerSiteURL().GetWithEmptyPath()); |
| 391 GURL file_url(owner_base_url.Resolve(file_src)); | 343 GURL file_url(owner_base_url.Resolve(file_src)); |
| 392 | 344 |
| 393 url_fetcher_.reset(new WebUIURLFetcher(this->browser_context(), callback)); | 345 url_fetcher_.reset(new WebUIURLFetcher( |
| 394 url_fetcher_->Start(render_view_host()->GetProcess()->GetID(), | 346 this->browser_context(), render_view_host()->GetProcess()->GetID(), |
| 395 render_view_host()->GetRoutingID(), file_url); | 347 render_view_host()->GetRoutingID(), file_url, callback)); |
| 348 url_fetcher_->Start(); |
| 396 return true; | 349 return true; |
| 397 } | 350 } |
| 398 | 351 |
| 399 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { | 352 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { |
| 400 if (!extension()) { | 353 if (!extension()) { |
| 401 if (LoadFileForWebUI( | 354 if (LoadFileForWebUI( |
| 402 *details_->file, | 355 *details_->file, |
| 403 base::Bind( | 356 base::Bind( |
| 404 &WebViewInternalExecuteCodeFunction::DidLoadAndLocalizeFile, | 357 &WebViewInternalExecuteCodeFunction::DidLoadAndLocalizeFile, |
| 405 this, file))) | 358 this, file))) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 bool incognito_enabled = browser_context()->IsOffTheRecord(); | 411 bool incognito_enabled = browser_context()->IsOffTheRecord(); |
| 459 | 412 |
| 460 if (!ParseContentScripts(params->content_script_list, extension(), host_id, | 413 if (!ParseContentScripts(params->content_script_list, extension(), host_id, |
| 461 incognito_enabled, owner_base_url, &result, &error_)) | 414 incognito_enabled, owner_base_url, &result, &error_)) |
| 462 return RespondNow(Error(error_)); | 415 return RespondNow(Error(error_)); |
| 463 | 416 |
| 464 WebViewContentScriptManager* manager = | 417 WebViewContentScriptManager* manager = |
| 465 WebViewContentScriptManager::Get(browser_context()); | 418 WebViewContentScriptManager::Get(browser_context()); |
| 466 DCHECK(manager); | 419 DCHECK(manager); |
| 467 | 420 |
| 468 manager->AddContentScripts(sender_web_contents, params->instance_id, host_id, | 421 manager->AddContentScripts(sender_web_contents, |
| 469 result); | 422 render_view_host()->GetRoutingID(), |
| 423 params->instance_id, host_id, result); |
| 470 | 424 |
| 471 return RespondNow(NoArguments()); | 425 return RespondNow(NoArguments()); |
| 472 } | 426 } |
| 473 | 427 |
| 474 WebViewInternalRemoveContentScriptsFunction:: | 428 WebViewInternalRemoveContentScriptsFunction:: |
| 475 WebViewInternalRemoveContentScriptsFunction() { | 429 WebViewInternalRemoveContentScriptsFunction() { |
| 476 } | 430 } |
| 477 | 431 |
| 478 WebViewInternalRemoveContentScriptsFunction:: | 432 WebViewInternalRemoveContentScriptsFunction:: |
| 479 ~WebViewInternalRemoveContentScriptsFunction() { | 433 ~WebViewInternalRemoveContentScriptsFunction() { |
| (...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 924 // Will finish asynchronously. | 878 // Will finish asynchronously. |
| 925 return true; | 879 return true; |
| 926 } | 880 } |
| 927 | 881 |
| 928 void WebViewInternalClearDataFunction::ClearDataDone() { | 882 void WebViewInternalClearDataFunction::ClearDataDone() { |
| 929 Release(); // Balanced in RunAsync(). | 883 Release(); // Balanced in RunAsync(). |
| 930 SendResponse(true); | 884 SendResponse(true); |
| 931 } | 885 } |
| 932 | 886 |
| 933 } // namespace extensions | 887 } // namespace extensions |
| OLD | NEW |