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