| 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 #ifndef EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ | 5 #ifndef EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
| 6 #define EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ | 6 #define EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
| 7 | 7 |
| 8 #include <list> | 8 #include <list> |
| 9 #include <map> | 9 #include <map> |
| 10 #include <memory> | 10 #include <memory> |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 int frame_routing_id, | 112 int frame_routing_id, |
| 113 const FrameDataCallback& callback); | 113 const FrameDataCallback& callback); |
| 114 | 114 |
| 115 // Attempts to populate |frame_data_out| with the FrameData for the specified | 115 // Attempts to populate |frame_data_out| with the FrameData for the specified |
| 116 // frame, but only does so if the data is already cached. Returns true if | 116 // frame, but only does so if the data is already cached. Returns true if |
| 117 // cached frame data was found. | 117 // cached frame data was found. |
| 118 bool GetCachedFrameDataOnIO(int render_process_id, | 118 bool GetCachedFrameDataOnIO(int render_process_id, |
| 119 int frame_routing_id, | 119 int frame_routing_id, |
| 120 FrameData* frame_data_out); | 120 FrameData* frame_data_out); |
| 121 | 121 |
| 122 // Looks up the frame ID and stores it in the map. This method should be | 122 // Retrieves the FrameData for a given |rfh|. The map may be updated with the |
| 123 // result if the map did not contain the FrameData before the lookup. |
| 124 FrameData GetFrameData(content::RenderFrameHost* rfh) WARN_UNUSED_RESULT; |
| 125 |
| 126 // Looks up the FrameData and stores it in the map. This method should be |
| 123 // called as early as possible, e.g. in a | 127 // called as early as possible, e.g. in a |
| 124 // WebContentsObserver::RenderFrameCreated notification. | 128 // WebContentsObserver::RenderFrameCreated notification. |
| 125 void CacheFrameData(content::RenderFrameHost* rfh); | 129 void CacheFrameData(content::RenderFrameHost* rfh); |
| 126 | 130 |
| 127 // Removes the frame ID mapping for a given frame. This method can be called | 131 // Removes the FrameData mapping for a given frame. This method can be called |
| 128 // at any time, but it is typically called when a frame is destroyed. | 132 // at any time, but it is typically called when a frame is destroyed. |
| 129 // If this method is not called, the cached mapping for the frame is retained | 133 // If this method is not called, the cached mapping for the frame is retained |
| 130 // forever. | 134 // forever. |
| 131 void RemoveFrameData(content::RenderFrameHost* rfh); | 135 void RemoveFrameData(content::RenderFrameHost* rfh); |
| 132 | 136 |
| 133 // Updates the tab and window id for the given RenderFrameHost, if any exists. | 137 // Updates the tab and window id for the given RenderFrameHost, if any exists. |
| 134 void UpdateTabAndWindowId(int tab_id, | 138 void UpdateTabAndWindowId(int tab_id, |
| 135 int window_id, | 139 int window_id, |
| 136 content::RenderFrameHost* rfh); | 140 content::RenderFrameHost* rfh); |
| 137 | 141 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 165 // To avoid re-entrant processing of callbacks. | 169 // To avoid re-entrant processing of callbacks. |
| 166 bool is_iterating; | 170 bool is_iterating; |
| 167 }; | 171 }; |
| 168 | 172 |
| 169 using FrameDataMap = std::map<RenderFrameIdKey, FrameData>; | 173 using FrameDataMap = std::map<RenderFrameIdKey, FrameData>; |
| 170 using FrameDataCallbacksMap = std::map<RenderFrameIdKey, FrameDataCallbacks>; | 174 using FrameDataCallbacksMap = std::map<RenderFrameIdKey, FrameDataCallbacks>; |
| 171 | 175 |
| 172 ExtensionApiFrameIdMap(); | 176 ExtensionApiFrameIdMap(); |
| 173 virtual ~ExtensionApiFrameIdMap(); | 177 virtual ~ExtensionApiFrameIdMap(); |
| 174 | 178 |
| 175 // Determines the value to be stored in |frame_id_map_| for a given key. This | 179 // Determines the value to be stored in |frame_data_map_| for a given key. |
| 176 // method is only called when |key| is not in |frame_id_map_|. | 180 // This method is only called when |key| is not in |frame_data_map_|. |
| 177 // virtual for testing. | 181 // virtual for testing. |
| 178 virtual FrameData KeyToValue(const RenderFrameIdKey& key) const; | 182 virtual FrameData KeyToValue(const RenderFrameIdKey& key) const; |
| 179 | 183 |
| 180 // Looks up the data for the given |key| and adds it to the |frame_data_map_|. | 184 // Looks up the data for the given |key| and adds it to the |frame_data_map_|. |
| 181 // |for_lookup| indicates whether this is for a pending lookup (as opposed to | 185 // |for_lookup| indicates whether this is for a pending lookup (as opposed to |
| 182 // preemptively caching the frame data). | 186 // preemptively caching the frame data). |
| 183 FrameData LookupFrameDataOnUI(const RenderFrameIdKey& key, bool for_lookup); | 187 FrameData LookupFrameDataOnUI(const RenderFrameIdKey& key, bool for_lookup); |
| 184 | 188 |
| 185 // Called as soon as the frame ID is found for the given |key|, and runs all | 189 // Called as soon as the frame data is found for the given |key|, and runs all |
| 186 // queued callbacks with |cached_frame_id_pair|. | 190 // queued callbacks with |cached_frame_data|. |
| 187 void ReceivedFrameDataOnIO(const RenderFrameIdKey& key, | 191 void ReceivedFrameDataOnIO(const RenderFrameIdKey& key, |
| 188 const FrameData& cached_frame_id_pair); | 192 const FrameData& cached_frame_data); |
| 189 | 193 |
| 190 // Implementation of CacheFrameId(RenderFrameHost), separated for testing. | 194 // Implementation of CacheFrameId(RenderFrameHost), separated for testing. |
| 191 void CacheFrameData(const RenderFrameIdKey& key); | 195 void CacheFrameData(const RenderFrameIdKey& key); |
| 192 | 196 |
| 193 // Implementation of RemoveFrameId(RenderFrameHost), separated for testing. | 197 // Implementation of RemoveFrameId(RenderFrameHost), separated for testing. |
| 194 void RemoveFrameData(const RenderFrameIdKey& key); | 198 void RemoveFrameData(const RenderFrameIdKey& key); |
| 195 | 199 |
| 196 std::unique_ptr<ExtensionApiFrameIdMapHelper> helper_; | 200 std::unique_ptr<ExtensionApiFrameIdMapHelper> helper_; |
| 197 | 201 |
| 198 // Queued callbacks for use on the IO thread. | 202 // Queued callbacks for use on the IO thread. |
| 199 FrameDataCallbacksMap callbacks_map_; | 203 FrameDataCallbacksMap callbacks_map_; |
| 200 | 204 |
| 201 // This map is only modified on the UI thread and is used to minimize the | 205 // This map is only modified on the UI thread and is used to minimize the |
| 202 // number of thread hops on the IO thread. | 206 // number of thread hops on the IO thread. |
| 203 FrameDataMap frame_data_map_; | 207 FrameDataMap frame_data_map_; |
| 204 | 208 |
| 205 // This lock protects |frame_id_map_| from being concurrently written on the | 209 // This lock protects |frame_data_map_| from being concurrently written on the |
| 206 // UI thread and read on the IO thread. | 210 // UI thread and read on the IO thread. |
| 207 base::Lock frame_data_map_lock_; | 211 base::Lock frame_data_map_lock_; |
| 208 | 212 |
| 209 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMap); | 213 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMap); |
| 210 }; | 214 }; |
| 211 | 215 |
| 212 } // namespace extensions | 216 } // namespace extensions |
| 213 | 217 |
| 214 #endif // EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ | 218 #endif // EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ |
| OLD | NEW |