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();
71 void ShowDevToolsForElementAt(int x, int y);
72 void ShowDevToolsForTest(const std::string& settings);
74 #if defined(TOOLKIT_GTK) || defined(OS_MACOSX)
75 // Resizes the main window to the given dimensions.
76 void SizeTo(const gfx::Size& content_size);
79 // Do one time initialization at application startup.
80 static void Initialize();
82 static Shell* CreateNewWindow(BrowserContext* browser_context,
84 SiteInstance* site_instance,
86 const gfx::Size& initial_size);
88 // Returns the Shell object corresponding to the given RenderViewHost.
89 static Shell* FromRenderViewHost(RenderViewHost* rvh);
91 // Returns the currently open windows.
92 static std::vector<Shell*>& windows() { return windows_; }
94 // Closes all windows and returns. This runs a message loop.
95 static void CloseAllWindows();
97 // Used for content_browsertests. Called once.
98 static void SetShellCreatedCallback(
99 base::Callback<void(Shell*)> shell_created_callback);
101 WebContents* web_contents() const { return web_contents_.get(); }
102 gfx::NativeWindow window() { return window_; }
104 #if defined(OS_MACOSX)
105 // Public to be called by an ObjC bridge object.
106 void ActionPerformed(int control);
107 void URLEntered(std::string url_string);
108 #elif defined(OS_ANDROID)
109 // Registers the Android Java to native methods.
110 static bool Register(JNIEnv* env);
113 // WebContentsDelegate
114 virtual WebContents* OpenURLFromTab(WebContents* source,
115 const OpenURLParams& params) OVERRIDE;
116 virtual void AddNewContents(WebContents* source,
117 WebContents* new_contents,
118 WindowOpenDisposition disposition,
119 const gfx::Rect& initial_pos,
121 bool* was_blocked) OVERRIDE;
122 virtual void LoadingStateChanged(WebContents* source) OVERRIDE;
123 #if defined(OS_ANDROID)
124 virtual void LoadProgressChanged(WebContents* source,
125 double progress) OVERRIDE;
127 virtual void ToggleFullscreenModeForTab(WebContents* web_contents,
128 bool enter_fullscreen) OVERRIDE;
129 virtual bool IsFullscreenForTabOrPending(
130 const WebContents* web_contents) const OVERRIDE;
131 virtual void RequestToLockMouse(WebContents* web_contents,
133 bool last_unlocked_by_target) OVERRIDE;
134 virtual void CloseContents(WebContents* source) OVERRIDE;
135 virtual bool CanOverscrollContent() const OVERRIDE;
136 virtual void DidNavigateMainFramePostCommit(
137 WebContents* web_contents) OVERRIDE;
138 virtual JavaScriptDialogManager* GetJavaScriptDialogManager() OVERRIDE;
139 #if defined(OS_MACOSX)
140 virtual void HandleKeyboardEvent(
142 const NativeWebKeyboardEvent& event) OVERRIDE;
144 virtual bool AddMessageToConsole(WebContents* source,
146 const base::string16& message,
148 const base::string16& source_id) OVERRIDE;
149 virtual void RendererUnresponsive(WebContents* source) OVERRIDE;
150 virtual void ActivateContents(WebContents* contents) OVERRIDE;
151 virtual void DeactivateContents(WebContents* contents) OVERRIDE;
152 virtual void WorkerCrashed(WebContents* source) OVERRIDE;
153 virtual bool HandleContextMenu(const content::ContextMenuParams& params)
155 virtual void WebContentsFocused(WebContents* contents) OVERRIDE;
164 class DevToolsWebContentsObserver;
166 explicit Shell(WebContents* web_contents);
168 // Helper to create a new Shell given a newly created WebContents.
169 static Shell* CreateShell(WebContents* web_contents,
170 const gfx::Size& initial_size);
172 // Helper for one time initialization of application
173 static void PlatformInitialize(const gfx::Size& default_window_size);
174 // Helper for one time deinitialization of platform specific state.
175 static void PlatformExit();
177 // Adjust the size when Blink sends 0 for width and/or height.
178 // This happens when Blink requests a default-sized window.
179 static gfx::Size AdjustWindowSize(const gfx::Size& initial_size);
181 // All the methods that begin with Platform need to be implemented by the
182 // platform specific Shell implementation.
183 // Called from the destructor to let each platform do any necessary cleanup.
184 void PlatformCleanUp();
185 // Creates the main window GUI.
186 void PlatformCreateWindow(int width, int height);
187 // Links the WebContents into the newly created window.
188 void PlatformSetContents();
189 // Resize the content area and GUI.
190 void PlatformResizeSubViews();
191 // Enable/disable a button.
192 void PlatformEnableUIControl(UIControl control, bool is_enabled);
193 // Updates the url in the url bar.
194 void PlatformSetAddressBarURL(const GURL& url);
195 // Sets whether the spinner is spinning.
196 void PlatformSetIsLoading(bool loading);
197 // Set the title of shell window
198 void PlatformSetTitle(const base::string16& title);
199 // User right-clicked on the web view
200 bool PlatformHandleContextMenu(const content::ContextMenuParams& params);
201 #if defined(OS_ANDROID)
202 void PlatformToggleFullscreenModeForTab(WebContents* web_contents,
203 bool enter_fullscreen);
204 bool PlatformIsFullscreenForTabOrPending(
205 const WebContents* web_contents) const;
207 #if defined(TOOLKIT_VIEWS)
208 void PlatformWebContentsFocused(WebContents* contents);
211 gfx::NativeView GetContentView();
213 // WebContentsObserver
214 virtual void TitleWasSet(NavigationEntry* entry, bool explicit_set) OVERRIDE;
216 void InnerShowDevTools(const std::string& settings);
217 void OnDevToolsWebContentsDestroyed();
219 #if defined(TOOLKIT_GTK)
220 CHROMEGTK_CALLBACK_0(Shell, void, OnBackButtonClicked);
221 CHROMEGTK_CALLBACK_0(Shell, void, OnForwardButtonClicked);
222 CHROMEGTK_CALLBACK_0(Shell, void, OnReloadButtonClicked);
223 CHROMEGTK_CALLBACK_0(Shell, void, OnStopButtonClicked);
224 CHROMEGTK_CALLBACK_0(Shell, void, OnURLEntryActivate);
225 CHROMEGTK_CALLBACK_0(Shell, gboolean, OnWindowDestroyed);
227 CHROMEG_CALLBACK_3(Shell, gboolean, OnCloseWindowKeyPressed, GtkAccelGroup*,
228 GObject*, guint, GdkModifierType);
229 CHROMEG_CALLBACK_3(Shell, gboolean, OnNewWindowKeyPressed, GtkAccelGroup*,
230 GObject*, guint, GdkModifierType);
231 CHROMEG_CALLBACK_3(Shell, gboolean, OnHighlightURLView, GtkAccelGroup*,
232 GObject*, guint, GdkModifierType);
233 CHROMEG_CALLBACK_3(Shell, gboolean, OnReloadKeyPressed, GtkAccelGroup*,
234 GObject*, guint, GdkModifierType);
237 scoped_ptr<ShellJavaScriptDialogManager> dialog_manager_;
239 scoped_ptr<WebContents> web_contents_;
241 scoped_ptr<DevToolsWebContentsObserver> devtools_observer_;
242 ShellDevToolsFrontend* devtools_frontend_;
246 gfx::NativeWindow window_;
247 gfx::NativeEditView url_edit_view_;
249 gfx::Size content_size_;
251 #if defined(TOOLKIT_GTK)
254 GtkToolItem* back_button_;
255 GtkToolItem* forward_button_;
256 GtkToolItem* reload_button_;
257 GtkToolItem* stop_button_;
260 GtkToolItem* spinner_item_;
262 int ui_elements_height_; // height of menubar, toolbar, etc.
263 #elif defined(OS_ANDROID)
264 base::android::ScopedJavaGlobalRef<jobject> java_object_;
265 #elif defined(USE_AURA)
266 #if defined(OS_CHROMEOS)
267 static wm::WMTestHelper* wm_test_helper_;
269 #if defined(TOOLKIT_VIEWS)
270 static views::ViewsDelegate* views_delegate_;
272 views::Widget* window_widget_;
273 #endif // defined(TOOLKIT_VIEWS)
274 static ShellPlatformDataAura* platform_;
275 #endif // defined(USE_AURA)
279 // A container of all the open windows. We use a vector so we can keep track
281 static std::vector<Shell*> windows_;
283 static base::Callback<void(Shell*)> shell_created_callback_;
285 // True if the destructur of Shell should post a quit closure on the current
286 // message loop if the destructed Shell object was the last one.
287 static bool quit_message_loop_;
290 } // namespace content
292 #endif // CONTENT_SHELL_BROWSER_SHELL_H_