- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / extension_host.h
1 // Copyright (c) 2012 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_EXTENSIONS_EXTENSION_HOST_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/logging.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/timer/elapsed_timer.h"
14 #include "chrome/browser/extensions/extension_function_dispatcher.h"
15 #include "content/public/browser/javascript_dialog_manager.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "extensions/common/stack_frame.h"
21 #include "extensions/common/view_type.h"
22
23 #if defined(TOOLKIT_VIEWS)
24 #include "chrome/browser/ui/views/extensions/extension_view_views.h"
25 #elif defined(OS_MACOSX)
26 #include "chrome/browser/ui/cocoa/extensions/extension_view_mac.h"
27 #elif defined(TOOLKIT_GTK)
28 #include "chrome/browser/ui/gtk/extensions/extension_view_gtk.h"
29 #elif defined(OS_ANDROID)
30 #include "chrome/browser/ui/android/extensions/extension_view_android.h"
31 #endif
32
33 #if !defined(OS_ANDROID)
34 #include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
35 #include "components/web_modal/web_contents_modal_dialog_host.h"
36 #endif
37
38 class Browser;
39 class PrefsTabHelper;
40
41 namespace content {
42 class BrowserContext;
43 class RenderProcessHost;
44 class RenderWidgetHostView;
45 class SiteInstance;
46 }
47
48 namespace extensions {
49 class Extension;
50 class WindowController;
51
52 // This class is the browser component of an extension component's RenderView.
53 // It handles setting up the renderer process, if needed, with special
54 // privileges available to extensions.  It may have a view to be shown in the
55 // browser UI, or it may be hidden.
56 class ExtensionHost : public content::WebContentsDelegate,
57 #if !defined(OS_ANDROID)
58                       public ChromeWebModalDialogManagerDelegate,
59                       public web_modal::WebContentsModalDialogHost,
60 #endif
61                       public content::WebContentsObserver,
62                       public ExtensionFunctionDispatcher::Delegate,
63                       public content::NotificationObserver {
64  public:
65   class ProcessCreationQueue;
66
67 #if defined(TOOLKIT_VIEWS)
68   typedef ExtensionViewViews PlatformExtensionView;
69 #elif defined(OS_MACOSX)
70   typedef ExtensionViewMac PlatformExtensionView;
71 #elif defined(TOOLKIT_GTK)
72   typedef ExtensionViewGtk PlatformExtensionView;
73 #elif defined(OS_ANDROID)
74   // Android does not support extensions.
75   typedef ExtensionViewAndroid PlatformExtensionView;
76 #endif
77
78   ExtensionHost(const Extension* extension,
79                 content::SiteInstance* site_instance,
80                 const GURL& url, ViewType host_type);
81   virtual ~ExtensionHost();
82
83 #if defined(TOOLKIT_VIEWS)
84   void set_view(PlatformExtensionView* view) { view_.reset(view); }
85 #endif
86
87   const PlatformExtensionView* view() const {
88 #if defined(OS_ANDROID)
89     NOTREACHED();
90 #endif
91     return view_.get();
92   }
93
94   PlatformExtensionView* view() {
95 #if defined(OS_ANDROID)
96     NOTREACHED();
97 #endif
98     return view_.get();
99   }
100
101   // Create an ExtensionView and tie it to this host and |browser|.  Note NULL
102   // is a valid argument for |browser|.  Extension views may be bound to
103   // tab-contents hosted in ExternalTabContainer objects, which do not
104   // instantiate Browser objects.
105   void CreateView(Browser* browser);
106
107   const Extension* extension() const { return extension_; }
108   const std::string& extension_id() const { return extension_id_; }
109   content::WebContents* host_contents() const { return host_contents_.get(); }
110   content::RenderViewHost* render_view_host() const;
111   content::RenderProcessHost* render_process_host() const;
112   bool did_stop_loading() const { return did_stop_loading_; }
113   bool document_element_available() const {
114     return document_element_available_;
115   }
116
117   Profile* profile() const { return profile_; }
118
119   // Returns the same value as profile() but as a BrowserContext. Implemented
120   // in the .cc file to avoid including profile.h in this header.
121   content::BrowserContext* browser_context();
122
123   ViewType extension_host_type() const { return extension_host_type_; }
124   const GURL& GetURL() const;
125
126   // ExtensionFunctionDispatcher::Delegate
127   virtual content::WebContents* GetAssociatedWebContents() const OVERRIDE;
128   virtual content::WebContents* GetVisibleWebContents() const OVERRIDE;
129   void SetAssociatedWebContents(content::WebContents* web_contents);
130
131   // Returns true if the render view is initialized and didn't crash.
132   bool IsRenderViewLive() const;
133
134   // Prepares to initializes our RenderViewHost by creating its RenderView and
135   // navigating to this host's url. Uses host_view for the RenderViewHost's view
136   // (can be NULL). This happens delayed to avoid locking the UI.
137   void CreateRenderViewSoon();
138
139   // Insert a default style sheet for Extension Infobars.
140   void InsertInfobarCSS();
141
142   // Notifications from the JavaScriptDialogManager when a dialog is being
143   // opened/closed.
144   void WillRunJavaScriptDialog();
145   void DidCloseJavaScriptDialog();
146
147   // content::WebContentsObserver
148   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
149   virtual void RenderViewCreated(
150       content::RenderViewHost* render_view_host) OVERRIDE;
151   virtual void RenderViewDeleted(
152       content::RenderViewHost* render_view_host) OVERRIDE;
153   virtual void RenderViewReady() OVERRIDE;
154   virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
155   virtual void DocumentAvailableInMainFrame() OVERRIDE;
156   virtual void DidStopLoading(
157       content::RenderViewHost* render_view_host) OVERRIDE;
158
159   // content::WebContentsDelegate
160   virtual content::WebContents* OpenURLFromTab(
161       content::WebContents* source,
162       const content::OpenURLParams& params) OVERRIDE;
163   virtual bool PreHandleKeyboardEvent(
164       content::WebContents* source,
165       const content::NativeWebKeyboardEvent& event,
166       bool* is_keyboard_shortcut) OVERRIDE;
167   virtual void HandleKeyboardEvent(
168       content::WebContents* source,
169       const content::NativeWebKeyboardEvent& event) OVERRIDE;
170   virtual void ResizeDueToAutoResize(content::WebContents* source,
171                                      const gfx::Size& new_size) OVERRIDE;
172   virtual content::JavaScriptDialogManager*
173       GetJavaScriptDialogManager() OVERRIDE;
174   virtual content::ColorChooser* OpenColorChooser(
175       content::WebContents* web_contents, SkColor color) OVERRIDE;
176   virtual void RunFileChooser(
177       content::WebContents* tab,
178       const content::FileChooserParams& params) OVERRIDE;
179   virtual void AddNewContents(content::WebContents* source,
180                               content::WebContents* new_contents,
181                               WindowOpenDisposition disposition,
182                               const gfx::Rect& initial_pos,
183                               bool user_gesture,
184                               bool* was_blocked) OVERRIDE;
185   virtual void CloseContents(content::WebContents* contents) OVERRIDE;
186   virtual void RequestMediaAccessPermission(
187       content::WebContents* web_contents,
188       const content::MediaStreamRequest& request,
189       const content::MediaResponseCallback& callback) OVERRIDE;
190
191   // content::NotificationObserver
192   virtual void Observe(int type,
193                        const content::NotificationSource& source,
194                        const content::NotificationDetails& details) OVERRIDE;
195
196  private:
197   friend class ProcessCreationQueue;
198
199   // Actually create the RenderView for this host. See CreateRenderViewSoon.
200   void CreateRenderViewNow();
201
202   // Navigates to the initial page.
203   void LoadInitialURL();
204
205   // Closes this host (results in deletion).
206   void Close();
207
208 #if !defined(OS_ANDROID)
209   // ChromeWebModalDialogManagerDelegate
210   virtual web_modal::WebContentsModalDialogHost*
211       GetWebContentsModalDialogHost() OVERRIDE;
212
213   // web_modal::WebContentsModalDialogHost
214   virtual gfx::NativeView GetHostView() const OVERRIDE;
215   virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
216   virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
217   virtual void AddObserver(
218       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
219   virtual void RemoveObserver(
220       web_modal::ModalDialogHostObserver* observer) OVERRIDE;
221 #endif
222
223   // ExtensionFunctionDispatcher::Delegate
224   virtual WindowController* GetExtensionWindowController() const OVERRIDE;
225
226   // Message handlers.
227   void OnRequest(const ExtensionHostMsg_Request_Params& params);
228   void OnEventAck();
229   void OnIncrementLazyKeepaliveCount();
230   void OnDecrementLazyKeepaliveCount();
231   void OnDetailedConsoleMessageAdded(
232       const base::string16& message,
233       const base::string16& source,
234       const StackTrace& stack_trace,
235       int32 severity_level);
236
237   // Handles keyboard events that were not handled by HandleKeyboardEvent().
238   // Platform specific implementation may override this method to handle the
239   // event in platform specific way.
240   virtual void UnhandledKeyboardEvent(
241       content::WebContents* source,
242       const content::NativeWebKeyboardEvent& event);
243
244   // Returns true if we're hosting a background page.
245   // This isn't valid until CreateRenderView is called.
246   bool is_background_page() const { return !view(); }
247
248   // The extension that we're hosting in this view.
249   const Extension* extension_;
250
251   // Id of extension that we're hosting in this view.
252   const std::string extension_id_;
253
254   // The profile that this host is tied to.
255   Profile* profile_;
256
257   // Optional view that shows the rendered content in the UI.
258   scoped_ptr<PlatformExtensionView> view_;
259
260   // Used to create dialog boxes.
261   // It must outlive host_contents_ as host_contents_ will access it
262   // during destruction.
263   scoped_ptr<content::JavaScriptDialogManager> dialog_manager_;
264
265   // The host for our HTML content.
266   scoped_ptr<content::WebContents> host_contents_;
267
268   // A weak pointer to the current or pending RenderViewHost. We don't access
269   // this through the host_contents because we want to deal with the pending
270   // host, so we can send messages to it before it finishes loading.
271   content::RenderViewHost* render_view_host_;
272
273   // Whether the RenderWidget has reported that it has stopped loading.
274   bool did_stop_loading_;
275
276   // True if the main frame has finished parsing.
277   bool document_element_available_;
278
279   // The original URL of the page being hosted.
280   GURL initial_url_;
281
282   content::NotificationRegistrar registrar_;
283
284   ExtensionFunctionDispatcher extension_function_dispatcher_;
285
286   // The type of view being hosted.
287   ViewType extension_host_type_;
288
289   // The relevant WebContents associated with this ExtensionHost, if any.
290   content::WebContents* associated_web_contents_;
291
292   // Used to measure how long it's been since the host was created.
293   base::ElapsedTimer since_created_;
294
295   DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
296 };
297
298 }  // namespace extensions
299
300 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_