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

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: Guarantee callback order, remove deleted frames from map Created 4 years, 12 months 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 const ExtensionApiFrameId& extension_api_frame_id = KeyToValue(key);
99 // Don't save invalid values in the map.
nasko 2015/12/22 22:12:09 nit: Comment above the variable or empty line betw
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(
nasko 2015/12/22 22:12:09 nit: GetFrameIdOnIO and GotFrameIdOnIO differ by o
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
nasko 2015/12/22 22:12:10 nit: Empty line before the comment.
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 // Not all callbacks of |key| are immediately called, because it is possible
132 // that GetFrameIdOnIO() is called with a different argument. E.g. in the
133 // 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 for GetFrameIdOnIO for key2.
137 // In the above example, |cb3| will be called as soon as |cb2| is removed from
138 // the callbacks queue.
nasko 2015/12/22 22:12:10 Why do we need all of this complexity? Why can't w
139 for (auto& callback_info : callbacks_) {
140 if (callback_info.key == key) {
141 callback_info.has_value = true;
142 callback_info.value = extension_api_frame_id;
143 }
144 }
145 }
146
147 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id,
148 int frame_routing_id,
149 const FrameIdCallback& callback) {
150 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
151
152 const RenderFrameIdKey key(render_process_id, frame_routing_id);
153 FrameIdCallbackInfo callback_info(key, callback);
154
155 {
156 base::AutoLock lock(frame_id_map_lock_);
157 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key);
158 if (frame_id_iter != frame_id_map_.end()) {
159 callback_info.value = frame_id_iter->second;
160 callback_info.has_value = true;
161 }
162 }
163
164 if (callback_info.has_value) {
165 // Value already cached, thread hopping is not needed. Run the callback if
166 // there are no pending callbacks, otherwise queue it.
167 if (callbacks_.empty())
168 callback_info.callback.Run(callback_info.value);
169 else
170 callbacks_.push_back(callback_info);
171 return;
172 }
173
174 // Check whether the frame ID lookup was requested before. If yes, then there
175 // is no need for posting a task to the UI thread.
176 for (const auto& other : callbacks_) {
177 if (other.key == key) {
178 callbacks_.push_back(callback_info);
179 return;
180 }
181 }
182
183 // The key was seen for the first time, hop to the UI thread to look up the
184 // extension frame ID.
185 callbacks_.push_back(callback_info);
186 content::BrowserThread::PostTaskAndReplyWithResult(
187 content::BrowserThread::UI, FROM_HERE,
188 base::Bind(&ExtensionApiFrameIdMap::LookupFrameIdOnUI,
189 base::Unretained(this), key),
190 base::Bind(&ExtensionApiFrameIdMap::GotFrameIdOnIO,
191 base::Unretained(this), key));
192 }
193
194 const ExtensionApiFrameId ExtensionApiFrameIdMap::GetFrameId(
195 int render_process_id,
196 int frame_routing_id) {
197 return LookupFrameIdOnUI(
198 RenderFrameIdKey(render_process_id, frame_routing_id));
199 }
200
201 const ExtensionApiFrameId ExtensionApiFrameIdMap::GetFrameId(
202 content::RenderFrameHost* rfh) {
203 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
204
205 if (!rfh)
206 return ExtensionApiFrameId();
207
208 return LookupFrameIdOnUI(
209 RenderFrameIdKey(rfh->GetProcess()->GetID(), rfh->GetRoutingID()));
210 }
211
212 void ExtensionApiFrameIdMap::RemoveFrameId(int render_process_id,
213 int frame_routing_id) {
214 // Defer removal of the key, so that RemoveFrameId() can be called when the
215 // RenderFrameHost is destroyed, without causing frame mappings to fail for
216 // other in-flight events / notifications.
217 pending_deletions_.push_back(
218 FrameIdRemovalTask(RenderFrameIdKey(render_process_id, frame_routing_id),
219 base::TimeTicks::Now()));
220
221 // Minimum number of seconds to wait before removing the frame ID.
nasko 2015/12/22 22:12:10 I found this comment to be confusing. I was expect
222 const int kSecondsUntilRemoval = 1;
nasko 2015/12/22 22:12:09 This should be declared at the top of the file. Al
223 base::TimeTicks expired_creation_time =
224 base::TimeTicks::Now() -
225 base::TimeDelta::FromSeconds(kSecondsUntilRemoval);
226
227 // Remove previously queued removal tasks. RenderFrameId() is usually called
228 // whenever a frame is removed, so this clean up eventually happens. In the
229 // worst case scenario, lots of frames are created and immediately removed
230 // before |kSecondsUntilRemoval| seconds have passed. This is extremely
nasko 2015/12/22 22:12:09 nit: I'd remove the "extremely" qualifier, as you'
231 // unlikely to happen. Even if it were to occur, the impact is just a slight
232 // waste of memory due to the retention of useless map entries.
233 base::AutoLock lock(frame_id_map_lock_);
234 while (!pending_deletions_.empty() &&
235 pending_deletions_.front().creation_time < expired_creation_time) {
236 frame_id_map_.erase(pending_deletions_.front().key);
237 pending_deletions_.pop_front();
238 }
239 }
240
241 void ExtensionApiFrameIdMap::RemoveFrameId(content::RenderFrameHost* rfh) {
242 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
243
244 if (!rfh)
245 return;
246
247 RemoveFrameId(rfh->GetProcess()->GetID(), rfh->GetRoutingID());
248 }
249
250 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById(
251 content::WebContents* web_contents,
252 int frame_id) {
253 // Although it is technically possible to map |frame_id| to a RenderFrameHost
254 // without WebContents, we choose to not do that because in the extension API
255 // frameIds are only guaranteed to be meaningful in combination with a tabId.
256 if (!web_contents)
257 return nullptr;
258
259 if (frame_id == -1)
260 return nullptr;
261
262 if (frame_id == 0)
263 return web_contents->GetMainFrame();
264
265 DCHECK_GE(frame_id, 1);
266 return web_contents->FindFrameByFrameTreeNodeId(frame_id);
267 }
268
269 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698