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

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: comments #47 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 <deque>
9 #include <list>
10 #include <map>
11
12 #include "base/callback.h"
13 #include "base/lazy_instance.h"
14 #include "base/synchronization/lock.h"
15
16 namespace base {
17 class TimeTicks;
18 } // namespace base
19
20 namespace content {
21 class RenderFrameHost;
22 class WebContents;
23 } // namespace content
24
25 namespace extensions {
26
27 // Extension frame IDs are exposed through the chrome.* APIs and have the
28 // following characteristics:
29 // - The top-level frame has ID 0.
30 // - Any child frame has a positive ID.
31 // - A non-existant frame has ID -1.
32 // - They are only guaranteed to be unique within a tab.
33 // - The ID does not change during the frame's lifetime and is not re-used after
34 // the frame is removed. The frame may change its current RenderFrameHost over
35 // time, so multiple RenderFrameHosts may map to the same extension frame ID.
36 struct ExtensionApiFrameId {
37 static const int kInvalidFrameId = -1;
38
39 ExtensionApiFrameId();
40 ExtensionApiFrameId(int frame_id, int parent_frame_id);
41
42 bool operator==(const ExtensionApiFrameId& other) const {
43 return frame_id == other.frame_id &&
44 parent_frame_id == other.parent_frame_id;
45 }
46
47 // The name "ExtensionApiFrameId" refers to |frame_id|.
48 int frame_id;
49
50 // The extension frame ID of the parent frame ID is also included because
51 // some extension APIs need both IDs.
52 int parent_frame_id;
53 };
54
55 // This class provides a mapping from a (render_process_id, frame_routing_id)
56 // pair that maps a RenderFrameHost to an extension frame ID.
57 // Unless stated otherwise, the methods can only be called on the UI thread.
58 class ExtensionApiFrameIdMap {
59 public:
60 using FrameIdCallback = base::Callback<void(const ExtensionApiFrameId)>;
61
62 static ExtensionApiFrameIdMap* Get();
63
64 // Runs |callback| with the result that is equivalent to calling GetFrameId()
65 // on the UI thread. Thread hopping is minimized if possible, and the
66 // callbacks are guaranteed to be run in order.
67 void GetFrameIdOnIO(int render_process_id,
68 int frame_routing_id,
69 const FrameIdCallback& callback);
70
71 // Get the frameId for a RenderFrameHost identified by |render_process_id| and
72 // |frame_routing_id|. These methods always returns the same value when called
battre 2015/12/23 14:02:12 nit: s/returns/return/;
robwu 2015/12/26 11:05:50 Done.
73 // with the same parameters.
74 const ExtensionApiFrameId GetFrameId(int render_process_id,
75 int frame_routing_id);
76 const ExtensionApiFrameId GetFrameId(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(int render_process_id, int frame_routing_id);
83 void RemoveFrameId(content::RenderFrameHost* rfh);
84
85 // Find the current RenderFrameHost for a given WebContents and extension
86 // frame ID.
87 // Returns nullptr if not found.
88 content::RenderFrameHost* GetRenderFrameHostById(
89 content::WebContents* web_contents,
90 int frame_id);
91
92 protected:
93 friend struct base::DefaultLazyInstanceTraits<ExtensionApiFrameIdMap>;
94
95 // A set of identifiers that uniquely identifies a RenderFrame.
96 struct RenderFrameIdKey {
97 RenderFrameIdKey();
98 RenderFrameIdKey(int render_process_id, int frame_routing_id);
99
100 // The process ID of the renderer that contains the RenderFrame.
101 int render_process_id;
102 // The routing ID of the RenderFrame.
103 int frame_routing_id;
104
105 bool operator<(const RenderFrameIdKey& other) const;
106 bool operator==(const RenderFrameIdKey& other) const;
107 };
108
109 // A queued callback. After mapping |key| to |value|, |callback| is run with
110 // |value|. The fields are only maintained when this struct is a part of the
111 // |callbacks_| list.
112 struct FrameIdCallbackInfo {
113 FrameIdCallbackInfo(const RenderFrameIdKey& key,
114 const FrameIdCallback& callback);
115 ~FrameIdCallbackInfo();
116
117 const RenderFrameIdKey key;
118 const FrameIdCallback callback;
119 ExtensionApiFrameId value;
120 bool has_value;
121 };
122
123 struct FrameIdRemovalTask {
124 FrameIdRemovalTask(const RenderFrameIdKey& key,
125 const base::TimeTicks& creation_time);
126
127 const RenderFrameIdKey key;
128 base::TimeTicks creation_time;
129 };
130
131 using FrameIdMap = std::map<RenderFrameIdKey, ExtensionApiFrameId>;
132
133 ExtensionApiFrameIdMap();
134 ~ExtensionApiFrameIdMap();
135
136 // Determines the value to be stored in |frame_id_map_| for a given key. This
137 // method is only called when |key| is not in |frame_id_map_|.
138 // virtual for testing.
139 virtual ExtensionApiFrameId KeyToValue(const RenderFrameIdKey& key) const;
140
141 ExtensionApiFrameId LookupFrameIdOnUI(const RenderFrameIdKey& key);
142 void GotFrameIdOnIO(const RenderFrameIdKey& key,
143 const ExtensionApiFrameId& extension_api_frame_id);
144
145 // Queued callbacks for use on the IO thread.
146 // This is a std::list so that iterators are not invalidated when the list is
147 // modified during an iteration.
148 std::list<FrameIdCallbackInfo> callbacks_;
149
150 // A list of frame IDs that should be deleted soon.
151 std::deque<FrameIdRemovalTask> pending_deletions_;
152
153 // This frameId map is only modified on the UI thread and used for 2 purposes:
154 // - On the IO thread, it avoids unnecessary thread hops.
155 // - On the UI thread and the IO thread, it ensures that the frameId remains
156 // constant, even after removing a frame.
157 FrameIdMap frame_id_map_;
158
159 // This lock protects |frame_id_map| from being concurrently written on the UI
160 // 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_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698