Chromium Code Reviews| 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/native/aw_contents_io_thread_client_impl.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "android_webview/native/intercepted_request_data_impl.h" | 10 #include "android_webview/native/intercepted_request_data_impl.h" |
| 11 #include "base/android/jni_helper.h" | 11 #include "base/android/jni_helper.h" |
| 12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
| 13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/observer_list_threadsafe.h" | |
| 16 #include "base/synchronization/lock.h" | 17 #include "base/synchronization/lock.h" |
| 17 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/browser/render_process_host.h" | 19 #include "content/public/browser/render_process_host.h" |
| 19 #include "content/public/browser/render_view_host.h" | 20 #include "content/public/browser/render_view_host.h" |
| 20 #include "content/public/browser/web_contents.h" | 21 #include "content/public/browser/web_contents.h" |
| 21 #include "content/public/browser/web_contents_observer.h" | 22 #include "content/public/browser/web_contents_observer.h" |
| 22 #include "googleurl/src/gurl.h" | 23 #include "googleurl/src/gurl.h" |
| 23 #include "net/url_request/url_request.h" | 24 #include "net/url_request/url_request.h" |
| 24 | 25 |
| 25 #include "jni/AwContentsIoThreadClient_jni.h" | 26 #include "jni/AwContentsIoThreadClient_jni.h" |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 42 typedef map<pair<int, int>, JavaObjectWeakGlobalRef> | 43 typedef map<pair<int, int>, JavaObjectWeakGlobalRef> |
| 43 RenderViewHostToWeakDelegateMapType; | 44 RenderViewHostToWeakDelegateMapType; |
| 44 | 45 |
| 45 static pair<int, int> GetRenderViewHostIdPair(RenderViewHost* rvh) { | 46 static pair<int, int> GetRenderViewHostIdPair(RenderViewHost* rvh) { |
| 46 return pair<int, int>(rvh->GetProcess()->GetID(), rvh->GetRoutingID()); | 47 return pair<int, int>(rvh->GetProcess()->GetID(), rvh->GetRoutingID()); |
| 47 } | 48 } |
| 48 | 49 |
| 49 // RvhToIoThreadClientMap ----------------------------------------------------- | 50 // RvhToIoThreadClientMap ----------------------------------------------------- |
| 50 class RvhToIoThreadClientMap { | 51 class RvhToIoThreadClientMap { |
| 51 public: | 52 public: |
| 53 RvhToIoThreadClientMap() | |
| 54 : ready_observers_(NULL) { | |
| 55 } | |
| 52 static RvhToIoThreadClientMap* GetInstance(); | 56 static RvhToIoThreadClientMap* GetInstance(); |
| 53 void Insert(pair<int, int> rvh_id, JavaObjectWeakGlobalRef jdelegate); | 57 void Insert(pair<int, int> rvh_id, JavaObjectWeakGlobalRef jdelegate); |
| 54 ScopedJavaLocalRef<jobject> Get(pair<int, int> rvh_id); | 58 ScopedJavaLocalRef<jobject> Get(pair<int, int> rvh_id); |
| 55 void Erase(pair<int, int> rvh_id); | 59 void Erase(pair<int, int> rvh_id); |
| 56 | 60 |
| 61 void AddObserver(AwContentsIoThreadClient::ReadyObserver* o); | |
| 62 void RemoveObserver(AwContentsIoThreadClient::ReadyObserver* o); | |
| 63 | |
| 57 private: | 64 private: |
| 58 static LazyInstance<RvhToIoThreadClientMap> g_instance_; | 65 static LazyInstance<RvhToIoThreadClientMap> g_instance_; |
| 59 base::Lock map_lock_; | 66 base::Lock map_lock_; |
| 60 RenderViewHostToWeakDelegateMapType rvh_to_weak_delegate_map_; | 67 RenderViewHostToWeakDelegateMapType rvh_to_weak_delegate_map_; |
| 68 scoped_refptr<ObserverListThreadSafe< | |
| 69 AwContentsIoThreadClient::ReadyObserver> > ready_observers_; | |
| 61 }; | 70 }; |
| 62 | 71 |
| 63 // static | 72 // static |
| 64 LazyInstance<RvhToIoThreadClientMap> RvhToIoThreadClientMap::g_instance_ = | 73 LazyInstance<RvhToIoThreadClientMap> RvhToIoThreadClientMap::g_instance_ = |
| 65 LAZY_INSTANCE_INITIALIZER; | 74 LAZY_INSTANCE_INITIALIZER; |
| 66 | 75 |
| 67 // static | 76 // static |
| 68 RvhToIoThreadClientMap* RvhToIoThreadClientMap::GetInstance() { | 77 RvhToIoThreadClientMap* RvhToIoThreadClientMap::GetInstance() { |
| 69 return g_instance_.Pointer(); | 78 return g_instance_.Pointer(); |
| 70 } | 79 } |
| 71 | 80 |
| 72 void RvhToIoThreadClientMap::Insert(pair<int, int> rvh_id, | 81 void RvhToIoThreadClientMap::Insert(pair<int, int> rvh_id, |
| 73 JavaObjectWeakGlobalRef jdelegate) { | 82 JavaObjectWeakGlobalRef jdelegate) { |
| 74 base::AutoLock lock(map_lock_); | 83 base::AutoLock lock(map_lock_); |
| 75 rvh_to_weak_delegate_map_[rvh_id] = jdelegate; | 84 rvh_to_weak_delegate_map_[rvh_id] = jdelegate; |
| 85 | |
| 86 if (ready_observers_) { | |
| 87 ready_observers_->Notify( | |
| 88 &AwContentsIoThreadClient::ReadyObserver::OnIoThreadClientReady, | |
| 89 rvh_id.first, | |
| 90 rvh_id.second); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void RvhToIoThreadClientMap::AddObserver( | |
| 95 AwContentsIoThreadClient::ReadyObserver* o) { | |
| 96 base::AutoLock lock(map_lock_); | |
| 97 if (!ready_observers_) { | |
| 98 ready_observers_ = | |
| 99 new ObserverListThreadSafe<AwContentsIoThreadClient::ReadyObserver>(); | |
| 100 } | |
| 101 | |
| 102 ready_observers_->AddObserver(o); | |
| 103 | |
| 104 // In case an AwIoThreadClient was added while we were adding the observer | |
| 105 // we should now notify. | |
| 106 RenderViewHostToWeakDelegateMapType::iterator it = | |
| 107 rvh_to_weak_delegate_map_.begin(); | |
| 108 for (; it != rvh_to_weak_delegate_map_.end(); ++it) { | |
| 109 ready_observers_->Notify( | |
| 110 &AwContentsIoThreadClient::ReadyObserver::OnIoThreadClientReady, | |
| 111 it->first.first, it->first.second); | |
| 112 } | |
|
joth
2012/11/16 21:52:57
thinking about it, rather than run through the ent
benm (inactive)
2012/11/28 20:00:05
How would we poke the AwResourceDispatcherHostDele
joth
2012/11/28 20:38:13
AwResourceDispatcherHostDelegate is indeed a sing
| |
| 113 } | |
| 114 | |
| 115 void RvhToIoThreadClientMap::RemoveObserver( | |
| 116 AwContentsIoThreadClient::ReadyObserver* o) { | |
| 117 DCHECK(ready_observers_); | |
| 118 ready_observers_->RemoveObserver(o); | |
| 76 } | 119 } |
| 77 | 120 |
| 78 ScopedJavaLocalRef<jobject> RvhToIoThreadClientMap::Get( | 121 ScopedJavaLocalRef<jobject> RvhToIoThreadClientMap::Get( |
| 79 pair<int, int> rvh_id) { | 122 pair<int, int> rvh_id) { |
| 80 base::AutoLock lock(map_lock_); | 123 base::AutoLock lock(map_lock_); |
| 81 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator = | 124 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator = |
| 82 rvh_to_weak_delegate_map_.find(rvh_id); | 125 rvh_to_weak_delegate_map_.find(rvh_id); |
| 83 if (weak_delegate_iterator == rvh_to_weak_delegate_map_.end()) | 126 if (weak_delegate_iterator == rvh_to_weak_delegate_map_.end()) |
| 84 return ScopedJavaLocalRef<jobject>(); | 127 return ScopedJavaLocalRef<jobject>(); |
| 85 | 128 |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 152 ScopedJavaLocalRef<jobject> java_delegate = | 195 ScopedJavaLocalRef<jobject> java_delegate = |
| 153 RvhToIoThreadClientMap::GetInstance()->Get(rvh_id); | 196 RvhToIoThreadClientMap::GetInstance()->Get(rvh_id); |
| 154 if (java_delegate.is_null()) | 197 if (java_delegate.is_null()) |
| 155 return scoped_ptr<AwContentsIoThreadClient>(); | 198 return scoped_ptr<AwContentsIoThreadClient>(); |
| 156 | 199 |
| 157 return scoped_ptr<AwContentsIoThreadClient>( | 200 return scoped_ptr<AwContentsIoThreadClient>( |
| 158 new AwContentsIoThreadClientImpl(java_delegate)); | 201 new AwContentsIoThreadClientImpl(java_delegate)); |
| 159 } | 202 } |
| 160 | 203 |
| 161 // static | 204 // static |
| 205 void AwContentsIoThreadClient::AddReadyObserver( | |
| 206 AwContentsIoThreadClient::ReadyObserver* o) { | |
| 207 RvhToIoThreadClientMap::GetInstance()->AddObserver(o); | |
| 208 } | |
| 209 | |
| 210 // static | |
| 211 void AwContentsIoThreadClient::RemoveReadyObserver( | |
| 212 AwContentsIoThreadClient::ReadyObserver* o) { | |
| 213 RvhToIoThreadClientMap::GetInstance()->RemoveObserver(o); | |
| 214 } | |
| 215 | |
| 216 // static | |
| 162 void AwContentsIoThreadClientImpl::Associate( | 217 void AwContentsIoThreadClientImpl::Associate( |
| 163 WebContents* web_contents, | 218 WebContents* web_contents, |
| 164 const JavaRef<jobject>& jclient) { | 219 const JavaRef<jobject>& jclient) { |
| 165 JNIEnv* env = AttachCurrentThread(); | 220 JNIEnv* env = AttachCurrentThread(); |
| 166 // The ClientMapEntryUpdater lifespan is tied to the WebContents. | 221 // The ClientMapEntryUpdater lifespan is tied to the WebContents. |
| 167 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); | 222 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); |
| 168 } | 223 } |
| 169 | 224 |
| 170 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( | 225 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( |
| 171 const JavaRef<jobject>& obj) | 226 const JavaRef<jobject>& obj) |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 224 JNIEnv* env = AttachCurrentThread(); | 279 JNIEnv* env = AttachCurrentThread(); |
| 225 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads( | 280 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads( |
| 226 env, java_object_.obj()); | 281 env, java_object_.obj()); |
| 227 } | 282 } |
| 228 | 283 |
| 229 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { | 284 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { |
| 230 return RegisterNativesImpl(env); | 285 return RegisterNativesImpl(env); |
| 231 } | 286 } |
| 232 | 287 |
| 233 } // namespace android_webview | 288 } // namespace android_webview |
| OLD | NEW |