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