extension_host.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1// Copyright (c) 2010 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
10#include "base/perftimer.h"
11#include "base/scoped_ptr.h"
12#include "chrome/browser/extensions/extension_function_dispatcher.h"
13#include "chrome/browser/jsmessage_box_client.h"
14#include "chrome/browser/renderer_host/render_view_host_delegate.h"
15#include "chrome/browser/tab_contents/render_view_host_delegate_helper.h"
16#if defined(TOOLKIT_VIEWS)
17#include "chrome/browser/views/extensions/extension_view.h"
18#elif defined(OS_MACOSX)
19#include "chrome/browser/cocoa/extension_view_mac.h"
20#elif defined(TOOLKIT_GTK)
21#include "chrome/browser/gtk/extension_view_gtk.h"
22#endif
23#include "chrome/common/notification_registrar.h"
24
25
26class Browser;
27class Extension;
28class ExtensionProcessManager;
29class RenderProcessHost;
30class RenderWidgetHost;
31class RenderWidgetHostView;
32class TabContents;
33struct WebPreferences;
34
35// This class is the browser component of an extension component's RenderView.
36// It handles setting up the renderer process, if needed, with special
37// privileges available to extensions.  It may have a view to be shown in the
38// in the browser UI, or it may be hidden.
39class ExtensionHost : public RenderViewHostDelegate,
40                      public RenderViewHostDelegate::View,
41                      public ExtensionFunctionDispatcher::Delegate,
42                      public NotificationObserver,
43                      public JavaScriptMessageBoxClient {
44 public:
45  class ProcessCreationQueue;
46
47  // Enable DOM automation in created render view hosts.
48  static void EnableDOMAutomation() { enable_dom_automation_ = true; }
49
50  ExtensionHost(Extension* extension, SiteInstance* site_instance,
51                const GURL& url, ViewType::Type host_type);
52  ~ExtensionHost();
53
54#if defined(TOOLKIT_VIEWS)
55  void set_view(ExtensionView* view) { view_.reset(view); }
56  ExtensionView* view() const { return view_.get(); }
57#elif defined(OS_MACOSX)
58  ExtensionViewMac* view() const { return view_.get(); }
59#elif defined(TOOLKIT_GTK)
60  ExtensionViewGtk* view() const { return view_.get(); }
61#else
62  // TODO(port): implement
63  void* view() const { return NULL; }
64#endif
65
66  // Create an ExtensionView and tie it to this host and |browser|.  Note NULL
67  // is a valid argument for |browser|.  Extension views may be bound to
68  // tab-contents hosted in ExternalTabContainer objects, which do not
69  // instantiate Browser objects.
70  void CreateView(Browser* browser);
71
72  Extension* extension() { return extension_; }
73  RenderViewHost* render_view_host() const { return render_view_host_; }
74  RenderProcessHost* render_process_host() const;
75  SiteInstance* site_instance() const;
76  bool did_stop_loading() const { return did_stop_loading_; }
77  bool document_element_available() const {
78    return document_element_available_;
79  }
80
81  Profile* profile() const { return profile_; }
82
83  ViewType::Type extension_host_type() const { return extension_host_type_; }
84
85  // ExtensionFunctionDispatcher::Delegate
86  virtual TabContents* associated_tab_contents() {
87    return associated_tab_contents_;
88  }
89  void set_associated_tab_contents(TabContents* associated_tab_contents) {
90    associated_tab_contents_ = associated_tab_contents;
91  }
92
93  // Sets the the ViewType of this host (e.g. mole, toolstrip).
94  void SetRenderViewType(ViewType::Type type);
95
96  // Returns true if the render view is initialized and didn't crash.
97  bool IsRenderViewLive() const;
98
99  // Prepares to initializes our RenderViewHost by creating its RenderView and
100  // navigating to this host's url. Uses host_view for the RenderViewHost's view
101  // (can be NULL). This happens delayed to avoid locking the UI.
102  void CreateRenderViewSoon(RenderWidgetHostView* host_view);
103
104  // Sets |url_| and navigates |render_view_host_|.
105  void NavigateToURL(const GURL& url);
106
107  // Insert a default style sheet for Extension Infobars.
108  void InsertInfobarCSS();
109
110  // Insert the theme CSS for a toolstrip/mole.
111  void InsertThemedToolstripCSS();
112
113  // Tell the renderer not to draw scrollbars on windows smaller than
114  // |size_limit| in both width and height.
115  void DisableScrollbarsForSmallWindows(const gfx::Size& size_limit);
116
117  // RenderViewHostDelegate implementation.
118  virtual RenderViewHostDelegate::View* GetViewDelegate();
119  virtual const GURL& GetURL() const { return url_; }
120  virtual void RenderViewCreated(RenderViewHost* render_view_host);
121  virtual ViewType::Type GetRenderViewType() const;
122  virtual int GetBrowserWindowID() const;
123  virtual void RenderViewGone(RenderViewHost* render_view_host);
124  virtual void DidNavigate(RenderViewHost* render_view_host,
125                           const ViewHostMsg_FrameNavigate_Params& params);
126  virtual void DidStopLoading();
127  virtual void DocumentAvailableInMainFrame(RenderViewHost* render_view_host);
128  virtual void DocumentOnLoadCompletedInMainFrame(
129      RenderViewHost* render_view_host);
130
131  virtual WebPreferences GetWebkitPrefs();
132  virtual void ProcessDOMUIMessage(const std::string& message,
133                                   const ListValue* content,
134                                   const GURL& source_url,
135                                   int request_id,
136                                   bool has_callback);
137  virtual void RunJavaScriptMessage(const std::wstring& message,
138                                    const std::wstring& default_prompt,
139                                    const GURL& frame_url,
140                                    const int flags,
141                                    IPC::Message* reply_msg,
142                                    bool* did_suppress_message);
143  virtual void Close(RenderViewHost* render_view_host);
144  virtual RendererPreferences GetRendererPrefs(Profile* profile) const;
145
146  // RenderViewHostDelegate::View
147  virtual void CreateNewWindow(
148      int route_id,
149      WindowContainerType window_container_type,
150      const string16& frame_name);
151  virtual void CreateNewWidget(int route_id, WebKit::WebPopupType popup_type);
152  virtual void ShowCreatedWindow(int route_id,
153                                 WindowOpenDisposition disposition,
154                                 const gfx::Rect& initial_pos,
155                                 bool user_gesture);
156  virtual void ShowCreatedWidget(int route_id,
157                                 const gfx::Rect& initial_pos);
158  virtual void ShowContextMenu(const ContextMenuParams& params);
159  virtual void StartDragging(const WebDropData& drop_data,
160                             WebKit::WebDragOperationsMask allowed_operations,
161                             const SkBitmap& image,
162                             const gfx::Point& image_offset);
163  virtual void UpdateDragCursor(WebKit::WebDragOperation operation);
164  virtual void GotFocus();
165  virtual void TakeFocus(bool reverse);
166  virtual bool PreHandleKeyboardEvent(const NativeWebKeyboardEvent& event,
167                                      bool* is_keyboard_shortcut);
168  virtual void HandleKeyboardEvent(const NativeWebKeyboardEvent& event);
169  virtual void HandleMouseEvent();
170  virtual void HandleMouseLeave();
171  virtual void UpdatePreferredSize(const gfx::Size& new_size);
172
173  // NotificationObserver
174  virtual void Observe(NotificationType type,
175                       const NotificationSource& source,
176                       const NotificationDetails& details);
177
178  // JavaScriptMessageBoxClient
179  virtual std::wstring GetMessageBoxTitle(const GURL& frame_url,
180                                          bool is_alert);
181  virtual gfx::NativeWindow GetMessageBoxRootWindow();
182  virtual void OnMessageBoxClosed(IPC::Message* reply_msg,
183                                  bool success,
184                                  const std::wstring& prompt);
185  virtual void SetSuppressMessageBoxes(bool suppress_message_boxes) {}
186  virtual TabContents* AsTabContents() { return NULL; }
187  virtual ExtensionHost* AsExtensionHost() { return this; }
188
189 protected:
190  // Internal functions used to support the CreateNewWidget() method. If a
191  // platform requires plugging into widget creation at a lower level, then a
192  // subclass might want to override these functions, but otherwise they should
193  // be fine just implementing RenderWidgetHostView::InitAsPopup().
194  //
195  // The Create function returns the newly created widget so it can be
196  // associated with the given route. When the widget needs to be shown later,
197  // we'll look it up again and pass the object to the Show functions rather
198  // than the route ID.
199  virtual RenderWidgetHostView* CreateNewWidgetInternal(
200      int route_id,
201      WebKit::WebPopupType popup_type);
202  virtual void ShowCreatedWidgetInternal(RenderWidgetHostView* widget_host_view,
203                                         const gfx::Rect& initial_pos);
204 private:
205  friend class ProcessCreationQueue;
206
207  // Whether to allow DOM automation for created RenderViewHosts. This is used
208  // for testing.
209  static bool enable_dom_automation_;
210
211  // Actually create the RenderView for this host. See CreateRenderViewSoon.
212  void CreateRenderViewNow();
213
214  // ExtensionFunctionDispatcher::Delegate
215  virtual Browser* GetBrowser() const {
216    return view() ? view()->browser() : NULL;
217  }
218  virtual gfx::NativeView GetNativeViewOfHost() {
219    return view() ? view()->native_view() : NULL;
220  }
221
222  // Handles keyboard events that were not handled by HandleKeyboardEvent().
223  // Platform specific implementation may override this method to handle the
224  // event in platform specific way.
225  virtual void UnhandledKeyboardEvent(const NativeWebKeyboardEvent& event) {}
226
227  // Returns true if we're hosting a background page.
228  // This isn't valid until CreateRenderView is called.
229  bool is_background_page() const { return !view(); }
230
231  // The extension that we're hosting in this view.
232  Extension* extension_;
233
234  // The profile that this host is tied to.
235  Profile* profile_;
236
237  // Optional view that shows the rendered content in the UI.
238#if defined(TOOLKIT_VIEWS)
239  scoped_ptr<ExtensionView> view_;
240#elif defined(OS_MACOSX)
241  scoped_ptr<ExtensionViewMac> view_;
242#elif defined(TOOLKIT_GTK)
243  scoped_ptr<ExtensionViewGtk> view_;
244#endif
245
246  // The host for our HTML content.
247  RenderViewHost* render_view_host_;
248
249  // Common implementations of some RenderViewHostDelegate::View methods.
250  RenderViewHostDelegateViewHelper delegate_view_helper_;
251
252  // Whether the RenderWidget has reported that it has stopped loading.
253  bool did_stop_loading_;
254
255  // True if the main frame has finished parsing.
256  bool document_element_available_;
257
258  // The URL being hosted.
259  GURL url_;
260
261  NotificationRegistrar registrar_;
262
263  scoped_ptr<ExtensionFunctionDispatcher> extension_function_dispatcher_;
264
265  // Only EXTENSION_TOOLSTRIP, EXTENSION_POPUP, and EXTENSION_BACKGROUND_PAGE
266  // are used here, others are not hosted by ExtensionHost.
267  ViewType::Type extension_host_type_;
268
269  // The relevant TabContents associated with this ExtensionHost, if any.
270  TabContents* associated_tab_contents_;
271
272  // Used to measure how long it's been since the host was created.
273  PerfTimer since_created_;
274
275  DISALLOW_COPY_AND_ASSIGN(ExtensionHost);
276};
277
278#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_HOST_H_
279