[email protected] | 945604a | 2014-04-28 12:29:59 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors. All rights reserved. |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |
| 6 | #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 7 | |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 8 | #include <queue> |
| 9 | |
[email protected] | f21d36e | 2014-01-16 19:24:04 | [diff] [blame] | 10 | #include "base/memory/weak_ptr.h" |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | 4c0e827 | 2013-07-03 23:39:22 | [diff] [blame] | 12 | #include "content/public/browser/browser_plugin_guest_delegate.h" |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 13 | #include "content/public/browser/web_contents.h" |
[email protected] | aec80ed | 2014-05-27 00:01:15 | [diff] [blame] | 14 | #include "content/public/browser/web_contents_delegate.h" |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 15 | #include "content/public/browser/web_contents_observer.h" |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 16 | |
[email protected] | 06153f0 | 2013-12-04 03:01:28 | [diff] [blame] | 17 | struct RendererContentSettingRules; |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 18 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 19 | // A GuestViewBase is the base class browser-side API implementation for a |
| 20 | // <*view> tag. GuestViewBase maintains an association between a guest |
| 21 | // WebContents and an embedder WebContents. It receives events issued from |
| 22 | // the guest and relays them to the embedder. |
[email protected] | aec80ed | 2014-05-27 00:01:15 | [diff] [blame] | 23 | class GuestViewBase : public content::BrowserPluginGuestDelegate, |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 24 | public content::WebContentsDelegate, |
| 25 | public content::WebContentsObserver { |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 26 | public: |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 27 | class Event { |
| 28 | public: |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 29 | Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); |
| 30 | ~Event(); |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 31 | |
[email protected] | 0aad647 | 2013-12-04 18:25:38 | [diff] [blame] | 32 | const std::string& name() const { return name_; } |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 33 | |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 34 | scoped_ptr<base::DictionaryValue> GetArguments(); |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 35 | |
| 36 | private: |
[email protected] | 0aad647 | 2013-12-04 18:25:38 | [diff] [blame] | 37 | const std::string name_; |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 38 | scoped_ptr<base::DictionaryValue> args_; |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 39 | }; |
| 40 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 41 | // Returns a *ViewGuest if this GuestView is of the given view type. |
| 42 | template <typename T> |
| 43 | T* As() { |
[email protected] | 2456926 | 2014-05-06 03:31:30 | [diff] [blame] | 44 | if (IsViewType(T::Type)) |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 45 | return static_cast<T*>(this); |
[email protected] | 2456926 | 2014-05-06 03:31:30 | [diff] [blame] | 46 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 47 | return NULL; |
| 48 | } |
[email protected] | 50c827d | 2013-09-13 21:36:09 | [diff] [blame] | 49 | |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 50 | static GuestViewBase* Create(int guest_instance_id, |
| 51 | content::WebContents* guest_web_contents, |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 52 | const std::string& embedder_extension_id, |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 53 | const std::string& view_type); |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 54 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 55 | static GuestViewBase* FromWebContents(content::WebContents* web_contents); |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 56 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 57 | static GuestViewBase* From(int embedder_process_id, int instance_id); |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 58 | |
[email protected] | a24efc2 | 2014-05-26 15:50:25 | [diff] [blame] | 59 | static bool IsGuest(content::WebContents* web_contents); |
| 60 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 61 | // For GuestViewBases, we create special guest processes, which host the |
[email protected] | 3f24f97c | 2013-11-17 21:53:33 | [diff] [blame] | 62 | // tag content separately from the main application that embeds the tag. |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 63 | // A GuestViewBase can specify both the partition name and whether the storage |
[email protected] | 3f24f97c | 2013-11-17 21:53:33 | [diff] [blame] | 64 | // for that partition should be persisted. Each tag gets a SiteInstance with |
| 65 | // a specially formatted URL, based on the application it is hosted by and |
| 66 | // the partition requested by it. The format for that URL is: |
| 67 | // chrome-guest://partition_domain/persist?partition_name |
| 68 | static bool GetGuestPartitionConfigForSite(const GURL& site, |
| 69 | std::string* partition_domain, |
| 70 | std::string* partition_name, |
| 71 | bool* in_memory); |
| 72 | |
[email protected] | 06153f0 | 2013-12-04 03:01:28 | [diff] [blame] | 73 | // By default, JavaScript and images are enabled in guest content. |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 74 | static void GetDefaultContentSettingRules(RendererContentSettingRules* rules, |
| 75 | bool incognito); |
| 76 | |
[email protected] | 38fe437 | 2014-05-01 08:38:32 | [diff] [blame] | 77 | virtual const char* GetViewType() const = 0; |
[email protected] | 06153f0 | 2013-12-04 03:01:28 | [diff] [blame] | 78 | |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 79 | // This method can be overridden by subclasses. It indicates that this guest's |
| 80 | // embedder has been destroyed and the guest will be destroyed shortly. This |
| 81 | // method gives derived classes the opportunity to perform some cleanup. |
| 82 | virtual void EmbedderDestroyed() {} |
| 83 | |
[email protected] | 2456926 | 2014-05-06 03:31:30 | [diff] [blame] | 84 | bool IsViewType(const char* const view_type) const { |
| 85 | return !strcmp(GetViewType(), view_type); |
| 86 | } |
| 87 | |
| 88 | base::WeakPtr<GuestViewBase> AsWeakPtr(); |
| 89 | |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 90 | virtual void Attach(content::WebContents* embedder_web_contents, |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 91 | const base::DictionaryValue& args); |
| 92 | |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 93 | content::WebContents* embedder_web_contents() const { |
| 94 | return embedder_web_contents_; |
| 95 | } |
| 96 | |
| 97 | // Returns the guest WebContents. |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 98 | content::WebContents* guest_web_contents() const { |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 99 | return web_contents(); |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 100 | } |
| 101 | |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 102 | // Returns the extra parameters associated with this GuestView passed |
| 103 | // in from JavaScript. |
| 104 | base::DictionaryValue* extra_params() const { |
| 105 | return extra_params_.get(); |
| 106 | } |
| 107 | |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 108 | // Returns whether this guest has an associated embedder. |
| 109 | bool attached() const { return !!embedder_web_contents_; } |
| 110 | |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 111 | // Returns the instance ID of the <*view> element. |
| 112 | int view_instance_id() const { return view_instance_id_; } |
| 113 | |
| 114 | // Returns the instance ID of the guest WebContents. |
| 115 | int guest_instance_id() const { return guest_instance_id_; } |
| 116 | |
| 117 | // Returns the extension ID of the embedder. |
[email protected] | 880331f97 | 2014-03-05 01:42:53 | [diff] [blame] | 118 | const std::string& embedder_extension_id() const { |
| 119 | return embedder_extension_id_; |
| 120 | } |
| 121 | |
| 122 | // Returns whether this GuestView is embedded in an extension/app. |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 123 | bool in_extension() const { return !embedder_extension_id_.empty(); } |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 124 | |
| 125 | // Returns the user browser context of the embedder. |
| 126 | content::BrowserContext* browser_context() const { return browser_context_; } |
| 127 | |
| 128 | // Returns the embedder's process ID. |
| 129 | int embedder_render_process_id() const { return embedder_render_process_id_; } |
| 130 | |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 131 | GuestViewBase* GetOpener() const { |
| 132 | return opener_.get(); |
| 133 | } |
| 134 | |
| 135 | void SetOpener(GuestViewBase* opener); |
| 136 | |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 137 | // WebContentsObserver implementation. |
| 138 | virtual void WebContentsDestroyed() OVERRIDE; |
| 139 | |
[email protected] | aec80ed | 2014-05-27 00:01:15 | [diff] [blame] | 140 | // WebContentsDelegate implementation. |
| 141 | virtual bool ShouldFocusPageAfterCrash() OVERRIDE; |
[email protected] | 87acf4d | 2014-05-22 22:18:49 | [diff] [blame] | 142 | virtual bool PreHandleGestureEvent( |
| 143 | content::WebContents* source, |
| 144 | const blink::WebGestureEvent& event) OVERRIDE; |
[email protected] | 2456926 | 2014-05-06 03:31:30 | [diff] [blame] | 145 | |
[email protected] | aec80ed | 2014-05-27 00:01:15 | [diff] [blame] | 146 | // BrowserPluginGuestDelegate implementation. |
| 147 | virtual void Destroy() OVERRIDE; |
| 148 | virtual void RegisterDestructionCallback( |
| 149 | const DestructionCallback& callback) OVERRIDE; |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 150 | protected: |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 151 | GuestViewBase(int guest_instance_id, |
| 152 | content::WebContents* guest_web_contents, |
| 153 | const std::string& embedder_extension_id); |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 154 | virtual ~GuestViewBase(); |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 155 | |
| 156 | // Dispatches an event |event_name| to the embedder with the |event| fields. |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 157 | void DispatchEvent(Event* event); |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 158 | |
| 159 | private: |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 160 | class EmbedderWebContentsObserver; |
| 161 | |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 162 | void SendQueuedEvents(); |
| 163 | |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 164 | content::WebContents* embedder_web_contents_; |
[email protected] | 880331f97 | 2014-03-05 01:42:53 | [diff] [blame] | 165 | const std::string embedder_extension_id_; |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 166 | int embedder_render_process_id_; |
[email protected] | 70e7c0e | 2013-11-08 00:33:21 | [diff] [blame] | 167 | content::BrowserContext* const browser_context_; |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 168 | // |guest_instance_id_| is a profile-wide unique identifier for a guest |
| 169 | // WebContents. |
| 170 | const int guest_instance_id_; |
| 171 | // |view_instance_id_| is an identifier that's unique within a particular |
| 172 | // embedder RenderViewHost for a particular <*view> instance. |
[email protected] | 738f57a | 2013-06-29 21:06:54 | [diff] [blame] | 173 | int view_instance_id_; |
| 174 | |
| 175 | // This is a queue of Events that are destined to be sent to the embedder once |
| 176 | // the guest is attached to a particular embedder. |
[email protected] | 0544ea9 | 2014-04-22 21:50:47 | [diff] [blame] | 177 | std::deque<linked_ptr<Event> > pending_events_; |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 178 | |
[email protected] | 2456926 | 2014-05-06 03:31:30 | [diff] [blame] | 179 | // The opener guest view. |
| 180 | base::WeakPtr<GuestViewBase> opener_; |
| 181 | |
[email protected] | 50d326e | 2014-05-20 17:59:06 | [diff] [blame] | 182 | DestructionCallback destruction_callback_; |
| 183 | |
| 184 | // The extra parameters associated with this GuestView passed |
| 185 | // in from JavaScript. This will typically be the view instance ID, |
| 186 | // the API to use, and view-specific parameters. These parameters |
| 187 | // are passed along to new guests that are created from this guest. |
| 188 | scoped_ptr<base::DictionaryValue> extra_params_; |
| 189 | |
[email protected] | 70ab264 | 2014-05-30 08:06:58 | [diff] [blame^] | 190 | scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_; |
| 191 | |
[email protected] | f21d36e | 2014-01-16 19:24:04 | [diff] [blame] | 192 | // This is used to ensure pending tasks will not fire after this object is |
| 193 | // destroyed. |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 194 | base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; |
[email protected] | f21d36e | 2014-01-16 19:24:04 | [diff] [blame] | 195 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 196 | DISALLOW_COPY_AND_ASSIGN(GuestViewBase); |
[email protected] | 0533cc6d | 2013-06-27 22:44:05 | [diff] [blame] | 197 | }; |
| 198 | |
[email protected] | 0e99fdc | 2014-04-30 05:10:33 | [diff] [blame] | 199 | #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |