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 EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_
6 #define EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_
11 #include "base/memory/scoped_ptr.h"
12 #include "base/memory/weak_ptr.h"
13 #include "components/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/web_contents_delegate.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/browser/extension_icon_image.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "ui/base/ui_base_types.h" // WindowShowState
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/image/image.h"
28 class DictionaryValue
;
40 namespace extensions
{
43 class AppWebContentsHelper
;
45 class ExtensionRegistry
;
46 class NativeAppWindow
;
47 class PlatformAppBrowserTest
;
48 class WindowController
;
50 struct DraggableRegion
;
52 // Manages the web contents for app windows. The implementation for this
53 // class should create and maintain the WebContents for the window, and handle
54 // any message passing between the web contents and the extension system or
56 class AppWindowContents
{
58 AppWindowContents() {}
59 virtual ~AppWindowContents() {}
61 // Called to initialize the WebContents, before the app window is created.
62 virtual void Initialize(content::BrowserContext
* context
,
65 // Called to load the contents, after the app window is created.
66 virtual void LoadContents(int32 creator_process_id
) = 0;
68 // Called when the native window changes.
69 virtual void NativeWindowChanged(NativeAppWindow
* native_app_window
) = 0;
71 // Called when the native window closes.
72 virtual void NativeWindowClosed() = 0;
74 // Called in tests when the window is shown
75 virtual void DispatchWindowShownForTests() const = 0;
77 virtual content::WebContents
* GetWebContents() const = 0;
80 DISALLOW_COPY_AND_ASSIGN(AppWindowContents
);
83 // AppWindow is the type of window used by platform apps. App windows
84 // have a WebContents but none of the chrome of normal browser windows.
85 class AppWindow
: public content::WebContentsDelegate
,
86 public content::WebContentsObserver
,
87 public web_modal::WebContentsModalDialogManagerDelegate
,
88 public IconImage::Observer
,
89 public ExtensionRegistryObserver
{
92 WINDOW_TYPE_DEFAULT
= 1 << 0, // Default app window.
93 WINDOW_TYPE_PANEL
= 1 << 1, // OS controlled panel window (Ash only).
94 WINDOW_TYPE_V1_PANEL
= 1 << 2, // For apps v1 support in Ash; deprecate
99 FRAME_CHROME
, // Chrome-style window frame.
100 FRAME_NONE
, // Frameless window.
103 enum FullscreenType
{
105 FULLSCREEN_TYPE_NONE
= 0,
107 // Fullscreen entered by the app.window api.
108 FULLSCREEN_TYPE_WINDOW_API
= 1 << 0,
110 // Fullscreen entered by HTML requestFullscreen().
111 FULLSCREEN_TYPE_HTML_API
= 1 << 1,
113 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
114 // enter immersive fullscreen when the user hits the <F4> key.
115 FULLSCREEN_TYPE_OS
= 1 << 2,
117 // Fullscreen mode that could not be exited by the user. ChromeOS uses
118 // this type of fullscreen to run an app in kiosk mode.
119 FULLSCREEN_TYPE_FORCED
= 1 << 3,
122 struct BoundsSpecification
{
123 // INT_MIN represents an unspecified position component.
124 static const int kUnspecifiedPosition
;
126 BoundsSpecification();
127 ~BoundsSpecification();
129 // INT_MIN designates 'unspecified' for the position components, and 0
130 // designates 'unspecified' for the size components. When unspecified,
131 // they should be replaced with a default value.
134 gfx::Size minimum_size
;
135 gfx::Size maximum_size
;
137 // Reset the bounds fields to their 'unspecified' values. The minimum and
138 // maximum size constraints remain unchanged.
142 struct CreateParams
{
146 WindowType window_type
;
149 bool has_frame_color
;
150 SkColor active_frame_color
;
151 SkColor inactive_frame_color
;
155 // The initial content/inner bounds specification (excluding any window
157 BoundsSpecification content_spec
;
159 // The initial window/outer bounds specification (including window
161 BoundsSpecification window_spec
;
163 std::string window_key
;
165 // The process ID of the process that requested the create.
166 int32 creator_process_id
;
168 // Initial state of the window.
169 ui::WindowShowState state
;
171 // If true, don't show the window after creation.
174 // If true, the window will be resizable by the user. Defaults to true.
177 // If true, the window will be focused on creation. Defaults to true.
180 // If true, the window will stay on top of other windows that are not
181 // configured to be always on top. Defaults to false.
184 // If true, the window will be visible on all workspaces. Defaults to false.
185 bool visible_on_all_workspaces
;
187 // The API enables developers to specify content or window bounds. This
188 // function combines them into a single, constrained window size.
189 gfx::Rect
GetInitialWindowBounds(const gfx::Insets
& frame_insets
) const;
191 // The API enables developers to specify content or window size constraints.
192 // These functions combine them so that we can work with one set of
194 gfx::Size
GetContentMinimumSize(const gfx::Insets
& frame_insets
) const;
195 gfx::Size
GetContentMaximumSize(const gfx::Insets
& frame_insets
) const;
196 gfx::Size
GetWindowMinimumSize(const gfx::Insets
& frame_insets
) const;
197 gfx::Size
GetWindowMaximumSize(const gfx::Insets
& frame_insets
) const;
200 // Convert draggable regions in raw format to SkRegion format. Caller is
201 // responsible for deleting the returned SkRegion instance.
202 static SkRegion
* RawDraggableRegionsToSkRegion(
203 const std::vector
<DraggableRegion
>& regions
);
205 // The constructor and Init methods are public for constructing a AppWindow
206 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
207 // Normally AppWindow::Create should be used.
208 // Takes ownership of |app_delegate| and |delegate|.
209 AppWindow(content::BrowserContext
* context
,
210 AppDelegate
* app_delegate
,
211 const Extension
* extension
);
213 // Initializes the render interface, web contents, and native window.
214 // |app_window_contents| will become owned by AppWindow.
215 void Init(const GURL
& url
,
216 AppWindowContents
* app_window_contents
,
217 const CreateParams
& params
);
219 const std::string
& window_key() const { return window_key_
; }
220 const SessionID
& session_id() const { return session_id_
; }
221 const std::string
& extension_id() const { return extension_id_
; }
222 content::WebContents
* web_contents() const;
223 WindowType
window_type() const { return window_type_
; }
224 bool window_type_is_panel() const {
225 return (window_type_
== WINDOW_TYPE_PANEL
||
226 window_type_
== WINDOW_TYPE_V1_PANEL
);
228 content::BrowserContext
* browser_context() const { return browser_context_
; }
229 const gfx::Image
& app_icon() const { return app_icon_
; }
230 const GURL
& app_icon_url() const { return app_icon_url_
; }
231 const gfx::Image
& badge_icon() const { return badge_icon_
; }
232 const GURL
& badge_icon_url() const { return badge_icon_url_
; }
233 bool is_hidden() const { return is_hidden_
; }
235 const Extension
* GetExtension() const;
236 NativeAppWindow
* GetBaseWindow();
237 gfx::NativeWindow
GetNativeWindow();
239 // Returns the bounds that should be reported to the renderer.
240 gfx::Rect
GetClientBounds() const;
242 // NativeAppWindows should call this to determine what the window's title
243 // is on startup and from within UpdateWindowTitle().
244 base::string16
GetTitle() const;
246 // Call to notify ShellRegistry and delete the window. Subclasses should
247 // invoke this method instead of using "delete this".
248 void OnNativeClose();
250 // Should be called by native implementations when the window size, position,
251 // or minimized/maximized state has changed.
252 void OnNativeWindowChanged();
254 // Should be called by native implementations when the window is activated.
255 void OnNativeWindowActivated();
257 // Specifies a url for the launcher icon.
258 void SetAppIconUrl(const GURL
& icon_url
);
260 // Specifies a url for the window badge.
261 void SetBadgeIconUrl(const GURL
& icon_url
);
263 // Clear the current badge.
266 // Set the window shape. Passing a NULL |region| sets the default shape.
267 void UpdateShape(scoped_ptr
<SkRegion
> region
);
269 // Called from the render interface to modify the draggable regions.
270 void UpdateDraggableRegions(const std::vector
<DraggableRegion
>& regions
);
272 // Updates the app image to |image|. Called internally from the image loader
273 // callback. Also called externally for v1 apps using Ash Panels.
274 void UpdateAppIcon(const gfx::Image
& image
);
276 // Enable or disable fullscreen mode. |type| specifies which type of
277 // fullscreen mode to change (note that disabling one type of fullscreen may
278 // not exit fullscreen mode because a window may have a different type of
279 // fullscreen enabled). If |type| is not FORCED, checks that the extension has
280 // the required permission.
281 void SetFullscreen(FullscreenType type
, bool enable
);
283 // Returns true if the app window is in a fullscreen state.
284 bool IsFullscreen() const;
286 // Returns true if the app window is in a forced fullscreen state (one that
287 // cannot be exited by the user).
288 bool IsForcedFullscreen() const;
290 // Returns true if the app window is in a fullscreen state entered from an
292 bool IsHtmlApiFullscreen() const;
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 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
306 void ForcedFullscreen();
308 // Set the minimum and maximum size of the content bounds.
309 void SetContentSizeConstraints(const gfx::Size
& min_size
,
310 const gfx::Size
& max_size
);
312 enum ShowType
{ SHOW_ACTIVE
, SHOW_INACTIVE
};
314 // Shows the window if its contents have been painted; otherwise flags the
315 // window to be shown as soon as its contents are painted for the first time.
316 void Show(ShowType show_type
);
318 // Hides the window. If the window was previously flagged to be shown on
319 // first paint, it will be unflagged.
322 AppWindowContents
* app_window_contents_for_test() {
323 return app_window_contents_
.get();
326 int fullscreen_types_for_test() {
327 return fullscreen_types_
;
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 // may be false if the bit is silently switched off for security reasons.
337 bool IsAlwaysOnTop() const;
339 // Set whether the window should get even reserved keys (modulo platform
341 void SetInterceptAllKeys(bool want_all_keys
);
343 // Retrieve the current state of the app window as a dictionary, to pass to
345 void GetSerializedState(base::DictionaryValue
* properties
) const;
347 // Called by the window API when events can be sent to the window for this
349 void WindowEventsReady();
351 // Whether the app window wants to be alpha enabled.
352 bool requested_alpha_enabled() const { return requested_alpha_enabled_
; }
354 // Whether the app window is created by IME extensions.
355 // TODO(bshe): rename to hide_app_window_in_launcher if it is not used
356 // anywhere other than app_window_launcher_controller after M45. Otherwise,
358 bool is_ime_window() const { return is_ime_window_
; }
360 void SetAppWindowContentsForTesting(scoped_ptr
<AppWindowContents
> contents
) {
361 app_window_contents_
= contents
.Pass();
365 ~AppWindow() override
;
368 // PlatformAppBrowserTest needs access to web_contents()
369 friend class PlatformAppBrowserTest
;
371 // content::WebContentsDelegate implementation.
372 void CloseContents(content::WebContents
* contents
) override
;
373 bool ShouldSuppressDialogs(content::WebContents
* source
) override
;
374 content::ColorChooser
* OpenColorChooser(
375 content::WebContents
* web_contents
,
377 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
378 void RunFileChooser(content::WebContents
* tab
,
379 const content::FileChooserParams
& params
) override
;
380 bool IsPopupOrPanel(const content::WebContents
* source
) const override
;
381 void MoveContents(content::WebContents
* source
,
382 const gfx::Rect
& pos
) override
;
383 void NavigationStateChanged(content::WebContents
* source
,
384 content::InvalidateTypes changed_flags
) override
;
385 void EnterFullscreenModeForTab(content::WebContents
* source
,
386 const GURL
& origin
) override
;
387 void ExitFullscreenModeForTab(content::WebContents
* source
) override
;
388 bool IsFullscreenForTabOrPending(
389 const content::WebContents
* source
) const override
;
390 void RequestMediaAccessPermission(
391 content::WebContents
* web_contents
,
392 const content::MediaStreamRequest
& request
,
393 const content::MediaResponseCallback
& callback
) override
;
394 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
395 const GURL
& security_origin
,
396 content::MediaStreamType type
) override
;
397 content::WebContents
* OpenURLFromTab(
398 content::WebContents
* source
,
399 const content::OpenURLParams
& params
) override
;
400 void AddNewContents(content::WebContents
* source
,
401 content::WebContents
* new_contents
,
402 WindowOpenDisposition disposition
,
403 const gfx::Rect
& initial_rect
,
405 bool* was_blocked
) override
;
406 bool PreHandleKeyboardEvent(content::WebContents
* source
,
407 const content::NativeWebKeyboardEvent
& event
,
408 bool* is_keyboard_shortcut
) override
;
409 void HandleKeyboardEvent(
410 content::WebContents
* source
,
411 const content::NativeWebKeyboardEvent
& event
) override
;
412 void RequestToLockMouse(content::WebContents
* web_contents
,
414 bool last_unlocked_by_target
) override
;
415 bool PreHandleGestureEvent(content::WebContents
* source
,
416 const blink::WebGestureEvent
& event
) override
;
418 // content::WebContentsObserver implementation.
419 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
420 void DidFirstVisuallyNonEmptyPaint() override
;
422 // ExtensionRegistryObserver implementation.
423 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
424 const Extension
* extension
,
425 UnloadedExtensionInfo::Reason reason
) override
;
426 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
427 const Extension
* extension
,
430 const std::string
& old_name
) override
;
432 // web_modal::WebContentsModalDialogManagerDelegate implementation.
433 void SetWebContentsBlocked(content::WebContents
* web_contents
,
434 bool blocked
) override
;
435 bool IsWebContentsVisible(content::WebContents
* web_contents
) override
;
437 void ToggleFullscreenModeForTab(content::WebContents
* source
,
438 bool enter_fullscreen
);
440 // Saves the window geometry/position/screen bounds.
441 void SaveWindowPosition();
443 // Helper method to adjust the cached bounds so that we can make sure it can
444 // be visible on the screen. See http://crbug.com/145752.
445 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect
& cached_bounds
,
446 const gfx::Rect
& cached_screen_bounds
,
447 const gfx::Rect
& current_screen_bounds
,
448 const gfx::Size
& minimum_size
,
449 gfx::Rect
* bounds
) const;
451 // Loads the appropriate default or cached window bounds. Returns a new
452 // CreateParams that should be used to create the window.
453 CreateParams
LoadDefaults(CreateParams params
) const;
455 // Load the app's image, firing a load state change when loaded.
456 void UpdateExtensionAppIcon();
458 // Set the fullscreen state in the native app window.
459 void SetNativeWindowFullscreen();
461 // Returns true if there is any overlap between the window and the taskbar
463 bool IntersectsWithTaskbar() const;
465 // Update the always-on-top bit in the native app window.
466 void UpdateNativeAlwaysOnTop();
468 // Sends the onWindowShown event to the app if the window has been shown. Only
469 // has an effect in tests.
470 void SendOnWindowShownIfShown();
472 // web_modal::WebContentsModalDialogManagerDelegate implementation.
473 web_modal::WebContentsModalDialogHost
* GetWebContentsModalDialogHost()
476 // Updates the badge to |image|. Called internally from the image loader
478 void UpdateBadgeIcon(const gfx::Image
& image
);
480 // Callback from web_contents()->DownloadFavicon.
481 void DidDownloadFavicon(int id
,
482 int http_status_code
,
483 const GURL
& image_url
,
484 const std::vector
<SkBitmap
>& bitmaps
,
485 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
487 // IconImage::Observer implementation.
488 void OnExtensionIconImageChanged(IconImage
* image
) override
;
490 // The browser context with which this window is associated. AppWindow does
491 // not own this object.
492 content::BrowserContext
* browser_context_
;
494 const std::string extension_id_
;
496 // Identifier that is used when saving and restoring geometry for this
498 std::string window_key_
;
500 const SessionID session_id_
;
501 WindowType window_type_
;
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
<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
<IconImage
> badge_icon_image_
;
522 scoped_ptr
<NativeAppWindow
> native_app_window_
;
523 scoped_ptr
<AppWindowContents
> app_window_contents_
;
524 scoped_ptr
<AppDelegate
> app_delegate_
;
525 scoped_ptr
<AppWebContentsHelper
> helper_
;
527 // Manages popup windows (bubbles, tab-modals) visible overlapping the
529 scoped_ptr
<web_modal::PopupManager
> popup_manager_
;
531 // Bit field of FullscreenType.
532 int fullscreen_types_
;
534 // Show has been called, so the window should be shown once the first visually
535 // non-empty paint occurs.
536 bool show_on_first_paint_
;
538 // The first visually non-empty paint has completed.
539 bool first_paint_complete_
;
541 // Whether the window has been shown or not.
542 bool has_been_shown_
;
544 // Whether events can be sent to the window.
545 bool can_send_events_
;
547 // Whether the window is hidden or not. Hidden in this context means actively
548 // by the chrome.app.window API, not in an operating system context. For
549 // example windows which are minimized are not hidden, and windows which are
550 // part of a hidden app on OS X are not hidden. Windows which were created
551 // with the |hidden| flag set to true, or which have been programmatically
552 // hidden, are considered hidden.
555 // Whether the delayed Show() call was for an active or inactive window.
556 ShowType delayed_show_type_
;
558 // Cache the desired value of the always-on-top property. When windows enter
559 // fullscreen or overlap the Windows taskbar, this property will be
560 // automatically and silently switched off for security reasons. It is
561 // reinstated when the window exits fullscreen and moves away from the
563 bool cached_always_on_top_
;
565 // Whether |alpha_enabled| was set in the CreateParams.
566 bool requested_alpha_enabled_
;
568 // Whether |is_ime_window| was set in the CreateParams.
571 base::WeakPtrFactory
<AppWindow
> image_loader_ptr_factory_
;
573 DISALLOW_COPY_AND_ASSIGN(AppWindow
);
576 } // namespace extensions
578 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_