Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(379)

Side by Side Diff: extensions/browser/extension_api_frame_id_map.cc

Issue 1413543005: Use FrameTreeNode ID as frameId in extension APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments #47 Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 "base/time/time.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/render_frame_host.h"
12 #include "content/public/browser/render_process_host.h"
13 #include "content/public/browser/web_contents.h"
14
15 namespace extensions {
16
17 namespace {
18
19 // The map is accessed on the IO and UI thread, so construct it once and never
20 // delete it.
21 base::LazyInstance<ExtensionApiFrameIdMap>::Leaky g_map_instance =
22 LAZY_INSTANCE_INITIALIZER;
23
24 int GetFrameIdFromFrame(content::RenderFrameHost* rfh) {
25 if (!rfh)
26 return ExtensionApiFrameId::kInvalidFrameId;
27 if (rfh->GetParent())
28 return rfh->GetFrameTreeNodeId();
29 return 0; // Main frame.
30 }
31
32 } // namespace
33
34 ExtensionApiFrameId::ExtensionApiFrameId()
35 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {}
36
37 ExtensionApiFrameId::ExtensionApiFrameId(int frame_id, int parent_frame_id)
38 : frame_id(frame_id), parent_frame_id(parent_frame_id) {}
39
40 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey()
41 : render_process_id(-1), frame_routing_id(-1) {}
42
43 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey(
44 int render_process_id,
45 int frame_routing_id)
46 : render_process_id(render_process_id),
47 frame_routing_id(frame_routing_id) {}
48
49 ExtensionApiFrameIdMap::FrameIdCallbackInfo::FrameIdCallbackInfo(
50 const RenderFrameIdKey& key,
51 const FrameIdCallback& callback)
52 : key(key), callback(callback), has_value(false) {}
53
54 ExtensionApiFrameIdMap::FrameIdCallbackInfo::~FrameIdCallbackInfo() {}
55
56 bool ExtensionApiFrameIdMap::RenderFrameIdKey::operator<(
57 const RenderFrameIdKey& other) const {
58 return std::tie(render_process_id, frame_routing_id) <
59 std::tie(other.render_process_id, other.frame_routing_id);
60 }
61
62 bool ExtensionApiFrameIdMap::RenderFrameIdKey::operator==(
63 const RenderFrameIdKey& other) const {
64 return render_process_id == other.render_process_id &&
65 frame_routing_id == other.frame_routing_id;
66 }
67
68 ExtensionApiFrameIdMap::FrameIdRemovalTask::FrameIdRemovalTask(
69 const RenderFrameIdKey& key,
70 const base::TimeTicks& creation_time)
71 : key(key), creation_time(creation_time) {}
72
73 ExtensionApiFrameIdMap::ExtensionApiFrameIdMap() {}
74
75 ExtensionApiFrameIdMap::~ExtensionApiFrameIdMap() {}
76
77 ExtensionApiFrameIdMap* ExtensionApiFrameIdMap::Get() {
78 return g_map_instance.Pointer();
79 }
80
81 ExtensionApiFrameId ExtensionApiFrameIdMap::KeyToValue(
82 const RenderFrameIdKey& key) const {
83 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID(
84 key.render_process_id, key.frame_routing_id);
85 return ExtensionApiFrameId(
86 GetFrameIdFromFrame(rfh),
87 GetFrameIdFromFrame(rfh ? rfh->GetParent() : nullptr));
88 }
89
90 ExtensionApiFrameId ExtensionApiFrameIdMap::LookupFrameIdOnUI(
91 const RenderFrameIdKey& key) {
92 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
93
94 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key);
95 if (frame_id_iter != frame_id_map_.end())
96 return frame_id_iter->second;
97
98 ExtensionApiFrameId extension_api_frame_id = KeyToValue(key);
99 // Don't save invalid values in the map.
100 if (extension_api_frame_id.frame_id == ExtensionApiFrameId::kInvalidFrameId)
101 return extension_api_frame_id;
102
103 auto kvpair = FrameIdMap::value_type(key, extension_api_frame_id);
104 base::AutoLock lock(frame_id_map_lock_);
105 return frame_id_map_.insert(kvpair).first->second;
106 }
107
108 void ExtensionApiFrameIdMap::GotFrameIdOnIO(
109 const RenderFrameIdKey& key,
110 const ExtensionApiFrameId& extension_api_frame_id) {
111 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
112 // BrowserThread::PostTaskAndReplyWithResult runs the tasks in order, so the
113 // order of calling GotFrameIdOnIO() should match the order of the callbacks.
114 // This implies that whenever GotFrameIdOnIO() is called, the key should be
115 // identical to the key at the front of the queue.
116 DCHECK(!callbacks_.empty());
117 DCHECK(callbacks_.front().key == key);
118
119 // Note: Extra items can be appended to |callbacks_| during this loop if the
120 // callback calls GetFrameIdOnIO().
121 while (!callbacks_.empty() &&
122 (callbacks_.front().has_value || callbacks_.front().key == key)) {
123 FrameIdCallbackInfo callback_info = callbacks_.front();
124 callbacks_.pop_front();
125 if (callback_info.has_value)
126 callback_info.callback.Run(callback_info.value);
127 else // callback_info.key == key
128 callback_info.callback.Run(extension_api_frame_id);
129 }
130
131 // Even after finding the value for |key|, not all callbacks for this |key|
132 // are called, because a callback with a different key might be queued between
133 // these callbacks. E.g. in the following scenario:
134 // GetFrameIdOnIO(key1, cb1); // cb1 run by GotFrameIdOnIO for key1.
135 // GetFrameIdOnIO(key2, cb2); // waiting for GotFrameIdOnIO for key2.
136 // GetFrameIdOnIO(key1, cb3); // waiting until |cb2| has run.
137 // When |cb1| is about to be called, the same result could also be used for
138 // |cb3|. However we wait until |cb2| has run to make sure that callbacks are
139 // run in order.
140 for (auto& callback_info : callbacks_) {
141 if (callback_info.key == key) {
142 callback_info.has_value = true;
143 callback_info.value = extension_api_frame_id;
144 }
145 }
146 }
147
148 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id,
149 int frame_routing_id,
150 const FrameIdCallback& callback) {
151 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
152
153 const RenderFrameIdKey key(render_process_id, frame_routing_id);
154 FrameIdCallbackInfo callback_info(key, callback);
155
156 {
157 base::AutoLock lock(frame_id_map_lock_);
158 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key);
159 if (frame_id_iter != frame_id_map_.end()) {
160 callback_info.value = frame_id_iter->second;
161 callback_info.has_value = true;
162 }
163 }
164
165 if (callback_info.has_value) {
166 // Value already cached, thread hopping is not needed. Run the callback if
167 // there are no pending callbacks, otherwise queue it.
168 if (callbacks_.empty())
169 callback_info.callback.Run(callback_info.value);
170 else
171 callbacks_.push_back(callback_info);
172 return;
173 }
174
175 // Check whether the frame ID lookup was requested before. If yes, then there
176 // is no need for posting a task to the UI thread.
177 for (const auto& other : callbacks_) {
178 if (other.key == key) {
179 callbacks_.push_back(callback_info);
180 return;
181 }
182 }
183
184 // The key was seen for the first time, hop to the UI thread to look up the
185 // extension frame ID.
186 callbacks_.push_back(callback_info);
187 content::BrowserThread::PostTaskAndReplyWithResult(
188 content::BrowserThread::UI, FROM_HERE,
189 base::Bind(&ExtensionApiFrameIdMap::LookupFrameIdOnUI,
190 base::Unretained(this), key),
191 base::Bind(&ExtensionApiFrameIdMap::GotFrameIdOnIO,
192 base::Unretained(this), key));
193 }
194
195 const ExtensionApiFrameId ExtensionApiFrameIdMap::GetFrameId(
196 int render_process_id,
197 int frame_routing_id) {
198 return LookupFrameIdOnUI(
199 RenderFrameIdKey(render_process_id, frame_routing_id));
200 }
201
202 const ExtensionApiFrameId ExtensionApiFrameIdMap::GetFrameId(
203 content::RenderFrameHost* rfh) {
204 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
205
206 if (!rfh)
207 return ExtensionApiFrameId();
208
209 return LookupFrameIdOnUI(
210 RenderFrameIdKey(rfh->GetProcess()->GetID(), rfh->GetRoutingID()));
211 }
212
213 void ExtensionApiFrameIdMap::RemoveFrameId(int render_process_id,
214 int frame_routing_id) {
215 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
216 // Defer removal of the key, so that RemoveFrameId() can be called when the
217 // RenderFrameHost is destroyed, without causing frame mappings to fail for
218 // other in-flight events / notifications.
219 pending_deletions_.push_back(
220 FrameIdRemovalTask(RenderFrameIdKey(render_process_id, frame_routing_id),
221 base::TimeTicks::Now()));
222
223 // Minimum number of seconds to wait before removing the frame ID.
224 const int kSecondsUntilRemoval = 1;
225 base::TimeTicks expired_creation_time =
226 base::TimeTicks::Now() -
227 base::TimeDelta::FromSeconds(kSecondsUntilRemoval);
228
229 // Remove previously queued removal tasks. RenderFrameId() is usually called
230 // whenever a frame is removed, so this clean up eventually happens. In the
231 // worst case scenario, lots of frames are created and immediately removed
232 // before |kSecondsUntilRemoval| seconds have passed. This is extremely
233 // unlikely to happen. Even if it were to occur, the impact is just a slight
234 // waste of memory due to the retention of useless map entries.
235 base::AutoLock lock(frame_id_map_lock_);
236 while (!pending_deletions_.empty() &&
237 pending_deletions_.front().creation_time < expired_creation_time) {
238 frame_id_map_.erase(pending_deletions_.front().key);
239 pending_deletions_.pop_front();
240 }
241 }
242
243 void ExtensionApiFrameIdMap::RemoveFrameId(content::RenderFrameHost* rfh) {
244 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
245
246 if (!rfh)
247 return;
248
249 RemoveFrameId(rfh->GetProcess()->GetID(), rfh->GetRoutingID());
250 }
251
252 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById(
253 content::WebContents* web_contents,
254 int frame_id) {
255 // Although it is technically possible to map |frame_id| to a RenderFrameHost
256 // without WebContents, we choose to not do that because in the extension API
257 // frameIds are only guaranteed to be meaningful in combination with a tabId.
258 if (!web_contents)
259 return nullptr;
260
261 if (frame_id == -1)
262 return nullptr;
263
264 if (frame_id == 0)
265 return web_contents->GetMainFrame();
266
267 DCHECK_GE(frame_id, 1);
268 return web_contents->FindFrameByFrameTreeNodeId(frame_id);
269 }
270
271 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698