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 <memory> | 8 #include <memory> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
50 JavaObjectWeakGlobalRef io_thread_client; | 50 JavaObjectWeakGlobalRef io_thread_client; |
51 | 51 |
52 IoThreadClientData(); | 52 IoThreadClientData(); |
53 }; | 53 }; |
54 | 54 |
55 IoThreadClientData::IoThreadClientData() : pending_association(false) {} | 55 IoThreadClientData::IoThreadClientData() : pending_association(false) {} |
56 | 56 |
57 typedef map<pair<int, int>, IoThreadClientData> | 57 typedef map<pair<int, int>, IoThreadClientData> |
58 RenderFrameHostToIoThreadClientType; | 58 RenderFrameHostToIoThreadClientType; |
59 | 59 |
60 // Needed for PlzNavigate. | |
jam
2017/03/09 22:13:05
nit: expand on the comment to explain why navigati
sgurun-gerrit only
2017/03/10 18:40:58
Done.
| |
61 typedef map<int, IoThreadClientData> FrameTreeNodeToIoThreadClientType; | |
62 | |
60 static pair<int, int> GetRenderFrameHostIdPair(RenderFrameHost* rfh) { | 63 static pair<int, int> GetRenderFrameHostIdPair(RenderFrameHost* rfh) { |
61 return pair<int, int>(rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | 64 return pair<int, int>(rfh->GetProcess()->GetID(), rfh->GetRoutingID()); |
62 } | 65 } |
63 | 66 |
64 // RfhToIoThreadClientMap ----------------------------------------------------- | 67 // RfhToIoThreadClientMap ----------------------------------------------------- |
65 class RfhToIoThreadClientMap { | 68 class RfhToIoThreadClientMap { |
66 public: | 69 public: |
67 static RfhToIoThreadClientMap* GetInstance(); | 70 static RfhToIoThreadClientMap* GetInstance(); |
68 void Set(pair<int, int> rfh_id, const IoThreadClientData& client); | 71 void Set(pair<int, int> rfh_id, const IoThreadClientData& client); |
69 bool Get(pair<int, int> rfh_id, IoThreadClientData* client); | 72 bool Get(pair<int, int> rfh_id, IoThreadClientData* client); |
70 void Erase(pair<int, int> rfh_id); | 73 void Erase(pair<int, int> rfh_id); |
71 | 74 |
75 void Set(int frame_tree_node_id, const IoThreadClientData& client); | |
76 bool Get(int frame_tree_node_id, IoThreadClientData* client); | |
77 void Erase(int frame_tree_node_id); | |
78 | |
72 private: | 79 private: |
73 base::Lock map_lock_; | 80 base::Lock map_lock_; |
74 RenderFrameHostToIoThreadClientType rfh_to_io_thread_client_; | 81 RenderFrameHostToIoThreadClientType rfh_to_io_thread_client_; |
82 FrameTreeNodeToIoThreadClientType frame_tree_node_to_io_thread_client_; | |
75 }; | 83 }; |
76 | 84 |
77 // static | 85 // static |
78 LazyInstance<RfhToIoThreadClientMap>::DestructorAtExit g_instance_ = | 86 LazyInstance<RfhToIoThreadClientMap>::DestructorAtExit g_instance_ = |
79 LAZY_INSTANCE_INITIALIZER; | 87 LAZY_INSTANCE_INITIALIZER; |
80 | 88 |
81 // static | 89 // static |
82 LazyInstance<JavaObjectWeakGlobalRef>::DestructorAtExit g_sw_instance_ = | 90 LazyInstance<JavaObjectWeakGlobalRef>::DestructorAtExit g_sw_instance_ = |
83 LAZY_INSTANCE_INITIALIZER; | 91 LAZY_INSTANCE_INITIALIZER; |
84 | 92 |
(...skipping 18 matching lines...) Expand all Loading... | |
103 | 111 |
104 *client = iterator->second; | 112 *client = iterator->second; |
105 return true; | 113 return true; |
106 } | 114 } |
107 | 115 |
108 void RfhToIoThreadClientMap::Erase(pair<int, int> rfh_id) { | 116 void RfhToIoThreadClientMap::Erase(pair<int, int> rfh_id) { |
109 base::AutoLock lock(map_lock_); | 117 base::AutoLock lock(map_lock_); |
110 rfh_to_io_thread_client_.erase(rfh_id); | 118 rfh_to_io_thread_client_.erase(rfh_id); |
111 } | 119 } |
112 | 120 |
121 void RfhToIoThreadClientMap::Set(int frame_tree_node_id, | |
122 const IoThreadClientData& client) { | |
123 base::AutoLock lock(map_lock_); | |
124 frame_tree_node_to_io_thread_client_[frame_tree_node_id] = client; | |
125 } | |
126 | |
127 bool RfhToIoThreadClientMap::Get(int frame_tree_node_id, | |
128 IoThreadClientData* client) { | |
129 base::AutoLock lock(map_lock_); | |
130 FrameTreeNodeToIoThreadClientType::iterator iterator = | |
131 frame_tree_node_to_io_thread_client_.find(frame_tree_node_id); | |
132 if (iterator == frame_tree_node_to_io_thread_client_.end()) | |
133 return false; | |
134 | |
135 *client = iterator->second; | |
136 return true; | |
137 } | |
138 | |
139 void RfhToIoThreadClientMap::Erase(int frame_tree_node_id) { | |
140 base::AutoLock lock(map_lock_); | |
141 frame_tree_node_to_io_thread_client_.erase(frame_tree_node_id); | |
142 } | |
143 | |
113 // ClientMapEntryUpdater ------------------------------------------------------ | 144 // ClientMapEntryUpdater ------------------------------------------------------ |
114 | 145 |
115 class ClientMapEntryUpdater : public content::WebContentsObserver { | 146 class ClientMapEntryUpdater : public content::WebContentsObserver { |
116 public: | 147 public: |
117 ClientMapEntryUpdater(JNIEnv* env, WebContents* web_contents, | 148 ClientMapEntryUpdater(JNIEnv* env, WebContents* web_contents, |
118 jobject jdelegate); | 149 jobject jdelegate); |
119 | 150 |
120 void RenderFrameCreated(RenderFrameHost* render_frame_host) override; | 151 void RenderFrameCreated(RenderFrameHost* render_frame_host) override; |
121 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; | 152 void RenderFrameDeleted(RenderFrameHost* render_frame_host) override; |
122 void WebContentsDestroyed() override; | 153 void WebContentsDestroyed() override; |
(...skipping 11 matching lines...) Expand all Loading... | |
134 DCHECK(jdelegate); | 165 DCHECK(jdelegate); |
135 | 166 |
136 if (web_contents->GetMainFrame()) | 167 if (web_contents->GetMainFrame()) |
137 RenderFrameCreated(web_contents->GetMainFrame()); | 168 RenderFrameCreated(web_contents->GetMainFrame()); |
138 } | 169 } |
139 | 170 |
140 void ClientMapEntryUpdater::RenderFrameCreated(RenderFrameHost* rfh) { | 171 void ClientMapEntryUpdater::RenderFrameCreated(RenderFrameHost* rfh) { |
141 IoThreadClientData client_data; | 172 IoThreadClientData client_data; |
142 client_data.io_thread_client = jdelegate_; | 173 client_data.io_thread_client = jdelegate_; |
143 client_data.pending_association = false; | 174 client_data.pending_association = false; |
144 RfhToIoThreadClientMap::GetInstance()->Set( | 175 RfhToIoThreadClientMap::GetInstance()->Set(GetRenderFrameHostIdPair(rfh), |
145 GetRenderFrameHostIdPair(rfh), client_data); | 176 client_data); |
177 RfhToIoThreadClientMap::GetInstance()->Set(rfh->GetFrameTreeNodeId(), | |
178 client_data); | |
146 } | 179 } |
147 | 180 |
148 void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) { | 181 void ClientMapEntryUpdater::RenderFrameDeleted(RenderFrameHost* rfh) { |
149 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); | 182 RfhToIoThreadClientMap::GetInstance()->Erase(GetRenderFrameHostIdPair(rfh)); |
183 RfhToIoThreadClientMap::GetInstance()->Erase(rfh->GetFrameTreeNodeId()); | |
150 } | 184 } |
151 | 185 |
152 void ClientMapEntryUpdater::WebContentsDestroyed() { | 186 void ClientMapEntryUpdater::WebContentsDestroyed() { |
153 delete this; | 187 delete this; |
154 } | 188 } |
155 | 189 |
156 } // namespace | 190 } // namespace |
157 | 191 |
158 // AwContentsIoThreadClientImpl ----------------------------------------------- | 192 // AwContentsIoThreadClientImpl ----------------------------------------------- |
159 | 193 |
160 // static | 194 // static |
161 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( | 195 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( |
162 int render_process_id, | 196 int render_process_id, |
163 int render_frame_id) { | 197 int render_frame_id) { |
164 pair<int, int> rfh_id(render_process_id, render_frame_id); | 198 pair<int, int> rfh_id(render_process_id, render_frame_id); |
165 IoThreadClientData client_data; | 199 IoThreadClientData client_data; |
166 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) | 200 if (!RfhToIoThreadClientMap::GetInstance()->Get(rfh_id, &client_data)) |
167 return std::unique_ptr<AwContentsIoThreadClient>(); | 201 return std::unique_ptr<AwContentsIoThreadClient>(); |
168 | 202 |
169 JNIEnv* env = AttachCurrentThread(); | 203 JNIEnv* env = AttachCurrentThread(); |
170 ScopedJavaLocalRef<jobject> java_delegate = | 204 ScopedJavaLocalRef<jobject> java_delegate = |
171 client_data.io_thread_client.get(env); | 205 client_data.io_thread_client.get(env); |
172 DCHECK(!client_data.pending_association || java_delegate.is_null()); | 206 DCHECK(!client_data.pending_association || java_delegate.is_null()); |
173 return std::unique_ptr<AwContentsIoThreadClient>( | 207 return std::unique_ptr<AwContentsIoThreadClient>( |
174 new AwContentsIoThreadClientImpl(client_data.pending_association, | 208 new AwContentsIoThreadClientImpl(client_data.pending_association, |
175 java_delegate)); | 209 java_delegate)); |
176 } | 210 } |
177 | 211 |
212 std::unique_ptr<AwContentsIoThreadClient> AwContentsIoThreadClient::FromID( | |
213 int frame_tree_node_id) { | |
214 IoThreadClientData client_data; | |
215 if (!RfhToIoThreadClientMap::GetInstance()->Get(frame_tree_node_id, | |
216 &client_data)) | |
217 return std::unique_ptr<AwContentsIoThreadClient>(); | |
218 | |
219 JNIEnv* env = AttachCurrentThread(); | |
220 ScopedJavaLocalRef<jobject> java_delegate = | |
221 client_data.io_thread_client.get(env); | |
222 DCHECK(!client_data.pending_association || java_delegate.is_null()); | |
223 return std::unique_ptr<AwContentsIoThreadClient>( | |
224 new AwContentsIoThreadClientImpl(client_data.pending_association, | |
225 java_delegate)); | |
226 } | |
227 | |
178 // static | 228 // static |
179 void AwContentsIoThreadClient::SubFrameCreated(int render_process_id, | 229 void AwContentsIoThreadClient::SubFrameCreated(int render_process_id, |
180 int parent_render_frame_id, | 230 int parent_render_frame_id, |
181 int child_render_frame_id) { | 231 int child_render_frame_id) { |
182 pair<int, int> parent_rfh_id(render_process_id, parent_render_frame_id); | 232 pair<int, int> parent_rfh_id(render_process_id, parent_render_frame_id); |
183 pair<int, int> child_rfh_id(render_process_id, child_render_frame_id); | 233 pair<int, int> child_rfh_id(render_process_id, child_render_frame_id); |
184 IoThreadClientData client_data; | 234 IoThreadClientData client_data; |
185 if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id, | 235 if (!RfhToIoThreadClientMap::GetInstance()->Get(parent_rfh_id, |
186 &client_data)) { | 236 &client_data)) { |
187 NOTREACHED(); | 237 NOTREACHED(); |
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
346 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 396 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
347 if (java_object_.is_null()) | 397 if (java_object_.is_null()) |
348 return false; | 398 return false; |
349 | 399 |
350 JNIEnv* env = AttachCurrentThread(); | 400 JNIEnv* env = AttachCurrentThread(); |
351 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env, | 401 return Java_AwContentsIoThreadClient_shouldBlockNetworkLoads(env, |
352 java_object_); | 402 java_object_); |
353 } | 403 } |
354 | 404 |
355 } // namespace android_webview | 405 } // namespace android_webview |
OLD | NEW |