Upstream version 8.37.180.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / guest_view / guest_view_manager.h
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 CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_
7
8 #include <map>
9
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"
16
17 class GuestViewBase;
18 class GuestViewManagerFactory;
19 class GuestWebContentsObserver;
20 class GURL;
21
22 namespace content {
23 class BrowserContext;
24 }  // namespace content
25
26 namespace guestview {
27 class TestGuestViewManager;
28 }  // namespace guestview
29
30 class GuestViewManager : public content::BrowserPluginGuestManager,
31                          public base::SupportsUserData::Data {
32  public:
33   explicit GuestViewManager(content::BrowserContext* context);
34   virtual ~GuestViewManager();
35
36   static GuestViewManager* FromBrowserContext(content::BrowserContext* context);
37
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;
42   }
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);
51
52   // BrowserPluginGuestManager implementation.
53   virtual content::WebContents* CreateGuest(
54       content::SiteInstance* embedder_site_instance,
55       int instance_id,
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;
64
65  protected:
66   friend class GuestViewBase;
67   friend class GuestWebContentsObserver;
68   friend class guestview::TestGuestViewManager;
69   FRIEND_TEST_ALL_PREFIXES(GuestViewManagerTest, AddRemove);
70
71   // Can be overriden in tests.
72   virtual void AddGuest(int guest_instance_id,
73                         content::WebContents* guest_web_contents);
74
75   void RemoveGuest(int guest_instance_id);
76
77   content::SiteInstance* GetGuestSiteInstance(
78       const GURL& guest_site);
79
80   content::WebContents* GetGuestByInstanceID(
81       int guest_instance_id,
82       int embedder_render_process_id);
83
84   bool CanEmbedderAccessInstanceIDMaybeKill(
85       int embedder_render_process_id,
86       int guest_instance_id);
87
88   bool CanEmbedderAccessInstanceID(int embedder_render_process_id,
89                                    int guest_instance_id);
90
91   // Returns true if |guest_instance_id| can be used to add a new guest to this
92   // manager.
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);
96
97   static bool CanEmbedderAccessGuest(int embedder_render_process_id,
98                                      GuestViewBase* guest);
99
100   // Static factory instance (always NULL for non-test).
101   static GuestViewManagerFactory* factory_;
102
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_;
106
107   int current_instance_id_;
108
109   // Any instance ID whose number not greater than this was removed via
110   // RemoveGuest.
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_;
117
118   content::BrowserContext* context_;
119
120   DISALLOW_COPY_AND_ASSIGN(GuestViewManager);
121 };
122
123 #endif  // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_MANAGER_H_