- add sources.
[platform/framework/web/crosswalk.git] / src / apps / shell_window.h
1 // Copyright 2013 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 APPS_SHELL_WINDOW_H_
6 #define APPS_SHELL_WINDOW_H_
7
8 #include "base/memory/scoped_ptr.h"
9 #include "base/memory/weak_ptr.h"
10 #include "chrome/browser/extensions/extension_icon_image.h"
11 #include "chrome/browser/extensions/extension_keybinding_registry.h"
12 #include "chrome/browser/sessions/session_id.h"
13 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "content/public/browser/web_contents_delegate.h"
17 #include "content/public/common/console_message_level.h"
18 #include "ui/base/ui_base_types.h"  // WindowShowState
19 #include "ui/gfx/image/image.h"
20 #include "ui/gfx/rect.h"
21
22 class GURL;
23 class Profile;
24 class SkRegion;
25
26 namespace content {
27 class WebContents;
28 }
29
30 namespace extensions {
31 class Extension;
32 class PlatformAppBrowserTest;
33 class WindowController;
34
35 struct DraggableRegion;
36 }
37
38 namespace ui {
39 class BaseWindow;
40 }
41
42 namespace apps {
43
44 class NativeAppWindow;
45
46 // Manages the web contents for Shell 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
49 // native window.
50 class ShellWindowContents {
51  public:
52   ShellWindowContents() {}
53   virtual ~ShellWindowContents() {}
54
55   // Called to initialize the WebContents, before the app window is created.
56   virtual void Initialize(Profile* profile, const GURL& url) = 0;
57
58   // Called to load the contents, after the app window is created.
59   virtual void LoadContents(int32 creator_process_id) = 0;
60
61   // Called when the native window changes.
62   virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0;
63
64   // Called when the native window closes.
65   virtual void NativeWindowClosed() = 0;
66
67   virtual content::WebContents* GetWebContents() const = 0;
68
69  private:
70   DISALLOW_COPY_AND_ASSIGN(ShellWindowContents);
71 };
72
73 // ShellWindow is the type of window used by platform apps. Shell windows
74 // have a WebContents but none of the chrome of normal browser windows.
75 class ShellWindow : public content::NotificationObserver,
76                     public content::WebContentsDelegate,
77                     public web_modal::WebContentsModalDialogManagerDelegate,
78                     public extensions::ExtensionKeybindingRegistry::Delegate,
79                     public extensions::IconImage::Observer {
80  public:
81   enum WindowType {
82     WINDOW_TYPE_DEFAULT  = 1 << 0,  // Default shell window.
83     WINDOW_TYPE_PANEL    = 1 << 1,  // OS controlled panel window (Ash only).
84     WINDOW_TYPE_V1_PANEL = 1 << 2,  // For apps v1 support in Ash; deprecate
85                                     // with v1 apps.
86   };
87
88   enum Frame {
89     FRAME_CHROME,  // Chrome-style window frame.
90     FRAME_NONE,  // Frameless window.
91   };
92
93   class SizeConstraints {
94    public:
95     // The value SizeConstraints uses to represent an unbounded width or height.
96     // This is an enum so that it can be declared inline here.
97     enum { kUnboundedSize = 0 };
98
99     SizeConstraints();
100     SizeConstraints(const gfx::Size& min_size, const gfx::Size& max_size);
101     ~SizeConstraints();
102
103     // Returns the bounds with its size clamped to the min/max size.
104     gfx::Size ClampSize(gfx::Size size) const;
105
106     // When gfx::Size is used as a min/max size, a zero represents an unbounded
107     // component. This method checks whether either component is specified.
108     // Note we can't use gfx::Size::IsEmpty as it returns true if either width
109     // or height is zero.
110     bool HasMinimumSize() const;
111     bool HasMaximumSize() const;
112
113     // This returns true if all components are specified, and min and max are
114     // equal.
115     bool HasFixedSize() const;
116
117     gfx::Size GetMaximumSize() const;
118     gfx::Size GetMinimumSize() const;
119
120     void set_minimum_size(const gfx::Size& min_size);
121     void set_maximum_size(const gfx::Size& max_size);
122
123    private:
124     gfx::Size minimum_size_;
125     gfx::Size maximum_size_;
126   };
127
128   struct CreateParams {
129     CreateParams();
130     ~CreateParams();
131
132     WindowType window_type;
133     Frame frame;
134     bool transparent_background;  // Only supported on ash.
135
136     // Specify the initial content bounds of the window (excluding any window
137     // decorations). INT_MIN designates 'unspecified' for the position
138     // components, and 0 for the size components. When unspecified, they should
139     // be replaced with a default value.
140     gfx::Rect bounds;
141
142     gfx::Size minimum_size;
143     gfx::Size maximum_size;
144
145     std::string window_key;
146
147     // The process ID of the process that requested the create.
148     int32 creator_process_id;
149
150     // Initial state of the window.
151     ui::WindowShowState state;
152
153     // If true, don't show the window after creation.
154     bool hidden;
155
156     // If true, the window will be resizable by the user. Defaults to true.
157     bool resizable;
158
159     // If true, the window will be focused on creation. Defaults to true.
160     bool focused;
161
162     // If true, the window will stay on top of other windows that are not
163     // configured to be always on top. Defaults to false.
164     bool always_on_top;
165   };
166
167   class Delegate {
168    public:
169     virtual ~Delegate();
170
171     // General initialization.
172     virtual void InitWebContents(content::WebContents* web_contents) = 0;
173     virtual NativeAppWindow* CreateNativeAppWindow(
174         ShellWindow* window,
175         const CreateParams& params) = 0;
176
177     // Link handling.
178     virtual content::WebContents* OpenURLFromTab(
179         Profile* profile,
180         content::WebContents* source,
181         const content::OpenURLParams& params) = 0;
182     virtual void AddNewContents(Profile* profile,
183                                 content::WebContents* new_contents,
184                                 WindowOpenDisposition disposition,
185                                 const gfx::Rect& initial_pos,
186                                 bool user_gesture,
187                                 bool* was_blocked) = 0;
188
189     // Feature support.
190     virtual content::ColorChooser* ShowColorChooser(
191         content::WebContents* web_contents,
192         SkColor initial_color) = 0;
193     virtual void RunFileChooser(content::WebContents* tab,
194                                 const content::FileChooserParams& params) = 0;
195     virtual void RequestMediaAccessPermission(
196         content::WebContents* web_contents,
197         const content::MediaStreamRequest& request,
198         const content::MediaResponseCallback& callback,
199       const extensions::Extension* extension) = 0;
200     virtual int PreferredIconSize() = 0;
201
202     // Web contents modal dialog support.
203     virtual void SetWebContentsBlocked(content::WebContents* web_contents,
204                                        bool blocked) = 0;
205     virtual bool IsWebContentsVisible(content::WebContents* web_contents) = 0;
206   };
207
208   // Convert draggable regions in raw format to SkRegion format. Caller is
209   // responsible for deleting the returned SkRegion instance.
210   static SkRegion* RawDraggableRegionsToSkRegion(
211       const std::vector<extensions::DraggableRegion>& regions);
212
213   // The constructor and Init methods are public for constructing a ShellWindow
214   // with a non-standard render interface (e.g. v1 apps using Ash Panels).
215   // Normally ShellWindow::Create should be used.
216   // The constructed shell window takes ownership of |delegate|.
217   ShellWindow(Profile* profile,
218               Delegate* delegate,
219               const extensions::Extension* extension);
220
221   // Initializes the render interface, web contents, and native window.
222   // |shell_window_contents| will become owned by ShellWindow.
223   void Init(const GURL& url,
224             ShellWindowContents* shell_window_contents,
225             const CreateParams& params);
226
227
228   const std::string& window_key() const { return window_key_; }
229   const SessionID& session_id() const { return session_id_; }
230   const extensions::Extension* extension() const { return extension_; }
231   const std::string& extension_id() const { return extension_id_; }
232   content::WebContents* web_contents() const;
233   WindowType window_type() const { return window_type_; }
234   bool window_type_is_panel() const {
235     return (window_type_ == WINDOW_TYPE_PANEL ||
236             window_type_ == WINDOW_TYPE_V1_PANEL);
237   }
238   Profile* profile() const { return profile_; }
239   const gfx::Image& app_icon() const { return app_icon_; }
240   const GURL& app_icon_url() { return app_icon_url_; }
241
242   NativeAppWindow* GetBaseWindow();
243   gfx::NativeWindow GetNativeWindow();
244
245   // Returns the bounds that should be reported to the renderer.
246   gfx::Rect GetClientBounds() const;
247
248   // NativeAppWindows should call this to determine what the window's title
249   // is on startup and from within UpdateWindowTitle().
250   string16 GetTitle() const;
251
252   // Call to notify ShellRegistry and delete the window. Subclasses should
253   // invoke this method instead of using "delete this".
254   void OnNativeClose();
255
256   // Should be called by native implementations when the window size, position,
257   // or minimized/maximized state has changed.
258   void OnNativeWindowChanged();
259
260   // Should be called by native implementations when the window is activated.
261   void OnNativeWindowActivated();
262
263   // Specifies a url for the launcher icon.
264   void SetAppIconUrl(const GURL& icon_url);
265
266   // Set the region in the window that will accept input events.
267   // If |region| is NULL, then the entire window will accept input events.
268   void UpdateInputRegion(scoped_ptr<SkRegion> region);
269
270   // Called from the render interface to modify the draggable regions.
271   void UpdateDraggableRegions(
272       const std::vector<extensions::DraggableRegion>& regions);
273
274   // Updates the app image to |image|. Called internally from the image loader
275   // callback. Also called externally for v1 apps using Ash Panels.
276   void UpdateAppIcon(const gfx::Image& image);
277
278   // Transitions window into fullscreen, maximized, minimized or restores based
279   // on chrome.app.window API.
280   void Fullscreen();
281   void Maximize();
282   void Minimize();
283   void Restore();
284
285   // Set the minimum and maximum size that this window is allowed to be.
286   void SetMinimumSize(const gfx::Size& min_size);
287   void SetMaximumSize(const gfx::Size& max_size);
288
289   ShellWindowContents* shell_window_contents_for_test() {
290     return shell_window_contents_.get();
291   }
292
293   // Get the size constraints.
294   const SizeConstraints& size_constraints() const {
295     return size_constraints_;
296   }
297
298  protected:
299   virtual ~ShellWindow();
300
301  private:
302   // PlatformAppBrowserTest needs access to web_contents()
303   friend class extensions::PlatformAppBrowserTest;
304
305   // content::WebContentsDelegate implementation.
306   virtual void CloseContents(content::WebContents* contents) OVERRIDE;
307   virtual bool ShouldSuppressDialogs() OVERRIDE;
308   virtual content::ColorChooser* OpenColorChooser(
309       content::WebContents* web_contents, SkColor color) OVERRIDE;
310   virtual void RunFileChooser(
311       content::WebContents* tab,
312       const content::FileChooserParams& params) OVERRIDE;
313   virtual bool IsPopupOrPanel(
314       const content::WebContents* source) const OVERRIDE;
315   virtual void MoveContents(
316       content::WebContents* source, const gfx::Rect& pos) OVERRIDE;
317   virtual void NavigationStateChanged(const content::WebContents* source,
318                                       unsigned changed_flags) OVERRIDE;
319   virtual void ToggleFullscreenModeForTab(content::WebContents* source,
320                                           bool enter_fullscreen) OVERRIDE;
321   virtual bool IsFullscreenForTabOrPending(
322       const content::WebContents* source) const OVERRIDE;
323   virtual void RequestMediaAccessPermission(
324       content::WebContents* web_contents,
325       const content::MediaStreamRequest& request,
326       const content::MediaResponseCallback& callback) OVERRIDE;
327   virtual content::WebContents* OpenURLFromTab(
328       content::WebContents* source,
329       const content::OpenURLParams& params) OVERRIDE;
330   virtual void AddNewContents(content::WebContents* source,
331                               content::WebContents* new_contents,
332                               WindowOpenDisposition disposition,
333                               const gfx::Rect& initial_pos,
334                               bool user_gesture,
335                               bool* was_blocked) OVERRIDE;
336   virtual void HandleKeyboardEvent(
337       content::WebContents* source,
338       const content::NativeWebKeyboardEvent& event) OVERRIDE;
339   virtual void RequestToLockMouse(content::WebContents* web_contents,
340                                   bool user_gesture,
341                                   bool last_unlocked_by_target) OVERRIDE;
342
343   // content::NotificationObserver implementation.
344   virtual void Observe(int type,
345                        const content::NotificationSource& source,
346                        const content::NotificationDetails& details) OVERRIDE;
347
348   // web_modal::WebContentsModalDialogManagerDelegate implementation.
349   virtual void SetWebContentsBlocked(content::WebContents* web_contents,
350                                      bool blocked) OVERRIDE;
351   virtual bool IsWebContentsVisible(
352       content::WebContents* web_contents) OVERRIDE;
353
354   // Helper method to add a message to the renderer's DevTools console.
355   void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level,
356                                    const std::string& message);
357
358   // Saves the window geometry/position/screen bounds.
359   void SaveWindowPosition();
360
361   // Helper method to adjust the cached bounds so that we can make sure it can
362   // be visible on the screen. See http://crbug.com/145752 .
363   void AdjustBoundsToBeVisibleOnScreen(
364       const gfx::Rect& cached_bounds,
365       const gfx::Rect& cached_screen_bounds,
366       const gfx::Rect& current_screen_bounds,
367       const gfx::Size& minimum_size,
368       gfx::Rect* bounds) const;
369
370   // Loads the appropriate default or cached window bounds and constrains them
371   // based on screen size and minimum/maximum size. Returns a new CreateParams
372   // that should be used to create the window.
373   CreateParams LoadDefaultsAndConstrain(CreateParams params) const;
374
375   // Load the app's image, firing a load state change when loaded.
376   void UpdateExtensionAppIcon();
377
378   // Called when size_constraints is changed.
379   void OnSizeConstraintsChanged();
380
381   // extensions::ExtensionKeybindingRegistry::Delegate implementation.
382   virtual extensions::ActiveTabPermissionGranter*
383       GetActiveTabPermissionGranter() OVERRIDE;
384
385   // web_modal::WebContentsModalDialogManagerDelegate implementation.
386   virtual web_modal::WebContentsModalDialogHost*
387       GetWebContentsModalDialogHost() OVERRIDE;
388
389   // Callback from web_contents()->DownloadFavicon.
390   void DidDownloadFavicon(int id,
391                           int http_status_code,
392                           const GURL& image_url,
393                           const std::vector<SkBitmap>& bitmaps,
394                           const std::vector<gfx::Size>& original_bitmap_sizes);
395
396   // extensions::IconImage::Observer implementation.
397   virtual void OnExtensionIconImageChanged(
398       extensions::IconImage* image) OVERRIDE;
399
400   Profile* profile_;  // weak pointer - owned by ProfileManager.
401   // weak pointer - owned by ExtensionService.
402   const extensions::Extension* extension_;
403   const std::string extension_id_;
404
405   // Identifier that is used when saving and restoring geometry for this
406   // window.
407   std::string window_key_;
408
409   const SessionID session_id_;
410   WindowType window_type_;
411   content::NotificationRegistrar registrar_;
412
413   // Icon shown in the task bar.
414   gfx::Image app_icon_;
415
416   // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
417   // be fetched and set using this URL.
418   GURL app_icon_url_;
419
420   // An object to load the app's icon as an extension resource.
421   scoped_ptr<extensions::IconImage> app_icon_image_;
422
423   scoped_ptr<NativeAppWindow> native_app_window_;
424   scoped_ptr<ShellWindowContents> shell_window_contents_;
425   scoped_ptr<Delegate> delegate_;
426
427   base::WeakPtrFactory<ShellWindow> image_loader_ptr_factory_;
428
429   // Fullscreen entered by app.window api.
430   bool fullscreen_for_window_api_;
431   // Fullscreen entered by HTML requestFullscreen.
432   bool fullscreen_for_tab_;
433
434   // Size constraints on the window.
435   SizeConstraints size_constraints_;
436
437   DISALLOW_COPY_AND_ASSIGN(ShellWindow);
438 };
439
440 }  // namespace apps
441
442 #endif  // APPS_SHELL_WINDOW_H_