Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / app_window / app_window.h
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.
4
5 #ifndef EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_
6 #define EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_
7
8 #include <string>
9 #include <vector>
10
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"
24
25 class GURL;
26 class SkRegion;
27
28 namespace base {
29 class DictionaryValue;
30 }
31
32 namespace content {
33 class BrowserContext;
34 class WebContents;
35 }
36
37 namespace ui {
38 class BaseWindow;
39 }
40
41 namespace extensions {
42
43 class AppDelegate;
44 class AppWebContentsHelper;
45 class Extension;
46 class NativeAppWindow;
47 class PlatformAppBrowserTest;
48 class WindowController;
49
50 struct DraggableRegion;
51
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
55 // native window.
56 class AppWindowContents {
57  public:
58   AppWindowContents() {}
59   virtual ~AppWindowContents() {}
60
61   // Called to initialize the WebContents, before the app window is created.
62   virtual void Initialize(content::BrowserContext* context,
63                           const GURL& url) = 0;
64
65   // Called to load the contents, after the app window is created.
66   virtual void LoadContents(int32 creator_process_id) = 0;
67
68   // Called when the native window changes.
69   virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0;
70
71   // Called when the native window closes.
72   virtual void NativeWindowClosed() = 0;
73
74   // Called in tests when the window is shown
75   virtual void DispatchWindowShownForTests() const = 0;
76
77   virtual content::WebContents* GetWebContents() const = 0;
78
79  private:
80   DISALLOW_COPY_AND_ASSIGN(AppWindowContents);
81 };
82
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::NotificationObserver,
86                   public content::WebContentsDelegate,
87                   public content::WebContentsObserver,
88                   public web_modal::WebContentsModalDialogManagerDelegate,
89                   public IconImage::Observer {
90  public:
91   enum WindowType {
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
95                                     // with v1 apps.
96   };
97
98   enum Frame {
99     FRAME_CHROME,  // Chrome-style window frame.
100     FRAME_NONE,    // Frameless window.
101   };
102
103   enum FullscreenType {
104     // Not fullscreen.
105     FULLSCREEN_TYPE_NONE = 0,
106
107     // Fullscreen entered by the app.window api.
108     FULLSCREEN_TYPE_WINDOW_API = 1 << 0,
109
110     // Fullscreen entered by HTML requestFullscreen().
111     FULLSCREEN_TYPE_HTML_API = 1 << 1,
112
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,
116
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,
120   };
121
122   struct BoundsSpecification {
123     // INT_MIN represents an unspecified position component.
124     static const int kUnspecifiedPosition;
125
126     BoundsSpecification();
127     ~BoundsSpecification();
128
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.
132     gfx::Rect bounds;
133
134     gfx::Size minimum_size;
135     gfx::Size maximum_size;
136
137     // Reset the bounds fields to their 'unspecified' values. The minimum and
138     // maximum size constraints remain unchanged.
139     void ResetBounds();
140   };
141
142   struct CreateParams {
143     CreateParams();
144     ~CreateParams();
145
146     WindowType window_type;
147     Frame frame;
148
149     bool has_frame_color;
150     SkColor active_frame_color;
151     SkColor inactive_frame_color;
152     bool alpha_enabled;
153     bool is_ime_window;
154
155     // The initial content/inner bounds specification (excluding any window
156     // decorations).
157     BoundsSpecification content_spec;
158
159     // The initial window/outer bounds specification (including window
160     // decorations).
161     BoundsSpecification window_spec;
162
163     std::string window_key;
164
165     // The process ID of the process that requested the create.
166     int32 creator_process_id;
167
168     // Initial state of the window.
169     ui::WindowShowState state;
170
171     // If true, don't show the window after creation.
172     bool hidden;
173
174     // If true, the window will be resizable by the user. Defaults to true.
175     bool resizable;
176
177     // If true, the window will be focused on creation. Defaults to true.
178     bool focused;
179
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.
182     bool always_on_top;
183
184     // If true, the window will be visible on all workspaces. Defaults to false.
185     bool visible_on_all_workspaces;
186
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;
190
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
193     // constraints.
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;
198   };
199
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);
204
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);
212
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);
218
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);
227   }
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_; }
234
235   const Extension* GetExtension() const;
236   NativeAppWindow* GetBaseWindow();
237   gfx::NativeWindow GetNativeWindow();
238
239   // Returns the bounds that should be reported to the renderer.
240   gfx::Rect GetClientBounds() const;
241
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;
245
246   // Call to notify ShellRegistry and delete the window. Subclasses should
247   // invoke this method instead of using "delete this".
248   void OnNativeClose();
249
250   // Should be called by native implementations when the window size, position,
251   // or minimized/maximized state has changed.
252   void OnNativeWindowChanged();
253
254   // Should be called by native implementations when the window is activated.
255   void OnNativeWindowActivated();
256
257   // Specifies a url for the launcher icon.
258   void SetAppIconUrl(const GURL& icon_url);
259
260   // Specifies a url for the window badge.
261   void SetBadgeIconUrl(const GURL& icon_url);
262
263   // Clear the current badge.
264   void ClearBadge();
265
266   // Set the window shape. Passing a NULL |region| sets the default shape.
267   void UpdateShape(scoped_ptr<SkRegion> region);
268
269   // Called from the render interface to modify the draggable regions.
270   void UpdateDraggableRegions(const std::vector<DraggableRegion>& regions);
271
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);
275
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);
282
283   // Returns true if the app window is in a fullscreen state.
284   bool IsFullscreen() const;
285
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;
289
290   // Returns true if the app window is in a fullscreen state entered from an
291   // HTML API request.
292   bool IsHtmlApiFullscreen() const;
293
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();
300
301   // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
302   void OSFullscreen();
303
304   // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
305   // details.
306   void ForcedFullscreen();
307
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);
311
312   enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE };
313
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);
317
318   // Hides the window. If the window was previously flagged to be shown on
319   // first paint, it will be unflagged.
320   void Hide();
321
322   AppWindowContents* app_window_contents_for_test() {
323     return app_window_contents_.get();
324   }
325
326   int fullscreen_types_for_test() {
327     return fullscreen_types_;
328   }
329
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);
333
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;
338
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;
342
343   // Called by the window API when events can be sent to the window for this
344   // app.
345   void WindowEventsReady();
346
347   // Whether the app window wants to be alpha enabled.
348   bool requested_alpha_enabled() const { return requested_alpha_enabled_; }
349
350   void SetAppWindowContentsForTesting(scoped_ptr<AppWindowContents> contents) {
351     app_window_contents_ = contents.Pass();
352   }
353
354  protected:
355   ~AppWindow() override;
356
357  private:
358   // PlatformAppBrowserTest needs access to web_contents()
359   friend class PlatformAppBrowserTest;
360
361   // content::WebContentsDelegate implementation.
362   void CloseContents(content::WebContents* contents) override;
363   bool ShouldSuppressDialogs() override;
364   content::ColorChooser* OpenColorChooser(
365       content::WebContents* web_contents,
366       SkColor color,
367       const std::vector<content::ColorSuggestion>& suggestions) override;
368   void RunFileChooser(content::WebContents* tab,
369                       const content::FileChooserParams& params) override;
370   bool IsPopupOrPanel(const content::WebContents* source) const override;
371   void MoveContents(content::WebContents* source,
372                     const gfx::Rect& pos) override;
373   void NavigationStateChanged(const content::WebContents* source,
374                               content::InvalidateTypes changed_flags) override;
375   void ToggleFullscreenModeForTab(content::WebContents* source,
376                                   bool enter_fullscreen) override;
377   bool IsFullscreenForTabOrPending(
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_pos,
393                       bool user_gesture,
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,
402                           bool user_gesture,
403                           bool last_unlocked_by_target) override;
404   bool PreHandleGestureEvent(content::WebContents* source,
405                              const blink::WebGestureEvent& event) override;
406
407   // content::WebContentsObserver implementation.
408   void DidFirstVisuallyNonEmptyPaint() override;
409
410   // content::NotificationObserver implementation.
411   void Observe(int type,
412                const content::NotificationSource& source,
413                const content::NotificationDetails& details) override;
414
415   // web_modal::WebContentsModalDialogManagerDelegate implementation.
416   void SetWebContentsBlocked(content::WebContents* web_contents,
417                              bool blocked) override;
418   bool IsWebContentsVisible(content::WebContents* web_contents) override;
419
420   // Saves the window geometry/position/screen bounds.
421   void SaveWindowPosition();
422
423   // Helper method to adjust the cached bounds so that we can make sure it can
424   // be visible on the screen. See http://crbug.com/145752.
425   void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
426                                        const gfx::Rect& cached_screen_bounds,
427                                        const gfx::Rect& current_screen_bounds,
428                                        const gfx::Size& minimum_size,
429                                        gfx::Rect* bounds) const;
430
431   // Loads the appropriate default or cached window bounds. Returns a new
432   // CreateParams that should be used to create the window.
433   CreateParams LoadDefaults(CreateParams params) const;
434
435   // Load the app's image, firing a load state change when loaded.
436   void UpdateExtensionAppIcon();
437
438   // Set the fullscreen state in the native app window.
439   void SetNativeWindowFullscreen();
440
441   // Returns true if there is any overlap between the window and the taskbar
442   // (Windows only).
443   bool IntersectsWithTaskbar() const;
444
445   // Update the always-on-top bit in the native app window.
446   void UpdateNativeAlwaysOnTop();
447
448   // Sends the onWindowShown event to the app if the window has been shown. Only
449   // has an effect in tests.
450   void SendOnWindowShownIfShown();
451
452   // web_modal::WebContentsModalDialogManagerDelegate implementation.
453   web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
454       override;
455
456   // Updates the badge to |image|. Called internally from the image loader
457   // callback.
458   void UpdateBadgeIcon(const gfx::Image& image);
459
460   // Callback from web_contents()->DownloadFavicon.
461   void DidDownloadFavicon(int id,
462                           int http_status_code,
463                           const GURL& image_url,
464                           const std::vector<SkBitmap>& bitmaps,
465                           const std::vector<gfx::Size>& original_bitmap_sizes);
466
467   // IconImage::Observer implementation.
468   void OnExtensionIconImageChanged(IconImage* image) override;
469
470   // The browser context with which this window is associated. AppWindow does
471   // not own this object.
472   content::BrowserContext* browser_context_;
473
474   const std::string extension_id_;
475
476   // Identifier that is used when saving and restoring geometry for this
477   // window.
478   std::string window_key_;
479
480   const SessionID session_id_;
481   WindowType window_type_;
482   content::NotificationRegistrar registrar_;
483
484   // Icon shown in the task bar.
485   gfx::Image app_icon_;
486
487   // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
488   // be fetched and set using this URL.
489   GURL app_icon_url_;
490
491   // An object to load the app's icon as an extension resource.
492   scoped_ptr<IconImage> app_icon_image_;
493
494   // Badge for icon shown in the task bar.
495   gfx::Image badge_icon_;
496
497   // URL to be used for setting the badge on the app icon.
498   GURL badge_icon_url_;
499
500   // An object to load the badge as an extension resource.
501   scoped_ptr<IconImage> badge_icon_image_;
502
503   scoped_ptr<NativeAppWindow> native_app_window_;
504   scoped_ptr<AppWindowContents> app_window_contents_;
505   scoped_ptr<AppDelegate> app_delegate_;
506   scoped_ptr<AppWebContentsHelper> helper_;
507
508   // Manages popup windows (bubbles, tab-modals) visible overlapping the
509   // app window.
510   scoped_ptr<web_modal::PopupManager> popup_manager_;
511
512   base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
513
514   // Bit field of FullscreenType.
515   int fullscreen_types_;
516
517   // Show has been called, so the window should be shown once the first visually
518   // non-empty paint occurs.
519   bool show_on_first_paint_;
520
521   // The first visually non-empty paint has completed.
522   bool first_paint_complete_;
523
524   // Whether the window has been shown or not.
525   bool has_been_shown_;
526
527   // Whether events can be sent to the window.
528   bool can_send_events_;
529
530   // Whether the window is hidden or not. Hidden in this context means actively
531   // by the chrome.app.window API, not in an operating system context. For
532   // example windows which are minimized are not hidden, and windows which are
533   // part of a hidden app on OS X are not hidden. Windows which were created
534   // with the |hidden| flag set to true, or which have been programmatically
535   // hidden, are considered hidden.
536   bool is_hidden_;
537
538   // Whether the delayed Show() call was for an active or inactive window.
539   ShowType delayed_show_type_;
540
541   // Cache the desired value of the always-on-top property. When windows enter
542   // fullscreen or overlap the Windows taskbar, this property will be
543   // automatically and silently switched off for security reasons. It is
544   // reinstated when the window exits fullscreen and moves away from the
545   // taskbar.
546   bool cached_always_on_top_;
547
548   // Whether |alpha_enabled| was set in the CreateParams.
549   bool requested_alpha_enabled_;
550
551   DISALLOW_COPY_AND_ASSIGN(AppWindow);
552 };
553
554 }  // namespace extensions
555
556 #endif  // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_