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_
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"
21 #if defined(TOOLKIT_GTK)
23 #include "ui/base/gtk/gtk_signal.h"
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)
33 #endif // defined(OS_CHROMEOS)
38 #endif // defined(USE_AURA)
44 class ShellAuraPlatformData
;
48 class ShellDevToolsFrontend
;
49 class ShellJavaScriptDialogManager
;
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
{
58 static const int kDefaultTestWindowWidthDip
;
59 static const int kDefaultTestWindowHeightDip
;
63 void LoadURL(const GURL
& url
);
64 void LoadURLForFrame(const GURL
& url
, const std::string
& frame_name
);
65 void GoBackOrForward(int offset
);
68 void UpdateNavigationControls();
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
);
78 // Do one time initialization at application startup.
79 static void Initialize();
81 static Shell
* CreateNewWindow(BrowserContext
* browser_context
,
83 SiteInstance
* site_instance
,
85 const gfx::Size
& initial_size
);
87 // Returns the Shell object corresponding to the given RenderViewHost.
88 static Shell
* FromRenderViewHost(RenderViewHost
* rvh
);
90 // Returns the currently open windows.
91 static std::vector
<Shell
*>& windows() { return windows_
; }
93 // Closes all windows and returns. This runs a message loop.
94 static void CloseAllWindows();
96 // Used for content_browsertests. Called once.
97 static void SetShellCreatedCallback(
98 base::Callback
<void(Shell
*)> shell_created_callback
);
100 WebContents
* web_contents() const { return web_contents_
.get(); }
101 gfx::NativeWindow
window() { return window_
; }
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
);
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
,
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
;
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
,
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(
141 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
143 virtual bool AddMessageToConsole(WebContents
* source
,
145 const base::string16
& message
,
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
;
160 class DevToolsWebContentsObserver
;
162 explicit Shell(WebContents
* web_contents
);
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
);
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();
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
);
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;
202 gfx::NativeView
GetContentView();
204 // WebContentsObserver
205 virtual void TitleWasSet(NavigationEntry
* entry
, bool explicit_set
) OVERRIDE
;
207 void OnDevToolsWebContentsDestroyed();
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
);
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
);
231 scoped_ptr
<ShellJavaScriptDialogManager
> dialog_manager_
;
233 scoped_ptr
<WebContents
> web_contents_
;
235 scoped_ptr
<DevToolsWebContentsObserver
> devtools_observer_
;
236 ShellDevToolsFrontend
* devtools_frontend_
;
240 gfx::NativeWindow window_
;
241 gfx::NativeEditView url_edit_view_
;
243 gfx::Size content_size_
;
245 #if defined(OS_WIN) && !defined(USE_AURA)
246 WNDPROC default_edit_wnd_proc_
;
247 static HINSTANCE instance_handle_
;
248 #elif defined(TOOLKIT_GTK)
251 GtkToolItem
* back_button_
;
252 GtkToolItem
* forward_button_
;
253 GtkToolItem
* reload_button_
;
254 GtkToolItem
* stop_button_
;
257 GtkToolItem
* spinner_item_
;
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_
;
266 #if defined(TOOLKIT_VIEWS)
267 static views::ViewsDelegate
* views_delegate_
;
269 views::Widget
* window_widget_
;
270 #else // defined(TOOLKIT_VIEWS)
271 static ShellAuraPlatformData
* platform_
;
272 #endif // defined(TOOLKIT_VIEWS)
277 // A container of all the open windows. We use a vector so we can keep track
279 static std::vector
<Shell
*> windows_
;
281 static base::Callback
<void(Shell
*)> shell_created_callback_
;
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_
;
288 } // namespace content
290 #endif // CONTENT_SHELL_BROWSER_SHELL_H_