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
|
// 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 <utility>
#include "android_webview/native/intercepted_request_data_impl.h"
#include "base/android/jni_helper.h"
#include "base/android/jni_string.h"
#include "base/lazy_instance.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "content/public/browser/browser_thread.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 "googleurl/src/gurl.h"
#include "net/url_request/url_request.h"
#include "jni/AwContentsIoThreadClient_jni.h"
using base::android::AttachCurrentThread;
using base::android::ConvertUTF8ToJavaString;
using base::android::JavaRef;
using base::android::ScopedJavaLocalRef;
using base::LazyInstance;
using content::BrowserThread;
using content::RenderViewHost;
using content::WebContents;
using std::map;
using std::pair;
namespace android_webview {
namespace {
typedef map<pair<int, int>, JavaObjectWeakGlobalRef>
RenderViewHostToWeakDelegateMapType;
static pair<int, int> GetRenderViewHostIdPair(RenderViewHost* rvh) {
return pair<int, int>(rvh->GetProcess()->GetID(), rvh->GetRoutingID());
}
// RvhToIoThreadClientMap -----------------------------------------------------
class RvhToIoThreadClientMap {
public:
static RvhToIoThreadClientMap* GetInstance();
void Insert(pair<int, int> rvh_id, JavaObjectWeakGlobalRef jdelegate);
ScopedJavaLocalRef<jobject> Get(pair<int, int> rvh_id);
void Erase(pair<int, int> rvh_id);
private:
static LazyInstance<RvhToIoThreadClientMap> g_instance_;
base::Lock map_lock_;
RenderViewHostToWeakDelegateMapType rvh_to_weak_delegate_map_;
};
// static
LazyInstance<RvhToIoThreadClientMap> RvhToIoThreadClientMap::g_instance_ =
LAZY_INSTANCE_INITIALIZER;
// static
RvhToIoThreadClientMap* RvhToIoThreadClientMap::GetInstance() {
return g_instance_.Pointer();
}
void RvhToIoThreadClientMap::Insert(pair<int, int> rvh_id,
JavaObjectWeakGlobalRef jdelegate) {
base::AutoLock lock(map_lock_);
rvh_to_weak_delegate_map_[rvh_id] = jdelegate;
}
ScopedJavaLocalRef<jobject> RvhToIoThreadClientMap::Get(
pair<int, int> rvh_id) {
base::AutoLock lock(map_lock_);
RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator =
rvh_to_weak_delegate_map_.find(rvh_id);
if (weak_delegate_iterator == rvh_to_weak_delegate_map_.end())
return ScopedJavaLocalRef<jobject>();
JNIEnv* env = AttachCurrentThread();
return weak_delegate_iterator->second.get(env);
}
void RvhToIoThreadClientMap::Erase(pair<int, int> rvh_id) {
base::AutoLock lock(map_lock_);
rvh_to_weak_delegate_map_.erase(rvh_id);
}
// ClientMapEntryUpdater ------------------------------------------------------
class ClientMapEntryUpdater : public content::WebContentsObserver {
public:
ClientMapEntryUpdater(JNIEnv* env, WebContents* web_contents,
jobject jdelegate);
virtual void RenderViewCreated(RenderViewHost* render_view_host) OVERRIDE;
virtual void RenderViewForInterstitialPageCreated(
RenderViewHost* render_view_host) OVERRIDE;
virtual void RenderViewDeleted(RenderViewHost* render_view_host) OVERRIDE;
virtual void WebContentsDestroyed(WebContents* web_contents);
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->GetRenderViewHost())
RenderViewCreated(web_contents->GetRenderViewHost());
}
void ClientMapEntryUpdater::RenderViewCreated(RenderViewHost* rvh) {
RvhToIoThreadClientMap::GetInstance()->Insert(
GetRenderViewHostIdPair(rvh), jdelegate_);
}
void ClientMapEntryUpdater::RenderViewForInterstitialPageCreated(
RenderViewHost* rvh) {
RenderViewCreated(rvh);
}
void ClientMapEntryUpdater::RenderViewDeleted(RenderViewHost* rvh) {
RvhToIoThreadClientMap::GetInstance()->Erase(GetRenderViewHostIdPair(rvh));
}
void ClientMapEntryUpdater::WebContentsDestroyed(WebContents* web_contents) {
if (web_contents->GetRenderViewHost())
RenderViewDeleted(web_contents->GetRenderViewHost());
delete this;
}
} // namespace
// AwContentsIoThreadClientImpl -----------------------------------------------
// static
scoped_ptr<AwContentsIoThreadClient>
AwContentsIoThreadClient::FromID(int render_process_id, int render_view_id) {
pair<int, int> rvh_id(render_process_id, render_view_id);
ScopedJavaLocalRef<jobject> java_delegate =
RvhToIoThreadClientMap::GetInstance()->Get(rvh_id);
if (java_delegate.is_null())
return scoped_ptr<AwContentsIoThreadClient>();
return scoped_ptr<AwContentsIoThreadClient>(
new AwContentsIoThreadClientImpl(java_delegate));
}
// 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());
}
AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl(
const JavaRef<jobject>& obj)
: java_object_(obj) {
}
AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() {
// explict, out-of-line destructor.
}
AwContentsIoThreadClient::CacheMode
AwContentsIoThreadClientImpl::GetCacheMode() const {
DCHECK(BrowserThread::CurrentlyOn(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_.obj()));
}
scoped_ptr<InterceptedRequestData>
AwContentsIoThreadClientImpl::ShouldInterceptRequest(
const GURL& location,
const net::URLRequest* request) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (java_object_.is_null())
return scoped_ptr<InterceptedRequestData>();
const content::ResourceRequestInfo* info =
content::ResourceRequestInfo::ForRequest(request);
bool is_main_frame = info && info->IsMainFrame();
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> jstring_url =
ConvertUTF8ToJavaString(env, location.spec());
ScopedJavaLocalRef<jobject> ret =
Java_AwContentsIoThreadClient_shouldInterceptRequest(
env, java_object_.obj(), jstring_url.obj(), is_main_frame);
if (ret.is_null())
return scoped_ptr<InterceptedRequestData>();
return scoped_ptr<InterceptedRequestData>(
new InterceptedRequestDataImpl(ret));
}
bool AwContentsIoThreadClientImpl::ShouldBlockContentUrls() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldBlockContentUrls(
env, java_object_.obj());
}
bool AwContentsIoThreadClientImpl::ShouldBlockFileUrls() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldBlockFileUrls(
env, java_object_.obj());
}
bool AwContentsIoThreadClientImpl::ShouldBlockNetworkLoads() const {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (java_object_.is_null())
return false;
JNIEnv* env = AttachCurrentThread();
return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(
env, java_object_.obj());
}
void AwContentsIoThreadClientImpl::NewDownload(
const GURL& url,
const std::string& user_agent,
const std::string& content_disposition,
const std::string& mime_type,
int64 content_length) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (java_object_.is_null())
return;
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> jstring_url =
ConvertUTF8ToJavaString(env, url.spec());
ScopedJavaLocalRef<jstring> jstring_user_agent =
ConvertUTF8ToJavaString(env, user_agent);
ScopedJavaLocalRef<jstring> jstring_content_disposition =
ConvertUTF8ToJavaString(env, content_disposition);
ScopedJavaLocalRef<jstring> jstring_mime_type =
ConvertUTF8ToJavaString(env, mime_type);
Java_AwContentsIoThreadClient_onDownloadStart(
env,
java_object_.obj(),
jstring_url.obj(),
jstring_user_agent.obj(),
jstring_content_disposition.obj(),
jstring_mime_type.obj(),
content_length);
}
void AwContentsIoThreadClientImpl::NewLoginRequest(const std::string& realm,
const std::string& account,
const std::string& args) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
if (java_object_.is_null())
return;
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm);
ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args);
ScopedJavaLocalRef<jstring> jaccount;
if (!account.empty())
jaccount = ConvertUTF8ToJavaString(env, account);
Java_AwContentsIoThreadClient_newLoginRequest(
env, java_object_.obj(), jrealm.obj(), jaccount.obj(), jargs.obj());
}
bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) {
return RegisterNativesImpl(env);
}
} // namespace android_webview
|