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 #ifndef EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
| 6 #define EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <map> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/lazy_instance.h" |
| 13 #include "base/macros.h" |
| 14 #include "base/synchronization/lock.h" |
| 15 |
| 16 namespace content { |
| 17 class RenderFrameHost; |
| 18 class WebContents; |
| 19 } // namespace content |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 // Extension frame IDs are exposed through the chrome.* APIs and have the |
| 24 // following characteristics: |
| 25 // - The top-level frame has ID 0. |
| 26 // - Any child frame has a positive ID. |
| 27 // - A non-existant frame has ID -1. |
| 28 // - They are only guaranteed to be unique within a tab. |
| 29 // - The ID does not change during the frame's lifetime and is not re-used after |
| 30 // the frame is removed. The frame may change its current RenderFrameHost over |
| 31 // time, so multiple RenderFrameHosts may map to the same extension frame ID. |
| 32 |
| 33 // This class provides a mapping from a (render_process_id, frame_routing_id) |
| 34 // pair that maps a RenderFrameHost to an extension frame ID. |
| 35 // Unless stated otherwise, the methods can only be called on the UI thread. |
| 36 // |
| 37 // The non-static methods of this class use an internal cache. This cache is |
| 38 // used to minimize IO->UI->IO round-trips of GetFrameIdOnIO. If the cost of |
| 39 // attaching FrameTreeNode IDs to requests is negligible (crbug.com/524228), |
| 40 // then we can remove all key caching and remove the cache from this class. |
| 41 // TODO(robwu): Keep an eye on crbug.com/524228 and act upon the outcome. |
| 42 class ExtensionApiFrameIdMap { |
| 43 public: |
| 44 using FrameIdCallback = |
| 45 base::Callback<void(int extension_api_frame_id, |
| 46 int extension_api_parent_frame_id)>; |
| 47 |
| 48 // An invalid extension API frame ID. |
| 49 static const int kInvalidFrameId; |
| 50 |
| 51 static ExtensionApiFrameIdMap* Get(); |
| 52 |
| 53 // Get the extension API frame ID for |rfh|. |
| 54 static int GetFrameId(content::RenderFrameHost* rfh); |
| 55 |
| 56 // Get the extension API frame ID for the parent of |rfh|. |
| 57 static int GetParentFrameId(content::RenderFrameHost* rfh); |
| 58 |
| 59 // Find the current RenderFrameHost for a given WebContents and extension |
| 60 // frame ID. |
| 61 // Returns nullptr if not found. |
| 62 static content::RenderFrameHost* GetRenderFrameHostById( |
| 63 content::WebContents* web_contents, |
| 64 int frame_id); |
| 65 |
| 66 // Runs |callback| with the result that is equivalent to calling GetFrameId() |
| 67 // on the UI thread. Thread hopping is minimized if possible. Callbacks for |
| 68 // the same |render_process_id| and |frame_routing_id| are guaranteed to be |
| 69 // run in order. The order of other callbacks is undefined. |
| 70 void GetFrameIdOnIO(int render_process_id, |
| 71 int frame_routing_id, |
| 72 const FrameIdCallback& callback); |
| 73 |
| 74 // Looks up the frame ID and stores it in the map. This method should be |
| 75 // called as early as possible, e.g. in a |
| 76 // WebContentsObserver::RenderFrameCreated notification. |
| 77 void CacheFrameId(content::RenderFrameHost* rfh); |
| 78 |
| 79 // Removes the frame ID mapping for a given frame. This method can be called |
| 80 // at any time, but it is typically called when a frame is destroyed. |
| 81 // If this method is not called, the cached mapping for the frame is retained |
| 82 // forever. |
| 83 void RemoveFrameId(content::RenderFrameHost* rfh); |
| 84 |
| 85 protected: |
| 86 friend struct base::DefaultLazyInstanceTraits<ExtensionApiFrameIdMap>; |
| 87 |
| 88 // A set of identifiers that uniquely identifies a RenderFrame. |
| 89 struct RenderFrameIdKey { |
| 90 RenderFrameIdKey(); |
| 91 RenderFrameIdKey(int render_process_id, int frame_routing_id); |
| 92 |
| 93 // The process ID of the renderer that contains the RenderFrame. |
| 94 int render_process_id; |
| 95 |
| 96 // The routing ID of the RenderFrame. |
| 97 int frame_routing_id; |
| 98 |
| 99 bool operator<(const RenderFrameIdKey& other) const; |
| 100 bool operator==(const RenderFrameIdKey& other) const; |
| 101 }; |
| 102 |
| 103 // The cached pair of frame IDs of the frame. Every RenderFrameIdKey |
| 104 // maps to a CachedFrameIdPair. |
| 105 struct CachedFrameIdPair { |
| 106 CachedFrameIdPair(); |
| 107 CachedFrameIdPair(int frame_id, int parent_frame_id); |
| 108 |
| 109 // The extension API frame ID of the frame. |
| 110 int frame_id; |
| 111 |
| 112 // The extension API frame ID of the parent of the frame. |
| 113 int parent_frame_id; |
| 114 }; |
| 115 |
| 116 struct FrameIdCallbacks { |
| 117 FrameIdCallbacks(); |
| 118 ~FrameIdCallbacks(); |
| 119 |
| 120 // This is a std::list so that iterators are not invalidated when the list |
| 121 // is modified during an iteration. |
| 122 std::list<FrameIdCallback> callbacks; |
| 123 |
| 124 // To avoid re-entrant processing of callbacks. |
| 125 bool is_iterating; |
| 126 }; |
| 127 |
| 128 using FrameIdMap = std::map<RenderFrameIdKey, CachedFrameIdPair>; |
| 129 using FrameIdCallbacksMap = std::map<RenderFrameIdKey, FrameIdCallbacks>; |
| 130 |
| 131 ExtensionApiFrameIdMap(); |
| 132 ~ExtensionApiFrameIdMap(); |
| 133 |
| 134 // Determines the value to be stored in |frame_id_map_| for a given key. This |
| 135 // method is only called when |key| is not in |frame_id_map_|. |
| 136 // virtual for testing. |
| 137 virtual CachedFrameIdPair KeyToValue(const RenderFrameIdKey& key) const; |
| 138 |
| 139 CachedFrameIdPair LookupFrameIdOnUI(const RenderFrameIdKey& key); |
| 140 |
| 141 // Called as soon as the frame ID is found for the given |key|, and runs all |
| 142 // queued callbacks with |cached_frame_id_pair|. |
| 143 void ReceivedFrameIdOnIO(const RenderFrameIdKey& key, |
| 144 const CachedFrameIdPair& cached_frame_id_pair); |
| 145 |
| 146 // Implementation of CacheFrameId(RenderFrameHost), separated for testing. |
| 147 void CacheFrameId(const RenderFrameIdKey& key); |
| 148 |
| 149 // Implementation of RemoveFrameId(RenderFrameHost), separated for testing. |
| 150 void RemoveFrameId(const RenderFrameIdKey& key); |
| 151 |
| 152 // Queued callbacks for use on the IO thread. |
| 153 FrameIdCallbacksMap callbacks_map_; |
| 154 |
| 155 // This map is only modified on the UI thread and is used to minimize the |
| 156 // number of thread hops on the IO thread. |
| 157 FrameIdMap frame_id_map_; |
| 158 |
| 159 // This lock protects |frame_id_map_| from being concurrently written on the |
| 160 // UI thread and read on the IO thread. |
| 161 base::Lock frame_id_map_lock_; |
| 162 |
| 163 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMap); |
| 164 }; |
| 165 |
| 166 } // namespace extensions |
| 167 |
| 168 #endif // EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
OLD | NEW |