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 ShellPlatformDataAura;
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(TOOLKIT_GTK) || defined(OS_MACOSX)
73 // Resizes the main window to the given dimensions.
74 void SizeTo(const gfx::Size& content_size);
77 // Do one time initialization at application startup.
78 static void Initialize();
80 static Shell* CreateNewWindow(BrowserContext* browser_context,
82 SiteInstance* site_instance,
84 const gfx::Size& initial_size);
86 // Returns the Shell object corresponding to the given RenderViewHost.
87 static Shell* FromRenderViewHost(RenderViewHost* rvh);
89 // Returns the currently open windows.
90 static std::vector<Shell*>& windows() { return windows_; }
92 // Closes all windows and returns. This runs a message loop.
93 static void CloseAllWindows();
95 // Used for content_browsertests. Called once.
96 static void SetShellCreatedCallback(
97 base::Callback<void(Shell*)> shell_created_callback);
99 WebContents* web_contents() const { return web_contents_.get(); }
100 gfx::NativeWindow window() { return window_; }
102 #if defined(OS_MACOSX)
103 // Public to be called by an ObjC bridge object.
104 void ActionPerformed(int control);
105 void URLEntered(std::string url_string);
106 #elif defined(OS_ANDROID)
107 // Registers the Android Java to native methods.
108 static bool Register(JNIEnv* env);
111 // WebContentsDelegate
112 virtual WebContents* OpenURLFromTab(WebContents* source,
113 const OpenURLParams& params) OVERRIDE;
114 virtual void AddNewContents(WebContents* source,
115 WebContents* new_contents,
116 WindowOpenDisposition disposition,
117 const gfx::Rect& initial_pos,
119 bool* was_blocked) OVERRIDE;
120 virtual void LoadingStateChanged(WebContents* source) OVERRIDE;
121 #if defined(OS_ANDROID)
122 virtual void LoadProgressChanged(WebContents* source,
123 double progress) OVERRIDE;
125 virtual void ToggleFullscreenModeForTab(WebContents* web_contents,
126 bool enter_fullscreen) OVERRIDE;
127 virtual bool IsFullscreenForTabOrPending(
128 const WebContents* web_contents) const OVERRIDE;
129 virtual void RequestToLockMouse(WebContents* web_contents,
131 bool last_unlocked_by_target) OVERRIDE;
132 virtual void CloseContents(WebContents* source) OVERRIDE;
133 virtual bool CanOverscrollContent() const OVERRIDE;
134 virtual void DidNavigateMainFramePostCommit(
135 WebContents* web_contents) OVERRIDE;
136 virtual JavaScriptDialogManager* GetJavaScriptDialogManager() OVERRIDE;
137 #if defined(OS_MACOSX)
138 virtual void HandleKeyboardEvent(
140 const NativeWebKeyboardEvent& event) OVERRIDE;
142 virtual bool AddMessageToConsole(WebContents* source,
144 const base::string16& message,
146 const base::string16& source_id) OVERRIDE;
147 virtual void RendererUnresponsive(WebContents* source) OVERRIDE;
148 virtual void ActivateContents(WebContents* contents) OVERRIDE;
149 virtual void DeactivateContents(WebContents* contents) OVERRIDE;
150 virtual void WorkerCrashed(WebContents* source) OVERRIDE;
159 class DevToolsWebContentsObserver;
161 explicit Shell(WebContents* web_contents);
163 // Helper to create a new Shell given a newly created WebContents.
164 static Shell* CreateShell(WebContents* web_contents,
165 const gfx::Size& initial_size);
167 // Helper for one time initialization of application
168 static void PlatformInitialize(const gfx::Size& default_window_size);
169 // Helper for one time deinitialization of platform specific state.
170 static void PlatformExit();
172 // Adjust the size when Blink sends 0 for width and/or height.
173 // This happens when Blink requests a default-sized window.
174 static gfx::Size AdjustWindowSize(const gfx::Size& initial_size);
176 // All the methods that begin with Platform need to be implemented by the
177 // platform specific Shell implementation.
178 // Called from the destructor to let each platform do any necessary cleanup.
179 void PlatformCleanUp();
180 // Creates the main window GUI.
181 void PlatformCreateWindow(int width, int height);
182 // Links the WebContents into the newly created window.
183 void PlatformSetContents();
184 // Resize the content area and GUI.
185 void PlatformResizeSubViews();
186 // Enable/disable a button.
187 void PlatformEnableUIControl(UIControl control, bool is_enabled);
188 // Updates the url in the url bar.
189 void PlatformSetAddressBarURL(const GURL& url);
190 // Sets whether the spinner is spinning.
191 void PlatformSetIsLoading(bool loading);
192 // Set the title of shell window
193 void PlatformSetTitle(const base::string16& title);
194 #if defined(OS_ANDROID)
195 void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
196 bool enter_fullscreen);
197 bool PlatformIsFullscreenForTabOrPending(
198 const WebContents* web_contents) const;
201 gfx::NativeView GetContentView();
203 // WebContentsObserver
204 virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) OVERRIDE;
206 void OnDevToolsWebContentsDestroyed();
208 #if defined(TOOLKIT_GTK)
209 CHROMEGTK_CALLBACK_0(Shell, void, OnBackButtonClicked);
210 CHROMEGTK_CALLBACK_0(Shell, void, OnForwardButtonClicked);
211 CHROMEGTK_CALLBACK_0(Shell, void, OnReloadButtonClicked);
212 CHROMEGTK_CALLBACK_0(Shell, void, OnStopButtonClicked);
213 CHROMEGTK_CALLBACK_0(Shell, void, OnURLEntryActivate);
214 CHROMEGTK_CALLBACK_0(Shell, gboolean, OnWindowDestroyed);
216 CHROMEG_CALLBACK_3(Shell, gboolean, OnCloseWindowKeyPressed, GtkAccelGroup*,
217 GObject*, guint, GdkModifierType);
218 CHROMEG_CALLBACK_3(Shell, gboolean, OnNewWindowKeyPressed, GtkAccelGroup*,
219 GObject*, guint, GdkModifierType);
220 CHROMEG_CALLBACK_3(Shell, gboolean, OnHighlightURLView, GtkAccelGroup*,
221 GObject*, guint, GdkModifierType);
222 CHROMEG_CALLBACK_3(Shell, gboolean, OnReloadKeyPressed, GtkAccelGroup*,
223 GObject*, guint, GdkModifierType);
226 scoped_ptr<ShellJavaScriptDialogManager> dialog_manager_;
228 scoped_ptr<WebContents> web_contents_;
230 scoped_ptr<DevToolsWebContentsObserver> devtools_observer_;
231 ShellDevToolsFrontend* devtools_frontend_;
235 gfx::NativeWindow window_;
236 gfx::NativeEditView url_edit_view_;
238 gfx::Size content_size_;
240 #if defined(TOOLKIT_GTK)
243 GtkToolItem* back_button_;
244 GtkToolItem* forward_button_;
245 GtkToolItem* reload_button_;
246 GtkToolItem* stop_button_;
249 GtkToolItem* spinner_item_;
251 int ui_elements_height_; // height of menubar, toolbar, etc.
252 #elif defined(OS_ANDROID)
253 base::android::ScopedJavaGlobalRef<jobject> java_object_;
254 #elif defined(USE_AURA)
255 #if defined(OS_CHROMEOS)
256 static wm::WMTestHelper* wm_test_helper_;
258 #if defined(TOOLKIT_VIEWS)
259 static views::ViewsDelegate* views_delegate_;
261 views::Widget* window_widget_;
262 #endif // defined(TOOLKIT_VIEWS)
263 static ShellPlatformDataAura* platform_;
264 #endif // defined(USE_AURA)
268 // A container of all the open windows. We use a vector so we can keep track
270 static std::vector<Shell*> windows_;
272 static base::Callback<void(Shell*)> shell_created_callback_;
274 // True if the destructur of Shell should post a quit closure on the current
275 // message loop if the destructed Shell object was the last one.
276 static bool quit_message_loop_;
279 } // namespace content
281 #endif // CONTENT_SHELL_BROWSER_SHELL_H_