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

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: Re-uploading with standard diff settings to work around Windows patch issues 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
(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 "android_webview/browser/net/aw_request_interceptor.h"
6
7 #include "android_webview/browser/aw_contents_io_thread_client.h"
8 #include "android_webview/browser/input_stream.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"
11 #include "base/memory/weak_ptr.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/supports_user_data.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/browser/resource_request_info.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/url_request/url_request_job.h"
18
19 namespace android_webview {
20
21 namespace {
22
23 const void* const kRequestAlreadyHasJobDataKey = &kRequestAlreadyHasJobDataKey;
24
25 class StreamReaderJobDelegateImpl
26 : public AndroidStreamReaderURLRequestJob::Delegate {
27 public:
28 StreamReaderJobDelegateImpl(
29 scoped_ptr<AwWebResourceResponse> aw_web_resource_response)
30 : aw_web_resource_response_(aw_web_resource_response.Pass()) {
31 DCHECK(aw_web_resource_response_);
32 }
33
34 scoped_ptr<InputStream> OpenInputStream(JNIEnv* env,
35 const GURL& url) override {
36 return aw_web_resource_response_->GetInputStream(env).Pass();
37 }
38
39 void OnInputStreamOpenFailed(net::URLRequest* request,
40 bool* restart) override {
41 *restart = false;
42 }
43
44 bool GetMimeType(JNIEnv* env,
45 net::URLRequest* request,
46 android_webview::InputStream* stream,
47 std::string* mime_type) override {
48 return aw_web_resource_response_->GetMimeType(env, mime_type);
49 }
50
51 bool GetCharset(JNIEnv* env,
52 net::URLRequest* request,
53 android_webview::InputStream* stream,
54 std::string* charset) override {
55 return aw_web_resource_response_->GetCharset(env, charset);
56 }
57
58 void AppendResponseHeaders(JNIEnv* env,
59 net::HttpResponseHeaders* headers) override {
60 int status_code;
61 std::string reason_phrase;
62 if (aw_web_resource_response_->GetStatusInfo(
63 env, &status_code, &reason_phrase)) {
64 std::string status_line("HTTP/1.1 ");
65 status_line.append(base::IntToString(status_code));
66 status_line.append(" ");
67 status_line.append(reason_phrase);
68 headers->ReplaceStatusLine(status_line);
69 }
70 aw_web_resource_response_->GetResponseHeaders(env, headers);
71 }
72
73 private:
74 scoped_ptr<AwWebResourceResponse> aw_web_resource_response_;
75 };
76
77 class ShouldInterceptRequestAdaptor
78 : public AndroidStreamReaderURLRequestJob::DelegateObtainer {
79 public:
80 explicit ShouldInterceptRequestAdaptor(
81 scoped_ptr<AwContentsIoThreadClient> io_thread_client)
82 : io_thread_client_(io_thread_client.Pass()), weak_factory_(this) {}
83 ~ShouldInterceptRequestAdaptor() override {}
84
85 void ObtainDelegate(net::URLRequest* request,
86 const Callback& callback) override {
87 callback_ = callback;
88 io_thread_client_->ShouldInterceptRequestAsync(
89 // The request is only used while preparing the call, not retained.
90 request,
91 base::Bind(&ShouldInterceptRequestAdaptor::WebResourceResponseObtained,
92 // The lifetime of the DelegateObtainer is managed by
93 // AndroidStreamReaderURLRequestJob, it might get deleted.
94 weak_factory_.GetWeakPtr()));
95 }
96
97 private:
98 void WebResourceResponseObtained(
99 scoped_ptr<AwWebResourceResponse> response) {
100 if (response) {
101 callback_.Run(
102 make_scoped_ptr(new StreamReaderJobDelegateImpl(response.Pass())));
103 } else {
104 callback_.Run(nullptr);
105 }
106 }
107
108 scoped_ptr<AwContentsIoThreadClient> io_thread_client_;
109 Callback callback_;
110 base::WeakPtrFactory<ShouldInterceptRequestAdaptor> weak_factory_;
111
112 DISALLOW_COPY_AND_ASSIGN(ShouldInterceptRequestAdaptor);
113 };
114
115 } // namespace
116
117 AwRequestInterceptor::AwRequestInterceptor() {}
118
119 AwRequestInterceptor::~AwRequestInterceptor() {}
120
121 net::URLRequestJob* AwRequestInterceptor::MaybeInterceptRequest(
122 net::URLRequest* request,
123 net::NetworkDelegate* network_delegate) const {
124 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
125
126 // MaybeInterceptRequest can be called multiple times for the same request.
127 if (request->GetUserData(kRequestAlreadyHasJobDataKey))
128 return nullptr;
129
130 int render_process_id, render_frame_id;
131 if (!content::ResourceRequestInfo::GetRenderFrameForRequest(
132 request, &render_process_id, &render_frame_id)) {
133 return nullptr;
134 }
135
136 scoped_ptr<AwContentsIoThreadClient> io_thread_client =
137 AwContentsIoThreadClient::FromID(render_process_id, render_frame_id);
138
139 if (!io_thread_client)
140 return nullptr;
141
142 GURL referrer(request->referrer());
143 if (referrer.is_valid() &&
144 (!request->is_pending() || request->is_redirecting())) {
145 request->SetExtraRequestHeaderByName(net::HttpRequestHeaders::kReferer,
146 referrer.spec(), true);
147 }
148 request->SetUserData(kRequestAlreadyHasJobDataKey,
149 new base::SupportsUserData::Data());
150 return new AndroidStreamReaderURLRequestJob(
151 request, network_delegate,
152 make_scoped_ptr(
153 new ShouldInterceptRequestAdaptor(io_thread_client.Pass())),
154 true);
155 }
156
157 } // namespace android_webview
OLDNEW
« no previous file with comments | « android_webview/browser/net/aw_request_interceptor.h ('k') | android_webview/browser/net/aw_url_request_context_getter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698