Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: extensions/browser/extension_api_frame_id_map.h

Issue 1413543005: Use FrameTreeNode ID as frameId in extension APIs (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add unit test for ExtensionApiFrameIdMap Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/gtest_prod_util.h"
13 #include "base/lazy_instance.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 struct ExtensionApiFrameId {
33 static const int kInvalidFrameId = -1;
34
35 ExtensionApiFrameId();
36 ExtensionApiFrameId(int frame_id, int parent_frame_id);
37
38 bool operator==(const ExtensionApiFrameId& other) const {
39 return frame_id == other.frame_id &&
40 parent_frame_id == other.parent_frame_id;
41 }
42
43 // The name "ExtensionApiFrameId" refers to |frame_id|.
44 int frame_id;
45
46 // The extension frame ID of the parent frame ID is also included because
47 // some extension APIs need both IDs.
48 int parent_frame_id;
49 };
50
51 // This class provides a mapping from a (render_process_id, frame_routing_id)
52 // pair that maps a RenderFrameHost to an extension frame ID.
53 // Unless stated otherwise, the methods can only be called on the UI thread.
54 class ExtensionApiFrameIdMap {
55 public:
56 using FrameIdCallback = base::Callback<void(const ExtensionApiFrameId&)>;
57
58 static ExtensionApiFrameIdMap* Get();
59
60 // Runs |callback| with the result that is equivalent to calling GetFrameId()
61 // on the UI thread. If the result was cached the callback is immediately run.
62 // Otherwise the callback is queued and run in order when the result becomes
63 // available. Queued callbacks are not called when the class is destroyed.
64 void GetFrameIdOnIO(int render_process_id,
65 int frame_routing_id,
66 const FrameIdCallback& callback);
67
68 // Get the frameId for a RenderFrameHost identified by |render_process_id| and
69 // |frame_routing_id|. These methods always returns the same value when called
70 // with the same parameters.
71 const ExtensionApiFrameId& GetFrameId(int render_process_id,
72 int frame_routing_id);
73 const ExtensionApiFrameId& GetFrameId(content::RenderFrameHost* rfh);
74
75 // Find the current RenderFrameHost for a given WebContents and extension
76 // frame ID.
77 // Returns nullptr if not found.
78 content::RenderFrameHost* GetRenderFrameHostById(
79 content::WebContents* web_contents,
80 int frame_id);
81
82 protected:
83 friend struct base::DefaultLazyInstanceTraits<ExtensionApiFrameIdMap>;
84 FRIEND_TEST_ALL_PREFIXES(ExtensionApiFrameIdMapTest, Basic);
85 FRIEND_TEST_ALL_PREFIXES(ExtensionApiFrameIdMapTest, GetFrameIdOnIO);
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 };
99
100 // This is a std::list so that iterators are not invalidated when the list is
101 // modified during an iteration.
102 using FrameIdCallbackList = std::list<FrameIdCallback>;
103 using FrameIdMap = std::map<RenderFrameIdKey, ExtensionApiFrameId>;
104 using CallbackMap = std::map<RenderFrameIdKey, FrameIdCallbackList>;
105
106 ExtensionApiFrameIdMap();
107 ~ExtensionApiFrameIdMap();
108
109 // Determines the value to be stored in |frame_id_map_| for a given key.
110 // virtual for testing.
111 virtual ExtensionApiFrameId KeyToValue(const RenderFrameIdKey& key) const;
112
113 const ExtensionApiFrameId& LookupFrameIdOnUI(const RenderFrameIdKey& key);
114 void GotFrameIdOnIO(const RenderFrameIdKey& key);
115
116 // Queued callbacks for use on the IO thread. This queue is only used when the
117 // frameId needs to be fetched from the UI thread.
118 CallbackMap callbacks_map_;
119
120 // This frameId map is only modified on the UI thread and used for 2 purposes:
121 // - On the IO thread, it avoids unnecessary thread hops.
122 // - On the UI thread and the IO thread, it ensures that the frameId remains
123 // constant, even after removing a frame.
124 // Items are never removed from this map.
125 FrameIdMap frame_id_map_;
126
127 // This lock protects |frame_id_map| from being concurrently written on the UI
128 // thread and read on the IO thread.
129 base::Lock frame_id_map_lock_;
130 };
131
132 } // namespace extensions
133
134 #endif // EXTENSIONS_BROWSER_EXTENSION_API_FRAME_ID_MAP_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698