forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaw_contents_io_thread_client_impl.cc
407 lines (336 loc) · 14.2 KB
/
aw_contents_io_thread_client_impl.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "android_webview/native/aw_contents_io_thread_client_impl.h"
#include <map>
#include <memory>
#include <utility>
#include "android_webview/browser/net/aw_web_resource_request.h"
#include "android_webview/common/devtools_instrumentation.h"
#include "android_webview/native/aw_contents_background_thread_client.h"
#include "android_webview/native/aw_web_resource_response_impl.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/android/jni_weak_ref.h"
#include "base/lazy_instance.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/resource_request_info.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_observer.h"
#include "jni/AwContentsIoThreadClient_jni.h"
#include "net/url_request/url_request.h"
using base::android::AttachCurrentThread;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
using base::android::ToJavaArrayOfStrings;
using base::LazyInstance;
using content::BrowserThread;
using content::RenderFrameHost;
using content::ResourceType;
using content::WebContents;
using std::map;
using std::pair;
using std::string;
namespace android_webview {
namespace {
struct IoThreadClientData {
bool pending_association;
JavaObjectWeakGlobalRef io_thread_client;
IoThreadClientData();
};
IoThreadClientData::IoThreadClientData() : pending_association(false) {}
typedef map<pair<int, int>, IoThreadClientData>
RenderFrameHostToIoThreadClientType;
// When browser side navigation is enabled, RenderFrameIDs do not have
// valid render process host and render frame ids for frame navigations.
// We need to identify these by using Frame Tree Node ids.
typedef map<int, IoThreadClientData> FrameTreeNodeToIoThreadClientType;
static pair<int, int> GetRenderFrameHostIdPair(RenderFrameHost* rfh) {
return pair<int, int>(rfh->GetProcess()->GetID(), rfh->GetRoutingID());
}
// RfhToIoThreadClientMap -----------------------------------------------------
class RfhToIoThreadClientMap {
public:
static RfhToIoThreadClientMap* GetInstance();
void Set(pair<int, int> rfh_id, const IoThreadClientData& client);
bool Get(pair<int, int> rfh_id, IoThreadClientData* client);
void Erase(pair<int, int> rfh_id);
void Set(int frame_tree_node_id, const IoThreadClientData& client);
bool Get(int frame_tree_node_id, IoThreadClientData* client);
void Erase(int frame_tree_node_id);
private:
base::Lock map_lock_;
RenderFrameHostToIoThreadClientType rfh_to_io_thread_client_;
FrameTreeNodeToIoThreadClientType frame_tree_node_to_io_thread_client_;
};
// static
LazyInstance<RfhToIoThreadClientMap>::DestructorAtExit g_instance_ =
LAZY_INSTANCE_INITIALIZER;
// static
LazyInstance<JavaObjectWeakGlobalRef>::DestructorAtExit g_sw_instance_ =
LAZY_INSTANCE_INITIALIZER;
// static
RfhToIoThreadClientMap* RfhToIoThreadClientMap::GetInstance() {
return g_instance_.Pointer();
}
void RfhToIoThreadClientMap::Set(pair<int, int> rfh_id,
const IoThreadClientData& client) {
base::AutoLock lock(map_lock_);
rfh_to_io_thread_client_[rfh_id] = client;
}
bool RfhToIoThreadClientMap::Get(
pair<int, int> rfh_id, IoThreadClientData* client) {
base::AutoLock lock(map_lock_);
RenderFrameHostToIoThreadClientType::iterator iterator =
rfh_to_io_thread_client_.find(rfh_id);
if (iterator == rfh_to_io_thread_client_.end())
return false;
*client = iterator->second;
return true;
}
void RfhToIoThreadClientMap::Erase(pair<int, int> rfh_id) {
base::AutoLock lock(map_lock_);
rfh_to_io_thread_client_.erase(rfh_id);
}
void RfhToIoThreadClientMap::Set(int frame_tree_node_id,
const IoThreadClientData& client) {
base::AutoLock lock(map_lock_);
frame_tree_node_to_io_thread_client_[frame_tree_node_id] = client;
}
bool RfhToIoThreadClientMap::Get(int frame_tree_node_id,
IoThreadClientData* client) {
base::AutoLock lock(map_lock_);
FrameTreeNodeToIoThreadClientType::iterator iterator =
frame_tree_node_to_io_thread_client_.find(frame_tree_node_id);
if (iterator == frame_tree_node_to_io_thread_client_.end())
return false;
*client = iterator->second;
return true;
}
void RfhToIoThreadClientMap::Erase(int frame_tree_node_id) {
base::AutoLock lock(map_lock_);
frame_tree_node_to_io_thread_client_.erase(frame_tree_node_id);
}
// ClientMapEntryUpdater ------------------------------------------------------
class ClientMapEntryUpdater : public content::WebContentsObserver {
public:
ClientMapEntryUpdater(JNIEnv* env, WebContents* web_contents,
jobject jdelegate);
void RenderFrameCreated(RenderFrameHost* render_frame_host) override;
void RenderFrameDeleted(RenderFrameHost* render_frame_host) override;
void WebContentsDestroyed() override;
private:
JavaObjectWeakGlobalRef jdelegate_;
};
ClientMapEntryUpdater::ClientMapEntryUpdater(JNIEnv* env,
WebContents* web_contents,
jobject jdelegate)
: content::WebContentsObserver(web_contents),
jdelegate_(env, jdelegate) {
DCHECK(web_contents);
DCHECK(jdelegate);
if (web_contents->GetMainFrame())
RenderFrameCreated(web_contents->GetMainFrame());
}
void ClientMapEntryUpdater::RenderFrameCreated(RenderFrameHost* rfh) {
IoThreadClientData client_data;
client_data.io_thread_client = jdelegate_;
client_data.pending_association = false;
RfhToIoThreadClientMap::GetInstance()->Set(GetRenderFrameHostIdPair(rfh),
client_data);
RfhToIoThreadClientMap::GetInstance()->Set(rfh->GetFrameTreeNodeId(),
client_data);
}
void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) {
RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh));
RfhToIoThreadClientMap::GetInstance()->Erase(rfh->GetFrameTreeNodeId());
}
void ClientMapEntryUpdater::WebContentsDestroyed() {
delete this;
}
} // namespace
// AwContentsIoThreadClientImpl -----------------------------------------------
// static
std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID(
int render_process_id,
int render_frame_id) {
pair<int, int> rfh_id(render_process_id, render_frame_id);
IoThreadClientData client_data;
if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data))
return std::unique_ptr<AwContentsIoThreadClient>();
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> java_delegate =
client_data.io_thread_client.get(env);
DCHECK(!client_data.pending_association || java_delegate.is_null());
return std::unique_ptr<AwContentsIoThreadClient>(
new AwContentsIoThreadClientImpl(client_data.pending_association,
java_delegate));
}
std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID(
int frame_tree_node_id) {
IoThreadClientData client_data;
if (!RfhToIoThreadClientMap::GetInstance()->Get(frame_tree_node_id,
&client_data))
return std::unique_ptr<AwContentsIoThreadClient>();
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> java_delegate =
client_data.io_thread_client.get(env);
DCHECK(!client_data.pending_association || java_delegate.is_null());
return std::unique_ptr<AwContentsIoThreadClient>(
new AwContentsIoThreadClientImpl(client_data.pending_association,
java_delegate));
}
// static
void AwContentsIoThreadClient::SubFrameCreated(int render_process_id,
int parent_render_frame_id,
int child_render_frame_id) {
pair<int, int> parent_rfh_id(render_process_id, parent_render_frame_id);
pair<int, int> child_rfh_id(render_process_id, child_render_frame_id);
IoThreadClientData client_data;
if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id,
&client_data)) {
NOTREACHED();
return;
}
RfhToIoThreadClientMap::GetInstance()->Set(child_rfh_id, client_data);
}
// static
void AwContentsIoThreadClientImpl::RegisterPendingContents(
WebContents* web_contents) {
IoThreadClientData client_data;
client_data.pending_association = true;
RfhToIoThreadClientMap::GetInstance()->Set(
GetRenderFrameHostIdPair(web_contents->GetMainFrame()), client_data);
}
// static
void AwContentsIoThreadClientImpl::Associate(
WebContents* web_contents,
const JavaRef<jobject>& jclient) {
JNIEnv* env = AttachCurrentThread();
// The ClientMapEntryUpdater lifespan is tied to the WebContents.
new ClientMapEntryUpdater(env, web_contents, jclient.obj());
}
// static
void AwContentsIoThreadClientImpl::SetServiceWorkerIoThreadClient(
const base::android::JavaRef<jobject>& jclient,
const base::android::JavaRef<jobject>& browser_context) {
// TODO: currently there is only one browser context so it is ok to
// store in a global variable, in the future use browser_context to
// obtain the correct instance.
JavaObjectWeakGlobalRef temp(AttachCurrentThread(), jclient.obj());
g_sw_instance_.Get() = temp;
}
// static
std::unique_ptr<AwContentsIoThreadClient>
AwContentsIoThreadClient::GetServiceWorkerIoThreadClient() {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobject> java_delegate = g_sw_instance_.Get().get(env);
if (java_delegate.is_null())
return std::unique_ptr<AwContentsIoThreadClient>();
return std::unique_ptr<AwContentsIoThreadClient>(
new AwContentsIoThreadClientImpl(false, java_delegate));
}
AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl(
bool pending_association,
const JavaRef<jobject>& obj)
: pending_association_(pending_association),
java_object_(obj) {
}
AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() {
// explict, out-of-line destructor.
}
bool AwContentsIoThreadClientImpl::PendingAssociation() const {
return pending_association_;
}
AwContentsIoThreadClient::CacheMode
AwContentsIoThreadClientImpl::GetCacheMode() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (java_object_.is_null())
return AwContentsIoThreadClient::LOAD_DEFAULT;
JNIEnv* env = AttachCurrentThread();
return static_cast<AwContentsIoThreadClient::CacheMode>(
Java_AwContentsIoThreadClient_getCacheMode(env, java_object_));
}
namespace {
std::unique_ptr<AwWebResourceResponse> RunShouldInterceptRequest(
const AwWebResourceRequest& request,
JavaObjectWeakGlobalRef ref) {
DCHECK_CURRENTLY_ON(BrowserThread::FILE);
JNIEnv* env = AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> obj = ref.get(env);
if (obj.is_null())
return nullptr;
AwWebResourceRequest::AwJavaWebResourceRequest java_web_resource_request;
AwWebResourceRequest::ConvertToJava(env, request, &java_web_resource_request);
devtools_instrumentation::ScopedEmbedderCallbackTask embedder_callback(
"shouldInterceptRequest");
ScopedJavaLocalRef<jobject> ret =
AwContentsBackgroundThreadClient::shouldInterceptRequest(
env, obj, java_web_resource_request.jurl, request.is_main_frame,
request.has_user_gesture, java_web_resource_request.jmethod,
java_web_resource_request.jheader_names,
java_web_resource_request.jheader_values);
return std::unique_ptr<AwWebResourceResponse>(
ret.is_null() ? nullptr : new AwWebResourceResponseImpl(ret));
}
std::unique_ptr<AwWebResourceResponse> ReturnNull() {
return std::unique_ptr<AwWebResourceResponse>();
}
} // namespace
void AwContentsIoThreadClientImpl::ShouldInterceptRequestAsync(
const net::URLRequest* request,
const ShouldInterceptRequestResultCallback callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
base::Callback<std::unique_ptr<AwWebResourceResponse>()> get_response =
base::Bind(&ReturnNull);
JNIEnv* env = AttachCurrentThread();
if (bg_thread_client_object_.is_null() && !java_object_.is_null()) {
bg_thread_client_object_.Reset(
Java_AwContentsIoThreadClient_getBackgroundThreadClient(env,
java_object_));
}
if (!bg_thread_client_object_.is_null()) {
get_response = base::Bind(
&RunShouldInterceptRequest, AwWebResourceRequest(*request),
JavaObjectWeakGlobalRef(env, bg_thread_client_object_.obj()));
}
BrowserThread::PostTaskAndReplyWithResult(BrowserThread::FILE, FROM_HERE,
get_response, callback);
}
bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldBlockContentUrls(env,
java_object_);
}
bool AwContentsIoThreadClientImpl::ShouldBlockFileUrls() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldBlockFileUrls(env, java_object_);
}
bool AwContentsIoThreadClientImpl::ShouldAcceptThirdPartyCookies() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldAcceptThirdPartyCookies(
env, java_object_);
}
bool AwContentsIoThreadClientImpl::ShouldBlockNetworkLoads() const {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env,
java_object_);
}
} // namespace android_webview