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)
31 #endif // defined(OS_CHROMEOS)
36 #endif // defined(USE_AURA)
42 class ShellPlatformDataAura
;
46 class ShellDevToolsFrontend
;
47 class ShellJavaScriptDialogManager
;
51 // This represents one window of the Content Shell, i.e. all the UI including
52 // buttons and url bar, as well as the web content area.
53 class Shell
: public WebContentsDelegate
,
54 public WebContentsObserver
{
56 static const int kDefaultTestWindowWidthDip
;
57 static const int kDefaultTestWindowHeightDip
;
61 void LoadURL(const GURL
& url
);
62 void LoadURLForFrame(const GURL
& url
, const std::string
& frame_name
);
63 void LoadDataWithBaseURL(const GURL
& url
,
64 const std::string
& data
,
65 const GURL
& base_url
);
66 void GoBackOrForward(int offset
);
69 void UpdateNavigationControls(bool to_different_document
);
72 void ShowDevToolsForElementAt(int x
, int y
);
73 void ShowDevToolsForTest(const std::string
& settings
,
74 const std::string
& frontend_url
);
76 #if defined(OS_MACOSX)
77 // Resizes the web content view to the given dimensions.
78 void SizeTo(const gfx::Size
& content_size
);
81 // Do one time initialization at application startup.
82 static void Initialize();
84 static Shell
* CreateNewWindow(BrowserContext
* browser_context
,
86 SiteInstance
* site_instance
,
88 const gfx::Size
& initial_size
);
90 // Returns the Shell object corresponding to the given RenderViewHost.
91 static Shell
* FromRenderViewHost(RenderViewHost
* rvh
);
93 // Returns the currently open windows.
94 static std::vector
<Shell
*>& windows() { return windows_
; }
96 // Closes all windows and returns. This runs a message loop.
97 static void CloseAllWindows();
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
,
125 bool to_different_document
) OVERRIDE
;
126 #if defined(OS_ANDROID)
127 virtual void LoadProgressChanged(WebContents
* source
,
128 double progress
) OVERRIDE
;
130 virtual void ToggleFullscreenModeForTab(WebContents
* web_contents
,
131 bool enter_fullscreen
) OVERRIDE
;
132 virtual bool IsFullscreenForTabOrPending(
133 const WebContents
* web_contents
) const OVERRIDE
;
134 virtual void RequestToLockMouse(WebContents
* web_contents
,
136 bool last_unlocked_by_target
) OVERRIDE
;
137 virtual void CloseContents(WebContents
* source
) OVERRIDE
;
138 virtual bool CanOverscrollContent() const OVERRIDE
;
139 virtual void DidNavigateMainFramePostCommit(
140 WebContents
* web_contents
) OVERRIDE
;
141 virtual JavaScriptDialogManager
* GetJavaScriptDialogManager() OVERRIDE
;
142 #if defined(OS_MACOSX)
143 virtual void HandleKeyboardEvent(
145 const NativeWebKeyboardEvent
& event
) OVERRIDE
;
147 virtual bool AddMessageToConsole(WebContents
* source
,
149 const base::string16
& message
,
151 const base::string16
& source_id
) OVERRIDE
;
152 virtual void RendererUnresponsive(WebContents
* source
) OVERRIDE
;
153 virtual void ActivateContents(WebContents
* contents
) OVERRIDE
;
154 virtual void DeactivateContents(WebContents
* contents
) OVERRIDE
;
155 virtual void WorkerCrashed(WebContents
* source
) OVERRIDE
;
156 virtual bool HandleContextMenu(const content::ContextMenuParams
& params
)
158 virtual void WebContentsFocused(WebContents
* contents
) OVERRIDE
;
167 class DevToolsWebContentsObserver
;
169 explicit Shell(WebContents
* web_contents
);
171 // Helper to create a new Shell given a newly created WebContents.
172 static Shell
* CreateShell(WebContents
* web_contents
,
173 const gfx::Size
& initial_size
);
175 // Helper for one time initialization of application
176 static void PlatformInitialize(const gfx::Size
& default_window_size
);
177 // Helper for one time deinitialization of platform specific state.
178 static void PlatformExit();
180 // Adjust the size when Blink sends 0 for width and/or height.
181 // This happens when Blink requests a default-sized window.
182 static gfx::Size
AdjustWindowSize(const gfx::Size
& initial_size
);
184 // All the methods that begin with Platform need to be implemented by the
185 // platform specific Shell implementation.
186 // Called from the destructor to let each platform do any necessary cleanup.
187 void PlatformCleanUp();
188 // Creates the main window GUI.
189 void PlatformCreateWindow(int width
, int height
);
190 // Links the WebContents into the newly created window.
191 void PlatformSetContents();
192 // Resize the content area and GUI.
193 void PlatformResizeSubViews();
194 // Enable/disable a button.
195 void PlatformEnableUIControl(UIControl control
, bool is_enabled
);
196 // Updates the url in the url bar.
197 void PlatformSetAddressBarURL(const GURL
& url
);
198 // Sets whether the spinner is spinning.
199 void PlatformSetIsLoading(bool loading
);
200 // Set the title of shell window
201 void PlatformSetTitle(const base::string16
& title
);
202 // User right-clicked on the web view
203 bool PlatformHandleContextMenu(const content::ContextMenuParams
& params
);
204 #if defined(OS_ANDROID)
205 void PlatformToggleFullscreenModeForTab(WebContents
* web_contents
,
206 bool enter_fullscreen
);
207 bool PlatformIsFullscreenForTabOrPending(
208 const WebContents
* web_contents
) const;
210 #if defined(TOOLKIT_VIEWS)
211 void PlatformWebContentsFocused(WebContents
* contents
);
214 gfx::NativeView
GetContentView();
216 // WebContentsObserver
217 virtual void TitleWasSet(NavigationEntry
* entry
, bool explicit_set
) OVERRIDE
;
219 void InnerShowDevTools(const std::string
& settings
,
220 const std::string
& frontend_url
);
221 void OnDevToolsWebContentsDestroyed();
223 scoped_ptr
<ShellJavaScriptDialogManager
> dialog_manager_
;
225 scoped_ptr
<WebContents
> web_contents_
;
227 scoped_ptr
<DevToolsWebContentsObserver
> devtools_observer_
;
228 ShellDevToolsFrontend
* devtools_frontend_
;
232 gfx::NativeWindow window_
;
233 gfx::NativeEditView url_edit_view_
;
235 gfx::Size content_size_
;
237 #if defined(OS_ANDROID)
238 base::android::ScopedJavaGlobalRef
<jobject
> java_object_
;
239 #elif defined(USE_AURA)
240 #if defined(OS_CHROMEOS)
241 static wm::WMTestHelper
* wm_test_helper_
;
242 static gfx::Screen
* test_screen_
;
244 #if defined(TOOLKIT_VIEWS)
245 static views::ViewsDelegate
* views_delegate_
;
247 views::Widget
* window_widget_
;
248 #endif // defined(TOOLKIT_VIEWS)
249 static ShellPlatformDataAura
* platform_
;
250 #endif // defined(USE_AURA)
254 // A container of all the open windows. We use a vector so we can keep track
256 static std::vector
<Shell
*> windows_
;
258 static base::Callback
<void(Shell
*)> shell_created_callback_
;
260 // True if the destructur of Shell should post a quit closure on the current
261 // message loop if the destructed Shell object was the last one.
262 static bool quit_message_loop_
;
265 } // namespace content
267 #endif // CONTENT_SHELL_BROWSER_SHELL_H_