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_); | |
joth
2012/11/16 06:24:31
move down to 105
mkosiba (inactive)
2012/11/16 12:28:32
I'd prefer not to. Moving the lock would make it p
benm (inactive)
2012/11/16 12:29:09
Done.
benm (inactive)
2012/11/16 12:36:31
As in, Martin makes a good point, so leaving the l
joth
2012/11/16 21:52:56
It's not obvious ready_observers_ is intended to b
| |
97 | |
98 if (!ready_observers_) { | |
99 ready_observers_ = | |
100 new ObserverListThreadSafe<AwContentsIoThreadClient::ReadyObserver>(); | |
101 } | |
102 | |
103 ready_observers_->AddObserver(o); | |
104 | |
105 // In case an AwIoThreadClient was added while we were adding the observer | |
106 // we should now notify. | |
joth
2012/11/16 06:24:31
AIUI AddObserver() and Insert(() will both only be
benm (inactive)
2012/11/16 12:29:09
AddObserver will be coming from the IO thread, but
| |
107 RenderViewHostToWeakDelegateMapType::iterator it = | |
108 rvh_to_weak_delegate_map_.begin(); | |
109 for (; it != rvh_to_weak_delegate_map_.end(); ++it) { | |
110 ready_observers_->Notify( | |
111 &AwContentsIoThreadClient::ReadyObserver::OnIoThreadClientReady, | |
112 it->first.first, it->first.second); | |
113 } | |
114 } | |
115 | |
116 void RvhToIoThreadClientMap::RemoveObserver( | |
117 AwContentsIoThreadClient::ReadyObserver* o) { | |
118 DCHECK(ready_observers_); | |
119 ready_observers_->RemoveObserver(o); | |
76 } | 120 } |
77 | 121 |
78 ScopedJavaLocalRef<jobject> RvhToIoThreadClientMap::Get( | 122 ScopedJavaLocalRef<jobject> RvhToIoThreadClientMap::Get( |
79 pair<int, int> rvh_id) { | 123 pair<int, int> rvh_id) { |
80 base::AutoLock lock(map_lock_); | 124 base::AutoLock lock(map_lock_); |
81 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator = | 125 RenderViewHostToWeakDelegateMapType::iterator weak_delegate_iterator = |
82 rvh_to_weak_delegate_map_.find(rvh_id); | 126 rvh_to_weak_delegate_map_.find(rvh_id); |
83 if (weak_delegate_iterator == rvh_to_weak_delegate_map_.end()) | 127 if (weak_delegate_iterator == rvh_to_weak_delegate_map_.end()) |
84 return ScopedJavaLocalRef<jobject>(); | 128 return ScopedJavaLocalRef<jobject>(); |
85 | 129 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
152 ScopedJavaLocalRef<jobject> java_delegate = | 196 ScopedJavaLocalRef<jobject> java_delegate = |
153 RvhToIoThreadClientMap::GetInstance()->Get(rvh_id); | 197 RvhToIoThreadClientMap::GetInstance()->Get(rvh_id); |
154 if (java_delegate.is_null()) | 198 if (java_delegate.is_null()) |
155 return scoped_ptr<AwContentsIoThreadClient>(); | 199 return scoped_ptr<AwContentsIoThreadClient>(); |
156 | 200 |
157 return scoped_ptr<AwContentsIoThreadClient>( | 201 return scoped_ptr<AwContentsIoThreadClient>( |
158 new AwContentsIoThreadClientImpl(java_delegate)); | 202 new AwContentsIoThreadClientImpl(java_delegate)); |
159 } | 203 } |
160 | 204 |
161 // static | 205 // static |
206 void AwContentsIoThreadClient::AddReadyObserver( | |
207 AwContentsIoThreadClient::ReadyObserver* o) { | |
208 RvhToIoThreadClientMap::GetInstance()->AddObserver(o); | |
209 } | |
210 | |
211 // static | |
212 void AwContentsIoThreadClient::RemoveReadyObserver( | |
213 AwContentsIoThreadClient::ReadyObserver* o) { | |
214 RvhToIoThreadClientMap::GetInstance()->RemoveObserver(o); | |
215 } | |
216 | |
217 // static | |
162 void AwContentsIoThreadClientImpl::Associate( | 218 void AwContentsIoThreadClientImpl::Associate( |
163 WebContents* web_contents, | 219 WebContents* web_contents, |
164 const JavaRef<jobject>& jclient) { | 220 const JavaRef<jobject>& jclient) { |
165 JNIEnv* env = AttachCurrentThread(); | 221 JNIEnv* env = AttachCurrentThread(); |
166 // The ClientMapEntryUpdater lifespan is tied to the WebContents. | 222 // The ClientMapEntryUpdater lifespan is tied to the WebContents. |
167 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); | 223 new ClientMapEntryUpdater(env, web_contents, jclient.obj()); |
168 } | 224 } |
169 | 225 |
170 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( | 226 AwContentsIoThreadClientImpl::AwContentsIoThreadClientImpl( |
171 const JavaRef<jobject>& obj) | 227 const JavaRef<jobject>& obj) |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
224 JNIEnv* env = AttachCurrentThread(); | 280 JNIEnv* env = AttachCurrentThread(); |
225 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads( | 281 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads( |
226 env, java_object_.obj()); | 282 env, java_object_.obj()); |
227 } | 283 } |
228 | 284 |
229 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { | 285 bool RegisterAwContentsIoThreadClientImpl(JNIEnv* env) { |
230 return RegisterNativesImpl(env); | 286 return RegisterNativesImpl(env); |
231 } | 287 } |
232 | 288 |
233 } // namespace android_webview | 289 } // namespace android_webview |
OLD | NEW |