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 CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
6 #define CHROME_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"
18 class GuestViewManagerFactory;
19 class GuestWebContentsObserver;
24 } // namespace content
27 class TestGuestViewManager;
28 } // namespace guestview
30 class GuestViewManager : public content::BrowserPluginGuestManager,
31 public base::SupportsUserData::Data {
33 explicit GuestViewManager(content::BrowserContext* context);
34 virtual ~GuestViewManager();
36 static GuestViewManager* FromBrowserContext(content::BrowserContext* context);
38 // Overrides factory for testing. Default (NULL) value indicates regular
39 // (non-test) environment.
40 static void set_factory_for_testing(GuestViewManagerFactory* factory) {
41 GuestViewManager::factory_ = factory;
43 // Returns the guest WebContents associated with the given |guest_instance_id|
44 // if the provided |embedder_render_process_id| is allowed to access it.
45 // If the embedder is not allowed access, the embedder will be killed, and
46 // this method will return NULL. If no WebContents exists with the given
47 // instance ID, then NULL will also be returned.
48 content::WebContents* GetGuestByInstanceIDSafely(
49 int guest_instance_id,
50 int embedder_render_process_id);
52 // BrowserPluginGuestManager implementation.
53 virtual content::WebContents* CreateGuest(
54 content::SiteInstance* embedder_site_instance,
56 scoped_ptr<base::DictionaryValue> extra_params) OVERRIDE;
57 virtual int GetNextInstanceID() OVERRIDE;
58 virtual void MaybeGetGuestByInstanceIDOrKill(
59 int guest_instance_id,
60 int embedder_render_process_id,
61 const GuestByInstanceIDCallback& callback) OVERRIDE;
62 virtual bool ForEachGuest(content::WebContents* embedder_web_contents,
63 const GuestCallback& callback) OVERRIDE;
66 friend class GuestViewBase;
67 friend class GuestWebContentsObserver;
68 friend class guestview::TestGuestViewManager;
69 FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
71 // Can be overriden in tests.
72 virtual void AddGuest(int guest_instance_id,
73 content::WebContents* guest_web_contents);
75 void RemoveGuest(int guest_instance_id);
77 content::SiteInstance* GetGuestSiteInstance(
78 const GURL& guest_site);
80 content::WebContents* GetGuestByInstanceID(
81 int guest_instance_id,
82 int embedder_render_process_id);
84 bool CanEmbedderAccessInstanceIDMaybeKill(
85 int embedder_render_process_id,
86 int guest_instance_id);
88 bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
89 int guest_instance_id);
91 // Returns true if |guest_instance_id| can be used to add a new guest to this
93 // We disallow adding new guest with instance IDs that were previously removed
94 // from this manager using RemoveGuest.
95 bool CanUseGuestInstanceID(int guest_instance_id);
97 static bool CanEmbedderAccessGuest(int embedder_render_process_id,
98 GuestViewBase* guest);
100 // Static factory instance (always NULL for non-test).
101 static GuestViewManagerFactory* factory_;
103 // Contains guests' WebContents, mapping from their instance ids.
104 typedef std::map<int, content::WebContents*> GuestInstanceMap;
105 GuestInstanceMap guest_web_contents_by_instance_id_;
107 int current_instance_id_;
109 // Any instance ID whose number not greater than this was removed via
111 // This is used so that we don't have store all removed instance IDs in
112 // |removed_instance_ids_|.
113 int last_instance_id_removed_;
114 // The remaining instance IDs that are greater than
115 // |last_instance_id_removed_| are kept here.
116 std::set<int> removed_instance_ids_;
118 content::BrowserContext* context_;
120 DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
123 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_