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