| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 5 #ifndef CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |
| 6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 6 #define CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |
| 7 | 7 |
| 8 #include <queue> | 8 #include <queue> |
| 9 | 9 |
| 10 #include "base/memory/weak_ptr.h" | 10 #include "base/memory/weak_ptr.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "content/public/browser/browser_plugin_guest_delegate.h" | 12 #include "content/public/browser/browser_plugin_guest_delegate.h" |
| 13 #include "content/public/browser/render_process_host_observer.h" |
| 13 #include "content/public/browser/web_contents.h" | 14 #include "content/public/browser/web_contents.h" |
| 14 #include "content/public/browser/web_contents_delegate.h" | 15 #include "content/public/browser/web_contents_delegate.h" |
| 15 #include "content/public/browser/web_contents_observer.h" | 16 #include "content/public/browser/web_contents_observer.h" |
| 16 | 17 |
| 17 struct RendererContentSettingRules; | 18 struct RendererContentSettingRules; |
| 18 | 19 |
| 19 // A GuestViewBase is the base class browser-side API implementation for a | 20 // A GuestViewBase is the base class browser-side API implementation for a |
| 20 // <*view> tag. GuestViewBase maintains an association between a guest | 21 // <*view> tag. GuestViewBase maintains an association between a guest |
| 21 // WebContents and an embedder WebContents. It receives events issued from | 22 // WebContents and an embedder WebContents. It receives events issued from |
| 22 // the guest and relays them to the embedder. | 23 // the guest and relays them to the embedder. GuestViewBase tracks the lifetime |
| 24 // of its embedder render process until it is attached to a particular embedder |
| 25 // WebContents. At that point, its lifetime is restricted in scope to the |
| 26 // lifetime of its embedder WebContents. |
| 23 class GuestViewBase : public content::BrowserPluginGuestDelegate, | 27 class GuestViewBase : public content::BrowserPluginGuestDelegate, |
| 28 public content::RenderProcessHostObserver, |
| 24 public content::WebContentsDelegate, | 29 public content::WebContentsDelegate, |
| 25 public content::WebContentsObserver { | 30 public content::WebContentsObserver { |
| 26 public: | 31 public: |
| 27 class Event { | 32 class Event { |
| 28 public: | 33 public: |
| 29 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); | 34 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); |
| 30 ~Event(); | 35 ~Event(); |
| 31 | 36 |
| 32 const std::string& name() const { return name_; } | 37 const std::string& name() const { return name_; } |
| 33 | 38 |
| 34 scoped_ptr<base::DictionaryValue> GetArguments(); | 39 scoped_ptr<base::DictionaryValue> GetArguments(); |
| 35 | 40 |
| 36 private: | 41 private: |
| 37 const std::string name_; | 42 const std::string name_; |
| 38 scoped_ptr<base::DictionaryValue> args_; | 43 scoped_ptr<base::DictionaryValue> args_; |
| 39 }; | 44 }; |
| 40 | 45 |
| 41 // Returns a *ViewGuest if this GuestView is of the given view type. | 46 // Returns a *ViewGuest if this GuestView is of the given view type. |
| 42 template <typename T> | 47 template <typename T> |
| 43 T* As() { | 48 T* As() { |
| 44 if (IsViewType(T::Type)) | 49 if (IsViewType(T::Type)) |
| 45 return static_cast<T*>(this); | 50 return static_cast<T*>(this); |
| 46 | 51 |
| 47 return NULL; | 52 return NULL; |
| 48 } | 53 } |
| 49 | 54 |
| 50 static GuestViewBase* Create(int guest_instance_id, | 55 static GuestViewBase* Create(int guest_instance_id, |
| 51 content::WebContents* guest_web_contents, | |
| 52 const std::string& embedder_extension_id, | |
| 53 const std::string& view_type); | 56 const std::string& view_type); |
| 54 | 57 |
| 55 static GuestViewBase* FromWebContents(content::WebContents* web_contents); | 58 static GuestViewBase* FromWebContents(content::WebContents* web_contents); |
| 56 | 59 |
| 57 static GuestViewBase* From(int embedder_process_id, int instance_id); | 60 static GuestViewBase* From(int embedder_process_id, int instance_id); |
| 58 | 61 |
| 59 static bool IsGuest(content::WebContents* web_contents); | 62 static bool IsGuest(content::WebContents* web_contents); |
| 60 | 63 |
| 61 // By default, JavaScript and images are enabled in guest content. | 64 // By default, JavaScript and images are enabled in guest content. |
| 62 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules, | 65 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules, |
| 63 bool incognito); | 66 bool incognito); |
| 64 | 67 |
| 65 virtual const char* GetViewType() const = 0; | 68 virtual const char* GetViewType() const = 0; |
| 66 | 69 |
| 67 // This method is called after the guest has been attached to an embedder and | 70 // This method is called after the guest has been attached to an embedder and |
| 68 // suspended resource loads have been resumed. | 71 // suspended resource loads have been resumed. |
| 69 // | 72 // |
| 70 // This method can be overriden by subclasses. This gives the derived class | 73 // This method can be overriden by subclasses. This gives the derived class |
| 71 // an opportunity to perform setup actions after attachment. | 74 // an opportunity to perform setup actions after attachment. |
| 72 virtual void DidAttachToEmbedder() {} | 75 virtual void DidAttachToEmbedder() {} |
| 73 | 76 |
| 74 // This method can be overridden by subclasses. This method is called when | 77 // This method is called after this GuestViewBase has been initiated. |
| 75 // the initial set of frames within the page have completed loading. | 78 // |
| 79 // This gives the derived class an opportunity to perform additional |
| 80 // initialization. |
| 81 virtual void DidInitialize() {} |
| 82 |
| 83 // This method is called when the initial set of frames within the page have |
| 84 // completed loading. |
| 76 virtual void DidStopLoading() {} | 85 virtual void DidStopLoading() {} |
| 77 | 86 |
| 78 // This method is called immediately before suspended resource loads have been | |
| 79 // resumed on attachment to an embedder. | |
| 80 // | |
| 81 // This method can be overriden by subclasses. This gives the derived class | |
| 82 // an opportunity to perform setup actions before attachment. | |
| 83 virtual void WillAttachToEmbedder() {} | |
| 84 | |
| 85 // This method is called when the guest WebContents is about to be destroyed. | |
| 86 // | |
| 87 // This method can be overridden by subclasses. This gives the derived class | |
| 88 // an opportunity to perform some cleanup prior to destruction. | |
| 89 virtual void WillDestroy() {} | |
| 90 | |
| 91 // This method is called when the guest's embedder WebContents has been | 87 // This method is called when the guest's embedder WebContents has been |
| 92 // destroyed and the guest will be destroyed shortly. | 88 // destroyed and the guest will be destroyed shortly. |
| 93 // | 89 // |
| 94 // This method can be overridden by subclasses. This gives the derived class | 90 // This gives the derived class an opportunity to perform some cleanup prior |
| 95 // an opportunity to perform some cleanup prior to destruction. | 91 // to destruction. |
| 96 virtual void EmbedderDestroyed() {} | 92 virtual void EmbedderDestroyed() {} |
| 97 | 93 |
| 98 // This method is called when the guest WebContents has been destroyed. This | 94 // This method is called when the guest WebContents has been destroyed. This |
| 99 // object will be destroyed after this call returns. | 95 // object will be destroyed after this call returns. |
| 100 // | 96 // |
| 101 // This method can be overridden by subclasses. This gives the derived class | 97 // This gives the derived class an opportunity to perform some cleanup. |
| 102 // opportunity to perform some cleanup. | |
| 103 virtual void GuestDestroyed() {} | 98 virtual void GuestDestroyed() {} |
| 104 | 99 |
| 105 // This method queries whether drag-and-drop is enabled for this particular | 100 // This method queries whether drag-and-drop is enabled for this particular |
| 106 // view. By default, drag-and-drop is disabled. Derived classes can override | 101 // view. By default, drag-and-drop is disabled. Derived classes can override |
| 107 // this behavior to enable drag-and-drop. | 102 // this behavior to enable drag-and-drop. |
| 108 virtual bool IsDragAndDropEnabled() const; | 103 virtual bool IsDragAndDropEnabled() const; |
| 109 | 104 |
| 110 // Once a guest WebContents is ready, this initiates the association of |this| | 105 // This method is called immediately before suspended resource loads have been |
| 111 // GuestView with |guest_web_contents|. | 106 // resumed on attachment to an embedder. |
| 112 void Init(content::WebContents* guest_web_contents, | 107 // |
| 113 const std::string& embedder_extension_id); | 108 // This method can be overriden by subclasses. This gives the derived class |
| 109 // an opportunity to perform setup actions before attachment. |
| 110 virtual void WillAttachToEmbedder() {} |
| 111 |
| 112 // This method is called when the guest WebContents is about to be destroyed. |
| 113 // |
| 114 // This gives the derived class an opportunity to perform some cleanup prior |
| 115 // to destruction. |
| 116 virtual void WillDestroy() {} |
| 117 |
| 118 // This method is to be implemented by the derived class. Given a set of |
| 119 // initialization parameters, a concrete subclass of GuestViewBase can |
| 120 // create a specialized WebContents that it returns back to GuestViewBase. |
| 121 typedef base::Callback<void(content::WebContents*)> |
| 122 WebContentsCreatedCallback; |
| 123 virtual void CreateWebContents( |
| 124 const std::string& embedder_extension_id, |
| 125 int embedder_render_process_id, |
| 126 const base::DictionaryValue& create_params, |
| 127 const WebContentsCreatedCallback& callback) = 0; |
| 128 |
| 129 // This creates a WebContents and initializes |this| GuestViewBase to use the |
| 130 // newly created WebContents. |
| 131 void Init(const std::string& embedder_extension_id, |
| 132 int embedder_render_process_id, |
| 133 const base::DictionaryValue& create_params); |
| 134 |
| 135 void InitWithWebContents( |
| 136 const std::string& embedder_extension_id, |
| 137 int embedder_render_process_id, |
| 138 content::WebContents* guest_web_contents); |
| 114 | 139 |
| 115 bool IsViewType(const char* const view_type) const { | 140 bool IsViewType(const char* const view_type) const { |
| 116 return !strcmp(GetViewType(), view_type); | 141 return !strcmp(GetViewType(), view_type); |
| 117 } | 142 } |
| 118 | 143 |
| 119 base::WeakPtr<GuestViewBase> AsWeakPtr(); | 144 base::WeakPtr<GuestViewBase> AsWeakPtr(); |
| 120 | 145 |
| 146 bool initialized() const { return initialized_; } |
| 147 |
| 121 content::WebContents* embedder_web_contents() const { | 148 content::WebContents* embedder_web_contents() const { |
| 122 return embedder_web_contents_; | 149 return embedder_web_contents_; |
| 123 } | 150 } |
| 124 | 151 |
| 125 // Returns the guest WebContents. | 152 // Returns the guest WebContents. |
| 126 content::WebContents* guest_web_contents() const { | 153 content::WebContents* guest_web_contents() const { |
| 127 return web_contents(); | 154 return web_contents(); |
| 128 } | 155 } |
| 129 | 156 |
| 130 // Returns the extra parameters associated with this GuestView passed | 157 // Returns the extra parameters associated with this GuestView passed |
| 131 // in from JavaScript. | 158 // in from JavaScript. |
| 132 base::DictionaryValue* extra_params() const { | 159 base::DictionaryValue* extra_params() const { |
| 133 return extra_params_.get(); | 160 return extra_params_.get(); |
| 134 } | 161 } |
| 135 | 162 |
| 136 // Returns whether this guest has an associated embedder. | 163 // Returns whether this guest has an associated embedder. |
| 137 bool attached() const { return !!embedder_web_contents_; } | 164 bool attached() const { return !!embedder_web_contents_; } |
| 138 | 165 |
| 139 // Returns the instance ID of the <*view> element. | 166 // Returns the instance ID of the <*view> element. |
| 140 int view_instance_id() const { return view_instance_id_; } | 167 int view_instance_id() const { return view_instance_id_; } |
| 141 | 168 |
| 142 // Returns the instance ID of the guest WebContents. | |
| 143 int guest_instance_id() const { return guest_instance_id_; } | |
| 144 | |
| 145 // Returns the extension ID of the embedder. | 169 // Returns the extension ID of the embedder. |
| 146 const std::string& embedder_extension_id() const { | 170 const std::string& embedder_extension_id() const { |
| 147 return embedder_extension_id_; | 171 return embedder_extension_id_; |
| 148 } | 172 } |
| 149 | 173 |
| 150 // Returns whether this GuestView is embedded in an extension/app. | 174 // Returns whether this GuestView is embedded in an extension/app. |
| 151 bool in_extension() const { return !embedder_extension_id_.empty(); } | 175 bool in_extension() const { return !embedder_extension_id_.empty(); } |
| 152 | 176 |
| 153 // Returns the user browser context of the embedder. | 177 // Returns the user browser context of the embedder. |
| 154 content::BrowserContext* browser_context() const { return browser_context_; } | 178 content::BrowserContext* browser_context() const { return browser_context_; } |
| 155 | 179 |
| 156 // Returns the embedder's process ID. | 180 // Returns the embedder's process ID. |
| 157 int embedder_render_process_id() const { return embedder_render_process_id_; } | 181 int embedder_render_process_id() const { return embedder_render_process_id_; } |
| 158 | 182 |
| 159 GuestViewBase* GetOpener() const { | 183 GuestViewBase* GetOpener() const { |
| 160 return opener_.get(); | 184 return opener_.get(); |
| 161 } | 185 } |
| 162 | 186 |
| 163 void SetOpener(GuestViewBase* opener); | 187 void SetOpener(GuestViewBase* opener); |
| 164 | 188 |
| 189 // RenderProcessHostObserver implementation |
| 190 virtual void RenderProcessExited(content::RenderProcessHost* host, |
| 191 base::ProcessHandle handle, |
| 192 base::TerminationStatus status, |
| 193 int exit_code) OVERRIDE; |
| 194 |
| 165 // BrowserPluginGuestDelegate implementation. | 195 // BrowserPluginGuestDelegate implementation. |
| 166 virtual void Destroy() OVERRIDE FINAL; | 196 virtual void Destroy() OVERRIDE FINAL; |
| 167 virtual void DidAttach() OVERRIDE FINAL; | 197 virtual void DidAttach() OVERRIDE FINAL; |
| 198 virtual int GetGuestInstanceID() const OVERRIDE; |
| 168 virtual void RegisterDestructionCallback( | 199 virtual void RegisterDestructionCallback( |
| 169 const DestructionCallback& callback) OVERRIDE FINAL; | 200 const DestructionCallback& callback) OVERRIDE FINAL; |
| 170 virtual void WillAttach( | 201 virtual void WillAttach( |
| 171 content::WebContents* embedder_web_contents, | 202 content::WebContents* embedder_web_contents, |
| 172 const base::DictionaryValue& extra_params) OVERRIDE FINAL; | 203 const base::DictionaryValue& extra_params) OVERRIDE FINAL; |
| 173 | 204 |
| 174 protected: | 205 protected: |
| 175 explicit GuestViewBase(int guest_instance_id); | 206 explicit GuestViewBase(int guest_instance_id); |
| 176 | 207 |
| 177 virtual ~GuestViewBase(); | 208 virtual ~GuestViewBase(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 226 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_; | 257 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_; |
| 227 | 258 |
| 228 // This is used to ensure pending tasks will not fire after this object is | 259 // This is used to ensure pending tasks will not fire after this object is |
| 229 // destroyed. | 260 // destroyed. |
| 230 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; | 261 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; |
| 231 | 262 |
| 232 DISALLOW_COPY_AND_ASSIGN(GuestViewBase); | 263 DISALLOW_COPY_AND_ASSIGN(GuestViewBase); |
| 233 }; | 264 }; |
| 234 | 265 |
| 235 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 266 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |
| OLD | NEW |