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