shell.h revision effb81e5f8246d0db0270817048dc992db66e9fb
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(bool to_different_document);
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,
123                                   bool to_different_document) OVERRIDE;
124#if defined(OS_ANDROID)
125  virtual void LoadProgressChanged(WebContents* source,
126                                   double progress) OVERRIDE;
127#endif
128  virtual void ToggleFullscreenModeForTab(WebContents* web_contents,
129                                          bool enter_fullscreen) OVERRIDE;
130  virtual bool IsFullscreenForTabOrPending(
131      const WebContents* web_contents) const OVERRIDE;
132  virtual void RequestToLockMouse(WebContents* web_contents,
133                                  bool user_gesture,
134                                  bool last_unlocked_by_target) OVERRIDE;
135  virtual void CloseContents(WebContents* source) OVERRIDE;
136  virtual bool CanOverscrollContent() const OVERRIDE;
137  virtual void DidNavigateMainFramePostCommit(
138      WebContents* web_contents) OVERRIDE;
139  virtual JavaScriptDialogManager* GetJavaScriptDialogManager() OVERRIDE;
140#if defined(OS_MACOSX)
141  virtual void HandleKeyboardEvent(
142      WebContents* source,
143      const NativeWebKeyboardEvent& event) OVERRIDE;
144#endif
145  virtual bool AddMessageToConsole(WebContents* source,
146                                   int32 level,
147                                   const base::string16& message,
148                                   int32 line_no,
149                                   const base::string16& source_id) OVERRIDE;
150  virtual void RendererUnresponsive(WebContents* source) OVERRIDE;
151  virtual void ActivateContents(WebContents* contents) OVERRIDE;
152  virtual void DeactivateContents(WebContents* contents) OVERRIDE;
153  virtual void WorkerCrashed(WebContents* source) OVERRIDE;
154  virtual bool HandleContextMenu(const content::ContextMenuParams& params)
155      OVERRIDE;
156  virtual void WebContentsFocused(WebContents* contents) OVERRIDE;
157
158 private:
159  enum UIControl {
160    BACK_BUTTON,
161    FORWARD_BUTTON,
162    STOP_BUTTON
163  };
164
165  class DevToolsWebContentsObserver;
166
167  explicit Shell(WebContents* web_contents);
168
169  // Helper to create a new Shell given a newly created WebContents.
170  static Shell* CreateShell(WebContents* web_contents,
171                            const gfx::Size& initial_size);
172
173  // Helper for one time initialization of application
174  static void PlatformInitialize(const gfx::Size& default_window_size);
175  // Helper for one time deinitialization of platform specific state.
176  static void PlatformExit();
177
178  // Adjust the size when Blink sends 0 for width and/or height.
179  // This happens when Blink requests a default-sized window.
180  static gfx::Size AdjustWindowSize(const gfx::Size& initial_size);
181
182  // All the methods that begin with Platform need to be implemented by the
183  // platform specific Shell implementation.
184  // Called from the destructor to let each platform do any necessary cleanup.
185  void PlatformCleanUp();
186  // Creates the main window GUI.
187  void PlatformCreateWindow(int width, int height);
188  // Links the WebContents into the newly created window.
189  void PlatformSetContents();
190  // Resize the content area and GUI.
191  void PlatformResizeSubViews();
192  // Enable/disable a button.
193  void PlatformEnableUIControl(UIControl control, bool is_enabled);
194  // Updates the url in the url bar.
195  void PlatformSetAddressBarURL(const GURL& url);
196  // Sets whether the spinner is spinning.
197  void PlatformSetIsLoading(bool loading);
198  // Set the title of shell window
199  void PlatformSetTitle(const base::string16& title);
200  // User right-clicked on the web view
201  bool PlatformHandleContextMenu(const content::ContextMenuParams& params);
202#if defined(OS_ANDROID)
203  void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
204                                          bool enter_fullscreen);
205  bool PlatformIsFullscreenForTabOrPending(
206      const WebContents* web_contents) const;
207#endif
208#if defined(TOOLKIT_VIEWS)
209  void PlatformWebContentsFocused(WebContents* contents);
210#endif
211
212  gfx::NativeView GetContentView();
213
214  // WebContentsObserver
215  virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) OVERRIDE;
216
217  void InnerShowDevTools(const std::string& settings);
218  void OnDevToolsWebContentsDestroyed();
219
220#if defined(TOOLKIT_GTK)
221  CHROMEGTK_CALLBACK_0(Shell, void, OnBackButtonClicked);
222  CHROMEGTK_CALLBACK_0(Shell, void, OnForwardButtonClicked);
223  CHROMEGTK_CALLBACK_0(Shell, void, OnReloadButtonClicked);
224  CHROMEGTK_CALLBACK_0(Shell, void, OnStopButtonClicked);
225  CHROMEGTK_CALLBACK_0(Shell, void, OnURLEntryActivate);
226  CHROMEGTK_CALLBACK_0(Shell, gboolean, OnWindowDestroyed);
227
228  CHROMEG_CALLBACK_3(Shell, gboolean, OnCloseWindowKeyPressed, GtkAccelGroup*,
229                     GObject*, guint, GdkModifierType);
230  CHROMEG_CALLBACK_3(Shell, gboolean, OnNewWindowKeyPressed, GtkAccelGroup*,
231                     GObject*, guint, GdkModifierType);
232  CHROMEG_CALLBACK_3(Shell, gboolean, OnHighlightURLView, GtkAccelGroup*,
233                     GObject*, guint, GdkModifierType);
234  CHROMEG_CALLBACK_3(Shell, gboolean, OnReloadKeyPressed, GtkAccelGroup*,
235                     GObject*, guint, GdkModifierType);
236#endif
237
238  scoped_ptr<ShellJavaScriptDialogManager> dialog_manager_;
239
240  scoped_ptr<WebContents> web_contents_;
241
242  scoped_ptr<DevToolsWebContentsObserver> devtools_observer_;
243  ShellDevToolsFrontend* devtools_frontend_;
244
245  bool is_fullscreen_;
246
247  gfx::NativeWindow window_;
248  gfx::NativeEditView url_edit_view_;
249
250  gfx::Size content_size_;
251
252#if defined(TOOLKIT_GTK)
253  GtkWidget* vbox_;
254
255  GtkToolItem* back_button_;
256  GtkToolItem* forward_button_;
257  GtkToolItem* reload_button_;
258  GtkToolItem* stop_button_;
259
260  GtkWidget* spinner_;
261  GtkToolItem* spinner_item_;
262
263  int ui_elements_height_; // height of menubar, toolbar, etc.
264#elif defined(OS_ANDROID)
265  base::android::ScopedJavaGlobalRef<jobject> java_object_;
266#elif defined(USE_AURA)
267#if defined(OS_CHROMEOS)
268  static wm::WMTestHelper* wm_test_helper_;
269#endif
270#if defined(TOOLKIT_VIEWS)
271  static views::ViewsDelegate* views_delegate_;
272
273  views::Widget* window_widget_;
274#endif // defined(TOOLKIT_VIEWS)
275  static ShellPlatformDataAura* platform_;
276#endif  // defined(USE_AURA)
277
278  bool headless_;
279
280  // A container of all the open windows. We use a vector so we can keep track
281  // of ordering.
282  static std::vector<Shell*> windows_;
283
284  static base::Callback<void(Shell*)> shell_created_callback_;
285
286  // True if the destructur of Shell should post a quit closure on the current
287  // message loop if the destructed Shell object was the last one.
288  static bool quit_message_loop_;
289};
290
291}  // namespace content
292
293#endif  // CONTENT_SHELL_BROWSER_SHELL_H_
294