Roll ANGLE cc54ab3..c5b2ba5
[chromium-blink-merge.git] / components / guest_view / browser / guest_view_manager.h
blob87b1831481bcc4ef3c19803355605ab8bf168d35
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.
5 #ifndef COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_
6 #define COMPONENTS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "base/bind.h"
13 #include "base/gtest_prod_util.h"
14 #include "base/lazy_instance.h"
15 #include "base/macros.h"
16 #include "base/memory/weak_ptr.h"
17 #include "content/public/browser/browser_plugin_guest_manager.h"
18 #include "content/public/browser/site_instance.h"
19 #include "content/public/browser/web_contents.h"
21 class GURL;
23 namespace content {
24 class BrowserContext;
25 class WebContents;
26 } // namespace content
28 namespace guest_view {
30 class GuestViewBase;
31 class GuestViewManagerDelegate;
32 class GuestViewManagerFactory;
34 class GuestViewManager : public content::BrowserPluginGuestManager,
35 public base::SupportsUserData::Data {
36 public:
37 GuestViewManager(content::BrowserContext* context,
38 scoped_ptr<GuestViewManagerDelegate> delegate);
39 ~GuestViewManager() override;
41 // Returns the GuestViewManager associated with |context|. If one isn't
42 // available, then it is created and returned.
43 static GuestViewManager* CreateWithDelegate(
44 content::BrowserContext* context,
45 scoped_ptr<GuestViewManagerDelegate> delegate);
47 // Returns the GuestViewManager associated with |context|. If one isn't
48 // available, then nullptr is returned.
49 static GuestViewManager* FromBrowserContext(content::BrowserContext* context);
51 // Overrides factory for testing. Default (NULL) value indicates regular
52 // (non-test) environment.
53 static void set_factory_for_testing(GuestViewManagerFactory* factory) {
54 GuestViewManager::factory_ = factory;
56 // Returns the guest WebContents associated with the given |guest_instance_id|
57 // if the provided |embedder_render_process_id| is allowed to access it.
58 // If the embedder is not allowed access, the embedder will be killed, and
59 // this method will return NULL. If no WebContents exists with the given
60 // instance ID, then NULL will also be returned.
61 content::WebContents* GetGuestByInstanceIDSafely(
62 int guest_instance_id,
63 int embedder_render_process_id);
65 // Associates the Browser Plugin with |element_instance_id| to a
66 // guest that has ID of |guest_instance_id| and sets initialization
67 // parameters, |params| for it.
68 void AttachGuest(int embedder_process_id,
69 int element_instance_id,
70 int guest_instance_id,
71 const base::DictionaryValue& attach_params);
73 // Removes the association between |element_instance_id| and a guest instance
74 // ID if one exists.
75 void DetachGuest(GuestViewBase* guest);
77 // Indicates whether the |guest| is owned by an extension or Chrome App.
78 bool IsOwnedByExtension(GuestViewBase* guest);
80 int GetNextInstanceID();
81 int GetGuestInstanceIDForElementID(
82 int owner_process_id,
83 int element_instance_id);
85 template <typename T>
86 void RegisterGuestViewType() {
87 // If the GuestView type |T| is already registered, then there is nothing
88 // more to do. If an existing entry in the registry was created by this
89 // function for type |T|, then registering again would have no effect, and
90 // if it was registered elsewhere, then we do not want to overwrite it. Note
91 // that it is possible for tests to have special test factory methods
92 // registered here.
93 if (guest_view_registry_.count(T::Type))
94 return;
95 auto registry_entry = std::make_pair(
96 T::Type,
97 GuestViewData(base::Bind(&T::Create), base::Bind(&T::CleanUp)));
98 guest_view_registry_.insert(registry_entry);
101 // Registers a callback to be called when the view identified by
102 // |embedder_process_id| and |view_instance_id| is destroyed.
103 // Note that multiple callbacks can be registered for one view.
104 void RegisterViewDestructionCallback(int embedder_process_id,
105 int view_instance_id,
106 const base::Closure& callback);
108 using WebContentsCreatedCallback =
109 base::Callback<void(content::WebContents*)>;
110 void CreateGuest(const std::string& view_type,
111 content::WebContents* owner_web_contents,
112 const base::DictionaryValue& create_params,
113 const WebContentsCreatedCallback& callback);
115 content::WebContents* CreateGuestWithWebContentsParams(
116 const std::string& view_type,
117 content::WebContents* owner_web_contents,
118 const content::WebContents::CreateParams& create_params);
120 content::SiteInstance* GetGuestSiteInstance(
121 const GURL& guest_site);
123 // BrowserPluginGuestManager implementation.
124 content::WebContents* GetGuestByInstanceID(
125 int owner_process_id,
126 int element_instance_id) override;
127 bool ForEachGuest(content::WebContents* owner_web_contents,
128 const GuestCallback& callback) override;
129 content::WebContents* GetFullPageGuest(
130 content::WebContents* embedder_web_contents) override;
132 protected:
133 friend class GuestViewBase;
134 friend class GuestViewEvent;
135 friend class GuestViewMessageFilter;
137 class EmbedderRenderProcessHostObserver;
139 // These methods are virtual so that they can be overriden in tests.
141 virtual void AddGuest(int guest_instance_id,
142 content::WebContents* guest_web_contents);
143 virtual void RemoveGuest(int guest_instance_id);
145 // This method is called when the embedder process with ID
146 // |embedder_process_id| has been destroyed.
147 virtual void EmbedderProcessDestroyed(int embedder_process_id);
149 // Called when a GuestView has been created in JavaScript.
150 virtual void ViewCreated(int embedder_process_id,
151 int view_instance_id,
152 const std::string& view_type);
154 // Called when a GuestView has been garbage collected in JavaScript.
155 virtual void ViewGarbageCollected(int embedder_process_id,
156 int view_instance_id);
158 // Calls all destruction callbacks registered for the GuestView identified by
159 // |embedder_process_id| and |view_instance_id|.
160 void CallViewDestructionCallbacks(int embedder_process_id,
161 int view_instance_id);
163 // Calls all destruction callbacks registered for GuestViews in the embedder
164 // with ID |embedder_process_id|.
165 void CallViewDestructionCallbacks(int embedder_process_id);
167 // Creates a guest of the provided |view_type|.
168 GuestViewBase* CreateGuestInternal(content::WebContents* owner_web_contents,
169 const std::string& view_type);
171 // Adds GuestView types to the GuestView registry.
172 void RegisterGuestViewTypes();
174 // Indicates whether the provided |guest| can be used in the context it has
175 // been created.
176 bool IsGuestAvailableToContext(GuestViewBase* guest);
178 // Dispatches the event with |name| with the provided |args| to the embedder
179 // of the given |guest| with |instance_id| for routing.
180 void DispatchEvent(const std::string& event_name,
181 scoped_ptr<base::DictionaryValue> args,
182 GuestViewBase* guest,
183 int instance_id);
185 content::WebContents* GetGuestByInstanceID(int guest_instance_id);
187 bool CanEmbedderAccessInstanceIDMaybeKill(
188 int embedder_render_process_id,
189 int guest_instance_id);
191 bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
192 int guest_instance_id);
194 // Returns true if |guest_instance_id| can be used to add a new guest to this
195 // manager.
196 // We disallow adding new guest with instance IDs that were previously removed
197 // from this manager using RemoveGuest.
198 bool CanUseGuestInstanceID(int guest_instance_id);
200 static bool GetFullPageGuestHelper(content::WebContents** result,
201 content::WebContents* guest_web_contents);
203 // Static factory instance (always NULL for non-test).
204 static GuestViewManagerFactory* factory_;
206 // Contains guests' WebContents, mapping from their instance ids.
207 using GuestInstanceMap = std::map<int, content::WebContents*>;
208 GuestInstanceMap guest_web_contents_by_instance_id_;
210 struct ElementInstanceKey {
211 int embedder_process_id;
212 int element_instance_id;
214 ElementInstanceKey();
215 ElementInstanceKey(int embedder_process_id,
216 int element_instance_id);
218 bool operator<(const ElementInstanceKey& other) const;
219 bool operator==(const ElementInstanceKey& other) const;
222 using GuestInstanceIDMap = std::map<ElementInstanceKey, int>;
223 GuestInstanceIDMap instance_id_map_;
225 // The reverse map of GuestInstanceIDMap.
226 using GuestInstanceIDReverseMap = std::map<int, ElementInstanceKey>;
227 GuestInstanceIDReverseMap reverse_instance_id_map_;
229 using GuestViewCreateFunction =
230 base::Callback<GuestViewBase*(content::WebContents*)>;
231 using GuestViewCleanUpFunction =
232 base::Callback<void(content::BrowserContext*, int, int)>;
233 struct GuestViewData {
234 GuestViewData(const GuestViewCreateFunction& create_function,
235 const GuestViewCleanUpFunction& cleanup_function);
236 ~GuestViewData();
237 const GuestViewCreateFunction create_function;
238 const GuestViewCleanUpFunction cleanup_function;
240 using GuestViewMethodMap = std::map<std::string, GuestViewData>;
241 GuestViewMethodMap guest_view_registry_;
243 int current_instance_id_;
245 // Any instance ID whose number not greater than this was removed via
246 // RemoveGuest.
247 // This is used so that we don't have store all removed instance IDs in
248 // |removed_instance_ids_|.
249 int last_instance_id_removed_;
250 // The remaining instance IDs that are greater than
251 // |last_instance_id_removed_| are kept here.
252 std::set<int> removed_instance_ids_;
254 content::BrowserContext* context_;
256 scoped_ptr<GuestViewManagerDelegate> delegate_;
258 // This tracks which GuestView embedders are currently being observed.
259 std::set<int> embedders_observed_;
261 // |view_destruction_callback_map_| maps from embedder process ID to view ID
262 // to a vector of callback functions to be called when that view is destroyed.
263 using Callbacks = std::vector<base::Closure> ;
264 using CallbacksForEachViewID = std::map<int, Callbacks> ;
265 using CallbacksForEachEmbedderID = std::map<int, CallbacksForEachViewID> ;
266 CallbacksForEachEmbedderID view_destruction_callback_map_;
268 // This is used to ensure that an EmbedderRenderProcessHostObserver will not
269 // call into this GuestViewManager after it has been destroyed.
270 base::WeakPtrFactory<GuestViewManager> weak_ptr_factory_;
272 DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
275 } // namespace guest_view
277 #endif // COMPONETS_GUEST_VIEW_BROWSER_GUEST_VIEW_MANAGER_H_