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

Side by Side Diff: extensions/browser/api/guest_view/web_view/web_view_internal_api.cc

Issue 1056533002: Implement <webview>.addContentScript/removeContentScript API [2] (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webview_addremove_contentscripts_2
Patch Set: Another round of comments. 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/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
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
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 AddRef();
394 url_fetcher_->Start(render_view_host()->GetProcess()->GetID(), 346 url_fetcher_.reset(new WebUIURLFetcher(
395 render_view_host()->GetRoutingID(), file_url); 347 this->browser_context(), render_view_host()->GetProcess()->GetID(),
348 render_view_host()->GetRoutingID(), file_url, callback));
349 url_fetcher_->Start();
396 return true; 350 return true;
397 } 351 }
398 352
399 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) { 353 bool WebViewInternalExecuteCodeFunction::LoadFile(const std::string& file) {
400 if (!extension()) { 354 if (!extension()) {
401 if (LoadFileForWebUI( 355 if (LoadFileForWebUI(
402 *details_->file, 356 *details_->file,
403 base::Bind( 357 base::Bind(
404 &WebViewInternalExecuteCodeFunction::DidLoadAndLocalizeFile, 358 &WebViewInternalExecuteCodeFunction::DidLoadAndLocalizeFile,
405 this, file))) 359 base::Unretained(this), file)))
Devlin 2015/04/21 22:32:20 I think I liked this better the other way. It's k
Xi Han 2015/04/22 15:32:10 Ok, revert these changes.
406 return true; 360 return true;
407 361
408 SendResponse(false); 362 SendResponse(false);
409 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file); 363 error_ = ErrorUtils::FormatErrorMessage(kLoadFileError, file);
364 Release();
410 return false; 365 return false;
411 } 366 }
412 return ExecuteCodeFunction::LoadFile(file); 367 return ExecuteCodeFunction::LoadFile(file);
413 } 368 }
414 369
370 void WebViewInternalExecuteCodeFunction::DidLoadAndLocalizeFile(
371 const std::string& file,
372 bool success,
373 const std::string& data) {
374 ExecuteCodeFunction::DidLoadAndLocalizeFile(file, success, data);
375 Release();
376 }
377
415 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() { 378 WebViewInternalExecuteScriptFunction::WebViewInternalExecuteScriptFunction() {
416 } 379 }
417 380
418 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished( 381 void WebViewInternalExecuteScriptFunction::OnExecuteCodeFinished(
419 const std::string& error, 382 const std::string& error,
420 const GURL& on_url, 383 const GURL& on_url,
421 const base::ListValue& result) { 384 const base::ListValue& result) {
422 if (error.empty()) 385 if (error.empty())
423 SetResult(result.DeepCopy()); 386 SetResult(result.DeepCopy());
424 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished( 387 WebViewInternalExecuteCodeFunction::OnExecuteCodeFinished(
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
458 bool incognito_enabled = browser_context()->IsOffTheRecord(); 421 bool incognito_enabled = browser_context()->IsOffTheRecord();
459 422
460 if (!ParseContentScripts(params->content_script_list, extension(), host_id, 423 if (!ParseContentScripts(params->content_script_list, extension(), host_id,
461 incognito_enabled, owner_base_url, &result, &error_)) 424 incognito_enabled, owner_base_url, &result, &error_))
462 return RespondNow(Error(error_)); 425 return RespondNow(Error(error_));
463 426
464 WebViewContentScriptManager* manager = 427 WebViewContentScriptManager* manager =
465 WebViewContentScriptManager::Get(browser_context()); 428 WebViewContentScriptManager::Get(browser_context());
466 DCHECK(manager); 429 DCHECK(manager);
467 430
468 manager->AddContentScripts(sender_web_contents, params->instance_id, host_id, 431 manager->AddContentScripts(sender_web_contents,
469 result); 432 render_view_host()->GetRoutingID(),
433 params->instance_id, host_id, result);
470 434
471 return RespondNow(NoArguments()); 435 return RespondNow(NoArguments());
472 } 436 }
473 437
474 WebViewInternalRemoveContentScriptsFunction:: 438 WebViewInternalRemoveContentScriptsFunction::
475 WebViewInternalRemoveContentScriptsFunction() { 439 WebViewInternalRemoveContentScriptsFunction() {
476 } 440 }
477 441
478 WebViewInternalRemoveContentScriptsFunction:: 442 WebViewInternalRemoveContentScriptsFunction::
479 ~WebViewInternalRemoveContentScriptsFunction() { 443 ~WebViewInternalRemoveContentScriptsFunction() {
(...skipping 444 matching lines...) Expand 10 before | Expand all | Expand 10 after
924 // Will finish asynchronously. 888 // Will finish asynchronously.
925 return true; 889 return true;
926 } 890 }
927 891
928 void WebViewInternalClearDataFunction::ClearDataDone() { 892 void WebViewInternalClearDataFunction::ClearDataDone() {
929 Release(); // Balanced in RunAsync(). 893 Release(); // Balanced in RunAsync().
930 SendResponse(true); 894 SendResponse(true);
931 } 895 }
932 896
933 } // namespace extensions 897 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698