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

Side by Side Diff: chrome/browser/renderer_host/intercept_navigation_resource_throttle.cc

Issue 10310124: Implement a ResourceThrottle for URL overriding in Chrome on Android. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: fix build (Referrer is a struct, not a class) Created 8 years, 6 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
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/renderer_host/intercept_navigation_resource_throttle.h"
6
7 #include "content/public/browser/browser_thread.h"
8 #include "content/public/browser/child_process_security_policy.h"
9 #include "content/public/browser/render_view_host.h"
10 #include "content/public/browser/render_process_host.h"
11 #include "content/public/browser/resource_request_info.h"
12 #include "content/public/browser/resource_controller.h"
13 #include "content/public/browser/web_contents.h"
14 #include "content/public/browser/web_contents_delegate.h"
15 #include "content/public/common/referrer.h"
16 #include "net/url_request/url_request.h"
17
18 using content::BrowserThread;
19 using content::ChildProcessSecurityPolicy;
20 using content::Referrer;
21 using content::RenderViewHost;
22 using content::ResourceRequestInfo;
23 using content::WebContents;
24 using content::WebContentsDelegate;
25
26 namespace {
27
28 void CheckIfShouldIgnoreNavigationOnUIThread(
29 int render_process_id,
30 int render_view_id,
31 const GURL& url,
32 const Referrer& referrer,
33 bool is_content_initiated,
34 InterceptNavigationResourceThrottle::CheckOnUIThreadCallback
35 should_ignore_callback,
36 base::Callback<void(bool)> callback) {
37
38 RenderViewHost* rvh =
39 RenderViewHost::FromID(render_process_id, render_view_id);
40
41 GURL validated_url(url);
42 RenderViewHost::FilterURL(
43 rvh->GetProcess()->GetID(),
44 false,
45 &validated_url);
46
47 bool should_ignore_navigation = false;
48 if (rvh) {
49 should_ignore_navigation = should_ignore_callback.Run(
50 rvh, validated_url, referrer, is_content_initiated);
51 }
52
53 BrowserThread::PostTask(
54 BrowserThread::IO,
55 FROM_HERE,
56 base::Bind(callback, should_ignore_navigation));
57 }
58
59 } // namespace
60
61 InterceptNavigationResourceThrottle::InterceptNavigationResourceThrottle(
62 net::URLRequest* request,
63 CheckOnUIThreadCallback should_ignore_callback)
64 : request_(request),
65 should_ignore_callback_(should_ignore_callback),
66 weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
67 }
68
69 InterceptNavigationResourceThrottle::~InterceptNavigationResourceThrottle() {
70 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
71 }
72
73 void InterceptNavigationResourceThrottle::WillStartRequest(bool* defer) {
74 *defer = CheckIfShouldIgnoreNavigation(request_->url());
75 }
76
77 void InterceptNavigationResourceThrottle::WillRedirectRequest(
78 const GURL& new_url,
79 bool* defer) {
80 *defer = CheckIfShouldIgnoreNavigation(new_url);
81 }
82
83 bool InterceptNavigationResourceThrottle::CheckIfShouldIgnoreNavigation(
84 const GURL& url) {
85 const ResourceRequestInfo* info = ResourceRequestInfo::ForRequest(request_);
86 if (!info)
87 return false;
88
89 int render_process_id, render_view_id;
90 if (!info->GetAssociatedRenderView(&render_process_id, &render_view_id))
91 return false;
92
93 // This class should only be instantiated for top level frame requests.
94 DCHECK(info->IsMainFrame());
95
96 BrowserThread::PostTask(
97 BrowserThread::UI,
98 FROM_HERE,
99 base::Bind(
100 &CheckIfShouldIgnoreNavigationOnUIThread,
101 render_process_id,
102 render_view_id,
103 url,
104 Referrer(GURL(request_->referrer()), info->GetReferrerPolicy()),
105 info->HasUserGesture(),
106 should_ignore_callback_,
107 base::Bind(
108 &InterceptNavigationResourceThrottle::OnResultObtained,
109 weak_ptr_factory_.GetWeakPtr())));
110
111 // Defer request while we wait for the UI thread to check if the navigation
112 // should be ignored.
113 return true;
114 }
115
116 void InterceptNavigationResourceThrottle::OnResultObtained(
117 bool should_ignore_navigation) {
118 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
119
120 if (should_ignore_navigation) {
121 // TODO(mkosiba): Cancel() results in a CANCELLED status but what we really
122 // want is a HANDLED_EXTERNALLY status.
123 controller()->Cancel();
124 } else {
125 controller()->Resume();
126 }
127 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698