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_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/sessions/session_id.h"
11 #include "components/web_modal/popup_manager.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "content/public/common/console_message_level.h"
18 #include "extensions/browser/extension_icon_image.h"
19 #include "ui/base/ui_base_types.h" // WindowShowState
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/rect.h"
27 class DictionaryValue
;
35 namespace extensions
{
37 class PlatformAppBrowserTest
;
38 class WindowController
;
40 struct DraggableRegion
;
49 class NativeAppWindow
;
51 // Manages the web contents for app windows. The implementation for this
52 // class should create and maintain the WebContents for the window, and handle
53 // any message passing between the web contents and the extension system or
55 class AppWindowContents
{
57 AppWindowContents() {}
58 virtual ~AppWindowContents() {}
60 // Called to initialize the WebContents, before the app window is created.
61 virtual void Initialize(content::BrowserContext
* context
,
64 // Called to load the contents, after the app window is created.
65 virtual void LoadContents(int32 creator_process_id
) = 0;
67 // Called when the native window changes.
68 virtual void NativeWindowChanged(NativeAppWindow
* native_app_window
) = 0;
70 // Called when the native window closes.
71 virtual void NativeWindowClosed() = 0;
73 // Called in tests when the window is shown
74 virtual void DispatchWindowShownForTests() const = 0;
76 virtual content::WebContents
* GetWebContents() const = 0;
79 DISALLOW_COPY_AND_ASSIGN(AppWindowContents
);
82 // AppWindow is the type of window used by platform apps. App windows
83 // have a WebContents but none of the chrome of normal browser windows.
84 class AppWindow
: public content::NotificationObserver
,
85 public content::WebContentsDelegate
,
86 public content::WebContentsObserver
,
87 public web_modal::WebContentsModalDialogManagerDelegate
,
88 public extensions::IconImage::Observer
{
91 WINDOW_TYPE_DEFAULT
= 1 << 0, // Default app window.
92 WINDOW_TYPE_PANEL
= 1 << 1, // OS controlled panel window (Ash only).
93 WINDOW_TYPE_V1_PANEL
= 1 << 2, // For apps v1 support in Ash; deprecate
98 FRAME_CHROME
, // Chrome-style window frame.
99 FRAME_NONE
, // Frameless window.
102 enum FullscreenType
{
104 FULLSCREEN_TYPE_NONE
= 0,
106 // Fullscreen entered by the app.window api.
107 FULLSCREEN_TYPE_WINDOW_API
= 1 << 0,
109 // Fullscreen entered by HTML requestFullscreen().
110 FULLSCREEN_TYPE_HTML_API
= 1 << 1,
112 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
113 // enter immersive fullscreen when the user hits the <F4> key.
114 FULLSCREEN_TYPE_OS
= 1 << 2,
116 // Fullscreen mode that could not be exited by the user. ChromeOS uses
117 // this type of fullscreen to run an app in kiosk mode.
118 FULLSCREEN_TYPE_FORCED
= 1 << 3,
121 struct BoundsSpecification
{
122 // INT_MIN represents an unspecified position component.
123 static const int kUnspecifiedPosition
;
125 BoundsSpecification();
126 ~BoundsSpecification();
128 // INT_MIN designates 'unspecified' for the position components, and 0
129 // designates 'unspecified' for the size components. When unspecified,
130 // they should be replaced with a default value.
133 gfx::Size minimum_size
;
134 gfx::Size maximum_size
;
136 // Reset the bounds fields to their 'unspecified' values. The minimum and
137 // maximum size constraints remain unchanged.
141 struct CreateParams
{
145 WindowType window_type
;
148 bool has_frame_color
;
149 SkColor active_frame_color
;
150 SkColor inactive_frame_color
;
151 bool transparent_background
; // Only supported on ash.
153 // The initial content/inner bounds specification (excluding any window
155 BoundsSpecification content_spec
;
157 // The initial window/outer bounds specification (including window
159 BoundsSpecification window_spec
;
161 std::string window_key
;
163 // The process ID of the process that requested the create.
164 int32 creator_process_id
;
166 // Initial state of the window.
167 ui::WindowShowState state
;
169 // If true, don't show the window after creation.
172 // If true, the window will be resizable by the user. Defaults to true.
175 // If true, the window will be focused on creation. Defaults to true.
178 // If true, the window will stay on top of other windows that are not
179 // configured to be always on top. Defaults to false.
182 // The API enables developers to specify content or window bounds. This
183 // function combines them into a single, constrained window size.
184 gfx::Rect
GetInitialWindowBounds(const gfx::Insets
& frame_insets
) const;
186 // The API enables developers to specify content or window size constraints.
187 // These functions combine them so that we can work with one set of
189 gfx::Size
GetContentMinimumSize(const gfx::Insets
& frame_insets
) const;
190 gfx::Size
GetContentMaximumSize(const gfx::Insets
& frame_insets
) const;
191 gfx::Size
GetWindowMinimumSize(const gfx::Insets
& frame_insets
) const;
192 gfx::Size
GetWindowMaximumSize(const gfx::Insets
& frame_insets
) const;
199 // General initialization.
200 virtual void InitWebContents(content::WebContents
* web_contents
) = 0;
201 virtual NativeAppWindow
* CreateNativeAppWindow(
203 const CreateParams
& params
) = 0;
206 virtual content::WebContents
* OpenURLFromTab(
207 content::BrowserContext
* context
,
208 content::WebContents
* source
,
209 const content::OpenURLParams
& params
) = 0;
210 virtual void AddNewContents(content::BrowserContext
* context
,
211 content::WebContents
* new_contents
,
212 WindowOpenDisposition disposition
,
213 const gfx::Rect
& initial_pos
,
215 bool* was_blocked
) = 0;
218 virtual content::ColorChooser
* ShowColorChooser(
219 content::WebContents
* web_contents
,
220 SkColor initial_color
) = 0;
221 virtual void RunFileChooser(content::WebContents
* tab
,
222 const content::FileChooserParams
& params
) = 0;
223 virtual void RequestMediaAccessPermission(
224 content::WebContents
* web_contents
,
225 const content::MediaStreamRequest
& request
,
226 const content::MediaResponseCallback
& callback
,
227 const extensions::Extension
* extension
) = 0;
228 virtual int PreferredIconSize() = 0;
230 // Web contents modal dialog support.
231 virtual void SetWebContentsBlocked(content::WebContents
* web_contents
,
233 virtual bool IsWebContentsVisible(content::WebContents
* web_contents
) = 0;
236 // Convert draggable regions in raw format to SkRegion format. Caller is
237 // responsible for deleting the returned SkRegion instance.
238 static SkRegion
* RawDraggableRegionsToSkRegion(
239 const std::vector
<extensions::DraggableRegion
>& regions
);
241 // The constructor and Init methods are public for constructing a AppWindow
242 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
243 // Normally AppWindow::Create should be used.
244 // The constructed app window takes ownership of |delegate|.
245 AppWindow(content::BrowserContext
* context
,
247 const extensions::Extension
* extension
);
249 // Initializes the render interface, web contents, and native window.
250 // |app_window_contents| will become owned by AppWindow.
251 void Init(const GURL
& url
,
252 AppWindowContents
* app_window_contents
,
253 const CreateParams
& params
);
255 const std::string
& window_key() const { return window_key_
; }
256 const SessionID
& session_id() const { return session_id_
; }
257 const std::string
& extension_id() const { return extension_id_
; }
258 content::WebContents
* web_contents() const;
259 WindowType
window_type() const { return window_type_
; }
260 bool window_type_is_panel() const {
261 return (window_type_
== WINDOW_TYPE_PANEL
||
262 window_type_
== WINDOW_TYPE_V1_PANEL
);
264 content::BrowserContext
* browser_context() const { return browser_context_
; }
265 const gfx::Image
& app_icon() const { return app_icon_
; }
266 const GURL
& app_icon_url() const { return app_icon_url_
; }
267 const gfx::Image
& badge_icon() const { return badge_icon_
; }
268 const GURL
& badge_icon_url() const { return badge_icon_url_
; }
269 bool is_hidden() const { return is_hidden_
; }
271 const extensions::Extension
* GetExtension() const;
272 NativeAppWindow
* GetBaseWindow();
273 gfx::NativeWindow
GetNativeWindow();
275 // Returns the bounds that should be reported to the renderer.
276 gfx::Rect
GetClientBounds() const;
278 // NativeAppWindows should call this to determine what the window's title
279 // is on startup and from within UpdateWindowTitle().
280 base::string16
GetTitle() const;
282 // Call to notify ShellRegistry and delete the window. Subclasses should
283 // invoke this method instead of using "delete this".
284 void OnNativeClose();
286 // Should be called by native implementations when the window size, position,
287 // or minimized/maximized state has changed.
288 void OnNativeWindowChanged();
290 // Should be called by native implementations when the window is activated.
291 void OnNativeWindowActivated();
293 // Specifies a url for the launcher icon.
294 void SetAppIconUrl(const GURL
& icon_url
);
296 // Specifies a url for the window badge.
297 void SetBadgeIconUrl(const GURL
& icon_url
);
299 // Clear the current badge.
302 // Set the window shape. Passing a NULL |region| sets the default shape.
303 void UpdateShape(scoped_ptr
<SkRegion
> region
);
305 // Called from the render interface to modify the draggable regions.
306 void UpdateDraggableRegions(
307 const std::vector
<extensions::DraggableRegion
>& regions
);
309 // Updates the app image to |image|. Called internally from the image loader
310 // callback. Also called externally for v1 apps using Ash Panels.
311 void UpdateAppIcon(const gfx::Image
& image
);
313 // Transitions window into fullscreen, maximized, minimized or restores based
314 // on chrome.app.window API.
320 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
323 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
325 void ForcedFullscreen();
327 // Set the minimum and maximum size of the content bounds.
328 void SetContentSizeConstraints(const gfx::Size
& min_size
,
329 const gfx::Size
& max_size
);
331 enum ShowType
{ SHOW_ACTIVE
, SHOW_INACTIVE
};
333 // Shows the window if its contents have been painted; otherwise flags the
334 // window to be shown as soon as its contents are painted for the first time.
335 void Show(ShowType show_type
);
337 // Hides the window. If the window was previously flagged to be shown on
338 // first paint, it will be unflagged.
341 AppWindowContents
* app_window_contents_for_test() {
342 return app_window_contents_
.get();
345 int fullscreen_types_for_test() {
346 return fullscreen_types_
;
349 // Set whether the window should stay above other windows which are not
350 // configured to be always-on-top.
351 void SetAlwaysOnTop(bool always_on_top
);
353 // Whether the always-on-top property has been set by the chrome.app.window
354 // API. Note that the actual value of this property in the native app window
355 // may be false if the bit is silently switched off for security reasons.
356 bool IsAlwaysOnTop() const;
358 // Retrieve the current state of the app window as a dictionary, to pass to
360 void GetSerializedState(base::DictionaryValue
* properties
) const;
362 // Called by the window API when events can be sent to the window for this
364 void WindowEventsReady();
367 virtual ~AppWindow();
370 // PlatformAppBrowserTest needs access to web_contents()
371 friend class extensions::PlatformAppBrowserTest
;
373 // content::WebContentsDelegate implementation.
374 virtual void CloseContents(content::WebContents
* contents
) OVERRIDE
;
375 virtual bool ShouldSuppressDialogs() OVERRIDE
;
376 virtual content::ColorChooser
* OpenColorChooser(
377 content::WebContents
* web_contents
,
379 const std::vector
<content::ColorSuggestion
>& suggestions
) OVERRIDE
;
380 virtual void RunFileChooser(content::WebContents
* tab
,
381 const content::FileChooserParams
& params
)
383 virtual bool IsPopupOrPanel(const content::WebContents
* source
)
385 virtual void MoveContents(content::WebContents
* source
,
386 const gfx::Rect
& pos
) OVERRIDE
;
387 virtual void NavigationStateChanged(const content::WebContents
* source
,
388 unsigned changed_flags
) OVERRIDE
;
389 virtual void ToggleFullscreenModeForTab(content::WebContents
* source
,
390 bool enter_fullscreen
) OVERRIDE
;
391 virtual bool IsFullscreenForTabOrPending(const content::WebContents
* source
)
393 virtual void RequestMediaAccessPermission(
394 content::WebContents
* web_contents
,
395 const content::MediaStreamRequest
& request
,
396 const content::MediaResponseCallback
& callback
) OVERRIDE
;
397 virtual content::WebContents
* OpenURLFromTab(
398 content::WebContents
* source
,
399 const content::OpenURLParams
& params
) OVERRIDE
;
400 virtual void AddNewContents(content::WebContents
* source
,
401 content::WebContents
* new_contents
,
402 WindowOpenDisposition disposition
,
403 const gfx::Rect
& initial_pos
,
405 bool* was_blocked
) OVERRIDE
;
406 virtual bool PreHandleKeyboardEvent(
407 content::WebContents
* source
,
408 const content::NativeWebKeyboardEvent
& event
,
409 bool* is_keyboard_shortcut
) OVERRIDE
;
410 virtual void HandleKeyboardEvent(content::WebContents
* source
,
411 const content::NativeWebKeyboardEvent
& event
)
413 virtual void RequestToLockMouse(content::WebContents
* web_contents
,
415 bool last_unlocked_by_target
) OVERRIDE
;
416 virtual bool PreHandleGestureEvent(content::WebContents
* source
,
417 const blink::WebGestureEvent
& event
)
420 // content::WebContentsObserver implementation.
421 virtual void DidFirstVisuallyNonEmptyPaint() OVERRIDE
;
423 // content::NotificationObserver implementation.
424 virtual void Observe(int type
,
425 const content::NotificationSource
& source
,
426 const content::NotificationDetails
& details
) OVERRIDE
;
428 // web_modal::WebContentsModalDialogManagerDelegate implementation.
429 virtual void SetWebContentsBlocked(content::WebContents
* web_contents
,
430 bool blocked
) OVERRIDE
;
431 virtual bool IsWebContentsVisible(content::WebContents
* web_contents
)
434 // Helper method to add a message to the renderer's DevTools console.
435 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level
,
436 const std::string
& message
);
438 // Saves the window geometry/position/screen bounds.
439 void SaveWindowPosition();
441 // Helper method to adjust the cached bounds so that we can make sure it can
442 // be visible on the screen. See http://crbug.com/145752.
443 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect
& cached_bounds
,
444 const gfx::Rect
& cached_screen_bounds
,
445 const gfx::Rect
& current_screen_bounds
,
446 const gfx::Size
& minimum_size
,
447 gfx::Rect
* bounds
) const;
449 // Loads the appropriate default or cached window bounds. Returns a new
450 // CreateParams that should be used to create the window.
451 CreateParams
LoadDefaults(CreateParams params
) const;
453 // Load the app's image, firing a load state change when loaded.
454 void UpdateExtensionAppIcon();
456 // Set the fullscreen state in the native app window.
457 void SetNativeWindowFullscreen();
459 // Returns true if there is any overlap between the window and the taskbar
461 bool IntersectsWithTaskbar() const;
463 // Update the always-on-top bit in the native app window.
464 void UpdateNativeAlwaysOnTop();
466 // Sends the onWindowShown event to the app if the window has been shown. Only
467 // has an effect in tests.
468 void SendOnWindowShownIfShown();
470 // web_modal::WebContentsModalDialogManagerDelegate implementation.
471 virtual web_modal::WebContentsModalDialogHost
* GetWebContentsModalDialogHost()
474 // Updates the badge to |image|. Called internally from the image loader
476 void UpdateBadgeIcon(const gfx::Image
& image
);
478 // Callback from web_contents()->DownloadFavicon.
479 void DidDownloadFavicon(int id
,
480 int http_status_code
,
481 const GURL
& image_url
,
482 const std::vector
<SkBitmap
>& bitmaps
,
483 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
485 // extensions::IconImage::Observer implementation.
486 virtual void OnExtensionIconImageChanged(extensions::IconImage
* image
)
489 // The browser context with which this window is associated. AppWindow does
490 // not own this object.
491 content::BrowserContext
* browser_context_
;
493 const std::string extension_id_
;
495 // Identifier that is used when saving and restoring geometry for this
497 std::string window_key_
;
499 const SessionID session_id_
;
500 WindowType window_type_
;
501 content::NotificationRegistrar registrar_
;
503 // Icon shown in the task bar.
504 gfx::Image app_icon_
;
506 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
507 // be fetched and set using this URL.
510 // An object to load the app's icon as an extension resource.
511 scoped_ptr
<extensions::IconImage
> app_icon_image_
;
513 // Badge for icon shown in the task bar.
514 gfx::Image badge_icon_
;
516 // URL to be used for setting the badge on the app icon.
517 GURL badge_icon_url_
;
519 // An object to load the badge as an extension resource.
520 scoped_ptr
<extensions::IconImage
> badge_icon_image_
;
522 scoped_ptr
<NativeAppWindow
> native_app_window_
;
523 scoped_ptr
<AppWindowContents
> app_window_contents_
;
524 scoped_ptr
<Delegate
> delegate_
;
526 // Manages popup windows (bubbles, tab-modals) visible overlapping the
528 scoped_ptr
<web_modal::PopupManager
> popup_manager_
;
530 base::WeakPtrFactory
<AppWindow
> image_loader_ptr_factory_
;
532 // Bit field of FullscreenType.
533 int fullscreen_types_
;
535 // Show has been called, so the window should be shown once the first visually
536 // non-empty paint occurs.
537 bool show_on_first_paint_
;
539 // The first visually non-empty paint has completed.
540 bool first_paint_complete_
;
542 // Whether the window has been shown or not.
543 bool has_been_shown_
;
545 // Whether events can be sent to the window.
546 bool can_send_events_
;
548 // Whether the window is hidden or not. Hidden in this context means actively
549 // by the chrome.app.window API, not in an operating system context. For
550 // example windows which are minimized are not hidden, and windows which are
551 // part of a hidden app on OS X are not hidden. Windows which were created
552 // with the |hidden| flag set to true, or which have been programmatically
553 // hidden, are considered hidden.
556 // Whether the delayed Show() call was for an active or inactive window.
557 ShowType delayed_show_type_
;
559 // Cache the desired value of the always-on-top property. When windows enter
560 // fullscreen or overlap the Windows taskbar, this property will be
561 // automatically and silently switched off for security reasons. It is
562 // reinstated when the window exits fullscreen and moves away from the
564 bool cached_always_on_top_
;
566 DISALLOW_COPY_AND_ASSIGN(AppWindow
);
571 #endif // APPS_APP_WINDOW_H_