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

Side by Side Diff: android_webview/browser/net/aw_request_interceptor.cc

Issue 1350553005: [Android WebView] Call shouldInterceptRequest on a background thread (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Moved code a bit Created 5 years, 2 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 (c) 2012 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 "android_webview/browser/aw_web_resource_response.h" 5 #include "android_webview/browser/net/aw_request_interceptor.h"
6 6
7 #include "android_webview/browser/aw_contents_io_thread_client.h"
7 #include "android_webview/browser/input_stream.h" 8 #include "android_webview/browser/input_stream.h"
8 #include "android_webview/browser/net/android_stream_reader_url_request_job.h" 9 #include "android_webview/browser/net/android_stream_reader_url_request_job.h"
10 #include "android_webview/browser/net/aw_web_resource_response.h"
9 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
12 #include "base/supports_user_data.h"
13 #include "content/public/browser/browser_thread.h"
14 #include "content/public/browser/resource_request_info.h"
10 #include "net/http/http_response_headers.h" 15 #include "net/http/http_response_headers.h"
16 #include "net/url_request/url_request_job.h"
11 17
12 namespace android_webview { 18 namespace android_webview {
13 19
14 namespace { 20 namespace {
15 21
22 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey;
23
16 class StreamReaderJobDelegateImpl 24 class StreamReaderJobDelegateImpl
17 : public AndroidStreamReaderURLRequestJob::Delegate { 25 : public AndroidStreamReaderURLRequestJob::Delegate {
18 public: 26 public:
19 StreamReaderJobDelegateImpl( 27 StreamReaderJobDelegateImpl(
20 scoped_ptr<AwWebResourceResponse> aw_web_resource_response) 28 scoped_ptr<AwWebResourceResponse> aw_web_resource_response)
21 : aw_web_resource_response_(aw_web_resource_response.Pass()) { 29 : aw_web_resource_response_(aw_web_resource_response.Pass()) {
22 DCHECK(aw_web_resource_response_); 30 DCHECK(aw_web_resource_response_);
23 } 31 }
24 32
25 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env, 33 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
58 status_line.append(reason_phrase); 66 status_line.append(reason_phrase);
59 headers->ReplaceStatusLine(status_line); 67 headers->ReplaceStatusLine(status_line);
60 } 68 }
61 aw_web_resource_response_->GetResponseHeaders(env, headers); 69 aw_web_resource_response_->GetResponseHeaders(env, headers);
62 } 70 }
63 71
64 private: 72 private:
65 scoped_ptr<AwWebResourceResponse> aw_web_resource_response_; 73 scoped_ptr<AwWebResourceResponse> aw_web_resource_response_;
66 }; 74 };
67 75
76 class ShouldInterceptRequestAdaptor
77 : public AndroidStreamReaderURLRequestJob::DelegateObtainer {
78 public:
79 explicit ShouldInterceptRequestAdaptor(
80 scoped_ptr<AwContentsIoThreadClient> io_thread_client)
81 : io_thread_client_(io_thread_client.Pass()) {}
82 ~ShouldInterceptRequestAdaptor() override {}
83
84 void ObtainDelegate(net::URLRequest* request,
85 Callback callback) override {
86 callback_ = callback;
87 io_thread_client_->ShouldInterceptRequestAsync(
mmenke 2015/10/01 18:34:40 Is it a problem if io_thread_client_ is deleted wh
mnaganov (inactive) 2015/10/01 19:51:04 ShouldInterceptRequestAdaptor owns io_thread_clien
mmenke 2015/10/01 20:06:53 Right, and the Job owns ShouldInterceptRequestAdap
boliu 2015/10/01 20:28:36 That case looks fine :)
88 request,
89 base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained,
90 // The lifetime of the DelegateObtainer is managed by
91 // AndroidStreamReaderURLRequestJob.
92 base::Unretained(this)));
mmenke 2015/10/01 18:34:40 This should be a weak_ptr, no? It is a bit unfort
mmenke 2015/10/01 18:41:35 On second thought, if IOThreadClient handles being
boliu 2015/10/01 19:13:56 I don't think IOThreadClient does that. So this do
mnaganov (inactive) 2015/10/01 19:51:04 Right, I have also realized that over the lunch. U
93 }
94
95 private:
96 void WebResourceResponseObtained(
97 scoped_ptr<AwWebResourceResponse> response) {
98 if (response) {
99 callback_.Run(
100 make_scoped_ptr(new StreamReaderJobDelegateImpl(response.Pass()))
101 .Pass());
102 } else {
103 callback_.Run(nullptr);
104 }
105 }
106
107 scoped_ptr<AwContentsIoThreadClient> io_thread_client_;
108 Callback callback_;
109
110 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor);
111 };
112
68 } // namespace 113 } // namespace
69 114
70 // static 115 AwRequestInterceptor::AwRequestInterceptor() {}
71 net::URLRequestJob* AwWebResourceResponse::CreateJobFor( 116
72 scoped_ptr<AwWebResourceResponse> aw_web_resource_response, 117 AwRequestInterceptor::~AwRequestInterceptor() {}
118
119 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest(
73 net::URLRequest* request, 120 net::URLRequest* request,
74 net::NetworkDelegate* network_delegate) { 121 net::NetworkDelegate* network_delegate) const {
75 DCHECK(aw_web_resource_response); 122 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
76 DCHECK(request);
77 DCHECK(network_delegate);
78 123
124 // MaybeInterceptRequest can be called multiple times for the same request.
125 if (request->GetUserData(kRequestAlreadyHasJobDataKey))
126 return nullptr;
127
128 int render_process_id, render_frame_id;
129 if (!content::ResourceRequestInfo::GetRenderFrameForRequest(
130 request, &render_process_id, &render_frame_id)) {
131 return nullptr;
132 }
133
134 scoped_ptr<AwContentsIoThreadClient> io_thread_client =
135 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id);
136
137 if (!io_thread_client)
138 return nullptr;
139
140 GURL referrer(request->referrer());
141 if (referrer.is_valid() &&
142 (!request->is_pending() || request->is_redirecting())) {
143 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer,
144 referrer.spec(), true);
145 }
146 request->SetUserData(kRequestAlreadyHasJobDataKey,
147 new base::SupportsUserData::Data());
79 return new AndroidStreamReaderURLRequestJob( 148 return new AndroidStreamReaderURLRequestJob(
80 request, 149 request, network_delegate,
81 network_delegate,
82 make_scoped_ptr( 150 make_scoped_ptr(
83 new StreamReaderJobDelegateImpl(aw_web_resource_response.Pass()))); 151 new ShouldInterceptRequestAdaptor(io_thread_client.Pass()))
152 .Pass(),
mmenke 2015/10/01 18:34:40 nit: .Pass() isn't needed with make_scoped_ptr, i
mnaganov (inactive) 2015/10/01 19:51:04 Neat, thanks!
153 true);
84 } 154 }
85 155
86 } // namespace android_webview 156 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698