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

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: Charlie's nits (#33) 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 <list>
8 #include <map>
9 #include <tuple>
10
11 #include "base/lazy_instance.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/render_frame_host.h"
14 #include "content/public/browser/render_process_host.h"
15 #include "content/public/browser/web_contents.h"
16
17 namespace extensions {
18
19 namespace {
20
21 struct RenderFrameIdKey;
22 typedef std::list<ExtensionApiFrameIdMap::FrameIdCallback> FrameIdCallbackList;
23 typedef std::map<RenderFrameIdKey, ExtensionApiFrameId> FrameIdMap;
24 typedef std::map<RenderFrameIdKey, FrameIdCallbackList> CallbackMap;
25
26 // Queued callbacks for use on the IO thread. This queue is only used when the
27 // frameId needs to be fetched from the UI thread.
28 base::LazyInstance<CallbackMap> g_callbacks_map = LAZY_INSTANCE_INITIALIZER;
29
30 // This frameId map is only modified on the UI thread and used for two purposes:
31 // - On the IO thread, it avoids unnecessary thread hops.
32 // - On the UI thread and the IO thread, it ensures that the frameId remains
33 // constant, even after removing a frame.
34 // Items are never removed from this map.
35 base::LazyInstance<FrameIdMap> g_frame_id_map = LAZY_INSTANCE_INITIALIZER;
battre 2015/12/10 15:31:39 Is it ok that data in this map is never removed? T
robwu 2015/12/10 16:39:29 Collisions are not probably because those process
36
37 // A set of identifiers that uniquely identifies a RenderFrame.
38 struct RenderFrameIdKey {
39 RenderFrameIdKey(int render_process_id, int frame_routing_id)
40 : render_process_id(render_process_id),
41 frame_routing_id(frame_routing_id) {}
42
43 // The process ID of the renderer that contains the RenderFrame.
44 int render_process_id;
45 // The routing ID of the RenderFrame.
46 int frame_routing_id;
47
48 bool operator<(const RenderFrameIdKey& other) const {
49 return std::tie(render_process_id, frame_routing_id) <
50 std::tie(other.render_process_id, other.frame_routing_id);
51 }
52 };
53
54 } // namespace
55
56 ExtensionApiFrameId::ExtensionApiFrameId()
57 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {}
58
59 int GetFrameIdFromFrame(content::RenderFrameHost* rfh) {
60 if (!rfh)
61 return ExtensionApiFrameId::kInvalidFrameId;
62 if (rfh->GetParent())
63 return rfh->GetFrameTreeNodeId();
64 return 0; // Main frame.
65 }
66
67 void InitializeFromFrame(ExtensionApiFrameId& extension_api_frame_id,
battre 2015/12/10 15:31:39 https://google.github.io/styleguide/cppguide.html#
robwu 2015/12/10 16:39:29 Done.
68 content::RenderFrameHost* rfh) {
69 if (!rfh)
70 return;
71
72 extension_api_frame_id.frame_id = GetFrameIdFromFrame(rfh);
73 extension_api_frame_id.parent_frame_id =
74 GetFrameIdFromFrame(rfh->GetParent());
75 }
76
77 const ExtensionApiFrameId& LookupFrameIdOnUI(const RenderFrameIdKey& key) {
78 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
79
80 FrameIdMap& frame_id_map = g_frame_id_map.Get();
81 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
82 if (frame_id_iter != frame_id_map.end())
83 return frame_id_iter->second;
84
85 ExtensionApiFrameId& extension_api_frame_id = frame_id_map[key];
86 InitializeFromFrame(
87 extension_api_frame_id,
88 content::RenderFrameHost::FromID(key.render_process_id,
89 key.frame_routing_id));
90 return extension_api_frame_id;
91 }
92
93 void GotFrameId(const RenderFrameIdKey& key) {
94 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
95
96 FrameIdMap& frame_id_map = g_frame_id_map.Get();
battre 2015/12/10 15:31:39 In LookupFrameIdOnUI you are accessing this on the
robwu 2015/12/10 16:39:29 This is the sequence of actions: GetFrameIdOnIO:
97 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
98 CHECK(frame_id_iter != frame_id_map.end());
99
100 const ExtensionApiFrameId& extension_api_frame_id = frame_id_iter->second;
101 CallbackMap& callbacks_map = g_callbacks_map.Get();
102 FrameIdCallbackList& callbacks = callbacks_map[key];
103 // If GetFrameIdOnIO is called in one of the callbacks, another callback is
104 // appended to the list. So we cannot use a fancy range-based for loop.
105 for (FrameIdCallbackList::iterator it = callbacks.begin();
106 it != callbacks.end(); ++it) {
107 it->Run(extension_api_frame_id);
108 }
109
110 // Remove the callback list. Because the extension frame ID is cached, this
111 // list is never created again for |key|.
112 callbacks_map.erase(key);
113 }
114
115 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id,
116 int frame_routing_id,
117 FrameIdCallback callback) {
118 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
119
120 const RenderFrameIdKey key(render_process_id, frame_routing_id);
121 CallbackMap& callbacks_map = g_callbacks_map.Get();
122 CallbackMap::iterator callbacks_iter = callbacks_map.find(key);
123 if (callbacks_iter != callbacks_map.end()) {
124 // There is a pending lookup for the extension frame ID.
125 callbacks_iter->second.push_back(callback);
126 return;
127 }
128
129 FrameIdMap& frame_id_map = g_frame_id_map.Get();
130 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
131 if (frame_id_iter != frame_id_map.end()) {
132 // The extension frame ID has already been looked up.
133 callback.Run(frame_id_iter->second);
134 return;
135 }
136
137 // The key was seen for the first time, hop to the UI thread to look up the
138 // extension frame ID.
139 callbacks_map[key].push_back(callback);
140 content::BrowserThread::PostTaskAndReply(
141 content::BrowserThread::UI, FROM_HERE,
142 base::Bind(base::IgnoreResult(&LookupFrameIdOnUI), key),
143 base::Bind(&GotFrameId, key));
144 }
145
146 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId(
147 int render_process_id, int frame_routing_id) {
148 return LookupFrameIdOnUI(RenderFrameIdKey(render_process_id,
149 frame_routing_id));
150 }
151
152 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId(
153 content::RenderFrameHost* rfh) {
154 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
155
156 FrameIdMap& frame_id_map = g_frame_id_map.Get();
157 if (!rfh)
158 return frame_id_map[RenderFrameIdKey(-1, -1)];
159
160 const RenderFrameIdKey key(rfh->GetProcess()->GetID(), rfh->GetRoutingID());
161
162 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
163 if (frame_id_iter != frame_id_map.end())
164 return frame_id_iter->second;
165
166 ExtensionApiFrameId& extension_api_frame_id = frame_id_map[key];
167 InitializeFromFrame(extension_api_frame_id, rfh);
168 return extension_api_frame_id;
169 }
170
171 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById(
172 content::WebContents* web_contents,
173 int frame_id) {
174 // Although it is technically possible to map |frame_id| to a RenderFrameHost
175 // without WebContents, we choose to not do that because in the extension API
176 // frameIds are only guaranteed to be meaningful in combination with a tabId.
177 if (!web_contents)
178 return nullptr;
179
180 if (frame_id == 0)
181 return web_contents->GetMainFrame();
182
183 if (frame_id == -1)
184 return nullptr;
185
186 DCHECK_GE(frame_id, 1);
187 return web_contents->FindFrameByFrameTreeNodeId(frame_id);
188 }
189
190 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698