shell.h revision 3551c9c881056c480085172ff9840cab31610854
1// Copyright 2013 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#ifndef CONTENT_SHELL_BROWSER_SHELL_H_
5#define CONTENT_SHELL_BROWSER_SHELL_H_
6
7
8#include <vector>
9
10#include "base/basictypes.h"
11#include "base/callback_forward.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/strings/string_piece.h"
14#include "content/public/browser/notification_observer.h"
15#include "content/public/browser/notification_registrar.h"
16#include "content/public/browser/web_contents_delegate.h"
17#include "ipc/ipc_channel.h"
18#include "ui/gfx/native_widget_types.h"
19#include "ui/gfx/size.h"
20
21#if defined(TOOLKIT_GTK)
22#include <gtk/gtk.h>
23#include "ui/base/gtk/gtk_signal.h"
24
25typedef struct _GtkToolItem GtkToolItem;
26#elif defined(OS_ANDROID)
27#include "base/android/scoped_java_ref.h"
28#elif defined(USE_AURA)
29#if defined(OS_CHROMEOS)
30namespace content {
31class MinimalShell;
32}
33#endif
34namespace views {
35class Widget;
36class ViewsDelegate;
37}
38#endif
39
40class GURL;
41namespace content {
42
43class BrowserContext;
44class ShellDevToolsFrontend;
45class ShellJavaScriptDialogManager;
46class SiteInstance;
47class WebContents;
48
49// This represents one window of the Content Shell, i.e. all the UI including
50// buttons and url bar, as well as the web content area.
51class Shell : public WebContentsDelegate,
52              public NotificationObserver {
53 public:
54  static const int kDefaultTestWindowWidthDip;
55  static const int kDefaultTestWindowHeightDip;
56
57  virtual ~Shell();
58
59  void LoadURL(const GURL& url);
60  void LoadURLForFrame(const GURL& url, const std::string& frame_name);
61  void GoBackOrForward(int offset);
62  void Reload();
63  void Stop();
64  void UpdateNavigationControls();
65  void Close();
66  void ShowDevTools();
67  void CloseDevTools();
68#if (defined(OS_WIN) && !defined(USE_AURA)) || \
69    defined(TOOLKIT_GTK) || defined(OS_MACOSX)
70  // Resizes the main window to the given dimensions.
71  void SizeTo(int width, int height);
72#endif
73
74  // Do one time initialization at application startup.
75  static void Initialize();
76
77  static Shell* CreateNewWindow(BrowserContext* browser_context,
78                                const GURL& url,
79                                SiteInstance* site_instance,
80                                int routing_id,
81                                const gfx::Size& initial_size);
82
83  // Returns the Shell object corresponding to the given RenderViewHost.
84  static Shell* FromRenderViewHost(RenderViewHost* rvh);
85
86  // Returns the currently open windows.
87  static std::vector<Shell*>& windows() { return windows_; }
88
89  // Closes all windows and returns. This runs a message loop.
90  static void CloseAllWindows();
91
92  // Closes all windows and exits.
93  static void PlatformExit();
94
95  // Used for content_browsertests. Called once.
96  static void SetShellCreatedCallback(
97      base::Callback<void(Shell*)> shell_created_callback);
98
99  WebContents* web_contents() const { return web_contents_.get(); }
100  gfx::NativeWindow window() { return window_; }
101
102#if defined(OS_MACOSX)
103  // Public to be called by an ObjC bridge object.
104  void ActionPerformed(int control);
105  void URLEntered(std::string url_string);
106#elif defined(OS_ANDROID)
107  // Registers the Android Java to native methods.
108  static bool Register(JNIEnv* env);
109#endif
110
111  // WebContentsDelegate
112  virtual WebContents* OpenURLFromTab(WebContents* source,
113                                      const OpenURLParams& params) OVERRIDE;
114  virtual void LoadingStateChanged(WebContents* source) OVERRIDE;
115#if defined(OS_ANDROID)
116  virtual void LoadProgressChanged(WebContents* source,
117                                   double progress) OVERRIDE;
118#endif
119  virtual void ToggleFullscreenModeForTab(WebContents* web_contents,
120                                          bool enter_fullscreen) OVERRIDE;
121  virtual bool IsFullscreenForTabOrPending(
122      const WebContents* web_contents) const OVERRIDE;
123  virtual void RequestToLockMouse(WebContents* web_contents,
124                                  bool user_gesture,
125                                  bool last_unlocked_by_target) OVERRIDE;
126  virtual void CloseContents(WebContents* source) OVERRIDE;
127  virtual bool CanOverscrollContent() const OVERRIDE;
128  virtual void WebContentsCreated(WebContents* source_contents,
129                                  int64 source_frame_id,
130                                  const string16& frame_name,
131                                  const GURL& target_url,
132                                  WebContents* new_contents) OVERRIDE;
133  virtual void DidNavigateMainFramePostCommit(
134      WebContents* web_contents) OVERRIDE;
135  virtual JavaScriptDialogManager* GetJavaScriptDialogManager() OVERRIDE;
136#if defined(OS_MACOSX)
137  virtual void HandleKeyboardEvent(
138      WebContents* source,
139      const NativeWebKeyboardEvent& event) OVERRIDE;
140#endif
141  virtual bool AddMessageToConsole(WebContents* source,
142                                   int32 level,
143                                   const string16& message,
144                                   int32 line_no,
145                                   const string16& source_id) OVERRIDE;
146  virtual void RendererUnresponsive(WebContents* source) OVERRIDE;
147  virtual void ActivateContents(WebContents* contents) OVERRIDE;
148  virtual void DeactivateContents(WebContents* contents) OVERRIDE;
149  virtual void WorkerCrashed(WebContents* source) OVERRIDE;
150
151 private:
152  enum UIControl {
153    BACK_BUTTON,
154    FORWARD_BUTTON,
155    STOP_BUTTON
156  };
157
158  class DevToolsWebContentsObserver;
159
160  explicit Shell(WebContents* web_contents);
161
162  // Helper to create a new Shell given a newly created WebContents.
163  static Shell* CreateShell(WebContents* web_contents,
164                            const gfx::Size& initial_size);
165
166  // Helper for one time initialization of application
167  static void PlatformInitialize(const gfx::Size& default_window_size);
168
169  // All the methods that begin with Platform need to be implemented by the
170  // platform specific Shell implementation.
171  // Called from the destructor to let each platform do any necessary cleanup.
172  void PlatformCleanUp();
173  // Creates the main window GUI.
174  void PlatformCreateWindow(int width, int height);
175  // Links the WebContents into the newly created window.
176  void PlatformSetContents();
177  // Resize the content area and GUI.
178  void PlatformResizeSubViews();
179  // Enable/disable a button.
180  void PlatformEnableUIControl(UIControl control, bool is_enabled);
181  // Updates the url in the url bar.
182  void PlatformSetAddressBarURL(const GURL& url);
183  // Sets whether the spinner is spinning.
184  void PlatformSetIsLoading(bool loading);
185  // Set the title of shell window
186  void PlatformSetTitle(const string16& title);
187#if defined(OS_ANDROID)
188  void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
189                                          bool enter_fullscreen);
190  bool PlatformIsFullscreenForTabOrPending(
191      const WebContents* web_contents) const;
192#endif
193
194  gfx::NativeView GetContentView();
195
196  // NotificationObserver
197  virtual void Observe(int type,
198                       const NotificationSource& source,
199                       const NotificationDetails& details) OVERRIDE;
200
201  void OnDevToolsWebContentsDestroyed();
202
203#if defined(OS_WIN) && !defined(USE_AURA)
204  static ATOM RegisterWindowClass();
205  static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
206  static LRESULT CALLBACK EditWndProc(HWND, UINT, WPARAM, LPARAM);
207#elif defined(TOOLKIT_GTK)
208  CHROMEGTK_CALLBACK_0(Shell, void, OnBackButtonClicked);
209  CHROMEGTK_CALLBACK_0(Shell, void, OnForwardButtonClicked);
210  CHROMEGTK_CALLBACK_0(Shell, void, OnReloadButtonClicked);
211  CHROMEGTK_CALLBACK_0(Shell, void, OnStopButtonClicked);
212  CHROMEGTK_CALLBACK_0(Shell, void, OnURLEntryActivate);
213  CHROMEGTK_CALLBACK_0(Shell, gboolean, OnWindowDestroyed);
214
215  CHROMEG_CALLBACK_3(Shell, gboolean, OnCloseWindowKeyPressed, GtkAccelGroup*,
216                     GObject*, guint, GdkModifierType);
217  CHROMEG_CALLBACK_3(Shell, gboolean, OnNewWindowKeyPressed, GtkAccelGroup*,
218                     GObject*, guint, GdkModifierType);
219  CHROMEG_CALLBACK_3(Shell, gboolean, OnHighlightURLView, GtkAccelGroup*,
220                     GObject*, guint, GdkModifierType);
221  CHROMEG_CALLBACK_3(Shell, gboolean, OnReloadKeyPressed, GtkAccelGroup*,
222                     GObject*, guint, GdkModifierType);
223#endif
224
225  scoped_ptr<ShellJavaScriptDialogManager> dialog_manager_;
226
227  scoped_ptr<WebContents> web_contents_;
228
229  scoped_ptr<DevToolsWebContentsObserver> devtools_observer_;
230  ShellDevToolsFrontend* devtools_frontend_;
231
232  bool is_fullscreen_;
233
234  gfx::NativeWindow window_;
235  gfx::NativeEditView url_edit_view_;
236
237  // Notification manager
238  NotificationRegistrar registrar_;
239
240#if defined(OS_WIN) && !defined(USE_AURA)
241  WNDPROC default_edit_wnd_proc_;
242  static HINSTANCE instance_handle_;
243#elif defined(TOOLKIT_GTK)
244  GtkWidget* vbox_;
245
246  GtkToolItem* back_button_;
247  GtkToolItem* forward_button_;
248  GtkToolItem* reload_button_;
249  GtkToolItem* stop_button_;
250
251  GtkWidget* spinner_;
252  GtkToolItem* spinner_item_;
253
254  int content_width_;
255  int content_height_;
256  int ui_elements_height_; // height of menubar, toolbar, etc.
257#elif defined(OS_ANDROID)
258  base::android::ScopedJavaGlobalRef<jobject> java_object_;
259#elif defined(USE_AURA)
260#if defined(OS_CHROMEOS)
261  static content::MinimalShell* minimal_shell_;
262#endif
263  static views::ViewsDelegate* views_delegate_;
264
265  views::Widget* window_widget_;
266#elif defined(OS_MACOSX)
267  int content_width_;
268  int content_height_;
269#endif
270
271  bool headless_;
272
273  // A container of all the open windows. We use a vector so we can keep track
274  // of ordering.
275  static std::vector<Shell*> windows_;
276
277  static base::Callback<void(Shell*)> shell_created_callback_;
278
279  // True if the destructur of Shell should post a quit closure on the current
280  // message loop if the destructed Shell object was the last one.
281  static bool quit_message_loop_;
282};
283
284}  // namespace content
285
286#endif  // CONTENT_SHELL_BROWSER_SHELL_H_
287