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
;
36 namespace extensions
{
39 class AppWebContentsHelper
;
41 class NativeAppWindow
;
42 class PlatformAppBrowserTest
;
44 struct DraggableRegion
;
46 // Manages the web contents for app windows. The implementation for this
47 // class should create and maintain the WebContents for the window, and handle
48 // any message passing between the web contents and the extension system or
50 class AppWindowContents
{
52 AppWindowContents() {}
53 virtual ~AppWindowContents() {}
55 // Called to initialize the WebContents, before the app window is created.
56 virtual void Initialize(content::BrowserContext
* context
,
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 // Called in tests when the window is shown
69 virtual void DispatchWindowShownForTests() const = 0;
71 virtual content::WebContents
* GetWebContents() const = 0;
74 DISALLOW_COPY_AND_ASSIGN(AppWindowContents
);
77 // AppWindow is the type of window used by platform apps. App windows
78 // have a WebContents but none of the chrome of normal browser windows.
79 class AppWindow
: public content::WebContentsDelegate
,
80 public content::WebContentsObserver
,
81 public web_modal::WebContentsModalDialogManagerDelegate
,
82 public IconImage::Observer
,
83 public ExtensionRegistryObserver
{
86 WINDOW_TYPE_DEFAULT
= 1 << 0, // Default app window.
87 WINDOW_TYPE_PANEL
= 1 << 1, // OS controlled panel window (Ash only).
88 WINDOW_TYPE_V1_PANEL
= 1 << 2, // For apps v1 support in Ash; deprecate
93 FRAME_CHROME
, // Chrome-style window frame.
94 FRAME_NONE
, // Frameless window.
99 FULLSCREEN_TYPE_NONE
= 0,
101 // Fullscreen entered by the app.window api.
102 FULLSCREEN_TYPE_WINDOW_API
= 1 << 0,
104 // Fullscreen entered by HTML requestFullscreen().
105 FULLSCREEN_TYPE_HTML_API
= 1 << 1,
107 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
108 // enter immersive fullscreen when the user hits the <F4> key.
109 FULLSCREEN_TYPE_OS
= 1 << 2,
111 // Fullscreen mode that could not be exited by the user. ChromeOS uses
112 // this type of fullscreen to run an app in kiosk mode.
113 FULLSCREEN_TYPE_FORCED
= 1 << 3,
116 struct BoundsSpecification
{
117 // INT_MIN represents an unspecified position component.
118 static const int kUnspecifiedPosition
;
120 BoundsSpecification();
121 ~BoundsSpecification();
123 // INT_MIN designates 'unspecified' for the position components, and 0
124 // designates 'unspecified' for the size components. When unspecified,
125 // they should be replaced with a default value.
128 gfx::Size minimum_size
;
129 gfx::Size maximum_size
;
131 // Reset the bounds fields to their 'unspecified' values. The minimum and
132 // maximum size constraints remain unchanged.
136 struct CreateParams
{
140 WindowType window_type
;
143 bool has_frame_color
;
144 SkColor active_frame_color
;
145 SkColor inactive_frame_color
;
149 // The initial content/inner bounds specification (excluding any window
151 BoundsSpecification content_spec
;
153 // The initial window/outer bounds specification (including window
155 BoundsSpecification window_spec
;
157 std::string window_key
;
159 // The process ID of the process that requested the create.
160 int32 creator_process_id
;
162 // Initial state of the window.
163 ui::WindowShowState state
;
165 // If true, don't show the window after creation.
168 // If true, the window will be resizable by the user. Defaults to true.
171 // If true, the window will be focused on creation. Defaults to true.
174 // If true, the window will stay on top of other windows that are not
175 // configured to be always on top. Defaults to false.
178 // If true, the window will be visible on all workspaces. Defaults to false.
179 bool visible_on_all_workspaces
;
181 // The API enables developers to specify content or window bounds. This
182 // function combines them into a single, constrained window size.
183 gfx::Rect
GetInitialWindowBounds(const gfx::Insets
& frame_insets
) const;
185 // The API enables developers to specify content or window size constraints.
186 // These functions combine them so that we can work with one set of
188 gfx::Size
GetContentMinimumSize(const gfx::Insets
& frame_insets
) const;
189 gfx::Size
GetContentMaximumSize(const gfx::Insets
& frame_insets
) const;
190 gfx::Size
GetWindowMinimumSize(const gfx::Insets
& frame_insets
) const;
191 gfx::Size
GetWindowMaximumSize(const gfx::Insets
& frame_insets
) const;
194 // Convert draggable regions in raw format to SkRegion format. Caller is
195 // responsible for deleting the returned SkRegion instance.
196 static SkRegion
* RawDraggableRegionsToSkRegion(
197 const std::vector
<DraggableRegion
>& regions
);
199 // The constructor and Init methods are public for constructing a AppWindow
200 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
201 // Normally AppWindow::Create should be used.
202 // Takes ownership of |app_delegate| and |delegate|.
203 AppWindow(content::BrowserContext
* context
,
204 AppDelegate
* app_delegate
,
205 const Extension
* extension
);
207 // Initializes the render interface, web contents, and native window.
208 // |app_window_contents| will become owned by AppWindow.
209 void Init(const GURL
& url
,
210 AppWindowContents
* app_window_contents
,
211 const CreateParams
& params
);
213 const std::string
& window_key() const { return window_key_
; }
214 const SessionID
& session_id() const { return session_id_
; }
215 const std::string
& extension_id() const { return extension_id_
; }
216 content::WebContents
* web_contents() const;
217 WindowType
window_type() const { return window_type_
; }
218 bool window_type_is_panel() const {
219 return (window_type_
== WINDOW_TYPE_PANEL
||
220 window_type_
== WINDOW_TYPE_V1_PANEL
);
222 content::BrowserContext
* browser_context() const { return browser_context_
; }
223 const gfx::Image
& app_icon() const { return app_icon_
; }
224 const GURL
& app_icon_url() const { return app_icon_url_
; }
225 const GURL
& initial_url() const { return initial_url_
; }
226 bool is_hidden() const { return is_hidden_
; }
228 const Extension
* GetExtension() const;
229 NativeAppWindow
* GetBaseWindow();
230 gfx::NativeWindow
GetNativeWindow();
232 // Returns the bounds that should be reported to the renderer.
233 gfx::Rect
GetClientBounds() const;
235 // NativeAppWindows should call this to determine what the window's title
236 // is on startup and from within UpdateWindowTitle().
237 base::string16
GetTitle() const;
239 // Call to notify ShellRegistry and delete the window. Subclasses should
240 // invoke this method instead of using "delete this".
241 void OnNativeClose();
243 // Should be called by native implementations when the window size, position,
244 // or minimized/maximized state has changed.
245 void OnNativeWindowChanged();
247 // Should be called by native implementations when the window is activated.
248 void OnNativeWindowActivated();
250 // Specifies a url for the launcher icon.
251 void SetAppIconUrl(const GURL
& icon_url
);
253 // Set the window shape. Passing a NULL |region| sets the default shape.
254 void UpdateShape(scoped_ptr
<SkRegion
> region
);
256 // Called from the render interface to modify the draggable regions.
257 void UpdateDraggableRegions(const std::vector
<DraggableRegion
>& regions
);
259 // Updates the app image to |image|. Called internally from the image loader
260 // callback. Also called externally for v1 apps using Ash Panels.
261 void UpdateAppIcon(const gfx::Image
& image
);
263 // Enable or disable fullscreen mode. |type| specifies which type of
264 // fullscreen mode to change (note that disabling one type of fullscreen may
265 // not exit fullscreen mode because a window may have a different type of
266 // fullscreen enabled). If |type| is not FORCED, checks that the extension has
267 // the required permission.
268 void SetFullscreen(FullscreenType type
, bool enable
);
270 // Returns true if the app window is in a fullscreen state.
271 bool IsFullscreen() const;
273 // Returns true if the app window is in a forced fullscreen state (one that
274 // cannot be exited by the user).
275 bool IsForcedFullscreen() const;
277 // Returns true if the app window is in a fullscreen state entered from an
279 bool IsHtmlApiFullscreen() const;
281 // Transitions window into fullscreen, maximized, minimized or restores based
282 // on chrome.app.window API.
288 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
291 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
293 void ForcedFullscreen();
295 // Set the minimum and maximum size of the content bounds.
296 void SetContentSizeConstraints(const gfx::Size
& min_size
,
297 const gfx::Size
& max_size
);
299 enum ShowType
{ SHOW_ACTIVE
, SHOW_INACTIVE
};
301 // Shows the window if its contents have been painted; otherwise flags the
302 // window to be shown as soon as its contents are painted for the first time.
303 void Show(ShowType show_type
);
305 // Hides the window. If the window was previously flagged to be shown on
306 // first paint, it will be unflagged.
309 AppWindowContents
* app_window_contents_for_test() {
310 return app_window_contents_
.get();
313 int fullscreen_types_for_test() {
314 return fullscreen_types_
;
317 // Set whether the window should stay above other windows which are not
318 // configured to be always-on-top.
319 void SetAlwaysOnTop(bool always_on_top
);
321 // Whether the always-on-top property has been set by the chrome.app.window
322 // API. Note that the actual value of this property in the native app window
323 // may be false if the bit is silently switched off for security reasons.
324 bool IsAlwaysOnTop() const;
326 // Set whether the window should get even reserved keys (modulo platform
328 void SetInterceptAllKeys(bool want_all_keys
);
330 // Retrieve the current state of the app window as a dictionary, to pass to
332 void GetSerializedState(base::DictionaryValue
* properties
) const;
334 // Called by the window API when events can be sent to the window for this
336 void WindowEventsReady();
338 // Whether the app window wants to be alpha enabled.
339 bool requested_alpha_enabled() const { return requested_alpha_enabled_
; }
341 // Whether the app window is created by IME extensions.
342 // TODO(bshe): rename to hide_app_window_in_launcher if it is not used
343 // anywhere other than app_window_launcher_controller after M45. Otherwise,
345 bool is_ime_window() const { return is_ime_window_
; }
347 void SetAppWindowContentsForTesting(scoped_ptr
<AppWindowContents
> contents
) {
348 app_window_contents_
= contents
.Pass();
352 ~AppWindow() override
;
355 // PlatformAppBrowserTest needs access to web_contents()
356 friend class PlatformAppBrowserTest
;
358 // content::WebContentsDelegate implementation.
359 void CloseContents(content::WebContents
* contents
) override
;
360 bool ShouldSuppressDialogs(content::WebContents
* source
) override
;
361 content::ColorChooser
* OpenColorChooser(
362 content::WebContents
* web_contents
,
364 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
365 void RunFileChooser(content::WebContents
* tab
,
366 const content::FileChooserParams
& params
) override
;
367 bool IsPopupOrPanel(const content::WebContents
* source
) const override
;
368 void MoveContents(content::WebContents
* source
,
369 const gfx::Rect
& pos
) override
;
370 void NavigationStateChanged(content::WebContents
* source
,
371 content::InvalidateTypes changed_flags
) override
;
372 void EnterFullscreenModeForTab(content::WebContents
* source
,
373 const GURL
& origin
) override
;
374 void ExitFullscreenModeForTab(content::WebContents
* source
) override
;
375 bool IsFullscreenForTabOrPending(
376 const content::WebContents
* source
) const override
;
377 blink::WebDisplayMode
GetDisplayMode(
378 const content::WebContents
* source
) const override
;
379 void RequestMediaAccessPermission(
380 content::WebContents
* web_contents
,
381 const content::MediaStreamRequest
& request
,
382 const content::MediaResponseCallback
& callback
) override
;
383 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
384 const GURL
& security_origin
,
385 content::MediaStreamType type
) override
;
386 content::WebContents
* OpenURLFromTab(
387 content::WebContents
* source
,
388 const content::OpenURLParams
& params
) override
;
389 void AddNewContents(content::WebContents
* source
,
390 content::WebContents
* new_contents
,
391 WindowOpenDisposition disposition
,
392 const gfx::Rect
& initial_rect
,
394 bool* was_blocked
) override
;
395 bool PreHandleKeyboardEvent(content::WebContents
* source
,
396 const content::NativeWebKeyboardEvent
& event
,
397 bool* is_keyboard_shortcut
) override
;
398 void HandleKeyboardEvent(
399 content::WebContents
* source
,
400 const content::NativeWebKeyboardEvent
& event
) override
;
401 void RequestToLockMouse(content::WebContents
* web_contents
,
403 bool last_unlocked_by_target
) override
;
404 bool PreHandleGestureEvent(content::WebContents
* source
,
405 const blink::WebGestureEvent
& event
) override
;
407 // content::WebContentsObserver implementation.
408 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
409 void DidFirstVisuallyNonEmptyPaint() override
;
411 // ExtensionRegistryObserver implementation.
412 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
413 const Extension
* extension
,
414 UnloadedExtensionInfo::Reason reason
) override
;
415 void OnExtensionWillBeInstalled(content::BrowserContext
* browser_context
,
416 const Extension
* extension
,
419 const std::string
& old_name
) override
;
421 // web_modal::WebContentsModalDialogManagerDelegate implementation.
422 void SetWebContentsBlocked(content::WebContents
* web_contents
,
423 bool blocked
) override
;
424 bool IsWebContentsVisible(content::WebContents
* web_contents
) override
;
426 void ToggleFullscreenModeForTab(content::WebContents
* source
,
427 bool enter_fullscreen
);
429 // Saves the window geometry/position/screen bounds.
430 void SaveWindowPosition();
432 // Helper method to adjust the cached bounds so that we can make sure it can
433 // be visible on the screen. See http://crbug.com/145752.
434 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect
& cached_bounds
,
435 const gfx::Rect
& cached_screen_bounds
,
436 const gfx::Rect
& current_screen_bounds
,
437 const gfx::Size
& minimum_size
,
438 gfx::Rect
* bounds
) const;
440 // Loads the appropriate default or cached window bounds. Returns a new
441 // CreateParams that should be used to create the window.
442 CreateParams
LoadDefaults(CreateParams params
) const;
444 // Load the app's image, firing a load state change when loaded.
445 void UpdateExtensionAppIcon();
447 // Set the fullscreen state in the native app window.
448 void SetNativeWindowFullscreen();
450 // Returns true if there is any overlap between the window and the taskbar
452 bool IntersectsWithTaskbar() const;
454 // Update the always-on-top bit in the native app window.
455 void UpdateNativeAlwaysOnTop();
457 // Sends the onWindowShown event to the app if the window has been shown. Only
458 // has an effect in tests.
459 void SendOnWindowShownIfShown();
461 // web_modal::WebContentsModalDialogManagerDelegate implementation.
462 web_modal::WebContentsModalDialogHost
* GetWebContentsModalDialogHost()
465 // Callback from web_contents()->DownloadFavicon.
466 void DidDownloadFavicon(int id
,
467 int http_status_code
,
468 const GURL
& image_url
,
469 const std::vector
<SkBitmap
>& bitmaps
,
470 const std::vector
<gfx::Size
>& original_bitmap_sizes
);
472 // IconImage::Observer implementation.
473 void OnExtensionIconImageChanged(IconImage
* image
) override
;
475 // The browser context with which this window is associated. AppWindow does
476 // not own this object.
477 content::BrowserContext
* browser_context_
;
479 const std::string extension_id_
;
481 // Identifier that is used when saving and restoring geometry for this
483 std::string window_key_
;
485 const SessionID session_id_
;
486 WindowType window_type_
;
488 // Icon shown in the task bar.
489 gfx::Image app_icon_
;
491 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
492 // be fetched and set using this URL.
495 // An object to load the app's icon as an extension resource.
496 scoped_ptr
<IconImage
> app_icon_image_
;
498 scoped_ptr
<NativeAppWindow
> native_app_window_
;
499 scoped_ptr
<AppWindowContents
> app_window_contents_
;
500 scoped_ptr
<AppDelegate
> app_delegate_
;
501 scoped_ptr
<AppWebContentsHelper
> helper_
;
503 // The initial url this AppWindow was navigated to.
506 // Manages popup windows (bubbles, tab-modals) visible overlapping the
508 scoped_ptr
<web_modal::PopupManager
> popup_manager_
;
510 // Bit field of FullscreenType.
511 int fullscreen_types_
;
513 // Show has been called, so the window should be shown once the first visually
514 // non-empty paint occurs.
515 bool show_on_first_paint_
;
517 // The first visually non-empty paint has completed.
518 bool first_paint_complete_
;
520 // Whether the window has been shown or not.
521 bool has_been_shown_
;
523 // Whether events can be sent to the window.
524 bool can_send_events_
;
526 // Whether the window is hidden or not. Hidden in this context means actively
527 // by the chrome.app.window API, not in an operating system context. For
528 // example windows which are minimized are not hidden, and windows which are
529 // part of a hidden app on OS X are not hidden. Windows which were created
530 // with the |hidden| flag set to true, or which have been programmatically
531 // hidden, are considered hidden.
534 // Whether the delayed Show() call was for an active or inactive window.
535 ShowType delayed_show_type_
;
537 // Cache the desired value of the always-on-top property. When windows enter
538 // fullscreen or overlap the Windows taskbar, this property will be
539 // automatically and silently switched off for security reasons. It is
540 // reinstated when the window exits fullscreen and moves away from the
542 bool cached_always_on_top_
;
544 // Whether |alpha_enabled| was set in the CreateParams.
545 bool requested_alpha_enabled_
;
547 // Whether |is_ime_window| was set in the CreateParams.
550 base::WeakPtrFactory
<AppWindow
> image_loader_ptr_factory_
;
552 DISALLOW_COPY_AND_ASSIGN(AppWindow
);
555 } // namespace extensions
557 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_