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.
5 #ifndef APPS_SHELL_WINDOW_H_
6 #define APPS_SHELL_WINDOW_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/extensions/extension_icon_image.h"
11 #include "chrome/browser/extensions/extension_keybinding_registry.h"
12 #include "chrome/browser/sessions/session_id.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "content/public/common/console_message_level.h"
19 #include "ui/base/ui_base_types.h" // WindowShowState
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/rect.h"
31 namespace extensions
{
33 class PlatformAppBrowserTest
;
34 class WindowController
;
36 struct DraggableRegion
;
45 class NativeAppWindow
;
47 // Manages the web contents for Shell Windows. The implementation for this
48 // class should create and maintain the WebContents for the window, and handle
49 // any message passing between the web contents and the extension system or
51 class ShellWindowContents
{
53 ShellWindowContents() {}
54 virtual ~ShellWindowContents() {}
56 // Called to initialize the WebContents, before the app window is created.
57 virtual void Initialize(Profile
* profile
, const GURL
& url
) = 0;
59 // Called to load the contents, after the app window is created.
60 virtual void LoadContents(int32 creator_process_id
) = 0;
62 // Called when the native window changes.
63 virtual void NativeWindowChanged(NativeAppWindow
* native_app_window
) = 0;
65 // Called when the native window closes.
66 virtual void NativeWindowClosed() = 0;
68 virtual content::WebContents
* GetWebContents() const = 0;
71 DISALLOW_COPY_AND_ASSIGN(ShellWindowContents
);
74 // ShellWindow is the type of window used by platform apps. Shell windows
75 // have a WebContents but none of the chrome of normal browser windows.
76 class ShellWindow
: public content::NotificationObserver
,
77 public content::WebContentsDelegate
,
78 public content::WebContentsObserver
,
79 public web_modal::WebContentsModalDialogManagerDelegate
,
80 public extensions::ExtensionKeybindingRegistry::Delegate
,
81 public extensions::IconImage::Observer
{
84 WINDOW_TYPE_DEFAULT
= 1 << 0, // Default shell window.
85 WINDOW_TYPE_PANEL
= 1 << 1, // OS controlled panel window (Ash only).
86 WINDOW_TYPE_V1_PANEL
= 1 << 2, // For apps v1 support in Ash; deprecate
91 FRAME_CHROME
, // Chrome-style window frame.
92 FRAME_NONE
, // Frameless window.
97 FULLSCREEN_TYPE_NONE
= 0,
99 // Fullscreen entered by the app.window api.
100 FULLSCREEN_TYPE_WINDOW_API
= 1 << 0,
102 // Fullscreen entered by HTML requestFullscreen().
103 FULLSCREEN_TYPE_HTML_API
= 1 << 1,
105 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
106 // enter immersive fullscreen when the user hits the <F4> key.
107 FULLSCREEN_TYPE_OS
= 1 << 2
110 class SizeConstraints
{
112 // The value SizeConstraints uses to represent an unbounded width or height.
113 // This is an enum so that it can be declared inline here.
114 enum { kUnboundedSize
= 0 };
117 SizeConstraints(const gfx::Size
& min_size
, const gfx::Size
& max_size
);
120 // Returns the bounds with its size clamped to the min/max size.
121 gfx::Size
ClampSize(gfx::Size size
) const;
123 // When gfx::Size is used as a min/max size, a zero represents an unbounded
124 // component. This method checks whether either component is specified.
125 // Note we can't use gfx::Size::IsEmpty as it returns true if either width
126 // or height is zero.
127 bool HasMinimumSize() const;
128 bool HasMaximumSize() const;
130 // This returns true if all components are specified, and min and max are
132 bool HasFixedSize() const;
134 gfx::Size
GetMaximumSize() const;
135 gfx::Size
GetMinimumSize() const;
137 void set_minimum_size(const gfx::Size
& min_size
);
138 void set_maximum_size(const gfx::Size
& max_size
);
141 gfx::Size minimum_size_
;
142 gfx::Size maximum_size_
;
145 struct CreateParams
{
149 WindowType window_type
;
151 bool transparent_background
; // Only supported on ash.
153 // Specify the initial content bounds of the window (excluding any window
154 // decorations). INT_MIN designates 'unspecified' for the position
155 // components, and 0 for the size components. When unspecified, they should
156 // be replaced with a default value.
159 gfx::Size minimum_size
;
160 gfx::Size maximum_size
;
162 std::string window_key
;
164 // The process ID of the process that requested the create.
165 int32 creator_process_id
;
167 // Initial state of the window.
168 ui::WindowShowState state
;
170 // If true, don't show the window after creation.
173 // If true, the window will be resizable by the user. Defaults to true.
176 // If true, the window will be focused on creation. Defaults to true.
179 // If true, the window will stay on top of other windows that are not
180 // configured to be always on top. Defaults to false.
188 // General initialization.
189 virtual void InitWebContents(content::WebContents
* web_contents
) = 0;
190 virtual NativeAppWindow
* CreateNativeAppWindow(
192 const CreateParams
& params
) = 0;
195 virtual content::WebContents
* OpenURLFromTab(
197 content::WebContents
* source
,
198 const content::OpenURLParams
& params
) = 0;
199 virtual void AddNewContents(Profile
* profile
,
200 content::WebContents
* new_contents
,
201 WindowOpenDisposition disposition
,
202 const gfx::Rect
& initial_pos
,
204 bool* was_blocked
) = 0;
207 virtual content::ColorChooser
* ShowColorChooser(
208 content::WebContents
* web_contents
,
209 SkColor initial_color
) = 0;
210 virtual void RunFileChooser(content::WebContents
* tab
,
211 const content::FileChooserParams
& params
) = 0;
212 virtual void RequestMediaAccessPermission(
213 content::WebContents
* web_contents
,
214 const content::MediaStreamRequest
& request
,
215 const content::MediaResponseCallback
& callback
,
216 const extensions::Extension
* extension
) = 0;
217 virtual int PreferredIconSize() = 0;
219 // Web contents modal dialog support.
220 virtual void SetWebContentsBlocked(content::WebContents
* web_contents
,
222 virtual bool IsWebContentsVisible(content::WebContents
* web_contents
) = 0;
225 // Convert draggable regions in raw format to SkRegion format. Caller is
226 // responsible for deleting the returned SkRegion instance.
227 static SkRegion
* RawDraggableRegionsToSkRegion(
228 const std::vector
<extensions::DraggableRegion
>& regions
);
230 // The constructor and Init methods are public for constructing a ShellWindow
231 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
232 // Normally ShellWindow::Create should be used.
233 // The constructed shell window takes ownership of |delegate|.
234 ShellWindow(Profile
* profile
,
236 const extensions::Extension
* extension
);
238 // Initializes the render interface, web contents, and native window.
239 // |shell_window_contents| will become owned by ShellWindow.
240 void Init(const GURL
& url
,
241 ShellWindowContents
* shell_window_contents
,
242 const CreateParams
& params
);
245 const std::string
& window_key() const { return window_key_
; }
246 const SessionID
& session_id() const { return session_id_
; }
247 const extensions::Extension
* extension() const { return extension_
; }
248 const std::string
& extension_id() const { return extension_id_
; }
249 content::WebContents
* web_contents() const;
250 WindowType
window_type() const { return window_type_
; }
251 bool window_type_is_panel() const {
252 return (window_type_
== WINDOW_TYPE_PANEL
||
253 window_type_
== WINDOW_TYPE_V1_PANEL
);
255 Profile
* profile() const { return profile_
; }
256 const gfx::Image
& app_icon() const { return app_icon_
; }
257 const GURL
& app_icon_url() { return app_icon_url_
; }
259 NativeAppWindow
* GetBaseWindow();
260 gfx::NativeWindow
GetNativeWindow();
262 // Returns the bounds that should be reported to the renderer.
263 gfx::Rect
GetClientBounds() const;
265 // NativeAppWindows should call this to determine what the window's title
266 // is on startup and from within UpdateWindowTitle().
267 string16
GetTitle() const;
269 // Call to notify ShellRegistry and delete the window. Subclasses should
270 // invoke this method instead of using "delete this".
271 void OnNativeClose();
273 // Should be called by native implementations when the window size, position,
274 // or minimized/maximized state has changed.
275 void OnNativeWindowChanged();
277 // Should be called by native implementations when the window is activated.
278 void OnNativeWindowActivated();
280 // Specifies a url for the launcher icon.
281 void SetAppIconUrl(const GURL
& icon_url
);
283 // Set the window shape. Passing a NULL |region| sets the default shape.
284 void UpdateShape(scoped_ptr
<SkRegion
> region
);
286 // Called from the render interface to modify the draggable regions.
287 void UpdateDraggableRegions(
288 const std::vector
<extensions::DraggableRegion
>& regions
);
290 // Updates the app image to |image|. Called internally from the image loader
291 // callback. Also called externally for v1 apps using Ash Panels.
292 void UpdateAppIcon(const gfx::Image
& image
);
294 // Transitions window into fullscreen, maximized, minimized or restores based
295 // on chrome.app.window API.
301 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
304 // Set the minimum and maximum size that this window is allowed to be.
305 void SetMinimumSize(const gfx::Size
& min_size
);
306 void SetMaximumSize(const gfx::Size
& max_size
);
313 // Shows the window if its contents have been painted; otherwise flags the
314 // window to be shown as soon as its contents are painted for the first time.
315 void Show(ShowType show_type
);
317 // Hides the window. If the window was previously flagged to be shown on
318 // first paint, it will be unflagged.
321 ShellWindowContents
* shell_window_contents_for_test() {
322 return shell_window_contents_
.get();
325 // Get the size constraints.
326 const SizeConstraints
& size_constraints() const {
327 return size_constraints_
;
330 // Set whether the window should stay above other windows which are not
331 // configured to be always-on-top.
332 void SetAlwaysOnTop(bool always_on_top
);
334 // Whether the always-on-top property has been set by the chrome.app.window
335 // API. Note that the actual value of this property in the native app window
336 // will be false in fullscreen mode.
337 bool IsAlwaysOnTop() const;
340 virtual ~ShellWindow();
343 // PlatformAppBrowserTest needs access to web_contents()
344 friend class extensions::PlatformAppBrowserTest
;
346 // content::WebContentsDelegate implementation.
347 virtual void CloseContents(content::WebContents
* contents
) OVERRIDE
;
348 virtual bool ShouldSuppressDialogs() OVERRIDE
;
349 virtual content::ColorChooser
* OpenColorChooser(
350 content::WebContents
* web_contents
,
352 const std::vector
<content::ColorSuggestion
>& suggestions
) OVERRIDE
;
353 virtual void RunFileChooser(
354 content::WebContents
* tab
,
355 const content::FileChooserParams
& params
) OVERRIDE
;
356 virtual bool IsPopupOrPanel(
357 const content::WebContents
* source
) const OVERRIDE
;
358 virtual void MoveContents(
359 content::WebContents
* source
, const gfx::Rect
& pos
) OVERRIDE
;
360 virtual void NavigationStateChanged(const content::WebContents
* source
,
361 unsigned changed_flags
) OVERRIDE
;
362 virtual void ToggleFullscreenModeForTab(content::WebContents
* source
,
363 bool enter_fullscreen
) OVERRIDE
;
364 virtual bool IsFullscreenForTabOrPending(
365 const content::WebContents
* source
) const OVERRIDE
;
366 virtual void RequestMediaAccessPermission(
367 content::WebContents
* web_contents
,
368 const content::MediaStreamRequest
& request
,
369 const content::MediaResponseCallback
& callback
) OVERRIDE
;
370 virtual content::WebContents
* OpenURLFromTab(
371 content::WebContents
* source
,
372 const content::OpenURLParams
& params
) OVERRIDE
;
373 virtual void AddNewContents(content::WebContents
* source
,
374 content::WebContents
* new_contents
,
375 WindowOpenDisposition disposition
,
376 const gfx::Rect
& initial_pos
,
378 bool* was_blocked
) OVERRIDE
;
379 virtual bool PreHandleKeyboardEvent(
380 content::WebContents
* source
,
381 const content::NativeWebKeyboardEvent
& event
,
382 bool* is_keyboard_shortcut
) OVERRIDE
;
383 virtual void HandleKeyboardEvent(
384 content::WebContents
* source
,
385 const content::NativeWebKeyboardEvent
& event
) OVERRIDE
;
386 virtual void RequestToLockMouse(content::WebContents
* web_contents
,
388 bool last_unlocked_by_target
) OVERRIDE
;
390 // content::WebContentsObserver implementation.
391 virtual void DidFirstVisuallyNonEmptyPaint(int32 page_id
) OVERRIDE
;
393 // content::NotificationObserver implementation.
394 virtual void Observe(int type
,
395 const content::NotificationSource
& source
,
396 const content::NotificationDetails
& details
) OVERRIDE
;
398 // web_modal::WebContentsModalDialogManagerDelegate implementation.
399 virtual void SetWebContentsBlocked(content::WebContents
* web_contents
,
400 bool blocked
) OVERRIDE
;
401 virtual bool IsWebContentsVisible(
402 content::WebContents
* web_contents
) OVERRIDE
;
404 // Helper method to add a message to the renderer's DevTools console.
405 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level
,
406 const std::string
& message
);
408 // Saves the window geometry/position/screen bounds.
409 void SaveWindowPosition();
411 // Helper method to adjust the cached bounds so that we can make sure it can
412 // be visible on the screen. See http://crbug.com/145752 .
413 void AdjustBoundsToBeVisibleOnScreen(
414 const gfx::Rect
& cached_bounds
,
415 const gfx::Rect
& cached_screen_bounds
,
416 const gfx::Rect
& current_screen_bounds
,
417 const gfx::Size
& minimum_size
,
418 gfx::Rect
* bounds
) const;
420 // Loads the appropriate default or cached window bounds and constrains them
421 // based on screen size and minimum/maximum size. Returns a new CreateParams
422 // that should be used to create the window.
423 CreateParams
LoadDefaultsAndConstrain(CreateParams params
) const;
425 // Load the app's image, firing a load state change when loaded.
426 void UpdateExtensionAppIcon();
428 // Called when size_constraints is changed.
429 void OnSizeConstraintsChanged();
431 // Set the fullscreen state in the native app window.
432 void SetNativeWindowFullscreen(int fullscreen_types
);
434 // extensions::ExtensionKeybindingRegistry::Delegate implementation.
435 virtual extensions::ActiveTabPermissionGranter
*
436 GetActiveTabPermissionGranter() OVERRIDE
;
438 // web_modal::WebContentsModalDialogManagerDelegate implementation.
439 virtual web_modal::WebContentsModalDialogHost
*
440 GetWebContentsModalDialogHost() OVERRIDE
;
442 // Callback from web_contents()->DownloadFavicon.
443 void DidDownloadFavicon(int id
,
444 int http_status_code
,
445 const GURL
& image_url
,
446 const std::vector
<SkBitmap
>& bitmaps
,
447 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
449 // extensions::IconImage::Observer implementation.
450 virtual void OnExtensionIconImageChanged(
451 extensions::IconImage
* image
) OVERRIDE
;
453 Profile
* profile_
; // weak pointer - owned by ProfileManager.
454 // weak pointer - owned by ExtensionService.
455 const extensions::Extension
* extension_
;
456 const std::string extension_id_
;
458 // Identifier that is used when saving and restoring geometry for this
460 std::string window_key_
;
462 const SessionID session_id_
;
463 WindowType window_type_
;
464 content::NotificationRegistrar registrar_
;
466 // Icon shown in the task bar.
467 gfx::Image app_icon_
;
469 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
470 // be fetched and set using this URL.
473 // An object to load the app's icon as an extension resource.
474 scoped_ptr
<extensions::IconImage
> app_icon_image_
;
476 scoped_ptr
<NativeAppWindow
> native_app_window_
;
477 scoped_ptr
<ShellWindowContents
> shell_window_contents_
;
478 scoped_ptr
<Delegate
> delegate_
;
480 base::WeakPtrFactory
<ShellWindow
> image_loader_ptr_factory_
;
482 // Bit field of FullscreenType.
483 int fullscreen_types_
;
485 // Size constraints on the window.
486 SizeConstraints size_constraints_
;
488 // Show has been called, so the window should be shown once the first visually
489 // non-empty paint occurs.
490 bool show_on_first_paint_
;
492 // The first visually non-empty paint has completed.
493 bool first_paint_complete_
;
495 // Whether the delayed Show() call was for an active or inactive window.
496 ShowType delayed_show_type_
;
498 // Cache the desired value of the always-on-top property. When windows enter
499 // fullscreen, this property will be automatically switched off for security
500 // reasons. It is reinstated when the window exits fullscreen.
501 bool cached_always_on_top_
;
503 DISALLOW_COPY_AND_ASSIGN(ShellWindow
);
508 #endif // APPS_SHELL_WINDOW_H_