Chromium Code Reviews| 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. |
| 23 class GuestViewBase : public content::BrowserPluginGuestDelegate, | 24 class GuestViewBase : public content::BrowserPluginGuestDelegate, |
| 25 public content::RenderProcessHostObserver, | |
| 24 public content::WebContentsDelegate, | 26 public content::WebContentsDelegate, |
| 25 public content::WebContentsObserver { | 27 public content::WebContentsObserver { |
| 26 public: | 28 public: |
| 27 class Event { | 29 class Event { |
| 28 public: | 30 public: |
| 29 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); | 31 Event(const std::string& name, scoped_ptr<base::DictionaryValue> args); |
| 30 ~Event(); | 32 ~Event(); |
| 31 | 33 |
| 32 const std::string& name() const { return name_; } | 34 const std::string& name() const { return name_; } |
| 33 | 35 |
| 34 scoped_ptr<base::DictionaryValue> GetArguments(); | 36 scoped_ptr<base::DictionaryValue> GetArguments(); |
| 35 | 37 |
| 36 private: | 38 private: |
| 37 const std::string name_; | 39 const std::string name_; |
| 38 scoped_ptr<base::DictionaryValue> args_; | 40 scoped_ptr<base::DictionaryValue> args_; |
| 39 }; | 41 }; |
| 40 | 42 |
| 41 // Returns a *ViewGuest if this GuestView is of the given view type. | 43 // Returns a *ViewGuest if this GuestView is of the given view type. |
| 42 template <typename T> | 44 template <typename T> |
| 43 T* As() { | 45 T* As() { |
| 44 if (IsViewType(T::Type)) | 46 if (IsViewType(T::Type)) |
| 45 return static_cast<T*>(this); | 47 return static_cast<T*>(this); |
| 46 | 48 |
| 47 return NULL; | 49 return NULL; |
| 48 } | 50 } |
| 49 | 51 |
| 50 static GuestViewBase* Create(int guest_instance_id, | 52 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); | 53 const std::string& view_type); |
| 54 | 54 |
| 55 static GuestViewBase* FromWebContents(content::WebContents* web_contents); | 55 static GuestViewBase* FromWebContents(content::WebContents* web_contents); |
| 56 | 56 |
| 57 static GuestViewBase* From(int embedder_process_id, int instance_id); | 57 static GuestViewBase* From(int embedder_process_id, int instance_id); |
| 58 | 58 |
| 59 static bool IsGuest(content::WebContents* web_contents); | 59 static bool IsGuest(content::WebContents* web_contents); |
| 60 | 60 |
| 61 // By default, JavaScript and images are enabled in guest content. | 61 // By default, JavaScript and images are enabled in guest content. |
| 62 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules, | 62 static void GetDefaultContentSettingRules(RendererContentSettingRules* rules, |
| 63 bool incognito); | 63 bool incognito); |
| 64 | 64 |
| 65 virtual const char* GetViewType() const = 0; | 65 virtual const char* GetViewType() const = 0; |
| 66 | 66 |
| 67 // This method is called after the guest has been attached to an embedder. | 67 // This method is called after the guest has been attached to an embedder. |
| 68 // | 68 // |
| 69 // This method can be overriden by subclasses. This gives the derived class | 69 // This method can be overriden by subclasses. This gives the derived class |
| 70 // an opportunity to perform setup actions after attachment. | 70 // an opportunity to perform setup actions after attachment. |
| 71 virtual void DidAttach() {} | 71 virtual void DidAttach() {} |
| 72 | 72 |
| 73 // This method can be overridden by subclasses. This method is called when | 73 // This method can be overridden by subclasses. This method is called when |
| 74 // the initial set of frames within the page have completed loading. | 74 // the initial set of frames within the page have completed loading. |
| 75 virtual void DidStopLoading() {} | 75 virtual void DidStopLoading() {} |
| 76 | 76 |
| 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 | 77 // This method is called when the guest's embedder WebContents has been |
| 84 // destroyed and the guest will be destroyed shortly. | 78 // destroyed and the guest will be destroyed shortly. |
| 85 // | 79 // |
| 86 // This method can be overridden by subclasses. This gives the derived class | 80 // This method can be overridden by subclasses. This gives the derived class |
| 87 // an opportunity to perform some cleanup prior to destruction. | 81 // an opportunity to perform some cleanup prior to destruction. |
| 88 virtual void EmbedderDestroyed() {} | 82 virtual void EmbedderDestroyed() {} |
| 89 | 83 |
| 90 // This method is called when the guest WebContents has been destroyed. This | 84 // This method is called when the guest WebContents has been destroyed. This |
| 91 // object will be destroyed after this call returns. | 85 // object will be destroyed after this call returns. |
| 92 // | 86 // |
| 93 // This method can be overridden by subclasses. This gives the derived class | 87 // This method can be overridden by subclasses. This gives the derived class |
| 94 // opportunity to perform some cleanup. | 88 // an opportunity to perform some cleanup. |
| 95 virtual void GuestDestroyed() {} | 89 virtual void GuestDestroyed() {} |
| 96 | 90 |
| 91 // This method is called after this GuestViewBase has been initiated. | |
| 92 // | |
| 93 // This method can be overriden by subclasses. This gives the derived class | |
|
lazyboy
2014/06/17 23:46:45
"This method can be overriden by subclasses." -> I
Fady Samuel
2014/06/18 21:08:33
Done.
| |
| 94 // an opportunity to perform additional initialization. | |
| 95 virtual void Init() {} | |
| 96 | |
| 97 // This method queries whether drag-and-drop is enabled for this particular | 97 // 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 | 98 // view. By default, drag-and-drop is disabled. Derived classes can override |
| 99 // this behavior to enable drag-and-drop. | 99 // this behavior to enable drag-and-drop. |
| 100 virtual bool IsDragAndDropEnabled() const; | 100 virtual bool IsDragAndDropEnabled() const; |
| 101 | 101 |
| 102 // Once a guest WebContents is ready, this initiates the association of |this| | 102 // This method is called when the guest WebContents is about to be destroyed. |
| 103 // GuestView with |guest_web_contents|. | 103 // |
| 104 void Init(content::WebContents* guest_web_contents, | 104 // This method can be overridden by subclasses. This gives the derived class |
| 105 const std::string& embedder_extension_id); | 105 // an opportunity to perform some cleanup prior to destruction. |
| 106 virtual void WillDestroy() {} | |
| 107 | |
| 108 // This method is to be implemented by the derived class. Given a set of | |
| 109 // initialization parameters, a concrete subclass of GuestViewBase can | |
| 110 // create a specialized WebContents that it returns back to GuestViewBase. | |
| 111 typedef base::Callback<void(content::WebContents*)> WebContentsCallback; | |
|
lazyboy
2014/06/17 23:46:45
WebContentsCreatedCallback
Fady Samuel
2014/06/18 21:08:33
Done.
| |
| 112 virtual void CreateWebContents( | |
| 113 const std::string& embedder_extension_id, | |
| 114 content::RenderProcessHost* embedder_render_process_host, | |
| 115 const base::DictionaryValue& create_params, | |
| 116 const WebContentsCallback& callback) = 0; | |
| 117 | |
| 118 // This creates a WebContents and initializes |this| GuestViewBase to use the | |
| 119 // newly created WebContents. | |
| 120 void Init(const std::string& embedder_extension_id, | |
|
lazyboy
2014/06/17 23:46:45
I found this function very confusing with the virt
Fady Samuel
2014/06/18 21:08:33
This pattern is used elsewhere: See DidStopLoading
lazyboy
2014/06/19 16:17:31
Since the parameterless Init() is called after ini
Fady Samuel
2014/06/20 15:16:35
Good idea! Done!
| |
| 121 content::RenderProcessHost* embedder_render_process_host, | |
| 122 const base::DictionaryValue& create_params); | |
| 123 | |
| 124 void InitWithWebContents( | |
| 125 const std::string& embedder_extension_id, | |
| 126 content::RenderProcessHost* embedder_render_process_host, | |
| 127 content::WebContents* guest_web_contents); | |
| 106 | 128 |
| 107 bool IsViewType(const char* const view_type) const { | 129 bool IsViewType(const char* const view_type) const { |
| 108 return !strcmp(GetViewType(), view_type); | 130 return !strcmp(GetViewType(), view_type); |
| 109 } | 131 } |
| 110 | 132 |
| 111 base::WeakPtr<GuestViewBase> AsWeakPtr(); | 133 base::WeakPtr<GuestViewBase> AsWeakPtr(); |
| 112 | 134 |
| 135 bool initialized() const { return initialized_; } | |
| 136 | |
| 113 content::WebContents* embedder_web_contents() const { | 137 content::WebContents* embedder_web_contents() const { |
| 114 return embedder_web_contents_; | 138 return embedder_web_contents_; |
| 115 } | 139 } |
| 116 | 140 |
| 117 // Returns the guest WebContents. | 141 // Returns the guest WebContents. |
| 118 content::WebContents* guest_web_contents() const { | 142 content::WebContents* guest_web_contents() const { |
| 119 return web_contents(); | 143 return web_contents(); |
| 120 } | 144 } |
| 121 | 145 |
| 122 // Returns the extra parameters associated with this GuestView passed | 146 // Returns the extra parameters associated with this GuestView passed |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 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 |