shell.h revision 116680a4aac90f2aa7413d9095a592090648e557
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(OS_ANDROID)
22#include "base/android/scoped_java_ref.h"
23#elif defined(USE_AURA)
24#if defined(OS_CHROMEOS)
25namespace gfx {
26class Screen;
27}
28namespace wm {
29class WMTestHelper;
30}
31#endif  // defined(OS_CHROMEOS)
32namespace views {
33class Widget;
34class ViewsDelegate;
35}
36#endif  // defined(USE_AURA)
37
38class GURL;
39namespace content {
40
41#if defined(USE_AURA)
42class ShellPlatformDataAura;
43#endif
44
45class BrowserContext;
46class ShellDevToolsFrontend;
47class ShellJavaScriptDialogManager;
48class SiteInstance;
49class WebContents;
50
51// This represents one window of the Content Shell, i.e. all the UI including
52// buttons and url bar, as well as the web content area.
53class Shell : public WebContentsDelegate,
54              public WebContentsObserver {
55 public:
56  static const int kDefaultTestWindowWidthDip;
57  static const int kDefaultTestWindowHeightDip;
58
59  virtual ~Shell();
60
61  void LoadURL(const GURL& url);
62  void LoadURLForFrame(const GURL& url, const std::string& frame_name);
63  void LoadDataWithBaseURL(const GURL& url,
64                           const std::string& data,
65                           const GURL& base_url);
66  void GoBackOrForward(int offset);
67  void Reload();
68  void Stop();
69  void UpdateNavigationControls(bool to_different_document);
70  void Close();
71  void ShowDevTools();
72  void ShowDevToolsForElementAt(int x, int y);
73  void ShowDevToolsForTest(const std::string& settings,
74                           const std::string& frontend_url);
75  void CloseDevTools();
76#if defined(OS_MACOSX)
77  // Resizes the web content view to the given dimensions.
78  void SizeTo(const gfx::Size& content_size);
79#endif
80
81  // Do one time initialization at application startup.
82  static void Initialize();
83
84  static Shell* CreateNewWindow(BrowserContext* browser_context,
85                                const GURL& url,
86                                SiteInstance* site_instance,
87                                int routing_id,
88                                const gfx::Size& initial_size);
89
90  // Returns the Shell object corresponding to the given RenderViewHost.
91  static Shell* FromRenderViewHost(RenderViewHost* rvh);
92
93  // Returns the currently open windows.
94  static std::vector<Shell*>& windows() { return windows_; }
95
96  // Closes all windows and returns. This runs a message loop.
97  static void CloseAllWindows();
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,
125                                   bool to_different_document) OVERRIDE;
126#if defined(OS_ANDROID)
127  virtual void LoadProgressChanged(WebContents* source,
128                                   double progress) OVERRIDE;
129#endif
130  virtual void ToggleFullscreenModeForTab(WebContents* web_contents,
131                                          bool enter_fullscreen) OVERRIDE;
132  virtual bool IsFullscreenForTabOrPending(
133      const WebContents* web_contents) const OVERRIDE;
134  virtual void RequestToLockMouse(WebContents* web_contents,
135                                  bool user_gesture,
136                                  bool last_unlocked_by_target) OVERRIDE;
137  virtual void CloseContents(WebContents* source) OVERRIDE;
138  virtual bool CanOverscrollContent() const OVERRIDE;
139  virtual void DidNavigateMainFramePostCommit(
140      WebContents* web_contents) OVERRIDE;
141  virtual JavaScriptDialogManager* GetJavaScriptDialogManager() OVERRIDE;
142#if defined(OS_MACOSX)
143  virtual void HandleKeyboardEvent(
144      WebContents* source,
145      const NativeWebKeyboardEvent& event) OVERRIDE;
146#endif
147  virtual bool AddMessageToConsole(WebContents* source,
148                                   int32 level,
149                                   const base::string16& message,
150                                   int32 line_no,
151                                   const base::string16& source_id) OVERRIDE;
152  virtual void RendererUnresponsive(WebContents* source) OVERRIDE;
153  virtual void ActivateContents(WebContents* contents) OVERRIDE;
154  virtual void DeactivateContents(WebContents* contents) OVERRIDE;
155  virtual void WorkerCrashed(WebContents* source) OVERRIDE;
156  virtual bool HandleContextMenu(const content::ContextMenuParams& params)
157      OVERRIDE;
158  virtual void WebContentsFocused(WebContents* contents) OVERRIDE;
159
160 private:
161  enum UIControl {
162    BACK_BUTTON,
163    FORWARD_BUTTON,
164    STOP_BUTTON
165  };
166
167  class DevToolsWebContentsObserver;
168
169  explicit Shell(WebContents* web_contents);
170
171  // Helper to create a new Shell given a newly created WebContents.
172  static Shell* CreateShell(WebContents* web_contents,
173                            const gfx::Size& initial_size);
174
175  // Helper for one time initialization of application
176  static void PlatformInitialize(const gfx::Size& default_window_size);
177  // Helper for one time deinitialization of platform specific state.
178  static void PlatformExit();
179
180  // Adjust the size when Blink sends 0 for width and/or height.
181  // This happens when Blink requests a default-sized window.
182  static gfx::Size AdjustWindowSize(const gfx::Size& initial_size);
183
184  // All the methods that begin with Platform need to be implemented by the
185  // platform specific Shell implementation.
186  // Called from the destructor to let each platform do any necessary cleanup.
187  void PlatformCleanUp();
188  // Creates the main window GUI.
189  void PlatformCreateWindow(int width, int height);
190  // Links the WebContents into the newly created window.
191  void PlatformSetContents();
192  // Resize the content area and GUI.
193  void PlatformResizeSubViews();
194  // Enable/disable a button.
195  void PlatformEnableUIControl(UIControl control, bool is_enabled);
196  // Updates the url in the url bar.
197  void PlatformSetAddressBarURL(const GURL& url);
198  // Sets whether the spinner is spinning.
199  void PlatformSetIsLoading(bool loading);
200  // Set the title of shell window
201  void PlatformSetTitle(const base::string16& title);
202  // User right-clicked on the web view
203  bool PlatformHandleContextMenu(const content::ContextMenuParams& params);
204#if defined(OS_ANDROID)
205  void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
206                                          bool enter_fullscreen);
207  bool PlatformIsFullscreenForTabOrPending(
208      const WebContents* web_contents) const;
209#endif
210#if defined(TOOLKIT_VIEWS)
211  void PlatformWebContentsFocused(WebContents* contents);
212#endif
213
214  gfx::NativeView GetContentView();
215
216  // WebContentsObserver
217  virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) OVERRIDE;
218
219  void InnerShowDevTools(const std::string& settings,
220                         const std::string& frontend_url);
221  void OnDevToolsWebContentsDestroyed();
222
223  scoped_ptr<ShellJavaScriptDialogManager> dialog_manager_;
224
225  scoped_ptr<WebContents> web_contents_;
226
227  scoped_ptr<DevToolsWebContentsObserver> devtools_observer_;
228  ShellDevToolsFrontend* devtools_frontend_;
229
230  bool is_fullscreen_;
231
232  gfx::NativeWindow window_;
233  gfx::NativeEditView url_edit_view_;
234
235  gfx::Size content_size_;
236
237#if defined(OS_ANDROID)
238  base::android::ScopedJavaGlobalRef<jobject> java_object_;
239#elif defined(USE_AURA)
240#if defined(OS_CHROMEOS)
241  static wm::WMTestHelper* wm_test_helper_;
242  static gfx::Screen* test_screen_;
243#endif
244#if defined(TOOLKIT_VIEWS)
245  static views::ViewsDelegate* views_delegate_;
246
247  views::Widget* window_widget_;
248#endif // defined(TOOLKIT_VIEWS)
249  static ShellPlatformDataAura* platform_;
250#endif  // defined(USE_AURA)
251
252  bool headless_;
253
254  // A container of all the open windows. We use a vector so we can keep track
255  // of ordering.
256  static std::vector<Shell*> windows_;
257
258  static base::Callback<void(Shell*)> shell_created_callback_;
259
260  // True if the destructur of Shell should post a quit closure on the current
261  // message loop if the destructed Shell object was the last one.
262  static bool quit_message_loop_;
263};
264
265}  // namespace content
266
267#endif  // CONTENT_SHELL_BROWSER_SHELL_H_
268