extension_host.h revision f2477e01787aa58f445919b809d89e252beef54f
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(OS_ANDROID)
24#include "chrome/browser/ui/chrome_web_modal_dialog_manager_delegate.h"
25#include "components/web_modal/web_contents_modal_dialog_host.h"
26#endif
27
28class PrefsTabHelper;
29
30namespace content {
31class BrowserContext;
32class RenderProcessHost;
33class RenderWidgetHostView;
34class SiteInstance;
35}
36
37namespace extensions {
38class Extension;
39class WindowController;
40
41// This class is the browser component of an extension component's RenderView.
42// It handles setting up the renderer process, if needed, with special
43// privileges available to extensions.  It may have a view to be shown in the
44// browser UI, or it may be hidden.
45// TODO(jamescook): Move the ChromeWebModalDialogManagerDelegate interface to
46// ExtensionViewHost.
47class ExtensionHost : public content::WebContentsDelegate,
48#if !defined(OS_ANDROID)
49                      public ChromeWebModalDialogManagerDelegate,
50                      public web_modal::WebContentsModalDialogHost,
51#endif
52                      public content::WebContentsObserver,
53                      public ExtensionFunctionDispatcher::Delegate,
54                      public content::NotificationObserver {
55 public:
56  class ProcessCreationQueue;
57
58  ExtensionHost(const Extension* extension,
59                content::SiteInstance* site_instance,
60                const GURL& url, ViewType host_type);
61  virtual ~ExtensionHost();
62
63  const Extension* extension() const { return extension_; }
64  const std::string& extension_id() const { return extension_id_; }
65  content::WebContents* host_contents() const { return host_contents_.get(); }
66  content::RenderViewHost* render_view_host() const;
67  content::RenderProcessHost* render_process_host() const;
68  bool did_stop_loading() const { return did_stop_loading_; }
69  bool document_element_available() const {
70    return document_element_available_;
71  }
72
73  content::BrowserContext* browser_context() { return browser_context_; }
74
75  ViewType extension_host_type() const { return extension_host_type_; }
76  const GURL& GetURL() const;
77
78  // Returns true if the render view is initialized and didn't crash.
79  bool IsRenderViewLive() const;
80
81  // Prepares to initializes our RenderViewHost by creating its RenderView and
82  // navigating to this host's url. Uses host_view for the RenderViewHost's view
83  // (can be NULL). This happens delayed to avoid locking the UI.
84  void CreateRenderViewSoon();
85
86  // Notifications from the JavaScriptDialogManager when a dialog is being
87  // opened/closed.
88  void WillRunJavaScriptDialog();
89  void DidCloseJavaScriptDialog();
90
91  // content::WebContentsObserver
92  virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
93  virtual void RenderViewCreated(
94      content::RenderViewHost* render_view_host) OVERRIDE;
95  virtual void RenderViewDeleted(
96      content::RenderViewHost* render_view_host) OVERRIDE;
97  virtual void RenderViewReady() OVERRIDE;
98  virtual void RenderProcessGone(base::TerminationStatus status) OVERRIDE;
99  virtual void DocumentAvailableInMainFrame() OVERRIDE;
100  virtual void DidStopLoading(
101      content::RenderViewHost* render_view_host) OVERRIDE;
102
103  // content::WebContentsDelegate
104  virtual content::JavaScriptDialogManager*
105      GetJavaScriptDialogManager() OVERRIDE;
106  virtual content::ColorChooser* OpenColorChooser(
107      content::WebContents* web_contents, SkColor color) OVERRIDE;
108  virtual void RunFileChooser(
109      content::WebContents* tab,
110      const content::FileChooserParams& params) OVERRIDE;
111  virtual void AddNewContents(content::WebContents* source,
112                              content::WebContents* new_contents,
113                              WindowOpenDisposition disposition,
114                              const gfx::Rect& initial_pos,
115                              bool user_gesture,
116                              bool* was_blocked) OVERRIDE;
117  virtual void CloseContents(content::WebContents* contents) OVERRIDE;
118  virtual void RequestMediaAccessPermission(
119      content::WebContents* web_contents,
120      const content::MediaStreamRequest& request,
121      const content::MediaResponseCallback& callback) OVERRIDE;
122
123  // content::NotificationObserver
124  virtual void Observe(int type,
125                       const content::NotificationSource& source,
126                       const content::NotificationDetails& details) OVERRIDE;
127
128 protected:
129  // Called after the extension page finishes loading but before the
130  // EXTENSION_HOST_DID_STOP_LOADING notification is sent.
131  virtual void OnDidStopLoading();
132
133  // Called once when the document first becomes available.
134  virtual void OnDocumentAvailable();
135
136  // Returns true if we're hosting a background page.
137  virtual bool IsBackgroundPage() const;
138
139  // Closes this host (results in deletion).
140  void Close();
141
142 private:
143  friend class ProcessCreationQueue;
144
145  // Actually create the RenderView for this host. See CreateRenderViewSoon.
146  void CreateRenderViewNow();
147
148  // Navigates to the initial page.
149  void LoadInitialURL();
150
151#if !defined(OS_ANDROID)
152  // TODO(jamescook): Move this to ExtensionViewHost.
153  // ChromeWebModalDialogManagerDelegate
154  virtual web_modal::WebContentsModalDialogHost*
155      GetWebContentsModalDialogHost() OVERRIDE;
156
157  // web_modal::WebContentsModalDialogHost
158  virtual gfx::NativeView GetHostView() const OVERRIDE;
159  virtual gfx::Point GetDialogPosition(const gfx::Size& size) OVERRIDE;
160  virtual gfx::Size GetMaximumDialogSize() OVERRIDE;
161  virtual void AddObserver(
162      web_modal::ModalDialogHostObserver* observer) OVERRIDE;
163  virtual void RemoveObserver(
164      web_modal::ModalDialogHostObserver* observer) OVERRIDE;
165#endif
166
167  // Message handlers.
168  void OnRequest(const ExtensionHostMsg_Request_Params& params);
169  void OnEventAck();
170  void OnIncrementLazyKeepaliveCount();
171  void OnDecrementLazyKeepaliveCount();
172  void OnDetailedConsoleMessageAdded(
173      const base::string16& message,
174      const base::string16& source,
175      const StackTrace& stack_trace,
176      int32 severity_level);
177
178  // The extension that we're hosting in this view.
179  const Extension* extension_;
180
181  // Id of extension that we're hosting in this view.
182  const std::string extension_id_;
183
184  // The browser context that this host is tied to.
185  content::BrowserContext* browser_context_;
186
187  // Used to create dialog boxes.
188  // It must outlive host_contents_ as host_contents_ will access it
189  // during destruction.
190  scoped_ptr<content::JavaScriptDialogManager> dialog_manager_;
191
192  // The host for our HTML content.
193  scoped_ptr<content::WebContents> host_contents_;
194
195  // A weak pointer to the current or pending RenderViewHost. We don't access
196  // this through the host_contents because we want to deal with the pending
197  // host, so we can send messages to it before it finishes loading.
198  content::RenderViewHost* render_view_host_;
199
200  // Whether the RenderWidget has reported that it has stopped loading.
201  bool did_stop_loading_;
202
203  // True if the main frame has finished parsing.
204  bool document_element_available_;
205
206  // The original URL of the page being hosted.
207  GURL initial_url_;
208
209  content::NotificationRegistrar registrar_;
210
211  ExtensionFunctionDispatcher extension_function_dispatcher_;
212
213  // The type of view being hosted.
214  ViewType extension_host_type_;
215
216  // Used to measure how long it's been since the host was created.
217  base::ElapsedTimer since_created_;
218
219  DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
220};
221
222}  // namespace extensions
223
224#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_
225