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(int width
, int height
);
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 // Closes all windows and exits.
97 static void PlatformExit();
99 // Used for content_browsertests. Called once.
100 static void SetShellCreatedCallback(
101 base::Callback
<void(Shell
*)> shell_created_callback
);
103 WebContents
* web_contents() const { return web_contents_
.get(); }
104 gfx::NativeWindow
window() { return window_
; }
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
);
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
,
123 bool* was_blocked
) OVERRIDE
;
124 virtual void LoadingStateChanged(WebContents
* source
) OVERRIDE
;
125 #if defined(OS_ANDROID)
126 virtual void LoadProgressChanged(WebContents
* source
,
127 double progress
) OVERRIDE
;
129 virtual void ToggleFullscreenModeForTab(WebContents
* web_contents
,
130 bool enter_fullscreen
) OVERRIDE
;
131 virtual bool IsFullscreenForTabOrPending(
132 const WebContents
* web_contents
) const OVERRIDE
;
133 virtual void RequestToLockMouse(WebContents
* web_contents
,
135 bool last_unlocked_by_target
) OVERRIDE
;
136 virtual void CloseContents(WebContents
* source
) OVERRIDE
;
137 virtual bool CanOverscrollContent() const OVERRIDE
;
138 virtual void DidNavigateMainFramePostCommit(
139 WebContents
* web_contents
) OVERRIDE
;
140 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager() OVERRIDE
;
141 #if defined(OS_MACOSX)
142 virtual void HandleKeyboardEvent(
144 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
146 virtual bool AddMessageToConsole(WebContents
* source
,
148 const string16
& message
,
150 const string16
& source_id
) OVERRIDE
;
151 virtual void RendererUnresponsive(WebContents
* source
) OVERRIDE
;
152 virtual void ActivateContents(WebContents
* contents
) OVERRIDE
;
153 virtual void DeactivateContents(WebContents
* contents
) OVERRIDE
;
154 virtual void WorkerCrashed(WebContents
* source
) OVERRIDE
;
163 class DevToolsWebContentsObserver
;
165 explicit Shell(WebContents
* web_contents
);
167 // Helper to create a new Shell given a newly created WebContents.
168 static Shell
* CreateShell(WebContents
* web_contents
,
169 const gfx::Size
& initial_size
);
171 // Helper for one time initialization of application
172 static void PlatformInitialize(const gfx::Size
& default_window_size
);
174 // Adjust the size when Blink sends 0 for width and/or height.
175 // This happens when Blink requests a default-sized window.
176 static gfx::Size
AdjustWindowSize(const gfx::Size
& initial_size
);
178 // All the methods that begin with Platform need to be implemented by the
179 // platform specific Shell implementation.
180 // Called from the destructor to let each platform do any necessary cleanup.
181 void PlatformCleanUp();
182 // Creates the main window GUI.
183 void PlatformCreateWindow(int width
, int height
);
184 // Links the WebContents into the newly created window.
185 void PlatformSetContents();
186 // Resize the content area and GUI.
187 void PlatformResizeSubViews();
188 // Enable/disable a button.
189 void PlatformEnableUIControl(UIControl control
, bool is_enabled
);
190 // Updates the url in the url bar.
191 void PlatformSetAddressBarURL(const GURL
& url
);
192 // Sets whether the spinner is spinning.
193 void PlatformSetIsLoading(bool loading
);
194 // Set the title of shell window
195 void PlatformSetTitle(const string16
& title
);
196 #if defined(OS_ANDROID)
197 void PlatformToggleFullscreenModeForTab(WebContents
* web_contents
,
198 bool enter_fullscreen
);
199 bool PlatformIsFullscreenForTabOrPending(
200 const WebContents
* web_contents
) const;
203 gfx::NativeView
GetContentView();
205 // WebContentsObserver
206 virtual void TitleWasSet(NavigationEntry
* entry
, bool explicit_set
) OVERRIDE
;
208 void OnDevToolsWebContentsDestroyed();
210 #if defined(OS_WIN) && !defined(USE_AURA)
211 static ATOM
RegisterWindowClass();
212 static LRESULT CALLBACK
WndProc(HWND
, UINT
, WPARAM
, LPARAM
);
213 static LRESULT CALLBACK
EditWndProc(HWND
, UINT
, WPARAM
, LPARAM
);
214 #elif defined(TOOLKIT_GTK)
215 CHROMEGTK_CALLBACK_0(Shell
, void, OnBackButtonClicked
);
216 CHROMEGTK_CALLBACK_0(Shell
, void, OnForwardButtonClicked
);
217 CHROMEGTK_CALLBACK_0(Shell
, void, OnReloadButtonClicked
);
218 CHROMEGTK_CALLBACK_0(Shell
, void, OnStopButtonClicked
);
219 CHROMEGTK_CALLBACK_0(Shell
, void, OnURLEntryActivate
);
220 CHROMEGTK_CALLBACK_0(Shell
, gboolean
, OnWindowDestroyed
);
222 CHROMEG_CALLBACK_3(Shell
, gboolean
, OnCloseWindowKeyPressed
, GtkAccelGroup
*,
223 GObject
*, guint
, GdkModifierType
);
224 CHROMEG_CALLBACK_3(Shell
, gboolean
, OnNewWindowKeyPressed
, GtkAccelGroup
*,
225 GObject
*, guint
, GdkModifierType
);
226 CHROMEG_CALLBACK_3(Shell
, gboolean
, OnHighlightURLView
, GtkAccelGroup
*,
227 GObject
*, guint
, GdkModifierType
);
228 CHROMEG_CALLBACK_3(Shell
, gboolean
, OnReloadKeyPressed
, GtkAccelGroup
*,
229 GObject
*, guint
, GdkModifierType
);
232 scoped_ptr
<ShellJavaScriptDialogManager
> dialog_manager_
;
234 scoped_ptr
<WebContents
> web_contents_
;
236 scoped_ptr
<DevToolsWebContentsObserver
> devtools_observer_
;
237 ShellDevToolsFrontend
* devtools_frontend_
;
241 gfx::NativeWindow window_
;
242 gfx::NativeEditView url_edit_view_
;
244 #if defined(OS_WIN) && !defined(USE_AURA)
245 WNDPROC default_edit_wnd_proc_
;
246 static HINSTANCE instance_handle_
;
247 #elif defined(TOOLKIT_GTK)
250 GtkToolItem
* back_button_
;
251 GtkToolItem
* forward_button_
;
252 GtkToolItem
* reload_button_
;
253 GtkToolItem
* stop_button_
;
256 GtkToolItem
* spinner_item_
;
260 int ui_elements_height_
; // height of menubar, toolbar, etc.
261 #elif defined(OS_ANDROID)
262 base::android::ScopedJavaGlobalRef
<jobject
> java_object_
;
263 #elif defined(USE_AURA)
264 #if defined(OS_CHROMEOS)
265 static shell::MinimalShell
* minimal_shell_
;
267 #if defined(TOOLKIT_VIEWS)
268 static views::ViewsDelegate
* views_delegate_
;
270 views::Widget
* window_widget_
;
271 #else // defined(TOOLKIT_VIEWS)
272 scoped_ptr
<ShellAuraPlatformData
> platform_
;
273 #endif // defined(TOOLKIT_VIEWS)
274 #elif defined(OS_MACOSX)
281 // A container of all the open windows. We use a vector so we can keep track
283 static std::vector
<Shell
*> windows_
;
285 static base::Callback
<void(Shell
*)> shell_created_callback_
;
287 // True if the destructur of Shell should post a quit closure on the current
288 // message loop if the destructed Shell object was the last one.
289 static bool quit_message_loop_
;
292 } // namespace content
294 #endif // CONTENT_SHELL_BROWSER_SHELL_H_