Chromium Code Reviews| 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 <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; | |
|
Devlin
2015/12/10 21:53:07
Can we just defined RenderFrameIdKey here?
robwu
2015/12/11 00:53:48
Done.
| |
| 22 typedef std::list<ExtensionApiFrameIdMap::FrameIdCallback> FrameIdCallbackList; | |
|
Devlin
2015/12/10 21:53:07
prefer using
robwu
2015/12/11 00:53:48
Done.
| |
| 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; | |
| 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(content::RenderFrameHost* rfh, | |
|
Devlin
2015/12/10 21:53:07
Seems like this could just be a constructor of Ext
robwu
2015/12/11 00:53:48
Done.
| |
| 68 ExtensionApiFrameId* extension_api_frame_id) { | |
| 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]; | |
|
Devlin
2015/12/10 21:53:07
and then this set would become something like:
//
robwu
2015/12/11 00:53:48
Done.
| |
| 86 InitializeFromFrame(content::RenderFrameHost::FromID(key.render_process_id, | |
| 87 key.frame_routing_id), | |
| 88 &extension_api_frame_id); | |
| 89 return extension_api_frame_id; | |
| 90 } | |
| 91 | |
| 92 void GotFrameId(const RenderFrameIdKey& key) { | |
|
Devlin
2015/12/10 21:53:07
put this in the anonymous namespace?
robwu
2015/12/11 00:53:48
Done.
Devlin
2015/12/12 14:25:00
Wasn't done?
robwu
2015/12/14 20:55:41
Done in 15, but made redundant by making it a priv
| |
| 93 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 94 | |
| 95 FrameIdMap& frame_id_map = g_frame_id_map.Get(); | |
| 96 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key); | |
| 97 CHECK(frame_id_iter != frame_id_map.end()); | |
| 98 | |
| 99 const ExtensionApiFrameId& extension_api_frame_id = frame_id_iter->second; | |
| 100 CallbackMap& callbacks_map = g_callbacks_map.Get(); | |
| 101 FrameIdCallbackList& callbacks = callbacks_map[key]; | |
| 102 // If GetFrameIdOnIO is called in one of the callbacks, another callback is | |
| 103 // appended to the list. So we cannot use a fancy range-based for loop. | |
|
Devlin
2015/12/10 21:53:07
Heh. Tricky.
Might be worth a comment on FrameId
robwu
2015/12/11 00:53:48
Done.
| |
| 104 for (FrameIdCallbackList::iterator it = callbacks.begin(); | |
| 105 it != callbacks.end(); ++it) { | |
| 106 it->Run(extension_api_frame_id); | |
| 107 } | |
| 108 | |
| 109 // Remove the callback list. Because the extension frame ID is cached, this | |
| 110 // list is never created again for |key|. | |
| 111 callbacks_map.erase(key); | |
| 112 } | |
| 113 | |
| 114 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id, | |
| 115 int frame_routing_id, | |
| 116 FrameIdCallback callback) { | |
| 117 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 118 | |
| 119 const RenderFrameIdKey key(render_process_id, frame_routing_id); | |
| 120 CallbackMap& callbacks_map = g_callbacks_map.Get(); | |
| 121 CallbackMap::iterator callbacks_iter = callbacks_map.find(key); | |
| 122 if (callbacks_iter != callbacks_map.end()) { | |
| 123 // There is a pending lookup for the extension frame ID. | |
| 124 callbacks_iter->second.push_back(callback); | |
| 125 return; | |
| 126 } | |
| 127 | |
| 128 FrameIdMap& frame_id_map = g_frame_id_map.Get(); | |
|
Devlin
2015/12/10 21:53:07
Is this safe? Map lookup operations aren't purely
robwu
2015/12/11 00:53:48
The initial version was safe, but after adding the
| |
| 129 FrameIdMap::const_iterator frame_id_iter = frame_id_map.find(key); | |
| 130 if (frame_id_iter != frame_id_map.end()) { | |
| 131 // The extension frame ID has already been looked up. | |
| 132 callback.Run(frame_id_iter->second); | |
| 133 return; | |
| 134 } | |
| 135 | |
| 136 // The key was seen for the first time, hop to the UI thread to look up the | |
| 137 // extension frame ID. | |
| 138 callbacks_map[key].push_back(callback); | |
| 139 content::BrowserThread::PostTaskAndReply( | |
| 140 content::BrowserThread::UI, FROM_HERE, | |
| 141 base::Bind(base::IgnoreResult(&LookupFrameIdOnUI), key), | |
| 142 base::Bind(&GotFrameId, key)); | |
| 143 } | |
| 144 | |
| 145 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId( | |
| 146 int render_process_id, | |
| 147 int frame_routing_id) { | |
| 148 return LookupFrameIdOnUI( | |
| 149 RenderFrameIdKey(render_process_id, 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(rfh, &extension_api_frame_id); | |
| 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 | |
| OLD | NEW |