Chromium Code Reviews| Index: extensions/browser/extension_api_frame_id_map.cc |
| diff --git a/extensions/browser/extension_api_frame_id_map.cc b/extensions/browser/extension_api_frame_id_map.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..adf6fa302957e2d2aa28813764cfdcab83eecf65 |
| --- /dev/null |
| +++ b/extensions/browser/extension_api_frame_id_map.cc |
| @@ -0,0 +1,179 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "extensions/browser/extension_api_frame_id_map.h" |
| + |
| +#include <tuple> |
| + |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/render_frame_host.h" |
| +#include "content/public/browser/render_process_host.h" |
| +#include "content/public/browser/web_contents.h" |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| + |
| +// The map is accessed on the IO and UI thread, so construct it once and never |
| +// delete it. |
| +base::LazyInstance<ExtensionApiFrameIdMap>::Leaky g_map_instance = |
| + LAZY_INSTANCE_INITIALIZER; |
| + |
| +int GetFrameIdFromFrame(content::RenderFrameHost* rfh) { |
| + if (!rfh) |
| + return ExtensionApiFrameId::kInvalidFrameId; |
| + if (rfh->GetParent()) |
| + return rfh->GetFrameTreeNodeId(); |
| + return 0; // Main frame. |
| +} |
| + |
| +} // namespace |
| + |
| +ExtensionApiFrameId::ExtensionApiFrameId() |
| + : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {} |
| + |
| +ExtensionApiFrameId::ExtensionApiFrameId(int frame_id, int parent_frame_id) |
| + : frame_id(frame_id), parent_frame_id(parent_frame_id) {} |
| + |
| +ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey() |
| + : render_process_id(-1), frame_routing_id(-1) {} |
| + |
| +ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey( |
| + int render_process_id, int frame_routing_id) |
| + : render_process_id(render_process_id), |
| + frame_routing_id(frame_routing_id) {} |
| + |
| +bool ExtensionApiFrameIdMap::RenderFrameIdKey::operator<( |
| + const RenderFrameIdKey& other) const { |
| + return std::tie(render_process_id, frame_routing_id) < |
| + std::tie(other.render_process_id, other.frame_routing_id); |
| +} |
| + |
| +ExtensionApiFrameIdMap::ExtensionApiFrameIdMap() {} |
| + |
| +ExtensionApiFrameIdMap::~ExtensionApiFrameIdMap() {} |
| + |
| +ExtensionApiFrameIdMap* ExtensionApiFrameIdMap::Get() { |
| + return g_map_instance.Pointer(); |
| +} |
| + |
| +ExtensionApiFrameId ExtensionApiFrameIdMap::KeyToValue( |
| + const RenderFrameIdKey& key) const { |
| + content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( |
| + key.render_process_id, key.frame_routing_id); |
| + return ExtensionApiFrameId( |
| + GetFrameIdFromFrame(rfh), |
| + GetFrameIdFromFrame(rfh ? rfh->GetParent() : nullptr)); |
| +} |
| + |
| +const ExtensionApiFrameId& ExtensionApiFrameIdMap::LookupFrameIdOnUI( |
| + const RenderFrameIdKey& key) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + 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
|
| + if (frame_id_iter != frame_id_map_.end()) |
| + return frame_id_iter->second; |
| + |
| + auto kvpair = FrameIdMap::value_type(key, KeyToValue(key)); |
| + base::AutoLock lock(frame_id_map_lock_); |
| + return frame_id_map_.insert(kvpair).first->second; |
| +} |
| + |
| +void ExtensionApiFrameIdMap::GotFrameIdOnIO(const RenderFrameIdKey& key) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + // |frame_id_map_lock_| is not needed here because GotFrameIdOnIO is only |
| + // called after returning from LookupFrameIdOnUI(). Items from the map are |
| + // never removed, so it is safe to read values without lock. |
| + FrameIdMap::const_iterator frame_id_iter = frame_id_map_.find(key); |
| + CHECK(frame_id_iter != frame_id_map_.end()); |
| + |
| + const ExtensionApiFrameId& extension_api_frame_id = frame_id_iter->second; |
| + FrameIdCallbackList& callbacks = callbacks_map_[key]; |
| + // If GetFrameIdOnIO is called in one of the callbacks, another callback is |
| + // appended to the list. So we cannot use a fancy range-based for loop. |
| + for (FrameIdCallbackList::iterator it = callbacks.begin(); |
| + it != callbacks.end(); ++it) { |
| + it->Run(extension_api_frame_id); |
| + } |
| + |
| + // Remove the callback list. Because the extension frame ID is cached, this |
| + // list is never created again for |key|. |
| + callbacks_map_.erase(key); |
| +} |
| + |
| +void ExtensionApiFrameIdMap::GetFrameIdOnIO(int render_process_id, |
| + int frame_routing_id, |
| + const FrameIdCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); |
| + |
| + const RenderFrameIdKey key(render_process_id, frame_routing_id); |
| + CallbackMap::iterator callbacks_iter = callbacks_map_.find(key); |
| + if (callbacks_iter != callbacks_map_.end()) { |
| + // There is a pending lookup for the extension frame ID. |
| + callbacks_iter->second.push_back(callback); |
| + return; |
| + } |
| + |
| + FrameIdMap::const_iterator frame_id_iter; |
| + bool is_frame_id_found = false; |
| + { |
| + base::AutoLock lock(frame_id_map_lock_); |
| + frame_id_iter = frame_id_map_.find(key); |
| + is_frame_id_found = frame_id_iter != frame_id_map_.end(); |
| + } |
| + if (is_frame_id_found) { |
| + callback.Run(frame_id_iter->second); |
| + return; |
| + } |
| + |
| + // The key was seen for the first time, hop to the UI thread to look up the |
| + // extension frame ID. |
| + callbacks_map_[key].push_back(callback); |
| + content::BrowserThread::PostTaskAndReply( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(base::IgnoreResult(&ExtensionApiFrameIdMap::LookupFrameIdOnUI), |
| + base::Unretained(this), key), |
| + base::Bind(&ExtensionApiFrameIdMap::GotFrameIdOnIO, |
| + base::Unretained(this), key)); |
| +} |
| + |
| +const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId( |
| + int render_process_id, |
| + int frame_routing_id) { |
| + return LookupFrameIdOnUI( |
| + RenderFrameIdKey(render_process_id, frame_routing_id)); |
| +} |
| + |
| +const ExtensionApiFrameId& ExtensionApiFrameIdMap::GetFrameId( |
| + content::RenderFrameHost* rfh) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + |
| + 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
|
| + return LookupFrameIdOnUI(RenderFrameIdKey()); |
| + |
| + return LookupFrameIdOnUI( |
| + RenderFrameIdKey(rfh->GetProcess()->GetID(), rfh->GetRoutingID())); |
| +} |
| + |
| +content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById( |
| + content::WebContents* web_contents, |
| + int frame_id) { |
| + // Although it is technically possible to map |frame_id| to a RenderFrameHost |
| + // without WebContents, we choose to not do that because in the extension API |
| + // frameIds are only guaranteed to be meaningful in combination with a tabId. |
| + if (!web_contents) |
| + return nullptr; |
| + |
| + if (frame_id == 0) |
| + return web_contents->GetMainFrame(); |
| + |
| + if (frame_id == -1) |
| + 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.
|
| + |
| + DCHECK_GE(frame_id, 1); |
| + return web_contents->FindFrameByFrameTreeNodeId(frame_id); |
| +} |
| + |
| +} // namespace extensions |