A few last Android infobar cleanups:
[chromium-blink-merge.git] / apps / shell_window.h
blobd8d3964ef0788f0d1a93e05931b8a590a1fdf279
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.
5 #ifndef APPS_SHELL_WINDOW_H_
6 #define APPS_SHELL_WINDOW_H_
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/browser/web_contents_observer.h"
18 #include "content/public/common/console_message_level.h"
19 #include "ui/base/ui_base_types.h" // WindowShowState
20 #include "ui/gfx/image/image.h"
21 #include "ui/gfx/rect.h"
23 class GURL;
24 class Profile;
25 class SkRegion;
27 namespace content {
28 class WebContents;
31 namespace extensions {
32 class Extension;
33 class PlatformAppBrowserTest;
34 class WindowController;
36 struct DraggableRegion;
39 namespace ui {
40 class BaseWindow;
43 namespace apps {
45 class NativeAppWindow;
47 // Manages the web contents for Shell Windows. The implementation for this
48 // class should create and maintain the WebContents for the window, and handle
49 // any message passing between the web contents and the extension system or
50 // native window.
51 class ShellWindowContents {
52 public:
53 ShellWindowContents() {}
54 virtual ~ShellWindowContents() {}
56 // Called to initialize the WebContents, before the app window is created.
57 virtual void Initialize(Profile* profile, const GURL& url) = 0;
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 virtual content::WebContents* GetWebContents() const = 0;
70 private:
71 DISALLOW_COPY_AND_ASSIGN(ShellWindowContents);
74 // ShellWindow is the type of window used by platform apps. Shell windows
75 // have a WebContents but none of the chrome of normal browser windows.
76 class ShellWindow : public content::NotificationObserver,
77 public content::WebContentsDelegate,
78 public content::WebContentsObserver,
79 public web_modal::WebContentsModalDialogManagerDelegate,
80 public extensions::ExtensionKeybindingRegistry::Delegate,
81 public extensions::IconImage::Observer {
82 public:
83 enum WindowType {
84 WINDOW_TYPE_DEFAULT = 1 << 0, // Default shell window.
85 WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only).
86 WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate
87 // with v1 apps.
90 enum Frame {
91 FRAME_CHROME, // Chrome-style window frame.
92 FRAME_NONE, // Frameless window.
95 enum FullscreenType {
96 // Not fullscreen.
97 FULLSCREEN_TYPE_NONE = 0,
99 // Fullscreen entered by the app.window api.
100 FULLSCREEN_TYPE_WINDOW_API = 1 << 0,
102 // Fullscreen entered by HTML requestFullscreen().
103 FULLSCREEN_TYPE_HTML_API = 1 << 1,
105 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
106 // enter immersive fullscreen when the user hits the <F4> key.
107 FULLSCREEN_TYPE_OS = 1 << 2
110 class SizeConstraints {
111 public:
112 // The value SizeConstraints uses to represent an unbounded width or height.
113 // This is an enum so that it can be declared inline here.
114 enum { kUnboundedSize = 0 };
116 SizeConstraints();
117 SizeConstraints(const gfx::Size& min_size, const gfx::Size& max_size);
118 ~SizeConstraints();
120 // Returns the bounds with its size clamped to the min/max size.
121 gfx::Size ClampSize(gfx::Size size) const;
123 // When gfx::Size is used as a min/max size, a zero represents an unbounded
124 // component. This method checks whether either component is specified.
125 // Note we can't use gfx::Size::IsEmpty as it returns true if either width
126 // or height is zero.
127 bool HasMinimumSize() const;
128 bool HasMaximumSize() const;
130 // This returns true if all components are specified, and min and max are
131 // equal.
132 bool HasFixedSize() const;
134 gfx::Size GetMaximumSize() const;
135 gfx::Size GetMinimumSize() const;
137 void set_minimum_size(const gfx::Size& min_size);
138 void set_maximum_size(const gfx::Size& max_size);
140 private:
141 gfx::Size minimum_size_;
142 gfx::Size maximum_size_;
145 struct CreateParams {
146 CreateParams();
147 ~CreateParams();
149 WindowType window_type;
150 Frame frame;
151 bool transparent_background; // Only supported on ash.
153 // Specify the initial content bounds of the window (excluding any window
154 // decorations). INT_MIN designates 'unspecified' for the position
155 // components, and 0 for the size components. When unspecified, they should
156 // be replaced with a default value.
157 gfx::Rect bounds;
159 gfx::Size minimum_size;
160 gfx::Size maximum_size;
162 std::string window_key;
164 // The process ID of the process that requested the create.
165 int32 creator_process_id;
167 // Initial state of the window.
168 ui::WindowShowState state;
170 // If true, don't show the window after creation.
171 bool hidden;
173 // If true, the window will be resizable by the user. Defaults to true.
174 bool resizable;
176 // If true, the window will be focused on creation. Defaults to true.
177 bool focused;
179 // If true, the window will stay on top of other windows that are not
180 // configured to be always on top. Defaults to false.
181 bool always_on_top;
184 class Delegate {
185 public:
186 virtual ~Delegate();
188 // General initialization.
189 virtual void InitWebContents(content::WebContents* web_contents) = 0;
190 virtual NativeAppWindow* CreateNativeAppWindow(
191 ShellWindow* window,
192 const CreateParams& params) = 0;
194 // Link handling.
195 virtual content::WebContents* OpenURLFromTab(
196 Profile* profile,
197 content::WebContents* source,
198 const content::OpenURLParams& params) = 0;
199 virtual void AddNewContents(Profile* profile,
200 content::WebContents* new_contents,
201 WindowOpenDisposition disposition,
202 const gfx::Rect& initial_pos,
203 bool user_gesture,
204 bool* was_blocked) = 0;
206 // Feature support.
207 virtual content::ColorChooser* ShowColorChooser(
208 content::WebContents* web_contents,
209 SkColor initial_color) = 0;
210 virtual void RunFileChooser(content::WebContents* tab,
211 const content::FileChooserParams& params) = 0;
212 virtual void RequestMediaAccessPermission(
213 content::WebContents* web_contents,
214 const content::MediaStreamRequest& request,
215 const content::MediaResponseCallback& callback,
216 const extensions::Extension* extension) = 0;
217 virtual int PreferredIconSize() = 0;
219 // Web contents modal dialog support.
220 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
221 bool blocked) = 0;
222 virtual bool IsWebContentsVisible(content::WebContents* web_contents) = 0;
225 // Convert draggable regions in raw format to SkRegion format. Caller is
226 // responsible for deleting the returned SkRegion instance.
227 static SkRegion* RawDraggableRegionsToSkRegion(
228 const std::vector<extensions::DraggableRegion>& regions);
230 // The constructor and Init methods are public for constructing a ShellWindow
231 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
232 // Normally ShellWindow::Create should be used.
233 // The constructed shell window takes ownership of |delegate|.
234 ShellWindow(Profile* profile,
235 Delegate* delegate,
236 const extensions::Extension* extension);
238 // Initializes the render interface, web contents, and native window.
239 // |shell_window_contents| will become owned by ShellWindow.
240 void Init(const GURL& url,
241 ShellWindowContents* shell_window_contents,
242 const CreateParams& params);
245 const std::string& window_key() const { return window_key_; }
246 const SessionID& session_id() const { return session_id_; }
247 const extensions::Extension* extension() const { return extension_; }
248 const std::string& extension_id() const { return extension_id_; }
249 content::WebContents* web_contents() const;
250 WindowType window_type() const { return window_type_; }
251 bool window_type_is_panel() const {
252 return (window_type_ == WINDOW_TYPE_PANEL ||
253 window_type_ == WINDOW_TYPE_V1_PANEL);
255 Profile* profile() const { return profile_; }
256 const gfx::Image& app_icon() const { return app_icon_; }
257 const GURL& app_icon_url() { return app_icon_url_; }
259 NativeAppWindow* GetBaseWindow();
260 gfx::NativeWindow GetNativeWindow();
262 // Returns the bounds that should be reported to the renderer.
263 gfx::Rect GetClientBounds() const;
265 // NativeAppWindows should call this to determine what the window's title
266 // is on startup and from within UpdateWindowTitle().
267 string16 GetTitle() const;
269 // Call to notify ShellRegistry and delete the window. Subclasses should
270 // invoke this method instead of using "delete this".
271 void OnNativeClose();
273 // Should be called by native implementations when the window size, position,
274 // or minimized/maximized state has changed.
275 void OnNativeWindowChanged();
277 // Should be called by native implementations when the window is activated.
278 void OnNativeWindowActivated();
280 // Specifies a url for the launcher icon.
281 void SetAppIconUrl(const GURL& icon_url);
283 // Set the region in the window that will accept input events.
284 // If |region| is NULL, then the entire window will accept input events.
285 void UpdateInputRegion(scoped_ptr<SkRegion> region);
287 // Called from the render interface to modify the draggable regions.
288 void UpdateDraggableRegions(
289 const std::vector<extensions::DraggableRegion>& regions);
291 // Updates the app image to |image|. Called internally from the image loader
292 // callback. Also called externally for v1 apps using Ash Panels.
293 void UpdateAppIcon(const gfx::Image& image);
295 // Transitions window into fullscreen, maximized, minimized or restores based
296 // on chrome.app.window API.
297 void Fullscreen();
298 void Maximize();
299 void Minimize();
300 void Restore();
302 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
303 void OSFullscreen();
305 // Set the minimum and maximum size that this window is allowed to be.
306 void SetMinimumSize(const gfx::Size& min_size);
307 void SetMaximumSize(const gfx::Size& max_size);
309 enum ShowType {
310 SHOW_ACTIVE,
311 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 ShellWindowContents* shell_window_contents_for_test() {
323 return shell_window_contents_.get();
326 // Get the size constraints.
327 const SizeConstraints& size_constraints() const {
328 return size_constraints_;
331 // Set whether the window should stay above other windows which are not
332 // configured to be always-on-top.
333 void SetAlwaysOnTop(bool always_on_top);
335 // Whether the always-on-top property has been set by the chrome.app.window
336 // API. Note that the actual value of this property in the native app window
337 // will be false in fullscreen mode.
338 bool IsAlwaysOnTop() const;
340 protected:
341 virtual ~ShellWindow();
343 private:
344 // PlatformAppBrowserTest needs access to web_contents()
345 friend class extensions::PlatformAppBrowserTest;
347 // content::WebContentsDelegate implementation.
348 virtual void CloseContents(content::WebContents* contents) OVERRIDE;
349 virtual bool ShouldSuppressDialogs() OVERRIDE;
350 virtual content::ColorChooser* OpenColorChooser(
351 content::WebContents* web_contents, SkColor color) OVERRIDE;
352 virtual void RunFileChooser(
353 content::WebContents* tab,
354 const content::FileChooserParams& params) OVERRIDE;
355 virtual bool IsPopupOrPanel(
356 const content::WebContents* source) const OVERRIDE;
357 virtual void MoveContents(
358 content::WebContents* source, const gfx::Rect& pos) OVERRIDE;
359 virtual void NavigationStateChanged(const content::WebContents* source,
360 unsigned changed_flags) OVERRIDE;
361 virtual void ToggleFullscreenModeForTab(content::WebContents* source,
362 bool enter_fullscreen) OVERRIDE;
363 virtual bool IsFullscreenForTabOrPending(
364 const content::WebContents* source) const OVERRIDE;
365 virtual void RequestMediaAccessPermission(
366 content::WebContents* web_contents,
367 const content::MediaStreamRequest& request,
368 const content::MediaResponseCallback& callback) OVERRIDE;
369 virtual content::WebContents* OpenURLFromTab(
370 content::WebContents* source,
371 const content::OpenURLParams& params) OVERRIDE;
372 virtual void AddNewContents(content::WebContents* source,
373 content::WebContents* new_contents,
374 WindowOpenDisposition disposition,
375 const gfx::Rect& initial_pos,
376 bool user_gesture,
377 bool* was_blocked) OVERRIDE;
378 virtual void HandleKeyboardEvent(
379 content::WebContents* source,
380 const content::NativeWebKeyboardEvent& event) OVERRIDE;
381 virtual void RequestToLockMouse(content::WebContents* web_contents,
382 bool user_gesture,
383 bool last_unlocked_by_target) OVERRIDE;
385 // content::WebContentsObserver implementation.
386 virtual void DidFirstVisuallyNonEmptyPaint(int32 page_id) OVERRIDE;
388 // content::NotificationObserver implementation.
389 virtual void Observe(int type,
390 const content::NotificationSource& source,
391 const content::NotificationDetails& details) OVERRIDE;
393 // web_modal::WebContentsModalDialogManagerDelegate implementation.
394 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
395 bool blocked) OVERRIDE;
396 virtual bool IsWebContentsVisible(
397 content::WebContents* web_contents) OVERRIDE;
399 // Helper method to add a message to the renderer's DevTools console.
400 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level,
401 const std::string& message);
403 // Saves the window geometry/position/screen bounds.
404 void SaveWindowPosition();
406 // Helper method to adjust the cached bounds so that we can make sure it can
407 // be visible on the screen. See http://crbug.com/145752 .
408 void AdjustBoundsToBeVisibleOnScreen(
409 const gfx::Rect& cached_bounds,
410 const gfx::Rect& cached_screen_bounds,
411 const gfx::Rect& current_screen_bounds,
412 const gfx::Size& minimum_size,
413 gfx::Rect* bounds) const;
415 // Loads the appropriate default or cached window bounds and constrains them
416 // based on screen size and minimum/maximum size. Returns a new CreateParams
417 // that should be used to create the window.
418 CreateParams LoadDefaultsAndConstrain(CreateParams params) const;
420 // Load the app's image, firing a load state change when loaded.
421 void UpdateExtensionAppIcon();
423 // Called when size_constraints is changed.
424 void OnSizeConstraintsChanged();
426 // Set the fullscreen state in the native app window.
427 void SetNativeWindowFullscreen(int fullscreen_types);
429 // extensions::ExtensionKeybindingRegistry::Delegate implementation.
430 virtual extensions::ActiveTabPermissionGranter*
431 GetActiveTabPermissionGranter() OVERRIDE;
433 // web_modal::WebContentsModalDialogManagerDelegate implementation.
434 virtual web_modal::WebContentsModalDialogHost*
435 GetWebContentsModalDialogHost() OVERRIDE;
437 // Callback from web_contents()->DownloadFavicon.
438 void DidDownloadFavicon(int id,
439 int http_status_code,
440 const GURL& image_url,
441 const std::vector<SkBitmap>& bitmaps,
442 const std::vector<gfx::Size>& original_bitmap_sizes);
444 // extensions::IconImage::Observer implementation.
445 virtual void OnExtensionIconImageChanged(
446 extensions::IconImage* image) OVERRIDE;
448 Profile* profile_; // weak pointer - owned by ProfileManager.
449 // weak pointer - owned by ExtensionService.
450 const extensions::Extension* extension_;
451 const std::string extension_id_;
453 // Identifier that is used when saving and restoring geometry for this
454 // window.
455 std::string window_key_;
457 const SessionID session_id_;
458 WindowType window_type_;
459 content::NotificationRegistrar registrar_;
461 // Icon shown in the task bar.
462 gfx::Image app_icon_;
464 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
465 // be fetched and set using this URL.
466 GURL app_icon_url_;
468 // An object to load the app's icon as an extension resource.
469 scoped_ptr<extensions::IconImage> app_icon_image_;
471 scoped_ptr<NativeAppWindow> native_app_window_;
472 scoped_ptr<ShellWindowContents> shell_window_contents_;
473 scoped_ptr<Delegate> delegate_;
475 base::WeakPtrFactory<ShellWindow> image_loader_ptr_factory_;
477 // Bit field of FullscreenType.
478 int fullscreen_types_;
480 // Size constraints on the window.
481 SizeConstraints size_constraints_;
483 // Show has been called, so the window should be shown once the first visually
484 // non-empty paint occurs.
485 bool show_on_first_paint_;
487 // The first visually non-empty paint has completed.
488 bool first_paint_complete_;
490 // Whether the delayed Show() call was for an active or inactive window.
491 ShowType delayed_show_type_;
493 // Cache the desired value of the always-on-top property. When windows enter
494 // fullscreen, this property will be automatically switched off for security
495 // reasons. It is reinstated when the window exits fullscreen.
496 bool cached_always_on_top_;
498 DISALLOW_COPY_AND_ASSIGN(ShellWindow);
501 } // namespace apps
503 #endif // APPS_SHELL_WINDOW_H_