Revert 269361 "Fix WebURLLoaderImpl::Context leak if a pending r..."
[chromium-blink-merge.git] / apps / app_window.h
blob1c19c0c8e77b5016c8dcef8e84c5843277ac3a9c
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 "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/sessions/session_id.h"
12 #include "components/web_modal/web_contents_modal_dialog_manager_delegate.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.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"
22 class GURL;
23 class SkRegion;
25 namespace base {
26 class DictionaryValue;
29 namespace content {
30 class BrowserContext;
31 class WebContents;
34 namespace extensions {
35 class Extension;
36 class PlatformAppBrowserTest;
37 class WindowController;
39 struct DraggableRegion;
42 namespace ui {
43 class BaseWindow;
46 namespace apps {
48 class NativeAppWindow;
50 // Manages the web contents for app windows. The implementation for this
51 // class should create and maintain the WebContents for the window, and handle
52 // any message passing between the web contents and the extension system or
53 // native window.
54 class AppWindowContents {
55 public:
56 AppWindowContents() {}
57 virtual ~AppWindowContents() {}
59 // Called to initialize the WebContents, before the app window is created.
60 virtual void Initialize(content::BrowserContext* context,
61 const GURL& url) = 0;
63 // Called to load the contents, after the app window is created.
64 virtual void LoadContents(int32 creator_process_id) = 0;
66 // Called when the native window changes.
67 virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0;
69 // Called when the native window closes.
70 virtual void NativeWindowClosed() = 0;
72 virtual content::WebContents* GetWebContents() const = 0;
74 private:
75 DISALLOW_COPY_AND_ASSIGN(AppWindowContents);
78 // AppWindow is the type of window used by platform apps. App windows
79 // have a WebContents but none of the chrome of normal browser windows.
80 class AppWindow : public content::NotificationObserver,
81 public content::WebContentsDelegate,
82 public content::WebContentsObserver,
83 public web_modal::WebContentsModalDialogManagerDelegate,
84 public extensions::IconImage::Observer {
85 public:
86 enum WindowType {
87 WINDOW_TYPE_DEFAULT = 1 << 0, // Default app window.
88 WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only).
89 WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate
90 // with v1 apps.
93 enum Frame {
94 FRAME_CHROME, // Chrome-style window frame.
95 FRAME_NONE, // Frameless window.
98 enum FullscreenType {
99 // Not fullscreen.
100 FULLSCREEN_TYPE_NONE = 0,
102 // Fullscreen entered by the app.window api.
103 FULLSCREEN_TYPE_WINDOW_API = 1 << 0,
105 // Fullscreen entered by HTML requestFullscreen().
106 FULLSCREEN_TYPE_HTML_API = 1 << 1,
108 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
109 // enter immersive fullscreen when the user hits the <F4> key.
110 FULLSCREEN_TYPE_OS = 1 << 2,
112 // Fullscreen mode that could not be exited by the user. ChromeOS uses
113 // this type of fullscreen to run an app in kiosk mode.
114 FULLSCREEN_TYPE_FORCED = 1 << 3,
117 struct BoundsSpecification {
118 // INT_MIN represents an unspecified position component.
119 static const int kUnspecifiedPosition;
121 BoundsSpecification();
122 ~BoundsSpecification();
124 // INT_MIN designates 'unspecified' for the position components, and 0
125 // designates 'unspecified' for the size components. When unspecified,
126 // they should be replaced with a default value.
127 gfx::Rect bounds;
129 gfx::Size minimum_size;
130 gfx::Size maximum_size;
132 // Reset the bounds fields to their 'unspecified' values. The minimum and
133 // maximum size constraints remain unchanged.
134 void ResetBounds();
137 struct CreateParams {
138 CreateParams();
139 ~CreateParams();
141 WindowType window_type;
142 Frame frame;
144 bool has_frame_color;
145 SkColor active_frame_color;
146 SkColor inactive_frame_color;
147 bool transparent_background; // Only supported on ash.
149 // The initial content/inner bounds specification (excluding any window
150 // decorations).
151 BoundsSpecification content_spec;
153 // The initial window/outer bounds specification (including window
154 // decorations).
155 BoundsSpecification window_spec;
157 std::string window_key;
159 // The process ID of the process that requested the create.
160 int32 creator_process_id;
162 // Initial state of the window.
163 ui::WindowShowState state;
165 // If true, don't show the window after creation.
166 bool hidden;
168 // If true, the window will be resizable by the user. Defaults to true.
169 bool resizable;
171 // If true, the window will be focused on creation. Defaults to true.
172 bool focused;
174 // If true, the window will stay on top of other windows that are not
175 // configured to be always on top. Defaults to false.
176 bool always_on_top;
178 // The API enables developers to specify content or window bounds. This
179 // function combines them into a single, constrained window size.
180 gfx::Rect GetInitialWindowBounds(const gfx::Insets& frame_insets) const;
182 // The API enables developers to specify content or window size constraints.
183 // These functions combine them so that we can work with one set of
184 // constraints.
185 gfx::Size GetContentMinimumSize(const gfx::Insets& frame_insets) const;
186 gfx::Size GetContentMaximumSize(const gfx::Insets& frame_insets) const;
187 gfx::Size GetWindowMinimumSize(const gfx::Insets& frame_insets) const;
188 gfx::Size GetWindowMaximumSize(const gfx::Insets& frame_insets) const;
191 class Delegate {
192 public:
193 virtual ~Delegate();
195 // General initialization.
196 virtual void InitWebContents(content::WebContents* web_contents) = 0;
197 virtual NativeAppWindow* CreateNativeAppWindow(
198 AppWindow* window,
199 const CreateParams& params) = 0;
201 // Link handling.
202 virtual content::WebContents* OpenURLFromTab(
203 content::BrowserContext* context,
204 content::WebContents* source,
205 const content::OpenURLParams& params) = 0;
206 virtual void AddNewContents(content::BrowserContext* context,
207 content::WebContents* new_contents,
208 WindowOpenDisposition disposition,
209 const gfx::Rect& initial_pos,
210 bool user_gesture,
211 bool* was_blocked) = 0;
213 // Feature support.
214 virtual content::ColorChooser* ShowColorChooser(
215 content::WebContents* web_contents,
216 SkColor initial_color) = 0;
217 virtual void RunFileChooser(content::WebContents* tab,
218 const content::FileChooserParams& params) = 0;
219 virtual void RequestMediaAccessPermission(
220 content::WebContents* web_contents,
221 const content::MediaStreamRequest& request,
222 const content::MediaResponseCallback& callback,
223 const extensions::Extension* extension) = 0;
224 virtual int PreferredIconSize() = 0;
226 // Web contents modal dialog support.
227 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
228 bool blocked) = 0;
229 virtual bool IsWebContentsVisible(content::WebContents* web_contents) = 0;
232 // Convert draggable regions in raw format to SkRegion format. Caller is
233 // responsible for deleting the returned SkRegion instance.
234 static SkRegion* RawDraggableRegionsToSkRegion(
235 const std::vector<extensions::DraggableRegion>& regions);
237 // The constructor and Init methods are public for constructing a AppWindow
238 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
239 // Normally AppWindow::Create should be used.
240 // The constructed app window takes ownership of |delegate|.
241 AppWindow(content::BrowserContext* context,
242 Delegate* delegate,
243 const extensions::Extension* extension);
245 // Initializes the render interface, web contents, and native window.
246 // |app_window_contents| will become owned by AppWindow.
247 void Init(const GURL& url,
248 AppWindowContents* app_window_contents,
249 const CreateParams& params);
251 const std::string& window_key() const { return window_key_; }
252 const SessionID& session_id() const { return session_id_; }
253 const std::string& extension_id() const { return extension_id_; }
254 content::WebContents* web_contents() const;
255 WindowType window_type() const { return window_type_; }
256 bool window_type_is_panel() const {
257 return (window_type_ == WINDOW_TYPE_PANEL ||
258 window_type_ == WINDOW_TYPE_V1_PANEL);
260 content::BrowserContext* browser_context() const { return browser_context_; }
261 const gfx::Image& app_icon() const { return app_icon_; }
262 const GURL& app_icon_url() const { return app_icon_url_; }
263 const gfx::Image& badge_icon() const { return badge_icon_; }
264 const GURL& badge_icon_url() const { return badge_icon_url_; }
265 bool is_hidden() const { return is_hidden_; }
267 const extensions::Extension* GetExtension() const;
268 NativeAppWindow* GetBaseWindow();
269 gfx::NativeWindow GetNativeWindow();
271 // Returns the bounds that should be reported to the renderer.
272 gfx::Rect GetClientBounds() const;
274 // NativeAppWindows should call this to determine what the window's title
275 // is on startup and from within UpdateWindowTitle().
276 base::string16 GetTitle() const;
278 // Call to notify ShellRegistry and delete the window. Subclasses should
279 // invoke this method instead of using "delete this".
280 void OnNativeClose();
282 // Should be called by native implementations when the window size, position,
283 // or minimized/maximized state has changed.
284 void OnNativeWindowChanged();
286 // Should be called by native implementations when the window is activated.
287 void OnNativeWindowActivated();
289 // Specifies a url for the launcher icon.
290 void SetAppIconUrl(const GURL& icon_url);
292 // Specifies a url for the window badge.
293 void SetBadgeIconUrl(const GURL& icon_url);
295 // Clear the current badge.
296 void ClearBadge();
298 // Set the window shape. Passing a NULL |region| sets the default shape.
299 void UpdateShape(scoped_ptr<SkRegion> region);
301 // Called from the render interface to modify the draggable regions.
302 void UpdateDraggableRegions(
303 const std::vector<extensions::DraggableRegion>& regions);
305 // Updates the app image to |image|. Called internally from the image loader
306 // callback. Also called externally for v1 apps using Ash Panels.
307 void UpdateAppIcon(const gfx::Image& image);
309 // Transitions window into fullscreen, maximized, minimized or restores based
310 // on chrome.app.window API.
311 void Fullscreen();
312 void Maximize();
313 void Minimize();
314 void Restore();
316 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
317 void OSFullscreen();
319 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
320 // details.
321 void ForcedFullscreen();
323 // Set the minimum and maximum size of the content bounds.
324 void SetContentSizeConstraints(const gfx::Size& min_size,
325 const gfx::Size& max_size);
327 enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE };
329 // Shows the window if its contents have been painted; otherwise flags the
330 // window to be shown as soon as its contents are painted for the first time.
331 void Show(ShowType show_type);
333 // Hides the window. If the window was previously flagged to be shown on
334 // first paint, it will be unflagged.
335 void Hide();
337 AppWindowContents* app_window_contents_for_test() {
338 return app_window_contents_.get();
341 int fullscreen_types_for_test() {
342 return fullscreen_types_;
345 // Set whether the window should stay above other windows which are not
346 // configured to be always-on-top.
347 void SetAlwaysOnTop(bool always_on_top);
349 // Whether the always-on-top property has been set by the chrome.app.window
350 // API. Note that the actual value of this property in the native app window
351 // may be false if the bit is silently switched off for security reasons.
352 bool IsAlwaysOnTop() const;
354 // Retrieve the current state of the app window as a dictionary, to pass to
355 // the renderer.
356 void GetSerializedState(base::DictionaryValue* properties) const;
358 protected:
359 virtual ~AppWindow();
361 private:
362 // PlatformAppBrowserTest needs access to web_contents()
363 friend class extensions::PlatformAppBrowserTest;
365 // content::WebContentsDelegate implementation.
366 virtual void CloseContents(content::WebContents* contents) OVERRIDE;
367 virtual bool ShouldSuppressDialogs() OVERRIDE;
368 virtual content::ColorChooser* OpenColorChooser(
369 content::WebContents* web_contents,
370 SkColor color,
371 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
372 virtual void RunFileChooser(content::WebContents* tab,
373 const content::FileChooserParams& params)
374 OVERRIDE;
375 virtual bool IsPopupOrPanel(const content::WebContents* source)
376 const OVERRIDE;
377 virtual void MoveContents(content::WebContents* source,
378 const gfx::Rect& pos) OVERRIDE;
379 virtual void NavigationStateChanged(const content::WebContents* source,
380 unsigned changed_flags) OVERRIDE;
381 virtual void ToggleFullscreenModeForTab(content::WebContents* source,
382 bool enter_fullscreen) OVERRIDE;
383 virtual bool IsFullscreenForTabOrPending(const content::WebContents* source)
384 const OVERRIDE;
385 virtual void RequestMediaAccessPermission(
386 content::WebContents* web_contents,
387 const content::MediaStreamRequest& request,
388 const content::MediaResponseCallback& callback) OVERRIDE;
389 virtual content::WebContents* OpenURLFromTab(
390 content::WebContents* source,
391 const content::OpenURLParams& params) OVERRIDE;
392 virtual void AddNewContents(content::WebContents* source,
393 content::WebContents* new_contents,
394 WindowOpenDisposition disposition,
395 const gfx::Rect& initial_pos,
396 bool user_gesture,
397 bool* was_blocked) OVERRIDE;
398 virtual bool PreHandleKeyboardEvent(
399 content::WebContents* source,
400 const content::NativeWebKeyboardEvent& event,
401 bool* is_keyboard_shortcut) OVERRIDE;
402 virtual void HandleKeyboardEvent(content::WebContents* source,
403 const content::NativeWebKeyboardEvent& event)
404 OVERRIDE;
405 virtual void RequestToLockMouse(content::WebContents* web_contents,
406 bool user_gesture,
407 bool last_unlocked_by_target) OVERRIDE;
408 virtual bool PreHandleGestureEvent(content::WebContents* source,
409 const blink::WebGestureEvent& event)
410 OVERRIDE;
412 // content::WebContentsObserver implementation.
413 virtual void DidFirstVisuallyNonEmptyPaint() OVERRIDE;
415 // content::NotificationObserver implementation.
416 virtual void Observe(int type,
417 const content::NotificationSource& source,
418 const content::NotificationDetails& details) OVERRIDE;
420 // web_modal::WebContentsModalDialogManagerDelegate implementation.
421 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
422 bool blocked) OVERRIDE;
423 virtual bool IsWebContentsVisible(content::WebContents* web_contents)
424 OVERRIDE;
426 // Helper method to add a message to the renderer's DevTools console.
427 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level,
428 const std::string& message);
430 // Saves the window geometry/position/screen bounds.
431 void SaveWindowPosition();
433 // Helper method to adjust the cached bounds so that we can make sure it can
434 // be visible on the screen. See http://crbug.com/145752.
435 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
436 const gfx::Rect& cached_screen_bounds,
437 const gfx::Rect& current_screen_bounds,
438 const gfx::Size& minimum_size,
439 gfx::Rect* bounds) const;
441 // Loads the appropriate default or cached window bounds. Returns a new
442 // CreateParams that should be used to create the window.
443 CreateParams LoadDefaults(CreateParams params) const;
445 // Load the app's image, firing a load state change when loaded.
446 void UpdateExtensionAppIcon();
448 // Set the fullscreen state in the native app window.
449 void SetNativeWindowFullscreen();
451 // Returns true if there is any overlap between the window and the taskbar
452 // (Windows only).
453 bool IntersectsWithTaskbar() const;
455 // Update the always-on-top bit in the native app window.
456 void UpdateNativeAlwaysOnTop();
458 // web_modal::WebContentsModalDialogManagerDelegate implementation.
459 virtual web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
460 OVERRIDE;
462 // Updates the badge to |image|. Called internally from the image loader
463 // callback.
464 void UpdateBadgeIcon(const gfx::Image& image);
466 // Callback from web_contents()->DownloadFavicon.
467 void DidDownloadFavicon(int id,
468 int http_status_code,
469 const GURL& image_url,
470 const std::vector<SkBitmap>& bitmaps,
471 const std::vector<gfx::Size>& original_bitmap_sizes);
473 // extensions::IconImage::Observer implementation.
474 virtual void OnExtensionIconImageChanged(extensions::IconImage* image)
475 OVERRIDE;
477 // The browser context with which this window is associated. AppWindow does
478 // not own this object.
479 content::BrowserContext* browser_context_;
481 const std::string extension_id_;
483 // Identifier that is used when saving and restoring geometry for this
484 // window.
485 std::string window_key_;
487 const SessionID session_id_;
488 WindowType window_type_;
489 content::NotificationRegistrar registrar_;
491 // Icon shown in the task bar.
492 gfx::Image app_icon_;
494 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
495 // be fetched and set using this URL.
496 GURL app_icon_url_;
498 // An object to load the app's icon as an extension resource.
499 scoped_ptr<extensions::IconImage> app_icon_image_;
501 // Badge for icon shown in the task bar.
502 gfx::Image badge_icon_;
504 // URL to be used for setting the badge on the app icon.
505 GURL badge_icon_url_;
507 // An object to load the badge as an extension resource.
508 scoped_ptr<extensions::IconImage> badge_icon_image_;
510 scoped_ptr<NativeAppWindow> native_app_window_;
511 scoped_ptr<AppWindowContents> app_window_contents_;
512 scoped_ptr<Delegate> delegate_;
514 base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
516 // Bit field of FullscreenType.
517 int fullscreen_types_;
519 // Show has been called, so the window should be shown once the first visually
520 // non-empty paint occurs.
521 bool show_on_first_paint_;
523 // The first visually non-empty paint has completed.
524 bool first_paint_complete_;
526 // Whether the window is hidden or not. Hidden in this context means actively
527 // by the chrome.app.window API, not in an operating system context. For
528 // example windows which are minimized are not hidden, and windows which are
529 // part of a hidden app on OS X are not hidden. Windows which were created
530 // with the |hidden| flag set to true, or which have been programmatically
531 // hidden, are considered hidden.
532 bool is_hidden_;
534 // Whether the delayed Show() call was for an active or inactive window.
535 ShowType delayed_show_type_;
537 // Cache the desired value of the always-on-top property. When windows enter
538 // fullscreen or overlap the Windows taskbar, this property will be
539 // automatically and silently switched off for security reasons. It is
540 // reinstated when the window exits fullscreen and moves away from the
541 // taskbar.
542 bool cached_always_on_top_;
544 DISALLOW_COPY_AND_ASSIGN(AppWindow);
547 } // namespace apps
549 #endif // APPS_APP_WINDOW_H_