| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/browser/extension_api_frame_id_map.h" | 5 #include "extensions/browser/extension_api_frame_id_map.h" |
| 6 | 6 |
| 7 #include <tuple> | 7 #include <tuple> |
| 8 | 8 |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/browser/render_frame_host.h" | 10 #include "content/public/browser/render_frame_host.h" |
| 11 #include "content/public/browser/render_process_host.h" | 11 #include "content/public/browser/render_process_host.h" |
| 12 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.h" |
| 13 #include "content/public/common/child_process_host.h" | 13 #include "content/public/common/child_process_host.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 // The map is accessed on the IO and UI thread, so construct it once and never | 19 // The map is accessed on the IO and UI thread, so construct it once and never |
| 20 // delete it. | 20 // delete it. |
| 21 base::LazyInstance<ExtensionApiFrameIdMap>::Leaky g_map_instance = | 21 base::LazyInstance<ExtensionApiFrameIdMap>::Leaky g_map_instance = |
| 22 LAZY_INSTANCE_INITIALIZER; | 22 LAZY_INSTANCE_INITIALIZER; |
| 23 | 23 |
| 24 } // namespace | 24 } // namespace |
| 25 | 25 |
| 26 const int ExtensionApiFrameIdMap::kInvalidFrameId = -1; | 26 const int ExtensionApiFrameIdMap::kInvalidFrameId = -1; |
| 27 const int ExtensionApiFrameIdMap::kTopFrameId = 0; |
| 27 | 28 |
| 28 ExtensionApiFrameIdMap::CachedFrameIdPair::CachedFrameIdPair() | 29 ExtensionApiFrameIdMap::CachedFrameIdPair::CachedFrameIdPair() |
| 29 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {} | 30 : frame_id(kInvalidFrameId), parent_frame_id(kInvalidFrameId) {} |
| 30 | 31 |
| 31 ExtensionApiFrameIdMap::CachedFrameIdPair::CachedFrameIdPair( | 32 ExtensionApiFrameIdMap::CachedFrameIdPair::CachedFrameIdPair( |
| 32 int frame_id, | 33 int frame_id, |
| 33 int parent_frame_id) | 34 int parent_frame_id) |
| 34 : frame_id(frame_id), parent_frame_id(parent_frame_id) {} | 35 : frame_id(frame_id), parent_frame_id(parent_frame_id) {} |
| 35 | 36 |
| 36 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey() | 37 ExtensionApiFrameIdMap::RenderFrameIdKey::RenderFrameIdKey() |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 return g_map_instance.Pointer(); | 69 return g_map_instance.Pointer(); |
| 69 } | 70 } |
| 70 | 71 |
| 71 int ExtensionApiFrameIdMap::GetFrameId(content::RenderFrameHost* rfh) { | 72 int ExtensionApiFrameIdMap::GetFrameId(content::RenderFrameHost* rfh) { |
| 72 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 73 | 74 |
| 74 if (!rfh) | 75 if (!rfh) |
| 75 return kInvalidFrameId; | 76 return kInvalidFrameId; |
| 76 if (rfh->GetParent()) | 77 if (rfh->GetParent()) |
| 77 return rfh->GetFrameTreeNodeId(); | 78 return rfh->GetFrameTreeNodeId(); |
| 78 return 0; // Main frame. | 79 return kTopFrameId; |
| 79 } | 80 } |
| 80 | 81 |
| 81 int ExtensionApiFrameIdMap::GetParentFrameId(content::RenderFrameHost* rfh) { | 82 int ExtensionApiFrameIdMap::GetParentFrameId(content::RenderFrameHost* rfh) { |
| 82 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 83 | 84 |
| 84 return rfh ? GetFrameId(rfh->GetParent()) : kInvalidFrameId; | 85 return rfh ? GetFrameId(rfh->GetParent()) : kInvalidFrameId; |
| 85 } | 86 } |
| 86 | 87 |
| 87 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById( | 88 content::RenderFrameHost* ExtensionApiFrameIdMap::GetRenderFrameHostById( |
| 88 content::WebContents* web_contents, | 89 content::WebContents* web_contents, |
| 89 int frame_id) { | 90 int frame_id) { |
| 90 // Although it is technically possible to map |frame_id| to a RenderFrameHost | 91 // Although it is technically possible to map |frame_id| to a RenderFrameHost |
| 91 // without WebContents, we choose to not do that because in the extension API | 92 // without WebContents, we choose to not do that because in the extension API |
| 92 // frameIds are only guaranteed to be meaningful in combination with a tabId. | 93 // frameIds are only guaranteed to be meaningful in combination with a tabId. |
| 93 if (!web_contents) | 94 if (!web_contents) |
| 94 return nullptr; | 95 return nullptr; |
| 95 | 96 |
| 96 if (frame_id == kInvalidFrameId) | 97 if (frame_id == kInvalidFrameId) |
| 97 return nullptr; | 98 return nullptr; |
| 98 | 99 |
| 99 if (frame_id == 0) | 100 if (frame_id == kTopFrameId) |
| 100 return web_contents->GetMainFrame(); | 101 return web_contents->GetMainFrame(); |
| 101 | 102 |
| 102 DCHECK_GE(frame_id, 1); | 103 DCHECK_GE(frame_id, 1); |
| 103 return web_contents->FindFrameByFrameTreeNodeId(frame_id); | 104 return web_contents->FindFrameByFrameTreeNodeId(frame_id); |
| 104 } | 105 } |
| 105 | 106 |
| 106 ExtensionApiFrameIdMap::CachedFrameIdPair ExtensionApiFrameIdMap::KeyToValue( | 107 ExtensionApiFrameIdMap::CachedFrameIdPair ExtensionApiFrameIdMap::KeyToValue( |
| 107 const RenderFrameIdKey& key) const { | 108 const RenderFrameIdKey& key) const { |
| 108 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( | 109 content::RenderFrameHost* rfh = content::RenderFrameHost::FromID( |
| 109 key.render_process_id, key.frame_routing_id); | 110 key.render_process_id, key.frame_routing_id); |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 } | 237 } |
| 237 | 238 |
| 238 void ExtensionApiFrameIdMap::RemoveFrameId(const RenderFrameIdKey& key) { | 239 void ExtensionApiFrameIdMap::RemoveFrameId(const RenderFrameIdKey& key) { |
| 239 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 240 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 240 | 241 |
| 241 base::AutoLock lock(frame_id_map_lock_); | 242 base::AutoLock lock(frame_id_map_lock_); |
| 242 frame_id_map_.erase(key); | 243 frame_id_map_.erase(key); |
| 243 } | 244 } |
| 244 | 245 |
| 245 } // namespace extensions | 246 } // namespace extensions |
| OLD | NEW |