Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / apps / app_window.h
blob1379922040351d7d853a725b5ea973f418964106
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 <string>
9 #include <vector>
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/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 "extensions/browser/extension_icon_image.h"
21 #include "ui/base/ui_base_types.h" // WindowShowState
22 #include "ui/gfx/image/image.h"
23 #include "ui/gfx/rect.h"
25 class GURL;
26 class SkRegion;
28 namespace base {
29 class DictionaryValue;
32 namespace content {
33 class BrowserContext;
34 class WebContents;
37 namespace extensions {
38 class AppDelegate;
39 class AppWebContentsHelper;
40 class Extension;
41 class NativeAppWindow;
42 class PlatformAppBrowserTest;
43 class WindowController;
45 struct DraggableRegion;
48 namespace ui {
49 class BaseWindow;
52 namespace apps {
54 // Manages the web contents for app windows. The implementation for this
55 // class should create and maintain the WebContents for the window, and handle
56 // any message passing between the web contents and the extension system or
57 // native window.
58 class AppWindowContents {
59 public:
60 AppWindowContents() {}
61 virtual ~AppWindowContents() {}
63 // Called to initialize the WebContents, before the app window is created.
64 virtual void Initialize(content::BrowserContext* context,
65 const GURL& url) = 0;
67 // Called to load the contents, after the app window is created.
68 virtual void LoadContents(int32 creator_process_id) = 0;
70 // Called when the native window changes.
71 virtual void NativeWindowChanged(
72 extensions::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;
82 private:
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 {
93 public:
94 enum WindowType {
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
98 // with v1 apps.
101 enum Frame {
102 FRAME_CHROME, // Chrome-style window frame.
103 FRAME_NONE, // Frameless window.
106 enum FullscreenType {
107 // Not fullscreen.
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.
135 gfx::Rect bounds;
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.
142 void ResetBounds();
145 struct CreateParams {
146 CreateParams();
147 ~CreateParams();
149 WindowType window_type;
150 Frame frame;
152 bool has_frame_color;
153 SkColor active_frame_color;
154 SkColor inactive_frame_color;
155 bool alpha_enabled;
157 // The initial content/inner bounds specification (excluding any window
158 // decorations).
159 BoundsSpecification content_spec;
161 // The initial window/outer bounds specification (including window
162 // decorations).
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.
174 bool hidden;
176 // If true, the window will be resizable by the user. Defaults to true.
177 bool resizable;
179 // If true, the window will be focused on creation. Defaults to true.
180 bool focused;
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.
184 bool always_on_top;
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
192 // constraints.
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;
199 // Convert draggable regions in raw format to SkRegion format. Caller is
200 // responsible for deleting the returned SkRegion instance.
201 static SkRegion* RawDraggableRegionsToSkRegion(
202 const std::vector<extensions::DraggableRegion>& regions);
204 // The constructor and Init methods are public for constructing a AppWindow
205 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
206 // Normally AppWindow::Create should be used.
207 // Takes ownership of |app_delegate| and |delegate|.
208 AppWindow(content::BrowserContext* context,
209 extensions::AppDelegate* app_delegate,
210 const extensions::Extension* extension);
212 // Initializes the render interface, web contents, and native window.
213 // |app_window_contents| will become owned by AppWindow.
214 void Init(const GURL& url,
215 AppWindowContents* app_window_contents,
216 const CreateParams& params);
218 const std::string& window_key() const { return window_key_; }
219 const SessionID& session_id() const { return session_id_; }
220 const std::string& extension_id() const { return extension_id_; }
221 content::WebContents* web_contents() const;
222 WindowType window_type() const { return window_type_; }
223 bool window_type_is_panel() const {
224 return (window_type_ == WINDOW_TYPE_PANEL ||
225 window_type_ == WINDOW_TYPE_V1_PANEL);
227 content::BrowserContext* browser_context() const { return browser_context_; }
228 const gfx::Image& app_icon() const { return app_icon_; }
229 const GURL& app_icon_url() const { return app_icon_url_; }
230 const gfx::Image& badge_icon() const { return badge_icon_; }
231 const GURL& badge_icon_url() const { return badge_icon_url_; }
232 bool is_hidden() const { return is_hidden_; }
234 const extensions::Extension* GetExtension() const;
235 extensions::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 // Call to notify ShellRegistry and delete the window. Subclasses should
246 // invoke this method instead of using "delete this".
247 void OnNativeClose();
249 // Should be called by native implementations when the window size, position,
250 // or minimized/maximized state has changed.
251 void OnNativeWindowChanged();
253 // Should be called by native implementations when the window is activated.
254 void OnNativeWindowActivated();
256 // Specifies a url for the launcher icon.
257 void SetAppIconUrl(const GURL& icon_url);
259 // Specifies a url for the window badge.
260 void SetBadgeIconUrl(const GURL& icon_url);
262 // Clear the current badge.
263 void ClearBadge();
265 // Set the window shape. Passing a NULL |region| sets the default shape.
266 void UpdateShape(scoped_ptr<SkRegion> region);
268 // Called from the render interface to modify the draggable regions.
269 void UpdateDraggableRegions(
270 const std::vector<extensions::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
291 // HTML API request.
292 bool IsHtmlApiFullscreen() const;
294 // Transitions window into fullscreen, maximized, minimized or restores based
295 // on chrome.app.window API.
296 void Fullscreen();
297 void Maximize();
298 void Minimize();
299 void Restore();
301 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
302 void OSFullscreen();
304 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
305 // details.
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.
320 void Hide();
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 // Retrieve the current state of the app window as a dictionary, to pass to
340 // the renderer.
341 void GetSerializedState(base::DictionaryValue* properties) const;
343 // Called by the window API when events can be sent to the window for this
344 // app.
345 void WindowEventsReady();
347 // Whether the app window wants to be alpha enabled.
348 bool requested_alpha_enabled() const { return requested_alpha_enabled_; }
350 void SetAppWindowContentsForTesting(scoped_ptr<AppWindowContents> contents) {
351 app_window_contents_ = contents.Pass();
354 protected:
355 virtual ~AppWindow();
357 private:
358 // PlatformAppBrowserTest needs access to web_contents()
359 friend class extensions::PlatformAppBrowserTest;
361 // content::WebContentsDelegate implementation.
362 virtual void CloseContents(content::WebContents* contents) OVERRIDE;
363 virtual bool ShouldSuppressDialogs() OVERRIDE;
364 virtual content::ColorChooser* OpenColorChooser(
365 content::WebContents* web_contents,
366 SkColor color,
367 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
368 virtual void RunFileChooser(content::WebContents* tab,
369 const content::FileChooserParams& params)
370 OVERRIDE;
371 virtual bool IsPopupOrPanel(const content::WebContents* source)
372 const OVERRIDE;
373 virtual void MoveContents(content::WebContents* source,
374 const gfx::Rect& pos) OVERRIDE;
375 virtual void NavigationStateChanged(
376 const content::WebContents* source,
377 content::InvalidateTypes changed_flags) OVERRIDE;
378 virtual void ToggleFullscreenModeForTab(content::WebContents* source,
379 bool enter_fullscreen) OVERRIDE;
380 virtual bool IsFullscreenForTabOrPending(const content::WebContents* source)
381 const OVERRIDE;
382 virtual void RequestMediaAccessPermission(
383 content::WebContents* web_contents,
384 const content::MediaStreamRequest& request,
385 const content::MediaResponseCallback& callback) OVERRIDE;
386 virtual content::WebContents* OpenURLFromTab(
387 content::WebContents* source,
388 const content::OpenURLParams& params) OVERRIDE;
389 virtual void AddNewContents(content::WebContents* source,
390 content::WebContents* new_contents,
391 WindowOpenDisposition disposition,
392 const gfx::Rect& initial_pos,
393 bool user_gesture,
394 bool* was_blocked) OVERRIDE;
395 virtual bool PreHandleKeyboardEvent(
396 content::WebContents* source,
397 const content::NativeWebKeyboardEvent& event,
398 bool* is_keyboard_shortcut) OVERRIDE;
399 virtual void HandleKeyboardEvent(content::WebContents* source,
400 const content::NativeWebKeyboardEvent& event)
401 OVERRIDE;
402 virtual void RequestToLockMouse(content::WebContents* web_contents,
403 bool user_gesture,
404 bool last_unlocked_by_target) OVERRIDE;
405 virtual bool PreHandleGestureEvent(content::WebContents* source,
406 const blink::WebGestureEvent& event)
407 OVERRIDE;
409 // content::WebContentsObserver implementation.
410 virtual void DidFirstVisuallyNonEmptyPaint() OVERRIDE;
412 // content::NotificationObserver implementation.
413 virtual void Observe(int type,
414 const content::NotificationSource& source,
415 const content::NotificationDetails& details) OVERRIDE;
417 // web_modal::WebContentsModalDialogManagerDelegate implementation.
418 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
419 bool blocked) OVERRIDE;
420 virtual bool IsWebContentsVisible(content::WebContents* web_contents)
421 OVERRIDE;
423 // Saves the window geometry/position/screen bounds.
424 void SaveWindowPosition();
426 // Helper method to adjust the cached bounds so that we can make sure it can
427 // be visible on the screen. See http://crbug.com/145752.
428 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
429 const gfx::Rect& cached_screen_bounds,
430 const gfx::Rect& current_screen_bounds,
431 const gfx::Size& minimum_size,
432 gfx::Rect* bounds) const;
434 // Loads the appropriate default or cached window bounds. Returns a new
435 // CreateParams that should be used to create the window.
436 CreateParams LoadDefaults(CreateParams params) const;
438 // Load the app's image, firing a load state change when loaded.
439 void UpdateExtensionAppIcon();
441 // Set the fullscreen state in the native app window.
442 void SetNativeWindowFullscreen();
444 // Returns true if there is any overlap between the window and the taskbar
445 // (Windows only).
446 bool IntersectsWithTaskbar() const;
448 // Update the always-on-top bit in the native app window.
449 void UpdateNativeAlwaysOnTop();
451 // Sends the onWindowShown event to the app if the window has been shown. Only
452 // has an effect in tests.
453 void SendOnWindowShownIfShown();
455 // web_modal::WebContentsModalDialogManagerDelegate implementation.
456 virtual web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
457 OVERRIDE;
459 // Updates the badge to |image|. Called internally from the image loader
460 // callback.
461 void UpdateBadgeIcon(const gfx::Image& image);
463 // Callback from web_contents()->DownloadFavicon.
464 void DidDownloadFavicon(int id,
465 int http_status_code,
466 const GURL& image_url,
467 const std::vector<SkBitmap>& bitmaps,
468 const std::vector<gfx::Size>& original_bitmap_sizes);
470 // extensions::IconImage::Observer implementation.
471 virtual void OnExtensionIconImageChanged(extensions::IconImage* image)
472 OVERRIDE;
474 // The browser context with which this window is associated. AppWindow does
475 // not own this object.
476 content::BrowserContext* browser_context_;
478 const std::string extension_id_;
480 // Identifier that is used when saving and restoring geometry for this
481 // window.
482 std::string window_key_;
484 const SessionID session_id_;
485 WindowType window_type_;
486 content::NotificationRegistrar registrar_;
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.
493 GURL app_icon_url_;
495 // An object to load the app's icon as an extension resource.
496 scoped_ptr<extensions::IconImage> app_icon_image_;
498 // Badge for icon shown in the task bar.
499 gfx::Image badge_icon_;
501 // URL to be used for setting the badge on the app icon.
502 GURL badge_icon_url_;
504 // An object to load the badge as an extension resource.
505 scoped_ptr<extensions::IconImage> badge_icon_image_;
507 scoped_ptr<extensions::NativeAppWindow> native_app_window_;
508 scoped_ptr<AppWindowContents> app_window_contents_;
509 scoped_ptr<extensions::AppDelegate> app_delegate_;
510 scoped_ptr<extensions::AppWebContentsHelper> helper_;
512 // Manages popup windows (bubbles, tab-modals) visible overlapping the
513 // app window.
514 scoped_ptr<web_modal::PopupManager> popup_manager_;
516 base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
518 // Bit field of FullscreenType.
519 int fullscreen_types_;
521 // Show has been called, so the window should be shown once the first visually
522 // non-empty paint occurs.
523 bool show_on_first_paint_;
525 // The first visually non-empty paint has completed.
526 bool first_paint_complete_;
528 // Whether the window has been shown or not.
529 bool has_been_shown_;
531 // Whether events can be sent to the window.
532 bool can_send_events_;
534 // Whether the window is hidden or not. Hidden in this context means actively
535 // by the chrome.app.window API, not in an operating system context. For
536 // example windows which are minimized are not hidden, and windows which are
537 // part of a hidden app on OS X are not hidden. Windows which were created
538 // with the |hidden| flag set to true, or which have been programmatically
539 // hidden, are considered hidden.
540 bool is_hidden_;
542 // Whether the delayed Show() call was for an active or inactive window.
543 ShowType delayed_show_type_;
545 // Cache the desired value of the always-on-top property. When windows enter
546 // fullscreen or overlap the Windows taskbar, this property will be
547 // automatically and silently switched off for security reasons. It is
548 // reinstated when the window exits fullscreen and moves away from the
549 // taskbar.
550 bool cached_always_on_top_;
552 // Whether |alpha_enabled| was set in the CreateParams.
553 bool requested_alpha_enabled_;
555 DISALLOW_COPY_AND_ASSIGN(AppWindow);
558 } // namespace apps
560 #endif // APPS_APP_WINDOW_H_