1 // Copyright 2014 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_APP_WINDOW_H_
6 #define APPS_APP_WINDOW_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "chrome/browser/sessions/session_id.h"
14 #include "components/web_modal/popup_manager.h"
15 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "content/public/browser/web_contents_delegate.h"
19 #include "content/public/browser/web_contents_observer.h"
20 #include "content/public/common/console_message_level.h"
21 #include "extensions/browser/extension_icon_image.h"
22 #include "ui/base/ui_base_types.h" // WindowShowState
23 #include "ui/gfx/image/image.h"
24 #include "ui/gfx/rect.h"
30 class DictionaryValue
;
38 namespace extensions
{
40 class PlatformAppBrowserTest
;
41 class WindowController
;
43 struct DraggableRegion
;
53 class NativeAppWindow
;
55 // Manages the web contents for app windows. The implementation for this
56 // class should create and maintain the WebContents for the window, and handle
57 // any message passing between the web contents and the extension system or
59 class AppWindowContents
{
61 AppWindowContents() {}
62 virtual ~AppWindowContents() {}
64 // Called to initialize the WebContents, before the app window is created.
65 virtual void Initialize(content::BrowserContext
* context
,
68 // Called to load the contents, after the app window is created.
69 virtual void LoadContents(int32 creator_process_id
) = 0;
71 // Called when the native window changes.
72 virtual void NativeWindowChanged(NativeAppWindow
* native_app_window
) = 0;
74 // Called when the native window closes.
75 virtual void NativeWindowClosed() = 0;
77 // Called in tests when the window is shown
78 virtual void DispatchWindowShownForTests() const = 0;
80 virtual content::WebContents
* GetWebContents() const = 0;
83 DISALLOW_COPY_AND_ASSIGN(AppWindowContents
);
86 // AppWindow is the type of window used by platform apps. App windows
87 // have a WebContents but none of the chrome of normal browser windows.
88 class AppWindow
: public content::NotificationObserver
,
89 public content::WebContentsDelegate
,
90 public content::WebContentsObserver
,
91 public web_modal::WebContentsModalDialogManagerDelegate
,
92 public extensions::IconImage::Observer
{
95 WINDOW_TYPE_DEFAULT
= 1 << 0, // Default app window.
96 WINDOW_TYPE_PANEL
= 1 << 1, // OS controlled panel window (Ash only).
97 WINDOW_TYPE_V1_PANEL
= 1 << 2, // For apps v1 support in Ash; deprecate
102 FRAME_CHROME
, // Chrome-style window frame.
103 FRAME_NONE
, // Frameless window.
106 enum FullscreenType
{
108 FULLSCREEN_TYPE_NONE
= 0,
110 // Fullscreen entered by the app.window api.
111 FULLSCREEN_TYPE_WINDOW_API
= 1 << 0,
113 // Fullscreen entered by HTML requestFullscreen().
114 FULLSCREEN_TYPE_HTML_API
= 1 << 1,
116 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
117 // enter immersive fullscreen when the user hits the <F4> key.
118 FULLSCREEN_TYPE_OS
= 1 << 2,
120 // Fullscreen mode that could not be exited by the user. ChromeOS uses
121 // this type of fullscreen to run an app in kiosk mode.
122 FULLSCREEN_TYPE_FORCED
= 1 << 3,
125 struct BoundsSpecification
{
126 // INT_MIN represents an unspecified position component.
127 static const int kUnspecifiedPosition
;
129 BoundsSpecification();
130 ~BoundsSpecification();
132 // INT_MIN designates 'unspecified' for the position components, and 0
133 // designates 'unspecified' for the size components. When unspecified,
134 // they should be replaced with a default value.
137 gfx::Size minimum_size
;
138 gfx::Size maximum_size
;
140 // Reset the bounds fields to their 'unspecified' values. The minimum and
141 // maximum size constraints remain unchanged.
145 struct CreateParams
{
149 WindowType window_type
;
152 bool has_frame_color
;
153 SkColor active_frame_color
;
154 SkColor inactive_frame_color
;
155 bool transparent_background
; // Only supported on ash.
157 // The initial content/inner bounds specification (excluding any window
159 BoundsSpecification content_spec
;
161 // The initial window/outer bounds specification (including window
163 BoundsSpecification window_spec
;
165 std::string window_key
;
167 // The process ID of the process that requested the create.
168 int32 creator_process_id
;
170 // Initial state of the window.
171 ui::WindowShowState state
;
173 // If true, don't show the window after creation.
176 // If true, the window will be resizable by the user. Defaults to true.
179 // If true, the window will be focused on creation. Defaults to true.
182 // If true, the window will stay on top of other windows that are not
183 // configured to be always on top. Defaults to false.
186 // The API enables developers to specify content or window bounds. This
187 // function combines them into a single, constrained window size.
188 gfx::Rect
GetInitialWindowBounds(const gfx::Insets
& frame_insets
) const;
190 // The API enables developers to specify content or window size constraints.
191 // These functions combine them so that we can work with one set of
193 gfx::Size
GetContentMinimumSize(const gfx::Insets
& frame_insets
) const;
194 gfx::Size
GetContentMaximumSize(const gfx::Insets
& frame_insets
) const;
195 gfx::Size
GetWindowMinimumSize(const gfx::Insets
& frame_insets
) const;
196 gfx::Size
GetWindowMaximumSize(const gfx::Insets
& frame_insets
) const;
203 virtual NativeAppWindow
* CreateNativeAppWindow(
205 const CreateParams
& params
) = 0;
208 // Convert draggable regions in raw format to SkRegion format. Caller is
209 // responsible for deleting the returned SkRegion instance.
210 static SkRegion
* RawDraggableRegionsToSkRegion(
211 const std::vector
<extensions::DraggableRegion
>& regions
);
213 // The constructor and Init methods are public for constructing a AppWindow
214 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
215 // Normally AppWindow::Create should be used.
216 // Takes ownership of |app_delegate| and |delegate|.
217 AppWindow(content::BrowserContext
* context
,
218 AppDelegate
* app_delegate
,
220 const extensions::Extension
* extension
);
222 // Initializes the render interface, web contents, and native window.
223 // |app_window_contents| will become owned by AppWindow.
224 void Init(const GURL
& url
,
225 AppWindowContents
* app_window_contents
,
226 const CreateParams
& params
);
228 const std::string
& window_key() const { return window_key_
; }
229 const SessionID
& session_id() const { return session_id_
; }
230 const std::string
& extension_id() const { return extension_id_
; }
231 content::WebContents
* web_contents() const;
232 WindowType
window_type() const { return window_type_
; }
233 bool window_type_is_panel() const {
234 return (window_type_
== WINDOW_TYPE_PANEL
||
235 window_type_
== WINDOW_TYPE_V1_PANEL
);
237 content::BrowserContext
* browser_context() const { return browser_context_
; }
238 const gfx::Image
& app_icon() const { return app_icon_
; }
239 const GURL
& app_icon_url() const { return app_icon_url_
; }
240 const gfx::Image
& badge_icon() const { return badge_icon_
; }
241 const GURL
& badge_icon_url() const { return badge_icon_url_
; }
242 bool is_hidden() const { return is_hidden_
; }
244 const extensions::Extension
* GetExtension() const;
245 NativeAppWindow
* GetBaseWindow();
246 gfx::NativeWindow
GetNativeWindow();
248 // Returns the bounds that should be reported to the renderer.
249 gfx::Rect
GetClientBounds() const;
251 // NativeAppWindows should call this to determine what the window's title
252 // is on startup and from within UpdateWindowTitle().
253 base::string16
GetTitle() const;
255 // Call to notify ShellRegistry and delete the window. Subclasses should
256 // invoke this method instead of using "delete this".
257 void OnNativeClose();
259 // Should be called by native implementations when the window size, position,
260 // or minimized/maximized state has changed.
261 void OnNativeWindowChanged();
263 // Should be called by native implementations when the window is activated.
264 void OnNativeWindowActivated();
266 // Specifies a url for the launcher icon.
267 void SetAppIconUrl(const GURL
& icon_url
);
269 // Specifies a url for the window badge.
270 void SetBadgeIconUrl(const GURL
& icon_url
);
272 // Clear the current badge.
275 // Set the window shape. Passing a NULL |region| sets the default shape.
276 void UpdateShape(scoped_ptr
<SkRegion
> region
);
278 // Called from the render interface to modify the draggable regions.
279 void UpdateDraggableRegions(
280 const std::vector
<extensions::DraggableRegion
>& regions
);
282 // Updates the app image to |image|. Called internally from the image loader
283 // callback. Also called externally for v1 apps using Ash Panels.
284 void UpdateAppIcon(const gfx::Image
& image
);
286 // Enable or disable fullscreen mode. |type| specifies which type of
287 // fullscreen mode to change (note that disabling one type of fullscreen may
288 // not exit fullscreen mode because a window may have a different type of
289 // fullscreen enabled). If |type| is not FORCED, checks that the extension has
290 // the required permission.
291 void SetFullscreen(FullscreenType type
, bool enable
);
293 // Returns true if the app window is in a fullscreen state.
294 bool IsFullscreen() const;
296 // Returns true if the app window is in a forced fullscreen state (one that
297 // cannot be exited by the user).
298 bool IsForcedFullscreen() const;
300 // Returns true if the app window is in a fullscreen state entered from an
302 bool IsHtmlApiFullscreen() const;
304 // Transitions window into fullscreen, maximized, minimized or restores based
305 // on chrome.app.window API.
311 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
314 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
316 void ForcedFullscreen();
318 // Set the minimum and maximum size of the content bounds.
319 void SetContentSizeConstraints(const gfx::Size
& min_size
,
320 const gfx::Size
& max_size
);
322 enum ShowType
{ SHOW_ACTIVE
, SHOW_INACTIVE
};
324 // Shows the window if its contents have been painted; otherwise flags the
325 // window to be shown as soon as its contents are painted for the first time.
326 void Show(ShowType show_type
);
328 // Hides the window. If the window was previously flagged to be shown on
329 // first paint, it will be unflagged.
332 AppWindowContents
* app_window_contents_for_test() {
333 return app_window_contents_
.get();
336 int fullscreen_types_for_test() {
337 return fullscreen_types_
;
340 // Set whether the window should stay above other windows which are not
341 // configured to be always-on-top.
342 void SetAlwaysOnTop(bool always_on_top
);
344 // Whether the always-on-top property has been set by the chrome.app.window
345 // API. Note that the actual value of this property in the native app window
346 // may be false if the bit is silently switched off for security reasons.
347 bool IsAlwaysOnTop() const;
349 // Retrieve the current state of the app window as a dictionary, to pass to
351 void GetSerializedState(base::DictionaryValue
* properties
) const;
353 // Called by the window API when events can be sent to the window for this
355 void WindowEventsReady();
357 // Whether the app window wants a transparent background.
358 bool requested_transparent_background() const {
359 return requested_transparent_background_
;
363 virtual ~AppWindow();
366 // PlatformAppBrowserTest needs access to web_contents()
367 friend class extensions::PlatformAppBrowserTest
;
369 // content::WebContentsDelegate implementation.
370 virtual void CloseContents(content::WebContents
* contents
) OVERRIDE
;
371 virtual bool ShouldSuppressDialogs() OVERRIDE
;
372 virtual content::ColorChooser
* OpenColorChooser(
373 content::WebContents
* web_contents
,
375 const std::vector
<content::ColorSuggestion
>& suggestions
) OVERRIDE
;
376 virtual void RunFileChooser(content::WebContents
* tab
,
377 const content::FileChooserParams
& params
)
379 virtual bool IsPopupOrPanel(const content::WebContents
* source
)
381 virtual void MoveContents(content::WebContents
* source
,
382 const gfx::Rect
& pos
) OVERRIDE
;
383 virtual void NavigationStateChanged(const content::WebContents
* source
,
384 unsigned changed_flags
) OVERRIDE
;
385 virtual void ToggleFullscreenModeForTab(content::WebContents
* source
,
386 bool enter_fullscreen
) OVERRIDE
;
387 virtual bool IsFullscreenForTabOrPending(const content::WebContents
* source
)
389 virtual void RequestMediaAccessPermission(
390 content::WebContents
* web_contents
,
391 const content::MediaStreamRequest
& request
,
392 const content::MediaResponseCallback
& callback
) OVERRIDE
;
393 virtual content::WebContents
* OpenURLFromTab(
394 content::WebContents
* source
,
395 const content::OpenURLParams
& params
) OVERRIDE
;
396 virtual void AddNewContents(content::WebContents
* source
,
397 content::WebContents
* new_contents
,
398 WindowOpenDisposition disposition
,
399 const gfx::Rect
& initial_pos
,
401 bool* was_blocked
) OVERRIDE
;
402 virtual bool PreHandleKeyboardEvent(
403 content::WebContents
* source
,
404 const content::NativeWebKeyboardEvent
& event
,
405 bool* is_keyboard_shortcut
) OVERRIDE
;
406 virtual void HandleKeyboardEvent(content::WebContents
* source
,
407 const content::NativeWebKeyboardEvent
& event
)
409 virtual void RequestToLockMouse(content::WebContents
* web_contents
,
411 bool last_unlocked_by_target
) OVERRIDE
;
412 virtual bool PreHandleGestureEvent(content::WebContents
* source
,
413 const blink::WebGestureEvent
& event
)
416 // content::WebContentsObserver implementation.
417 virtual void DidFirstVisuallyNonEmptyPaint() OVERRIDE
;
419 // content::NotificationObserver implementation.
420 virtual void Observe(int type
,
421 const content::NotificationSource
& source
,
422 const content::NotificationDetails
& details
) OVERRIDE
;
424 // web_modal::WebContentsModalDialogManagerDelegate implementation.
425 virtual void SetWebContentsBlocked(content::WebContents
* web_contents
,
426 bool blocked
) OVERRIDE
;
427 virtual bool IsWebContentsVisible(content::WebContents
* web_contents
)
430 // Helper method to add a message to the renderer's DevTools console.
431 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level
,
432 const std::string
& message
);
434 // Saves the window geometry/position/screen bounds.
435 void SaveWindowPosition();
437 // Helper method to adjust the cached bounds so that we can make sure it can
438 // be visible on the screen. See http://crbug.com/145752.
439 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect
& cached_bounds
,
440 const gfx::Rect
& cached_screen_bounds
,
441 const gfx::Rect
& current_screen_bounds
,
442 const gfx::Size
& minimum_size
,
443 gfx::Rect
* bounds
) const;
445 // Loads the appropriate default or cached window bounds. Returns a new
446 // CreateParams that should be used to create the window.
447 CreateParams
LoadDefaults(CreateParams params
) const;
449 // Load the app's image, firing a load state change when loaded.
450 void UpdateExtensionAppIcon();
452 // Set the fullscreen state in the native app window.
453 void SetNativeWindowFullscreen();
455 // Returns true if there is any overlap between the window and the taskbar
457 bool IntersectsWithTaskbar() const;
459 // Update the always-on-top bit in the native app window.
460 void UpdateNativeAlwaysOnTop();
462 // Sends the onWindowShown event to the app if the window has been shown. Only
463 // has an effect in tests.
464 void SendOnWindowShownIfShown();
466 // web_modal::WebContentsModalDialogManagerDelegate implementation.
467 virtual web_modal::WebContentsModalDialogHost
* GetWebContentsModalDialogHost()
470 // Updates the badge to |image|. Called internally from the image loader
472 void UpdateBadgeIcon(const gfx::Image
& image
);
474 // Callback from web_contents()->DownloadFavicon.
475 void DidDownloadFavicon(int id
,
476 int http_status_code
,
477 const GURL
& image_url
,
478 const std::vector
<SkBitmap
>& bitmaps
,
479 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
481 // extensions::IconImage::Observer implementation.
482 virtual void OnExtensionIconImageChanged(extensions::IconImage
* image
)
485 // The browser context with which this window is associated. AppWindow does
486 // not own this object.
487 content::BrowserContext
* browser_context_
;
489 const std::string extension_id_
;
491 // Identifier that is used when saving and restoring geometry for this
493 std::string window_key_
;
495 const SessionID session_id_
;
496 WindowType window_type_
;
497 content::NotificationRegistrar registrar_
;
499 // Icon shown in the task bar.
500 gfx::Image app_icon_
;
502 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
503 // be fetched and set using this URL.
506 // An object to load the app's icon as an extension resource.
507 scoped_ptr
<extensions::IconImage
> app_icon_image_
;
509 // Badge for icon shown in the task bar.
510 gfx::Image badge_icon_
;
512 // URL to be used for setting the badge on the app icon.
513 GURL badge_icon_url_
;
515 // An object to load the badge as an extension resource.
516 scoped_ptr
<extensions::IconImage
> badge_icon_image_
;
518 scoped_ptr
<NativeAppWindow
> native_app_window_
;
519 scoped_ptr
<AppWindowContents
> app_window_contents_
;
520 scoped_ptr
<AppDelegate
> app_delegate_
;
521 scoped_ptr
<Delegate
> delegate_
;
523 // Manages popup windows (bubbles, tab-modals) visible overlapping the
525 scoped_ptr
<web_modal::PopupManager
> popup_manager_
;
527 base::WeakPtrFactory
<AppWindow
> image_loader_ptr_factory_
;
529 // Bit field of FullscreenType.
530 int fullscreen_types_
;
532 // Show has been called, so the window should be shown once the first visually
533 // non-empty paint occurs.
534 bool show_on_first_paint_
;
536 // The first visually non-empty paint has completed.
537 bool first_paint_complete_
;
539 // Whether the window has been shown or not.
540 bool has_been_shown_
;
542 // Whether events can be sent to the window.
543 bool can_send_events_
;
545 // Whether the window is hidden or not. Hidden in this context means actively
546 // by the chrome.app.window API, not in an operating system context. For
547 // example windows which are minimized are not hidden, and windows which are
548 // part of a hidden app on OS X are not hidden. Windows which were created
549 // with the |hidden| flag set to true, or which have been programmatically
550 // hidden, are considered hidden.
553 // Whether the delayed Show() call was for an active or inactive window.
554 ShowType delayed_show_type_
;
556 // Cache the desired value of the always-on-top property. When windows enter
557 // fullscreen or overlap the Windows taskbar, this property will be
558 // automatically and silently switched off for security reasons. It is
559 // reinstated when the window exits fullscreen and moves away from the
561 bool cached_always_on_top_
;
563 // Whether |transparent_background| was set in the CreateParams.
564 bool requested_transparent_background_
;
566 DISALLOW_COPY_AND_ASSIGN(AppWindow
);
571 #endif // APPS_APP_WINDOW_H_