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 EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
6 #define EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
10 #include "base/gtest_prod_util.h"
11 #include "base/lazy_instance.h"
12 #include "base/macros.h"
13 #include "content/public/browser/browser_plugin_guest_manager.h"
14 #include "content/public/browser/site_instance.h"
15 #include "content/public/browser/web_contents.h"
22 } // namespace content
26 class GuestViewManagerFactory;
28 class GuestViewManager : public content::BrowserPluginGuestManager,
29 public base::SupportsUserData::Data {
31 explicit GuestViewManager(content::BrowserContext* context);
32 virtual ~GuestViewManager();
34 static GuestViewManager* FromBrowserContext(content::BrowserContext* context);
36 // Overrides factory for testing. Default (NULL) value indicates regular
37 // (non-test) environment.
38 static void set_factory_for_testing(GuestViewManagerFactory* factory) {
39 GuestViewManager::factory_ = factory;
41 // Returns the guest WebContents associated with the given |guest_instance_id|
42 // if the provided |embedder_render_process_id| is allowed to access it.
43 // If the embedder is not allowed access, the embedder will be killed, and
44 // this method will return NULL. If no WebContents exists with the given
45 // instance ID, then NULL will also be returned.
46 content::WebContents* GetGuestByInstanceIDSafely(
47 int guest_instance_id,
48 int embedder_render_process_id);
50 int GetNextInstanceID();
52 typedef base::Callback<void(content::WebContents*)>
53 WebContentsCreatedCallback;
54 void CreateGuest(const std::string& view_type,
55 const std::string& embedder_extension_id,
56 content::WebContents* embedder_web_contents,
57 const base::DictionaryValue& create_params,
58 const WebContentsCreatedCallback& callback);
60 content::WebContents* CreateGuestWithWebContentsParams(
61 const std::string& view_type,
62 const std::string& embedder_extension_id,
63 int embedder_render_process_id,
64 const content::WebContents::CreateParams& create_params);
66 content::SiteInstance* GetGuestSiteInstance(
67 const GURL& guest_site);
69 // BrowserPluginGuestManager implementation.
70 virtual void MaybeGetGuestByInstanceIDOrKill(
71 int guest_instance_id,
72 int embedder_render_process_id,
73 const GuestByInstanceIDCallback& callback) OVERRIDE;
74 virtual bool ForEachGuest(content::WebContents* embedder_web_contents,
75 const GuestCallback& callback) OVERRIDE;
78 friend class GuestViewBase;
79 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
81 // Can be overriden in tests.
82 virtual void AddGuest(int guest_instance_id,
83 content::WebContents* guest_web_contents);
85 void RemoveGuest(int guest_instance_id);
87 content::WebContents* GetGuestByInstanceID(int guest_instance_id);
89 bool CanEmbedderAccessInstanceIDMaybeKill(
90 int embedder_render_process_id,
91 int guest_instance_id);
93 bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
94 int guest_instance_id);
96 // Returns true if |guest_instance_id| can be used to add a new guest to this
98 // We disallow adding new guest with instance IDs that were previously removed
99 // from this manager using RemoveGuest.
100 bool CanUseGuestInstanceID(int guest_instance_id);
102 // Static factory instance (always NULL for non-test).
103 static GuestViewManagerFactory* factory_;
105 // Contains guests' WebContents, mapping from their instance ids.
106 typedef std::map<int, content::WebContents*> GuestInstanceMap;
107 GuestInstanceMap guest_web_contents_by_instance_id_;
109 int current_instance_id_;
111 // Any instance ID whose number not greater than this was removed via
113 // This is used so that we don't have store all removed instance IDs in
114 // |removed_instance_ids_|.
115 int last_instance_id_removed_;
116 // The remaining instance IDs that are greater than
117 // |last_instance_id_removed_| are kept here.
118 std::set<int> removed_instance_ids_;
120 content::BrowserContext* context_;
122 DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
125 } // namespace extensions
127 #endif // EXTENSIONS_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_