OLD | NEW |
| (Empty) |
1 // Copyright 2014 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_GUEST_VIEW_GUEST_VIEW_MANAGER_H_ | |
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/bind.h" | |
11 #include "base/gtest_prod_util.h" | |
12 #include "base/lazy_instance.h" | |
13 #include "base/macros.h" | |
14 #include "content/public/browser/browser_plugin_guest_manager.h" | |
15 #include "content/public/browser/site_instance.h" | |
16 #include "content/public/browser/web_contents.h" | |
17 | |
18 class GURL; | |
19 | |
20 namespace content { | |
21 class BrowserContext; | |
22 class WebContents; | |
23 } // namespace content | |
24 | |
25 namespace guestview { | |
26 class GuestViewManagerDelegate; | |
27 } // namespace guestview | |
28 | |
29 namespace extensions{ | |
30 class GuestViewBase; | |
31 class GuestViewManagerFactory; | |
32 | |
33 class GuestViewManager : public content::BrowserPluginGuestManager, | |
34 public base::SupportsUserData::Data { | |
35 public: | |
36 GuestViewManager(content::BrowserContext* context, | |
37 scoped_ptr<guestview::GuestViewManagerDelegate> delegate); | |
38 ~GuestViewManager() override; | |
39 | |
40 // Returns the GuestViewManager associated with |context|. If one isn't | |
41 // available, then it is created and returned. | |
42 static GuestViewManager* CreateWithDelegate( | |
43 content::BrowserContext* context, | |
44 scoped_ptr<guestview::GuestViewManagerDelegate> delegate); | |
45 | |
46 // Returns the GuestViewManager associated with |context|. If one isn't | |
47 // available, then nullptr is returned. | |
48 static GuestViewManager* FromBrowserContext(content::BrowserContext* context); | |
49 | |
50 // Overrides factory for testing. Default (NULL) value indicates regular | |
51 // (non-test) environment. | |
52 static void set_factory_for_testing(GuestViewManagerFactory* factory) { | |
53 GuestViewManager::factory_ = factory; | |
54 } | |
55 // Returns the guest WebContents associated with the given |guest_instance_id| | |
56 // if the provided |embedder_render_process_id| is allowed to access it. | |
57 // If the embedder is not allowed access, the embedder will be killed, and | |
58 // this method will return NULL. If no WebContents exists with the given | |
59 // instance ID, then NULL will also be returned. | |
60 content::WebContents* GetGuestByInstanceIDSafely( | |
61 int guest_instance_id, | |
62 int embedder_render_process_id); | |
63 | |
64 // Associates the Browser Plugin with |element_instance_id| to a | |
65 // guest that has ID of |guest_instance_id| and sets initialization | |
66 // parameters, |params| for it. | |
67 void AttachGuest(int embedder_process_id, | |
68 int element_instance_id, | |
69 int guest_instance_id, | |
70 const base::DictionaryValue& attach_params); | |
71 | |
72 // Removes the association between |element_instance_id| and a guest instance | |
73 // ID if one exists. | |
74 void DetachGuest(GuestViewBase* guest); | |
75 | |
76 // Indicates whether the |guest| is owned by an extension or Chrome App. | |
77 bool IsOwnedByExtension(GuestViewBase* guest); | |
78 | |
79 int GetNextInstanceID(); | |
80 int GetGuestInstanceIDForElementID( | |
81 int owner_process_id, | |
82 int element_instance_id); | |
83 | |
84 template <typename T> | |
85 void RegisterGuestViewType() { | |
86 auto it = guest_view_registry_.find(T::Type); | |
87 DCHECK(it == guest_view_registry_.end()); | |
88 guest_view_registry_[T::Type] = base::Bind(&T::Create); | |
89 } | |
90 | |
91 using WebContentsCreatedCallback = | |
92 base::Callback<void(content::WebContents*)>; | |
93 void CreateGuest(const std::string& view_type, | |
94 content::WebContents* owner_web_contents, | |
95 const base::DictionaryValue& create_params, | |
96 const WebContentsCreatedCallback& callback); | |
97 | |
98 content::WebContents* CreateGuestWithWebContentsParams( | |
99 const std::string& view_type, | |
100 content::WebContents* owner_web_contents, | |
101 const content::WebContents::CreateParams& create_params); | |
102 | |
103 content::SiteInstance* GetGuestSiteInstance( | |
104 const GURL& guest_site); | |
105 | |
106 // BrowserPluginGuestManager implementation. | |
107 content::WebContents* GetGuestByInstanceID( | |
108 int owner_process_id, | |
109 int element_instance_id) override; | |
110 bool ForEachGuest(content::WebContents* owner_web_contents, | |
111 const GuestCallback& callback) override; | |
112 content::WebContents* GetFullPageGuest( | |
113 content::WebContents* embedder_web_contents) override; | |
114 | |
115 protected: | |
116 friend class GuestViewBase; | |
117 friend class GuestViewEvent; | |
118 | |
119 // Can be overriden in tests. | |
120 virtual void AddGuest(int guest_instance_id, | |
121 content::WebContents* guest_web_contents); | |
122 | |
123 // Can be overriden in tests. | |
124 virtual void RemoveGuest(int guest_instance_id); | |
125 | |
126 // Creates a guest of the provided |view_type|. | |
127 GuestViewBase* CreateGuestInternal(content::WebContents* owner_web_contents, | |
128 const std::string& view_type); | |
129 | |
130 // Adds GuestView types to the GuestView registry. | |
131 void RegisterGuestViewTypes(); | |
132 | |
133 // Indicates whether the provided |guest| can be used in the context it has | |
134 // been created. | |
135 bool IsGuestAvailableToContext(GuestViewBase* guest); | |
136 | |
137 // Dispatches the event with |name| with the provided |args| to the embedder | |
138 // of the given |guest| with |instance_id| for routing. | |
139 void DispatchEvent(const std::string& event_name, | |
140 scoped_ptr<base::DictionaryValue> args, | |
141 GuestViewBase* guest, | |
142 int instance_id); | |
143 | |
144 content::WebContents* GetGuestByInstanceID(int guest_instance_id); | |
145 | |
146 bool CanEmbedderAccessInstanceIDMaybeKill( | |
147 int embedder_render_process_id, | |
148 int guest_instance_id); | |
149 | |
150 bool CanEmbedderAccessInstanceID(int embedder_render_process_id, | |
151 int guest_instance_id); | |
152 | |
153 // Returns true if |guest_instance_id| can be used to add a new guest to this | |
154 // manager. | |
155 // We disallow adding new guest with instance IDs that were previously removed | |
156 // from this manager using RemoveGuest. | |
157 bool CanUseGuestInstanceID(int guest_instance_id); | |
158 | |
159 static bool GetFullPageGuestHelper(content::WebContents** result, | |
160 content::WebContents* guest_web_contents); | |
161 | |
162 // Static factory instance (always NULL for non-test). | |
163 static GuestViewManagerFactory* factory_; | |
164 | |
165 // Contains guests' WebContents, mapping from their instance ids. | |
166 using GuestInstanceMap = std::map<int, content::WebContents*>; | |
167 GuestInstanceMap guest_web_contents_by_instance_id_; | |
168 | |
169 struct ElementInstanceKey { | |
170 int embedder_process_id; | |
171 int element_instance_id; | |
172 | |
173 ElementInstanceKey(); | |
174 ElementInstanceKey(int embedder_process_id, | |
175 int element_instance_id); | |
176 | |
177 bool operator<(const ElementInstanceKey& other) const; | |
178 bool operator==(const ElementInstanceKey& other) const; | |
179 }; | |
180 | |
181 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>; | |
182 GuestInstanceIDMap instance_id_map_; | |
183 | |
184 // The reverse map of GuestInstanceIDMap. | |
185 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>; | |
186 GuestInstanceIDReverseMap reverse_instance_id_map_; | |
187 | |
188 using GuestCreationCallback = | |
189 base::Callback<GuestViewBase*(content::WebContents*)>; | |
190 using GuestViewCreationMap = | |
191 std::map<std::string, GuestViewManager::GuestCreationCallback>; | |
192 GuestViewCreationMap guest_view_registry_; | |
193 | |
194 int current_instance_id_; | |
195 | |
196 // Any instance ID whose number not greater than this was removed via | |
197 // RemoveGuest. | |
198 // This is used so that we don't have store all removed instance IDs in | |
199 // |removed_instance_ids_|. | |
200 int last_instance_id_removed_; | |
201 // The remaining instance IDs that are greater than | |
202 // |last_instance_id_removed_| are kept here. | |
203 std::set<int> removed_instance_ids_; | |
204 | |
205 content::BrowserContext* context_; | |
206 | |
207 scoped_ptr<guestview::GuestViewManagerDelegate> delegate_; | |
208 | |
209 DISALLOW_COPY_AND_ASSIGN(GuestViewManager); | |
210 }; | |
211 | |
212 } // namespace extensions | |
213 | |
214 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_ | |
OLD | NEW |