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 <tuple> | |
| 8 | |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 #include "content/public/browser/render_frame_host.h" | |
| 11 #include "content/public/browser/render_process_host.h" | |
| 12 #include "content/public/browser/web_contents.h" | |
| 13 | |
| 14 namespace extensions { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 // The map is accessed on the IO and UI thread, so construct it once and never | |
| 19 // delete it. | |
| 20 base::LazyInstance<ExtensionApiFrameIdMap>::Leaky g_map_instance = | |
| 21 LAZY_INSTANCE_INITIALIZER; | |
| 22 | |
| 23 int GetFrameIdFromFrame(content::RenderFrameHost* rfh) { | |
| 24 if (!rfh) | |
| 25 return ExtensionApiFrameId::kInvalidFrameId; | |
| 26 if (rfh->GetParent()) | |
| 27 return rfh->GetFrameTreeNodeId(); | |
| 28 return 0; // Main frame. | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 ExtensionApiFrameId::ExtensionApiFrameId() | |
| 34 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {} | |
| 35 | |
| 36 ExtensionApiFrameId::ExtensionApiFrameId(int frame_id, int parent_frame_id) | |
| 37 : frame_id(frame_id), parent_frame_id(parent_frame_id) {} | |
| 38 | |
| 39 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey() | |
| 40 : render_process_id(-1), frame_routing_id(-1) {} | |
| 41 | |
| 42 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey( | |
| 43 int render_process_id, int frame_routing_id) | |
| 44 : render_process_id(render_process_id), | |
| 45 frame_routing_id(frame_routing_id) {} | |
| 46 | |
| 47 bool ExtensionApiFrameIdMap::RenderFrameIdKey::operator<( | |
| 48 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 ExtensionApiFrameIdMap::ExtensionApiFrameIdMap() {} | |
| 54 | |
| 55 ExtensionApiFrameIdMap::~ExtensionApiFrameIdMap() {} | |
| 56 | |
| 57 ExtensionApiFrameIdMap* ExtensionApiFrameIdMap::Get() { | |
| 58 return g_map_instance.Pointer(); | |
| 59 } | |
| 60 | |
| 61 ExtensionApiFrameId ExtensionApiFrameIdMap::KeyToValue( | |
| 62 const RenderFrameIdKey& key) const { | |
| 63 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( | |
| 64 key.render_process_id, key.frame_routing_id); | |
| 65 return ExtensionApiFrameId( | |
| 66 GetFrameIdFromFrame(rfh), | |
| 67 GetFrameIdFromFrame(rfh ? rfh->GetParent() : nullptr)); | |
| 68 } | |
| 69 | |
| 70 const ExtensionApiFrameId& ExtensionApiFrameIdMap::LookupFrameIdOnUI( | |
| 71 const RenderFrameIdKey& key) { | |
| 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 73 | |
| 74 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key); | |
|
nasko
2015/12/17 01:19:48
All access to frame_id_map_ must be locked if it i
robwu
2015/12/22 16:55:46
The only method that writes to the map is on the U
| |
| 75 if (frame_id_iter != frame_id_map_.end()) | |
| 76 return frame_id_iter->second; | |
| 77 | |
| 78 auto kvpair = FrameIdMap::value_type(key, KeyToValue(key)); | |
| 79 base::AutoLock lock(frame_id_map_lock_); | |
| 80 return frame_id_map_.insert(kvpair).first->second; | |
| 81 } | |
| 82 | |
| 83 void ExtensionApiFrameIdMap::GotFrameIdOnIO(const RenderFrameIdKey& key) { | |
| 84 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 85 | |
| 86 // |frame_id_map_lock_| is not needed here because GotFrameIdOnIO is only | |
| 87 // called after returning from LookupFrameIdOnUI(). Items from the map are | |
| 88 // never removed, so it is safe to read values without lock. | |
| 89 FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key); | |
| 90 CHECK(frame_id_iter != frame_id_map_.end()); | |
| 91 | |
| 92 const ExtensionApiFrameId& extension_api_frame_id = frame_id_iter->second; | |
| 93 FrameIdCallbackList& callbacks = callbacks_map_[key]; | |
| 94 // If GetFrameIdOnIO is called in one of the callbacks, another callback is | |
| 95 // appended to the list. So we cannot use a fancy range-based for loop. | |
| 96 for (FrameIdCallbackList::iterator it = callbacks.begin(); | |
| 97 it != callbacks.end(); ++it) { | |
| 98 it->Run(extension_api_frame_id); | |
| 99 } | |
| 100 | |
| 101 // Remove the callback list. Because the extension frame ID is cached, this | |
| 102 // list is never created again for |key|. | |
| 103 callbacks_map_.erase(key); | |
| 104 } | |
| 105 | |
| 106 void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id, | |
| 107 int frame_routing_id, | |
| 108 const FrameIdCallback& callback) { | |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 110 | |
| 111 const RenderFrameIdKey key(render_process_id, frame_routing_id); | |
| 112 CallbackMap::iterator callbacks_iter = callbacks_map_.find(key); | |
| 113 if (callbacks_iter != callbacks_map_.end()) { | |
| 114 // There is a pending lookup for the extension frame ID. | |
| 115 callbacks_iter->second.push_back(callback); | |
| 116 return; | |
| 117 } | |
| 118 | |
| 119 FrameIdMap::const_iterator frame_id_iter; | |
| 120 bool is_frame_id_found = false; | |
| 121 { | |
| 122 base::AutoLock lock(frame_id_map_lock_); | |
| 123 frame_id_iter = frame_id_map_.find(key); | |
| 124 is_frame_id_found = frame_id_iter != frame_id_map_.end(); | |
| 125 } | |
| 126 if (is_frame_id_found) { | |
| 127 callback.Run(frame_id_iter->second); | |
| 128 return; | |
| 129 } | |
| 130 | |
| 131 // The key was seen for the first time, hop to the UI thread to look up the | |
| 132 // extension frame ID. | |
| 133 callbacks_map_[key].push_back(callback); | |
| 134 content::BrowserThread::PostTaskAndReply( | |
| 135 content::BrowserThread::UI, FROM_HERE, | |
| 136 base::Bind(base::IgnoreResult(&ExtensionApiFrameIdMap::LookupFrameIdOnUI), | |
| 137 base::Unretained(this), key), | |
| 138 base::Bind(&ExtensionApiFrameIdMap::GotFrameIdOnIO, | |
| 139 base::Unretained(this), key)); | |
| 140 } | |
| 141 | |
| 142 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId( | |
| 143 int render_process_id, | |
| 144 int frame_routing_id) { | |
| 145 return LookupFrameIdOnUI( | |
| 146 RenderFrameIdKey(render_process_id, frame_routing_id)); | |
| 147 } | |
| 148 | |
| 149 const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId( | |
| 150 content::RenderFrameHost* rfh) { | |
| 151 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 152 | |
| 153 if (!rfh) | |
|
nasko
2015/12/17 01:19:48
If the RFH is null, why not directly return Extens
robwu
2015/12/22 16:55:46
I used to need a const&, so I took the value from
| |
| 154 return LookupFrameIdOnUI(RenderFrameIdKey()); | |
| 155 | |
| 156 return LookupFrameIdOnUI( | |
| 157 RenderFrameIdKey(rfh->GetProcess()->GetID(), rfh->GetRoutingID())); | |
| 158 } | |
| 159 | |
| 160 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById( | |
| 161 content::WebContents* web_contents, | |
| 162 int frame_id) { | |
| 163 // Although it is technically possible to map |frame_id| to a RenderFrameHost | |
| 164 // without WebContents, we choose to not do that because in the extension API | |
| 165 // frameIds are only guaranteed to be meaningful in combination with a tabId. | |
| 166 if (!web_contents) | |
| 167 return nullptr; | |
| 168 | |
| 169 if (frame_id == 0) | |
| 170 return web_contents->GetMainFrame(); | |
| 171 | |
| 172 if (frame_id == -1) | |
| 173 return nullptr; | |
|
nasko
2015/12/17 01:19:49
I'd move this before the frame_id == 0, so all nul
robwu
2015/12/22 16:55:46
Done.
| |
| 174 | |
| 175 DCHECK_GE(frame_id, 1); | |
| 176 return web_contents->FindFrameByFrameTreeNodeId(frame_id); | |
| 177 } | |
| 178 | |
| 179 } // namespace extensions | |
| OLD | NEW |