Chromium Code Reviews| 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|. | |
|
nasko
2016/01/05 00:39:58
nit: Empty line above the comment.
robwu
2016/01/05 10:59:03
Done.
| |
| 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 | |
|
nasko
2016/01/05 00:39:58
nit: "Looks up", "stores it".
robwu
2016/01/05 10:59:03
Done.
| |
| 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 | |
|
nasko
2016/01/05 00:39:58
nit: "Removes the"
robwu
2016/01/05 10:59:03
Done.
| |
| 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: | |
|
nasko
2016/01/05 00:39:58
Why are all the members protected instead of priva
robwu
2016/01/05 10:59:03
For unit tests, so that the internal state of the
| |
| 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. | |
|
nasko
2016/01/05 00:39:58
nit: Empty line before comment.
robwu
2016/01/05 10:59:03
Done.
| |
| 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. | |
|
nasko
2016/01/05 00:39:58
nit: Empty line before comment.
robwu
2016/01/05 10:59:03
Done.
| |
| 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 | |
| 125 using FrameIdMap = std::map<RenderFrameIdKey, CachedFrameIdPair>; | |
| 126 using FrameIdCallbacksMap = std::map<RenderFrameIdKey, FrameIdCallbacks>; | |
| 127 | |
| 128 ExtensionApiFrameIdMap(); | |
| 129 ~ExtensionApiFrameIdMap(); | |
| 130 | |
| 131 // Determines the value to be stored in |frame_id_map_| for a given key. This | |
| 132 // method is only called when |key| is not in |frame_id_map_|. | |
| 133 // virtual for testing. | |
| 134 virtual CachedFrameIdPair KeyToValue(const RenderFrameIdKey& key) const; | |
| 135 | |
| 136 CachedFrameIdPair LookupFrameIdOnUI(const RenderFrameIdKey& key); | |
| 137 | |
| 138 // Called as soon as the frame ID is found for the given |key|, and runs all | |
| 139 // queued callbacks with |cached_frame_id_pair|. | |
| 140 void GotFrameIdOnIO(const RenderFrameIdKey& key, | |
| 141 const CachedFrameIdPair& cached_frame_id_pair); | |
| 142 | |
| 143 // Implementation of CacheFrameId(RenderFrameHost), separated for testing. | |
| 144 void CacheFrameId(const RenderFrameIdKey& key); | |
| 145 // Implementation of RemoveFrameId(RenderFrameHost), separated for testing. | |
|
nasko
2016/01/05 00:39:58
nit: Empty line before comment.
robwu
2016/01/05 10:59:03
Done.
| |
| 146 void RemoveFrameId(const RenderFrameIdKey& key); | |
| 147 | |
| 148 // Queued callbacks for use on the IO thread. | |
| 149 FrameIdCallbacksMap callbacks_map_; | |
| 150 | |
| 151 // This frameId map is only modified on the UI thread and used to minimize the | |
|
nasko
2016/01/05 00:39:58
"frameId" is not used before and it doesn't match
robwu
2016/01/05 10:59:03
Done.
| |
| 152 // number of thread hops on the IO thread. | |
| 153 FrameIdMap frame_id_map_; | |
| 154 | |
| 155 // This lock protects |frame_id_map| from being concurrently written on the UI | |
|
nasko
2016/01/05 00:39:58
frame_id_map_
robwu
2016/01/05 10:59:03
Done.
| |
| 156 // thread and read on the IO thread. | |
| 157 base::Lock frame_id_map_lock_; | |
| 158 | |
| 159 DISALLOW_COPY_AND_ASSIGN(ExtensionApiFrameIdMap); | |
| 160 }; | |
| 161 | |
| 162 } // namespace extensions | |
| 163 | |
| 164 #endif // EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_ | |
| OLD | NEW |