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/geometry/size.h"
19 #include "ui/gfx/native_widget_types.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
{
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
);
71 #if defined(OS_MACOSX)
72 // Resizes the web content view to the given dimensions.
73 void SizeTo(const gfx::Size
& content_size
);
76 // Do one time initialization at application startup.
77 static void Initialize();
79 static Shell
* CreateNewWindow(BrowserContext
* browser_context
,
81 SiteInstance
* site_instance
,
82 const gfx::Size
& initial_size
);
84 // Returns the Shell object corresponding to the given RenderViewHost.
85 static Shell
* FromRenderViewHost(RenderViewHost
* rvh
);
87 // Returns the currently open windows.
88 static std::vector
<Shell
*>& windows() { return windows_
; }
90 // Closes all windows and returns. This runs a message loop.
91 static void CloseAllWindows();
93 // Used for content_browsertests. Called once.
94 static void SetShellCreatedCallback(
95 base::Callback
<void(Shell
*)> shell_created_callback
);
97 WebContents
* web_contents() const { return web_contents_
.get(); }
98 gfx::NativeWindow
window() { return window_
; }
100 #if defined(OS_MACOSX)
101 // Public to be called by an ObjC bridge object.
102 void ActionPerformed(int control
);
103 void URLEntered(std::string url_string
);
104 #elif defined(OS_ANDROID)
105 // Registers the Android Java to native methods.
106 static bool Register(JNIEnv
* env
);
109 // WebContentsDelegate
110 WebContents
* OpenURLFromTab(WebContents
* source
,
111 const OpenURLParams
& params
) override
;
112 void AddNewContents(WebContents
* source
,
113 WebContents
* new_contents
,
114 WindowOpenDisposition disposition
,
115 const gfx::Rect
& initial_rect
,
117 bool* was_blocked
) override
;
118 void LoadingStateChanged(WebContents
* source
,
119 bool to_different_document
) override
;
120 #if defined(OS_ANDROID)
121 void LoadProgressChanged(WebContents
* source
, double progress
) override
;
123 void EnterFullscreenModeForTab(WebContents
* web_contents
,
124 const GURL
& origin
) override
;
125 void ExitFullscreenModeForTab(WebContents
* web_contents
) override
;
126 bool IsFullscreenForTabOrPending(
127 const WebContents
* web_contents
) const override
;
128 blink::WebDisplayMode
GetDisplayMode(
129 const WebContents
* web_contents
) const override
;
130 void RequestToLockMouse(WebContents
* web_contents
,
132 bool last_unlocked_by_target
) override
;
133 void CloseContents(WebContents
* source
) override
;
134 bool CanOverscrollContent() const override
;
135 void DidNavigateMainFramePostCommit(WebContents
* web_contents
) override
;
136 JavaScriptDialogManager
* GetJavaScriptDialogManager(
137 WebContents
* source
) override
;
138 #if defined(OS_MACOSX)
139 void HandleKeyboardEvent(WebContents
* source
,
140 const NativeWebKeyboardEvent
& event
) override
;
142 bool AddMessageToConsole(WebContents
* source
,
144 const base::string16
& message
,
146 const base::string16
& source_id
) override
;
147 void RendererUnresponsive(WebContents
* source
) override
;
148 void ActivateContents(WebContents
* contents
) override
;
149 void DeactivateContents(WebContents
* contents
) override
;
150 bool HandleContextMenu(const content::ContextMenuParams
& params
) override
;
152 static gfx::Size
GetShellDefaultSize();
161 class DevToolsWebContentsObserver
;
163 explicit Shell(WebContents
* web_contents
);
165 // Helper to create a new Shell given a newly created WebContents.
166 static Shell
* CreateShell(WebContents
* web_contents
,
167 const gfx::Size
& initial_size
);
169 // Helper for one time initialization of application
170 static void PlatformInitialize(const gfx::Size
& default_window_size
);
171 // Helper for one time deinitialization of platform specific state.
172 static void PlatformExit();
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 base::string16
& title
);
196 // User right-clicked on the web view
197 bool PlatformHandleContextMenu(const content::ContextMenuParams
& params
);
198 #if defined(OS_ANDROID)
199 void PlatformToggleFullscreenModeForTab(WebContents
* web_contents
,
200 bool enter_fullscreen
);
201 bool PlatformIsFullscreenForTabOrPending(
202 const WebContents
* web_contents
) const;
205 gfx::NativeView
GetContentView();
207 void ToggleFullscreenModeForTab(WebContents
* web_contents
,
208 bool enter_fullscreen
);
209 // WebContentsObserver
210 void TitleWasSet(NavigationEntry
* entry
, bool explicit_set
) override
;
212 void InnerShowDevTools();
213 void OnDevToolsWebContentsDestroyed();
215 scoped_ptr
<ShellJavaScriptDialogManager
> dialog_manager_
;
217 scoped_ptr
<WebContents
> web_contents_
;
219 scoped_ptr
<DevToolsWebContentsObserver
> devtools_observer_
;
220 ShellDevToolsFrontend
* devtools_frontend_
;
224 gfx::NativeWindow window_
;
225 gfx::NativeEditView url_edit_view_
;
227 gfx::Size content_size_
;
229 #if defined(OS_ANDROID)
230 base::android::ScopedJavaGlobalRef
<jobject
> java_object_
;
231 #elif defined(USE_AURA)
232 #if defined(OS_CHROMEOS)
233 static wm::WMTestHelper
* wm_test_helper_
;
234 static gfx::Screen
* test_screen_
;
236 #if defined(TOOLKIT_VIEWS)
237 static views::ViewsDelegate
* views_delegate_
;
239 views::Widget
* window_widget_
;
240 #endif // defined(TOOLKIT_VIEWS)
241 static ShellPlatformDataAura
* platform_
;
242 #endif // defined(USE_AURA)
246 // A container of all the open windows. We use a vector so we can keep track
248 static std::vector
<Shell
*> windows_
;
250 static base::Callback
<void(Shell
*)> shell_created_callback_
;
252 // True if the destructur of Shell should post a quit closure on the current
253 // message loop if the destructed Shell object was the last one.
254 static bool quit_message_loop_
;
257 } // namespace content
259 #endif // CONTENT_SHELL_BROWSER_SHELL_H_