OLD | NEW |
1 // Copyright (c) 2012 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/native/aw_contents_io_thread_client_impl.h" | 5 #include "android_webview/browser/aw_contents_io_thread_client_impl.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
| 11 #include "android_webview/browser/aw_contents_background_thread_client.h" |
| 12 #include "android_webview/browser/aw_web_resource_response_impl.h" |
11 #include "android_webview/browser/net/aw_web_resource_request.h" | 13 #include "android_webview/browser/net/aw_web_resource_request.h" |
12 #include "android_webview/common/devtools_instrumentation.h" | 14 #include "android_webview/common/devtools_instrumentation.h" |
13 #include "android_webview/native/aw_contents_background_thread_client.h" | |
14 #include "android_webview/native/aw_web_resource_response_impl.h" | |
15 #include "base/android/jni_array.h" | 15 #include "base/android/jni_array.h" |
16 #include "base/android/jni_string.h" | 16 #include "base/android/jni_string.h" |
17 #include "base/android/jni_weak_ref.h" | 17 #include "base/android/jni_weak_ref.h" |
18 #include "base/lazy_instance.h" | 18 #include "base/lazy_instance.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
21 #include "content/public/browser/render_frame_host.h" | 21 #include "content/public/browser/render_frame_host.h" |
22 #include "content/public/browser/render_process_host.h" | 22 #include "content/public/browser/render_process_host.h" |
23 #include "content/public/browser/render_view_host.h" | 23 #include "content/public/browser/render_view_host.h" |
24 #include "content/public/browser/resource_request_info.h" | 24 #include "content/public/browser/resource_request_info.h" |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 RfhToIoThreadClientMap* RfhToIoThreadClientMap::GetInstance() { | 96 RfhToIoThreadClientMap* RfhToIoThreadClientMap::GetInstance() { |
97 return g_instance_.Pointer(); | 97 return g_instance_.Pointer(); |
98 } | 98 } |
99 | 99 |
100 void RfhToIoThreadClientMap::Set(pair<int, int> rfh_id, | 100 void RfhToIoThreadClientMap::Set(pair<int, int> rfh_id, |
101 const IoThreadClientData& client) { | 101 const IoThreadClientData& client) { |
102 base::AutoLock lock(map_lock_); | 102 base::AutoLock lock(map_lock_); |
103 rfh_to_io_thread_client_[rfh_id] = client; | 103 rfh_to_io_thread_client_[rfh_id] = client; |
104 } | 104 } |
105 | 105 |
106 bool RfhToIoThreadClientMap::Get( | 106 bool RfhToIoThreadClientMap::Get(pair<int, int> rfh_id, |
107 pair<int, int> rfh_id, IoThreadClientData* client) { | 107 IoThreadClientData* client) { |
108 base::AutoLock lock(map_lock_); | 108 base::AutoLock lock(map_lock_); |
109 RenderFrameHostToIoThreadClientType::iterator iterator = | 109 RenderFrameHostToIoThreadClientType::iterator iterator = |
110 rfh_to_io_thread_client_.find(rfh_id); | 110 rfh_to_io_thread_client_.find(rfh_id); |
111 if (iterator == rfh_to_io_thread_client_.end()) | 111 if (iterator == rfh_to_io_thread_client_.end()) |
112 return false; | 112 return false; |
113 | 113 |
114 *client = iterator->second; | 114 *client = iterator->second; |
115 return true; | 115 return true; |
116 } | 116 } |
117 | 117 |
(...skipping 22 matching lines...) Expand all Loading... |
140 | 140 |
141 void RfhToIoThreadClientMap::Erase(int frame_tree_node_id) { | 141 void RfhToIoThreadClientMap::Erase(int frame_tree_node_id) { |
142 base::AutoLock lock(map_lock_); | 142 base::AutoLock lock(map_lock_); |
143 frame_tree_node_to_io_thread_client_.erase(frame_tree_node_id); | 143 frame_tree_node_to_io_thread_client_.erase(frame_tree_node_id); |
144 } | 144 } |
145 | 145 |
146 // ClientMapEntryUpdater ------------------------------------------------------ | 146 // ClientMapEntryUpdater ------------------------------------------------------ |
147 | 147 |
148 class ClientMapEntryUpdater : public content::WebContentsObserver { | 148 class ClientMapEntryUpdater : public content::WebContentsObserver { |
149 public: | 149 public: |
150 ClientMapEntryUpdater(JNIEnv* env, WebContents* web_contents, | 150 ClientMapEntryUpdater(JNIEnv* env, |
| 151 WebContents* web_contents, |
151 jobject jdelegate); | 152 jobject jdelegate); |
152 | 153 |
153 void RenderFrameCreated(RenderFrameHost* render_frame_host) override; | 154 void RenderFrameCreated(RenderFrameHost* render_frame_host) override; |
154 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; | 155 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; |
155 void WebContentsDestroyed() override; | 156 void WebContentsDestroyed() override; |
156 | 157 |
157 private: | 158 private: |
158 JavaObjectWeakGlobalRef jdelegate_; | 159 JavaObjectWeakGlobalRef jdelegate_; |
159 }; | 160 }; |
160 | 161 |
161 ClientMapEntryUpdater::ClientMapEntryUpdater(JNIEnv* env, | 162 ClientMapEntryUpdater::ClientMapEntryUpdater(JNIEnv* env, |
162 WebContents* web_contents, | 163 WebContents* web_contents, |
163 jobject jdelegate) | 164 jobject jdelegate) |
164 : content::WebContentsObserver(web_contents), | 165 : content::WebContentsObserver(web_contents), jdelegate_(env, jdelegate) { |
165 jdelegate_(env, jdelegate) { | |
166 DCHECK(web_contents); | 166 DCHECK(web_contents); |
167 DCHECK(jdelegate); | 167 DCHECK(jdelegate); |
168 | 168 |
169 if (web_contents->GetMainFrame()) | 169 if (web_contents->GetMainFrame()) |
170 RenderFrameCreated(web_contents->GetMainFrame()); | 170 RenderFrameCreated(web_contents->GetMainFrame()); |
171 } | 171 } |
172 | 172 |
173 void ClientMapEntryUpdater::RenderFrameCreated(RenderFrameHost* rfh) { | 173 void ClientMapEntryUpdater::RenderFrameCreated(RenderFrameHost* rfh) { |
174 IoThreadClientData client_data; | 174 IoThreadClientData client_data; |
175 client_data.io_thread_client = jdelegate_; | 175 client_data.io_thread_client = jdelegate_; |
176 client_data.pending_association = false; | 176 client_data.pending_association = false; |
177 RfhToIoThreadClientMap::GetInstance()->Set(GetRenderFrameHostIdPair(rfh), | 177 RfhToIoThreadClientMap::GetInstance()->Set(GetRenderFrameHostIdPair(rfh), |
178 client_data); | 178 client_data); |
179 RfhToIoThreadClientMap::GetInstance()->Set(rfh->GetFrameTreeNodeId(), | 179 RfhToIoThreadClientMap::GetInstance()->Set(rfh->GetFrameTreeNodeId(), |
180 client_data); | 180 client_data); |
181 } | 181 } |
182 | 182 |
183 void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) { | 183 void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) { |
184 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); | 184 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); |
185 RfhToIoThreadClientMap::GetInstance()->Erase(rfh->GetFrameTreeNodeId()); | 185 RfhToIoThreadClientMap::GetInstance()->Erase(rfh->GetFrameTreeNodeId()); |
186 } | 186 } |
187 | 187 |
188 void ClientMapEntryUpdater::WebContentsDestroyed() { | 188 void ClientMapEntryUpdater::WebContentsDestroyed() { |
189 delete this; | 189 delete this; |
190 } | 190 } |
191 | 191 |
192 } // namespace | 192 } // namespace |
193 | 193 |
194 // AwContentsIoThreadClientImpl ----------------------------------------------- | 194 // AwContentsIoThreadClientImpl ----------------------------------------------- |
195 | 195 |
196 // static | 196 // static |
197 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( | 197 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( |
198 int render_process_id, | 198 int render_process_id, |
199 int render_frame_id) { | 199 int render_frame_id) { |
200 pair<int, int> rfh_id(render_process_id, render_frame_id); | 200 pair<int, int> rfh_id(render_process_id, render_frame_id); |
201 IoThreadClientData client_data; | 201 IoThreadClientData client_data; |
202 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) | 202 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
246 // static | 246 // static |
247 void AwContentsIoThreadClientImpl::RegisterPendingContents( | 247 void AwContentsIoThreadClientImpl::RegisterPendingContents( |
248 WebContents* web_contents) { | 248 WebContents* web_contents) { |
249 IoThreadClientData client_data; | 249 IoThreadClientData client_data; |
250 client_data.pending_association = true; | 250 client_data.pending_association = true; |
251 RfhToIoThreadClientMap::GetInstance()->Set( | 251 RfhToIoThreadClientMap::GetInstance()->Set( |
252 GetRenderFrameHostIdPair(web_contents->GetMainFrame()), client_data); | 252 GetRenderFrameHostIdPair(web_contents->GetMainFrame()), client_data); |
253 } | 253 } |
254 | 254 |
255 // static | 255 // static |
256 void AwContentsIoThreadClientImpl::Associate( | 256 void AwContentsIoThreadClientImpl::Associate(WebContents* web_contents, |
257 WebContents* web_contents, | 257 const JavaRef<jobject>& jclient) { |
258 const JavaRef<jobject>& jclient) { | |
259 JNIEnv* env = AttachCurrentThread(); | 258 JNIEnv* env = AttachCurrentThread(); |
260 // The ClientMapEntryUpdater lifespan is tied to the WebContents. | 259 // The ClientMapEntryUpdater lifespan is tied to the WebContents. |
261 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); | 260 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); |
262 } | 261 } |
263 | 262 |
264 // static | 263 // static |
265 void AwContentsIoThreadClientImpl::SetServiceWorkerIoThreadClient( | 264 void AwContentsIoThreadClientImpl::SetServiceWorkerIoThreadClient( |
266 const base::android::JavaRef<jobject>& jclient, | 265 const base::android::JavaRef<jobject>& jclient, |
267 const base::android::JavaRef<jobject>& browser_context) { | 266 const base::android::JavaRef<jobject>& browser_context) { |
268 // TODO: currently there is only one browser context so it is ok to | 267 // TODO: currently there is only one browser context so it is ok to |
(...skipping 12 matching lines...) Expand all Loading... |
281 if (java_delegate.is_null()) | 280 if (java_delegate.is_null()) |
282 return std::unique_ptr<AwContentsIoThreadClient>(); | 281 return std::unique_ptr<AwContentsIoThreadClient>(); |
283 | 282 |
284 return std::unique_ptr<AwContentsIoThreadClient>( | 283 return std::unique_ptr<AwContentsIoThreadClient>( |
285 new AwContentsIoThreadClientImpl(false, java_delegate)); | 284 new AwContentsIoThreadClientImpl(false, java_delegate)); |
286 } | 285 } |
287 | 286 |
288 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( | 287 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( |
289 bool pending_association, | 288 bool pending_association, |
290 const JavaRef<jobject>& obj) | 289 const JavaRef<jobject>& obj) |
291 : pending_association_(pending_association), | 290 : pending_association_(pending_association), java_object_(obj) {} |
292 java_object_(obj) { | |
293 } | |
294 | 291 |
295 AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() { | 292 AwContentsIoThreadClientImpl::~AwContentsIoThreadClientImpl() { |
296 // explict, out-of-line destructor. | 293 // explict, out-of-line destructor. |
297 } | 294 } |
298 | 295 |
299 bool AwContentsIoThreadClientImpl::PendingAssociation() const { | 296 bool AwContentsIoThreadClientImpl::PendingAssociation() const { |
300 return pending_association_; | 297 return pending_association_; |
301 } | 298 } |
302 | 299 |
303 AwContentsIoThreadClient::CacheMode | 300 AwContentsIoThreadClient::CacheMode AwContentsIoThreadClientImpl::GetCacheMode() |
304 AwContentsIoThreadClientImpl::GetCacheMode() const { | 301 const { |
305 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 302 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
306 if (java_object_.is_null()) | 303 if (java_object_.is_null()) |
307 return AwContentsIoThreadClient::LOAD_DEFAULT; | 304 return AwContentsIoThreadClient::LOAD_DEFAULT; |
308 | 305 |
309 JNIEnv* env = AttachCurrentThread(); | 306 JNIEnv* env = AttachCurrentThread(); |
310 return static_cast<AwContentsIoThreadClient::CacheMode>( | 307 return static_cast<AwContentsIoThreadClient::CacheMode>( |
311 Java_AwContentsIoThreadClient_getCacheMode(env, java_object_)); | 308 Java_AwContentsIoThreadClient_getCacheMode(env, java_object_)); |
312 } | 309 } |
313 | 310 |
314 | |
315 namespace { | 311 namespace { |
316 | 312 |
317 std::unique_ptr<AwWebResourceResponse> RunShouldInterceptRequest( | 313 std::unique_ptr<AwWebResourceResponse> RunShouldInterceptRequest( |
318 const AwWebResourceRequest& request, | 314 const AwWebResourceRequest& request, |
319 JavaObjectWeakGlobalRef ref) { | 315 JavaObjectWeakGlobalRef ref) { |
320 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 316 DCHECK_CURRENTLY_ON(BrowserThread::FILE); |
321 JNIEnv* env = AttachCurrentThread(); | 317 JNIEnv* env = AttachCurrentThread(); |
322 base::android::ScopedJavaLocalRef<jobject> obj = ref.get(env); | 318 base::android::ScopedJavaLocalRef<jobject> obj = ref.get(env); |
323 if (obj.is_null()) | 319 if (obj.is_null()) |
324 return nullptr; | 320 return nullptr; |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 bool AwContentsIoThreadClientImpl::ShouldBlockNetworkLoads() const { | 403 bool AwContentsIoThreadClientImpl::ShouldBlockNetworkLoads() const { |
408 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 404 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
409 if (java_object_.is_null()) | 405 if (java_object_.is_null()) |
410 return false; | 406 return false; |
411 | 407 |
412 JNIEnv* env = AttachCurrentThread(); | 408 JNIEnv* env = AttachCurrentThread(); |
413 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env, | 409 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env, |
414 java_object_); | 410 java_object_); |
415 } | 411 } |
416 | 412 |
417 } // namespace android_webview | 413 } // namespace android_webview |
OLD | NEW |