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(OS_ANDROID)
22 #include "base/android/scoped_java_ref.h"
23 #elif defined(USE_AURA)
24 #if defined(OS_CHROMEOS)
28 #endif // defined(OS_CHROMEOS)
33 #endif // defined(USE_AURA)
39 class ShellPlatformDataAura
;
43 class ShellDevToolsFrontend
;
44 class ShellJavaScriptDialogManager
;
48 // This represents one window of the Content Shell, i.e. all the UI including
49 // buttons and url bar, as well as the web content area.
50 class Shell
: public WebContentsDelegate
,
51 public WebContentsObserver
{
53 static const int kDefaultTestWindowWidthDip
;
54 static const int kDefaultTestWindowHeightDip
;
58 void LoadURL(const GURL
& url
);
59 void LoadURLForFrame(const GURL
& url
, const std::string
& frame_name
);
60 void LoadDataWithBaseURL(const GURL
& url
,
61 const std::string
& data
,
62 const GURL
& base_url
);
63 void GoBackOrForward(int offset
);
66 void UpdateNavigationControls(bool to_different_document
);
69 void ShowDevToolsForElementAt(int x
, int y
);
70 void ShowDevToolsForTest(const std::string
& settings
,
71 const std::string
& frontend_url
);
73 #if defined(OS_MACOSX)
74 // Resizes the web content view 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
,
122 bool to_different_document
) 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 const std::string
& frontend_url
);
218 void OnDevToolsWebContentsDestroyed();
220 scoped_ptr
<ShellJavaScriptDialogManager
> dialog_manager_
;
222 scoped_ptr
<WebContents
> web_contents_
;
224 scoped_ptr
<DevToolsWebContentsObserver
> devtools_observer_
;
225 ShellDevToolsFrontend
* devtools_frontend_
;
229 gfx::NativeWindow window_
;
230 gfx::NativeEditView url_edit_view_
;
232 gfx::Size content_size_
;
234 #if defined(OS_ANDROID)
235 base::android::ScopedJavaGlobalRef
<jobject
> java_object_
;
236 #elif defined(USE_AURA)
237 #if defined(OS_CHROMEOS)
238 static wm::WMTestHelper
* wm_test_helper_
;
240 #if defined(TOOLKIT_VIEWS)
241 static views::ViewsDelegate
* views_delegate_
;
243 views::Widget
* window_widget_
;
244 #endif // defined(TOOLKIT_VIEWS)
245 static ShellPlatformDataAura
* platform_
;
246 #endif // defined(USE_AURA)
250 // A container of all the open windows. We use a vector so we can keep track
252 static std::vector
<Shell
*> windows_
;
254 static base::Callback
<void(Shell
*)> shell_created_callback_
;
256 // True if the destructur of Shell should post a quit closure on the current
257 // message loop if the destructed Shell object was the last one.
258 static bool quit_message_loop_
;
261 } // namespace content
263 #endif // CONTENT_SHELL_BROWSER_SHELL_H_