| 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. | 70 // This method is called after the guest has been attached to an embedder. |
| 68 // | 71 // |
| 69 // This method can be overriden by subclasses. This gives the derived class | 72 // This gives the derived class an opportunity to perform setup actions after |
| 70 // an opportunity to perform setup actions after attachment. | 73 // attachment. |
| 71 virtual void DidAttach() {} | 74 virtual void DidAttach() {} |
| 72 | 75 |
| 73 // This method can be overridden by subclasses. This method is called when | 76 // This method is called when the initial set of frames within the page have |
| 74 // the initial set of frames within the page have completed loading. | 77 // completed loading. |
| 75 virtual void DidStopLoading() {} | 78 virtual void DidStopLoading() {} |
| 76 | 79 |
| 77 // This method is called when the guest WebContents is about to be destroyed. | |
| 78 // | |
| 79 // This method can be overridden by subclasses. This gives the derived class | |
| 80 // an opportunity to perform some cleanup prior to destruction. | |
| 81 virtual void WillDestroy() {} | |
| 82 | |
| 83 // This method is called when the guest's embedder WebContents has been | 80 // This method is called when the guest's embedder WebContents has been |
| 84 // destroyed and the guest will be destroyed shortly. | 81 // destroyed and the guest will be destroyed shortly. |
| 85 // | 82 // |
| 86 // This method can be overridden by subclasses. This gives the derived class | 83 // This gives the derived class an opportunity to perform some cleanup prior |
| 87 // an opportunity to perform some cleanup prior to destruction. | 84 // to destruction. |
| 88 virtual void EmbedderDestroyed() {} | 85 virtual void EmbedderDestroyed() {} |
| 89 | 86 |
| 90 // This method is called when the guest WebContents has been destroyed. This | 87 // This method is called when the guest WebContents has been destroyed. This |
| 91 // object will be destroyed after this call returns. | 88 // object will be destroyed after this call returns. |
| 92 // | 89 // |
| 93 // This method can be overridden by subclasses. This gives the derived class | 90 // This gives the derived class an opportunity to perform some cleanup. |
| 94 // opportunity to perform some cleanup. | |
| 95 virtual void GuestDestroyed() {} | 91 virtual void GuestDestroyed() {} |
| 96 | 92 |
| 93 // This method is called after this GuestViewBase has been initiated. |
| 94 // |
| 95 // This gives the derived class an opportunity to perform additional |
| 96 // initialization. |
| 97 virtual void Init() {} |
| 98 |
| 97 // This method queries whether drag-and-drop is enabled for this particular | 99 // This method queries whether drag-and-drop is enabled for this particular |
| 98 // view. By default, drag-and-drop is disabled. Derived classes can override | 100 // view. By default, drag-and-drop is disabled. Derived classes can override |
| 99 // this behavior to enable drag-and-drop. | 101 // this behavior to enable drag-and-drop. |
| 100 virtual bool IsDragAndDropEnabled() const; | 102 virtual bool IsDragAndDropEnabled() const; |
| 101 | 103 |
| 102 // Once a guest WebContents is ready, this initiates the association of |this| | 104 // This method is called when the guest WebContents is about to be destroyed. |
| 103 // GuestView with |guest_web_contents|. | 105 // |
| 104 void Init(content::WebContents* guest_web_contents, | 106 // This gives the derived class an opportunity to perform some cleanup prior |
| 105 const std::string& embedder_extension_id); | 107 // to destruction. |
| 108 virtual void WillDestroy() {} |
| 109 |
| 110 // This method is to be implemented by the derived class. Given a set of |
| 111 // initialization parameters, a concrete subclass of GuestViewBase can |
| 112 // create a specialized WebContents that it returns back to GuestViewBase. |
| 113 typedef base::Callback<void(content::WebContents*)> |
| 114 WebContentsCreatedCallback; |
| 115 virtual void CreateWebContents( |
| 116 const std::string& embedder_extension_id, |
| 117 int embedder_render_process_id, |
| 118 const base::DictionaryValue& create_params, |
| 119 const WebContentsCreatedCallback& callback) = 0; |
| 120 |
| 121 // This creates a WebContents and initializes |this| GuestViewBase to use the |
| 122 // newly created WebContents. |
| 123 void Init(const std::string& embedder_extension_id, |
| 124 int embedder_render_process_id, |
| 125 const base::DictionaryValue& create_params); |
| 126 |
| 127 void InitWithWebContents( |
| 128 const std::string& embedder_extension_id, |
| 129 int embedder_render_process_id, |
| 130 content::WebContents* guest_web_contents); |
| 106 | 131 |
| 107 bool IsViewType(const char* const view_type) const { | 132 bool IsViewType(const char* const view_type) const { |
| 108 return !strcmp(GetViewType(), view_type); | 133 return !strcmp(GetViewType(), view_type); |
| 109 } | 134 } |
| 110 | 135 |
| 111 base::WeakPtr<GuestViewBase> AsWeakPtr(); | 136 base::WeakPtr<GuestViewBase> AsWeakPtr(); |
| 112 | 137 |
| 138 bool initialized() const { return initialized_; } |
| 139 |
| 113 content::WebContents* embedder_web_contents() const { | 140 content::WebContents* embedder_web_contents() const { |
| 114 return embedder_web_contents_; | 141 return embedder_web_contents_; |
| 115 } | 142 } |
| 116 | 143 |
| 117 // Returns the guest WebContents. | 144 // Returns the guest WebContents. |
| 118 content::WebContents* guest_web_contents() const { | 145 content::WebContents* guest_web_contents() const { |
| 119 return web_contents(); | 146 return web_contents(); |
| 120 } | 147 } |
| 121 | 148 |
| 122 // Returns the extra parameters associated with this GuestView passed | 149 // Returns the extra parameters associated with this GuestView passed |
| 123 // in from JavaScript. | 150 // in from JavaScript. |
| 124 base::DictionaryValue* extra_params() const { | 151 base::DictionaryValue* extra_params() const { |
| 125 return extra_params_.get(); | 152 return extra_params_.get(); |
| 126 } | 153 } |
| 127 | 154 |
| 128 // Returns whether this guest has an associated embedder. | 155 // Returns whether this guest has an associated embedder. |
| 129 bool attached() const { return !!embedder_web_contents_; } | 156 bool attached() const { return !!embedder_web_contents_; } |
| 130 | 157 |
| 131 // Returns the instance ID of the <*view> element. | 158 // Returns the instance ID of the <*view> element. |
| 132 int view_instance_id() const { return view_instance_id_; } | 159 int view_instance_id() const { return view_instance_id_; } |
| 133 | 160 |
| 134 // Returns the instance ID of the guest WebContents. | |
| 135 int guest_instance_id() const { return guest_instance_id_; } | |
| 136 | |
| 137 // Returns the extension ID of the embedder. | 161 // Returns the extension ID of the embedder. |
| 138 const std::string& embedder_extension_id() const { | 162 const std::string& embedder_extension_id() const { |
| 139 return embedder_extension_id_; | 163 return embedder_extension_id_; |
| 140 } | 164 } |
| 141 | 165 |
| 142 // Returns whether this GuestView is embedded in an extension/app. | 166 // Returns whether this GuestView is embedded in an extension/app. |
| 143 bool in_extension() const { return !embedder_extension_id_.empty(); } | 167 bool in_extension() const { return !embedder_extension_id_.empty(); } |
| 144 | 168 |
| 145 // Returns the user browser context of the embedder. | 169 // Returns the user browser context of the embedder. |
| 146 content::BrowserContext* browser_context() const { return browser_context_; } | 170 content::BrowserContext* browser_context() const { return browser_context_; } |
| 147 | 171 |
| 148 // Returns the embedder's process ID. | 172 // Returns the embedder's process ID. |
| 149 int embedder_render_process_id() const { return embedder_render_process_id_; } | 173 int embedder_render_process_id() const { return embedder_render_process_id_; } |
| 150 | 174 |
| 151 GuestViewBase* GetOpener() const { | 175 GuestViewBase* GetOpener() const { |
| 152 return opener_.get(); | 176 return opener_.get(); |
| 153 } | 177 } |
| 154 | 178 |
| 155 void SetOpener(GuestViewBase* opener); | 179 void SetOpener(GuestViewBase* opener); |
| 156 | 180 |
| 181 // RenderProcessHostObserver implementation |
| 182 virtual void RenderProcessExited(content::RenderProcessHost* host, |
| 183 base::ProcessHandle handle, |
| 184 base::TerminationStatus status, |
| 185 int exit_code) OVERRIDE; |
| 186 |
| 157 // BrowserPluginGuestDelegate implementation. | 187 // BrowserPluginGuestDelegate implementation. |
| 158 virtual void Destroy() OVERRIDE FINAL; | 188 virtual void Destroy() OVERRIDE FINAL; |
| 159 virtual void DidAttach( | 189 virtual void DidAttach( |
| 160 content::WebContents* embedder_web_contents, | 190 content::WebContents* embedder_web_contents, |
| 161 const base::DictionaryValue& extra_params) OVERRIDE FINAL; | 191 const base::DictionaryValue& extra_params) OVERRIDE FINAL; |
| 192 virtual int GetGuestInstanceID() const OVERRIDE; |
| 162 virtual void RegisterDestructionCallback( | 193 virtual void RegisterDestructionCallback( |
| 163 const DestructionCallback& callback) OVERRIDE FINAL; | 194 const DestructionCallback& callback) OVERRIDE FINAL; |
| 164 | 195 |
| 165 protected: | 196 protected: |
| 166 explicit GuestViewBase(int guest_instance_id); | 197 explicit GuestViewBase(int guest_instance_id); |
| 167 | 198 |
| 168 virtual ~GuestViewBase(); | 199 virtual ~GuestViewBase(); |
| 169 | 200 |
| 170 // Dispatches an event |event_name| to the embedder with the |event| fields. | 201 // Dispatches an event |event_name| to the embedder with the |event| fields. |
| 171 void DispatchEvent(Event* event); | 202 void DispatchEvent(Event* event); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_; | 248 scoped_ptr<EmbedderWebContentsObserver> embedder_web_contents_observer_; |
| 218 | 249 |
| 219 // This is used to ensure pending tasks will not fire after this object is | 250 // This is used to ensure pending tasks will not fire after this object is |
| 220 // destroyed. | 251 // destroyed. |
| 221 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; | 252 base::WeakPtrFactory<GuestViewBase> weak_ptr_factory_; |
| 222 | 253 |
| 223 DISALLOW_COPY_AND_ASSIGN(GuestViewBase); | 254 DISALLOW_COPY_AND_ASSIGN(GuestViewBase); |
| 224 }; | 255 }; |
| 225 | 256 |
| 226 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ | 257 #endif // CHROME_BROWSER_GUEST_VIEW_GUEST_VIEW_BASE_H_ |
| OLD | NEW |