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/web_contents_modal_dialog_manager_delegate.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "extensions/browser/extension_function_dispatcher.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 // Called when the renderer notifies the browser that the window is ready.
72 virtual void OnWindowReady() = 0;
74 virtual content::WebContents
* GetWebContents() const = 0;
76 virtual extensions::WindowController
* GetWindowController() 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::WebContentsDelegate
,
85 public content::WebContentsObserver
,
86 public web_modal::WebContentsModalDialogManagerDelegate
,
87 public IconImage::Observer
,
88 public ExtensionFunctionDispatcher::Delegate
,
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 GURL
& initial_url() const { return initial_url_
; }
232 bool is_hidden() const { return is_hidden_
; }
234 const Extension
* GetExtension() const;
235 NativeAppWindow
* GetBaseWindow();
236 gfx::NativeWindow
GetNativeWindow();
238 // Returns the bounds that should be reported to the renderer.
239 gfx::Rect
GetClientBounds() const;
241 // NativeAppWindows should call this to determine what the window's title
242 // is on startup and from within UpdateWindowTitle().
243 base::string16
GetTitle() const;
245 // |callback| will then be called when the first navigation in the window is
247 void SetOnFirstCommitCallback(const base::Closure
& callback
);
249 // Called when the first navigation in the window is ready to commit.
250 void OnReadyToCommitFirstNavigation();
252 // Call to notify ShellRegistry and delete the window. Subclasses should
253 // invoke this method instead of using "delete this".
254 void OnNativeClose();
256 // Should be called by native implementations when the window size, position,
257 // or minimized/maximized state has changed.
258 void OnNativeWindowChanged();
260 // Should be called by native implementations when the window is activated.
261 void OnNativeWindowActivated();
263 // Specifies a url for the launcher icon.
264 void SetAppIconUrl(const GURL
& icon_url
);
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 // Restores the always-on-top property according to |cached_always_on_top_|.
340 void RestoreAlwaysOnTop();
342 // Retrieve the current state of the app window as a dictionary, to pass to
344 void GetSerializedState(base::DictionaryValue
* properties
) const;
346 // Called by the window API when events can be sent to the window for this
348 void WindowEventsReady();
350 // Notifies the window's contents that the render view is ready and it can
351 // unblock resource requests.
352 void NotifyRenderViewReady();
354 // Whether the app window wants to be alpha enabled.
355 bool requested_alpha_enabled() const { return requested_alpha_enabled_
; }
357 // Whether the app window is created by IME extensions.
358 // TODO(bshe): rename to hide_app_window_in_launcher if it is not used
359 // anywhere other than app_window_launcher_controller after M45. Otherwise,
361 bool is_ime_window() const { return is_ime_window_
; }
363 void SetAppWindowContentsForTesting(scoped_ptr
<AppWindowContents
> contents
) {
364 app_window_contents_
= contents
.Pass();
368 ~AppWindow() override
;
371 // PlatformAppBrowserTest needs access to web_contents()
372 friend class PlatformAppBrowserTest
;
374 // content::WebContentsDelegate implementation.
375 void CloseContents(content::WebContents
* contents
) override
;
376 bool ShouldSuppressDialogs(content::WebContents
* source
) override
;
377 content::ColorChooser
* OpenColorChooser(
378 content::WebContents
* web_contents
,
380 const std::vector
<content::ColorSuggestion
>& suggestions
) override
;
381 void RunFileChooser(content::WebContents
* tab
,
382 const content::FileChooserParams
& params
) override
;
383 bool IsPopupOrPanel(const content::WebContents
* source
) const override
;
384 void MoveContents(content::WebContents
* source
,
385 const gfx::Rect
& pos
) override
;
386 void NavigationStateChanged(content::WebContents
* source
,
387 content::InvalidateTypes changed_flags
) override
;
388 void EnterFullscreenModeForTab(content::WebContents
* source
,
389 const GURL
& origin
) override
;
390 void ExitFullscreenModeForTab(content::WebContents
* source
) override
;
391 bool IsFullscreenForTabOrPending(
392 const content::WebContents
* source
) const override
;
393 blink::WebDisplayMode
GetDisplayMode(
394 const content::WebContents
* source
) const override
;
395 void RequestMediaAccessPermission(
396 content::WebContents
* web_contents
,
397 const content::MediaStreamRequest
& request
,
398 const content::MediaResponseCallback
& callback
) override
;
399 bool CheckMediaAccessPermission(content::WebContents
* web_contents
,
400 const GURL
& security_origin
,
401 content::MediaStreamType type
) override
;
402 content::WebContents
* OpenURLFromTab(
403 content::WebContents
* source
,
404 const content::OpenURLParams
& params
) override
;
405 void AddNewContents(content::WebContents
* source
,
406 content::WebContents
* new_contents
,
407 WindowOpenDisposition disposition
,
408 const gfx::Rect
& initial_rect
,
410 bool* was_blocked
) override
;
411 bool PreHandleKeyboardEvent(content::WebContents
* source
,
412 const content::NativeWebKeyboardEvent
& event
,
413 bool* is_keyboard_shortcut
) override
;
414 void HandleKeyboardEvent(
415 content::WebContents
* source
,
416 const content::NativeWebKeyboardEvent
& event
) override
;
417 void RequestToLockMouse(content::WebContents
* web_contents
,
419 bool last_unlocked_by_target
) override
;
420 bool PreHandleGestureEvent(content::WebContents
* source
,
421 const blink::WebGestureEvent
& event
) override
;
423 // content::WebContentsObserver implementation.
424 void RenderViewCreated(content::RenderViewHost
* render_view_host
) override
;
425 void DidFirstVisuallyNonEmptyPaint() override
;
427 // ExtensionFunctionDispatcher::Delegate implementation.
428 WindowController
* GetExtensionWindowController() const override
;
429 content::WebContents
* GetAssociatedWebContents() const override
;
431 // ExtensionRegistryObserver implementation.
432 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
433 const Extension
* extension
,
434 UnloadedExtensionInfo::Reason reason
) override
;
436 // web_modal::WebContentsModalDialogManagerDelegate implementation.
437 void SetWebContentsBlocked(content::WebContents
* web_contents
,
438 bool blocked
) override
;
439 bool IsWebContentsVisible(content::WebContents
* web_contents
) override
;
441 void ToggleFullscreenModeForTab(content::WebContents
* source
,
442 bool enter_fullscreen
);
444 // Saves the window geometry/position/screen bounds.
445 void SaveWindowPosition();
447 // Helper method to adjust the cached bounds so that we can make sure it can
448 // be visible on the screen. See http://crbug.com/145752.
449 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect
& cached_bounds
,
450 const gfx::Rect
& cached_screen_bounds
,
451 const gfx::Rect
& current_screen_bounds
,
452 const gfx::Size
& minimum_size
,
453 gfx::Rect
* bounds
) const;
455 // Loads the appropriate default or cached window bounds. Returns a new
456 // CreateParams that should be used to create the window.
457 CreateParams
LoadDefaults(CreateParams params
) const;
459 // Load the app's image, firing a load state change when loaded.
460 void UpdateExtensionAppIcon();
462 // Set the fullscreen state in the native app window.
463 void SetNativeWindowFullscreen();
465 // Returns true if there is any overlap between the window and the taskbar
467 bool IntersectsWithTaskbar() const;
469 // Update the always-on-top bit in the native app window.
470 void UpdateNativeAlwaysOnTop();
472 // Sends the onWindowShown event to the app if the window has been shown. Only
473 // has an effect in tests.
474 void SendOnWindowShownIfShown();
476 // web_modal::WebContentsModalDialogManagerDelegate implementation.
477 web_modal::WebContentsModalDialogHost
* GetWebContentsModalDialogHost()
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 scoped_ptr
<NativeAppWindow
> native_app_window_
;
514 scoped_ptr
<AppWindowContents
> app_window_contents_
;
515 scoped_ptr
<AppDelegate
> app_delegate_
;
516 scoped_ptr
<AppWebContentsHelper
> helper_
;
518 // The initial url this AppWindow was navigated to.
521 // Bit field of FullscreenType.
522 int fullscreen_types_
;
524 // Show has been called, so the window should be shown once the first visually
525 // non-empty paint occurs.
526 bool show_on_first_paint_
;
528 // The first visually non-empty paint has completed.
529 bool first_paint_complete_
;
531 // Whether the window has been shown or not.
532 bool has_been_shown_
;
534 // Whether events can be sent to the window.
535 bool can_send_events_
;
537 // Whether the window is hidden or not. Hidden in this context means actively
538 // by the chrome.app.window API, not in an operating system context. For
539 // example windows which are minimized are not hidden, and windows which are
540 // part of a hidden app on OS X are not hidden. Windows which were created
541 // with the |hidden| flag set to true, or which have been programmatically
542 // hidden, are considered hidden.
545 // Whether the delayed Show() call was for an active or inactive window.
546 ShowType delayed_show_type_
;
548 // Cache the desired value of the always-on-top property. When windows enter
549 // fullscreen or overlap the Windows taskbar, this property will be
550 // automatically and silently switched off for security reasons. It is
551 // reinstated when the window exits fullscreen and moves away from the
553 bool cached_always_on_top_
;
555 // Whether |alpha_enabled| was set in the CreateParams.
556 bool requested_alpha_enabled_
;
558 // Whether |is_ime_window| was set in the CreateParams.
561 // PlzNavigate: this is called when the first navigation is ready to commit.
562 base::Closure on_first_commit_callback_
;
564 base::WeakPtrFactory
<AppWindow
> image_loader_ptr_factory_
;
566 DISALLOW_COPY_AND_ASSIGN(AppWindow
);
569 } // namespace extensions
571 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_