| 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 // Get the extension API frame ID for the parent of |rfh|. |
| 56 static int GetParentFrameId(content::RenderFrameHost* rfh); |
| 57 |
| 58 // Find the current RenderFrameHost for a given WebContents and extension |
| 59 // frame ID. |
| 60 // Returns nullptr if not found. |
| 61 static content::RenderFrameHost* GetRenderFrameHostById( |
| 62 content::WebContents* web_contents, |
| 63 int frame_id); |
| 64 |
| 65 // Runs |callback| with the result that is equivalent to calling GetFrameId() |
| 66 // on the UI thread. Thread hopping is minimized if possible. Callbacks for |
| 67 // the same |render_process_id| and |frame_routing_id| are guaranteed to be |
| 68 // run in order. The order of other callbacks is undefined. |
| 69 void GetFrameIdOnIO(int render_process_id, |
| 70 int frame_routing_id, |
| 71 const FrameIdCallback& callback); |
| 72 |
| 73 // Look up the frame ID and store it in the map. This method should be called |
| 74 // as early as possible, e.g. in a WebContentsObserver::RenderFrameCreated |
| 75 // notification. |
| 76 void CacheFrameId(content::RenderFrameHost* rfh); |
| 77 |
| 78 // Remove the frame ID mapping for a given frame. This method can be called at |
| 79 // any time, but it is typical to call this method when a frame is destroyed. |
| 80 // If this method is not called, the cached mapping for the frame is retained |
| 81 // forever. |
| 82 void RemoveFrameId(content::RenderFrameHost* rfh); |
| 83 |
| 84 protected: |
| 85 friend struct base::DefaultLazyInstanceTraits<ExtensionApiFrameIdMap>; |
| 86 |
| 87 // A set of identifiers that uniquely identifies a RenderFrame. |
| 88 struct RenderFrameIdKey { |
| 89 RenderFrameIdKey(); |
| 90 RenderFrameIdKey(int render_process_id, int frame_routing_id); |
| 91 |
| 92 // The process ID of the renderer that contains the RenderFrame. |
| 93 int render_process_id; |
| 94 // The routing ID of the RenderFrame. |
| 95 int frame_routing_id; |
| 96 |
| 97 bool operator<(const RenderFrameIdKey& other) const; |
| 98 bool operator==(const RenderFrameIdKey& other) const; |
| 99 }; |
| 100 |
| 101 // The cached pair of frame IDs of the frame. Every RenderFrameIdKey |
| 102 // maps to a CachedFrameIdPair. |
| 103 struct CachedFrameIdPair { |
| 104 CachedFrameIdPair(); |
| 105 CachedFrameIdPair(int frame_id, int parent_frame_id); |
| 106 |
| 107 // The extension API frame ID of the frame. |
| 108 int frame_id; |
| 109 // The extension API frame ID of the parent of the frame. |
| 110 int parent_frame_id; |
| 111 }; |
| 112 |
| 113 struct FrameIdCallbacks { |
| 114 FrameIdCallbacks(); |
| 115 ~FrameIdCallbacks(); |
| 116 |
| 117 // This is a std::list so that iterators are not invalidated when the list |
| 118 // is modified during an iteration. |
| 119 std::list<FrameIdCallback> callbacks; |
| 120 |
| 121 // To avoid re-entrant processing of callbacks. |
| 122 bool is_iterating; |
| 123 |
| 124 private: |
| 125 DISALLOW_COPY_AND_ASSIGN(FrameIdCallbacks); |
| 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 GotFrameIdOnIO(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 // Implementation of RemoveFrameId(RenderFrameHost), separated for testing. |
| 149 void RemoveFrameId(const RenderFrameIdKey& key); |
| 150 |
| 151 // Queued callbacks for use on the IO thread. |
| 152 FrameIdCallbacksMap callbacks_map_; |
| 153 |
| 154 // This frameId map is only modified on the UI thread and used to minimize the |
| 155 // number of thread hops on the IO thread. |
| 156 FrameIdMap frame_id_map_; |
| 157 |
| 158 // This lock protects |frame_id_map| from being concurrently written on the UI |
| 159 // thread and read on the IO thread. |
| 160 base::Lock frame_id_map_lock_; |
| 161 |
| 162 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMap); |
| 163 }; |
| 164 |
| 165 } // namespace extensions |
| 166 |
| 167 #endif // EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
| OLD | NEW |