OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "extensions/browser/extension_api_frame_id_map.h" | |
6 | |
7 #include <tuple> | |
8 | |
9 #include "content/public/browser/browser_thread.h" | |
10 #include "content/public/browser/render_frame_host.h" | |
11 #include "content/public/browser/render_process_host.h" | |
12 #include "content/public/browser/web_contents.h" | |
13 | |
14 namespace extensions { | |
15 | |
16 namespace { | |
17 | |
18 // The map is accessed on the IO and UI thread, so construct it once and never | |
19 // delete it. | |
nasko
2016/01/05 00:39:58
Should construction of the map also be protected b
robwu
2016/01/05 10:59:03
LazyInstance is documented to be thread-safe: http
| |
20 base::LazyInstance<ExtensionApiFrameIdMap>::Leaky g_map_instance = | |
21 LAZY_INSTANCE_INITIALIZER; | |
22 | |
23 } // namespace | |
24 | |
25 const int ExtensionApiFrameIdMap::kInvalidFrameId = -1; | |
26 | |
27 ExtensionApiFrameIdMap::CachedFrameIdPair::CachedFrameIdPair() | |
28 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {} | |
29 | |
30 ExtensionApiFrameIdMap::CachedFrameIdPair::CachedFrameIdPair( | |
31 int frame_id, | |
32 int parent_frame_id) | |
33 : frame_id(frame_id), parent_frame_id(parent_frame_id) {} | |
34 | |
35 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey() | |
36 : render_process_id(-1), frame_routing_id(-1) {} | |
nasko
2016/01/05 00:39:58
MSG_ROUTING_NONE for the routing id. 0 or kInvalid
robwu
2016/01/05 10:59:03
Done. The IDs have always been -1, but changing it
| |
37 | |
38 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey( | |
39 int render_process_id, | |
40 int frame_routing_id) | |
41 : render_process_id(render_process_id), | |
42 frame_routing_id(frame_routing_id) {} | |
43 | |
44 ExtensionApiFrameIdMap::FrameIdCallbacks::FrameIdCallbacks() | |
45 : is_iterating(false) {} | |
46 | |
47 ExtensionApiFrameIdMap::FrameIdCallbacks::~FrameIdCallbacks() {} | |
48 | |
49 bool ExtensionApiFrameIdMap::RenderFrameIdKey::operator<( | |
50 const RenderFrameIdKey& other) const { | |
51 return std::tie(render_process_id, frame_routing_id) < | |
52 std::tie(other.render_process_id, other.frame_routing_id); | |
53 } | |
54 | |
55 bool ExtensionApiFrameIdMap::RenderFrameIdKey::operator==( | |
56 const RenderFrameIdKey& other) const { | |
57 return render_process_id == other.render_process_id && | |
58 frame_routing_id == other.frame_routing_id; | |
59 } | |
60 | |
61 ExtensionApiFrameIdMap::ExtensionApiFrameIdMap() {} | |
62 | |
63 ExtensionApiFrameIdMap::~ExtensionApiFrameIdMap() {} | |
64 | |
65 ExtensionApiFrameIdMap* ExtensionApiFrameIdMap::Get() { | |
66 return g_map_instance.Pointer(); | |
67 } | |
68 | |
69 int ExtensionApiFrameIdMap::GetFrameId(content::RenderFrameHost* rfh) { | |
70 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
71 | |
72 if (!rfh) | |
73 return kInvalidFrameId; | |
74 if (rfh->GetParent()) | |
75 return rfh->GetFrameTreeNodeId(); | |
76 return 0; // Main frame. | |
77 } | |
78 | |
79 int ExtensionApiFrameIdMap::GetParentFrameId(content::RenderFrameHost* rfh) { | |
80 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
81 | |
82 return rfh ? GetFrameId(rfh->GetParent()) : kInvalidFrameId; | |
83 } | |
84 | |
85 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById( | |
86 content::WebContents* web_contents, | |
87 int frame_id) { | |
88 // Although it is technically possible to map |frame_id| to a RenderFrameHost | |
89 // without WebContents, we choose to not do that because in the extension API | |
90 // frameIds are only guaranteed to be meaningful in combination with a tabId. | |
91 if (!web_contents) | |
92 return nullptr; | |
93 | |
94 if (frame_id == -1) | |
nasko
2016/01/05 00:39:58
kInvalidFrameId?
robwu
2016/01/05 10:59:03
Done.
| |
95 return nullptr; | |
96 | |
97 if (frame_id == 0) | |
98 return web_contents->GetMainFrame(); | |
99 | |
100 DCHECK_GE(frame_id, 1); | |
101 return web_contents->FindFrameByFrameTreeNodeId(frame_id); | |
102 } | |
103 | |
104 ExtensionApiFrameIdMap::CachedFrameIdPair ExtensionApiFrameIdMap::KeyToValue( | |
105 const RenderFrameIdKey& key) const { | |
106 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( | |
107 key.render_process_id, key.frame_routing_id); | |
108 return CachedFrameIdPair(GetFrameId(rfh), GetParentFrameId(rfh)); | |
109 } | |
110 | |
111 ExtensionApiFrameIdMap::CachedFrameIdPair | |
112 ExtensionApiFrameIdMap::LookupFrameIdOnUI(const RenderFrameIdKey& key) { | |
113 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
114 | |
115 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key); | |
116 if (frame_id_iter != frame_id_map_.end()) | |
117 return frame_id_iter->second; | |
118 | |
119 CachedFrameIdPair cached_frame_id_pair = KeyToValue(key); | |
120 // Don't save invalid values in the map. | |
121 if (cached_frame_id_pair.frame_id == kInvalidFrameId) | |
122 return cached_frame_id_pair; | |
123 | |
124 auto kvpair = FrameIdMap::value_type(key, cached_frame_id_pair); | |
125 base::AutoLock lock(frame_id_map_lock_); | |
126 return frame_id_map_.insert(kvpair).first->second; | |
127 } | |
128 | |
129 void ExtensionApiFrameIdMap::GotFrameIdOnIO( | |
nasko
2016/01/05 00:39:58
I thought I put a comment earlier on (but can't fi
robwu
2016/01/05 10:59:03
Done.
| |
130 const RenderFrameIdKey& key, | |
131 const CachedFrameIdPair& cached_frame_id_pair) { | |
132 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
133 | |
134 FrameIdCallbacksMap::iterator map_iter = callbacks_map_.find(key); | |
135 if (map_iter == callbacks_map_.end()) { | |
136 // Can happen if GotFrameIdOnIO was called after the frame ID was resolved | |
137 // (e.g. via GetFrameIdOnIO), but before PostTaskAndReply replied. | |
138 return; | |
139 } | |
140 | |
141 FrameIdCallbacks& callbacks = map_iter->second; | |
142 | |
143 if (callbacks.is_iterating) | |
144 return; | |
145 callbacks.is_iterating = true; | |
146 | |
147 // Note: Extra items can be appended to |callbacks| during this loop if a | |
148 // callback calls GetFrameIdOnIO(). | |
149 for (std::list<FrameIdCallback>::iterator it = callbacks.callbacks.begin(); | |
150 it != callbacks.callbacks.end(); ++it) { | |
151 it->Run(cached_frame_id_pair.frame_id, | |
152 cached_frame_id_pair.parent_frame_id); | |
153 } | |
154 callbacks_map_.erase(key); | |
155 } | |
156 | |
157 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id, | |
158 int frame_routing_id, | |
159 const FrameIdCallback& callback) { | |
160 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
161 | |
162 if (frame_routing_id <= -1) { | |
163 // frame_routing_id == -2 = MSG_ROUTING_NONE -> not a RenderFrameHost. | |
164 // frame_routing_id == -1 -> unknown RenderFrameHost | |
nasko
2016/01/05 00:39:58
Let's avoid using -1 if possible. MSG_ROUTING_NONE
robwu
2016/01/05 10:59:03
Updated comment. I cannot add DCHECK_EQ(frame_rout
nasko
2016/01/06 17:19:38
Thanks for the TODO.
| |
165 callback.Run(kInvalidFrameId, kInvalidFrameId); | |
166 return; | |
167 } | |
168 // A valid routing ID is only meaningful with a valid process ID. | |
169 DCHECK_GE(render_process_id, 0); | |
170 | |
171 const RenderFrameIdKey key(render_process_id, frame_routing_id); | |
172 CachedFrameIdPair cached_frame_id_pair; | |
173 bool did_find_cached_frame_id_pair = false; | |
174 | |
175 { | |
176 base::AutoLock lock(frame_id_map_lock_); | |
177 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key); | |
178 if (frame_id_iter != frame_id_map_.end()) { | |
179 // This is very likely to happen because CacheFrameId() is called as soon | |
180 // as the frame is created. | |
181 cached_frame_id_pair = frame_id_iter->second; | |
182 did_find_cached_frame_id_pair = true; | |
183 } | |
184 } | |
185 | |
186 FrameIdCallbacksMap::iterator map_iter = callbacks_map_.find(key); | |
187 | |
188 if (did_find_cached_frame_id_pair) { | |
189 // Value already cached, thread hopping is not needed. | |
190 if (map_iter == callbacks_map_.end()) { | |
191 // If the frame ID was cached, then it is likely that there are no pending | |
192 // callbacks. So do not unnecessarily copy the callback, but run it. | |
193 callback.Run(cached_frame_id_pair.frame_id, | |
194 cached_frame_id_pair.parent_frame_id); | |
195 } else { | |
196 map_iter->second.callbacks.push_back(callback); | |
197 GotFrameIdOnIO(key, cached_frame_id_pair); | |
198 } | |
199 return; | |
200 } | |
201 | |
202 // The key was seen for the first time (or the frame has been removed). | |
203 // Hop to the UI thread to look up the extension API frame ID. | |
204 callbacks_map_[key].callbacks.push_back(callback); | |
205 content::BrowserThread::PostTaskAndReplyWithResult( | |
206 content::BrowserThread::UI, FROM_HERE, | |
207 base::Bind(&ExtensionApiFrameIdMap::LookupFrameIdOnUI, | |
208 base::Unretained(this), key), | |
209 base::Bind(&ExtensionApiFrameIdMap::GotFrameIdOnIO, | |
210 base::Unretained(this), key)); | |
211 } | |
212 | |
213 void ExtensionApiFrameIdMap::CacheFrameId(content::RenderFrameHost* rfh) { | |
214 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
215 | |
216 const RenderFrameIdKey key(rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | |
217 CacheFrameId(key); | |
218 DCHECK(frame_id_map_.find(key) != frame_id_map_.end()); | |
219 } | |
220 | |
221 void ExtensionApiFrameIdMap::CacheFrameId(const RenderFrameIdKey& key) { | |
222 LookupFrameIdOnUI(key); | |
223 } | |
224 | |
225 void ExtensionApiFrameIdMap::RemoveFrameId(content::RenderFrameHost* rfh) { | |
226 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
227 DCHECK(rfh); | |
228 | |
229 const RenderFrameIdKey key(rfh->GetProcess()->GetID(), rfh->GetRoutingID()); | |
230 RemoveFrameId(key); | |
231 } | |
232 | |
233 void ExtensionApiFrameIdMap::RemoveFrameId(const RenderFrameIdKey& key) { | |
234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
235 | |
236 base::AutoLock lock(frame_id_map_lock_); | |
237 frame_id_map_.erase(key); | |
238 } | |
239 | |
240 } // namespace extensions | |
OLD | NEW |