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

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: Vend frameIds at 1 location; minimize thread hops for webRequest 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;
nasko 2015/12/09 23:06:09 Why is this map needed and must grow unbound? Why
robwu 2015/12/10 00:00:51 Because I think that there is no guarantee that al
nasko 2015/12/17 01:19:48 There should be no events for webNavigation after
36
37 // A set of identifiers that uniquely identifies a RenderFrame.
38 struct RenderFrameIdKey {
39 RenderFrameIdKey(int process_id, int routing_id)
nasko 2015/12/09 23:06:09 nit: frame_routing_id
robwu 2015/12/10 00:00:51 Done. (everywhere, and also changed process_id to
40 : process_id(process_id), routing_id(routing_id) {}
41
42 // The process ID of the renderer that contains the RenderFrame.
43 int process_id;
44 // The routing ID of the RenderFrame.
45 int routing_id;
46
47 bool operator<(const RenderFrameIdKey& other) const {
48 return std::tie(process_id, routing_id) <
49 std::tie(other.process_id, other.routing_id);
50 }
51 };
52
53 } // namespace
54
55 ExtensionApiFrameId::ExtensionApiFrameId()
56 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {}
57
58 int GetFrameIdFromFrame(content::RenderFrameHost* rfh) {
59 if (!rfh)
60 return ExtensionApiFrameId::kInvalidFrameId;
61 if (rfh->GetParent())
62 return rfh->GetFrameTreeNodeID();
63 return 0; // Main frame.
64 }
65
66 void InitializeFromFrame(ExtensionApiFrameId& extension_api_frame_id,
67 content::RenderFrameHost* rfh) {
68 if (rfh) {
nasko 2015/12/09 23:06:09 We usually use early returns. Example pattern: if
robwu 2015/12/10 00:00:51 Done.
69 extension_api_frame_id.frame_id = GetFrameIdFromFrame(rfh);
70 extension_api_frame_id.parent_frame_id =
71 GetFrameIdFromFrame(rfh->GetParent());
72 }
73 }
74
75 const ExtensionApiFrameId& LookupFrameIdOnUI(const RenderFrameIdKey& key) {
76 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
77
78 FrameIdMap& frame_id_map = g_frame_id_map.Get();
79 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
80 if (frame_id_iter != frame_id_map.end())
81 return frame_id_iter->second;
82
83 ExtensionApiFrameId& extension_api_frame_id = frame_id_map[key];
84 InitializeFromFrame(
85 extension_api_frame_id,
86 content::RenderFrameHost::FromID(key.process_id, key.routing_id));
87 return extension_api_frame_id;
88 }
89
90 void GotFrameId(const RenderFrameIdKey& key) {
91 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
92
93 FrameIdMap& frame_id_map = g_frame_id_map.Get();
94 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
95 CHECK(frame_id_iter != frame_id_map.end());
96
97 const ExtensionApiFrameId& extension_api_frame_id = frame_id_iter->second;
98 CallbackMap& callbacks_map = g_callbacks_map.Get();
99 FrameIdCallbackList& callbacks = callbacks_map[key];
100 // If GetFrameIdOnIO is called in one of the callbacks, another callback is
101 // appended to the list. So we cannot use a fancy range-based for loop.
102 for (FrameIdCallbackList::iterator it = callbacks.begin();
103 it != callbacks.end(); ++it) {
104 it->Run(extension_api_frame_id);
105 }
106
107 // Remove the callback list. Because the extension frame ID is cached, this
108 // list is never created again for |key|.
109 callbacks_map.erase(key);
110 }
111
112 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int process_id,
113 int routing_id,
114 FrameIdCallback callback) {
115 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
116
117 const RenderFrameIdKey key(process_id, routing_id);
118 CallbackMap& callbacks_map = g_callbacks_map.Get();
119 CallbackMap::iterator callbacks_iter = callbacks_map.find(key);
120 if (callbacks_iter != callbacks_map.end()) {
121 // There is a pending lookup for the extension frame ID.
122 callbacks_iter->second.push_back(callback);
123 return;
124 }
125
126 FrameIdMap& frame_id_map = g_frame_id_map.Get();
127 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
128 if (frame_id_iter != frame_id_map.end()) {
129 // The extension frame ID has already been looked up.
130 callback.Run(frame_id_iter->second);
131 return;
132 }
133
134 // The key was seen for the first time, hop to the UI thread to look up the
135 // extension frame ID.
136 callbacks_map[key].push_back(callback);
137 content::BrowserThread::PostTaskAndReply(
138 content::BrowserThread::UI, FROM_HERE,
139 base::Bind(base::IgnoreResult(&LookupFrameIdOnUI), key),
140 base::Bind(&GotFrameId, key));
141 }
142
143 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId(int process_id,
144 int routing_id) {
145 return LookupFrameIdOnUI(RenderFrameIdKey(process_id, routing_id));
146 }
147
148 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId(
149 content::RenderFrameHost* rfh) {
150 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
151
152 FrameIdMap& frame_id_map = g_frame_id_map.Get();
153 if (!rfh)
154 return frame_id_map[RenderFrameIdKey(-1, -1)];
155
156 const RenderFrameIdKey key(rfh->GetProcess()->GetID(), rfh->GetRoutingID());
157
158 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key);
159 if (frame_id_iter != frame_id_map.end())
160 return frame_id_iter->second;
161
162 ExtensionApiFrameId& extension_api_frame_id = frame_id_map[key];
163 InitializeFromFrame(extension_api_frame_id, rfh);
164 return extension_api_frame_id;
165 }
166
167 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById(
168 int frame_id,
169 content::WebContents* web_contents) {
170 // Although it is technically possible to map |frame_id| to a RenderFrameHost
171 // without WebContents, we choose to not do that because in the extension API
172 // frameIds are only guaranteed to be meaningful in combination with a tabId.
173 if (!web_contents)
174 return nullptr;
175
176 if (frame_id == 0)
177 return web_contents->GetMainFrame();
178
179 if (frame_id == -1)
180 return nullptr;
181
182 DCHECK_GE(frame_id, 1);
183 return web_contents->FindFrameByFrameTreeNodeID(frame_id);
184 }
185
186 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698