Files.app: Dispatch 'drive-connection-changed' event on initialization of VolumeManag...
[chromium-blink-merge.git] / extensions / browser / app_window / app_window.h
blob12eb78d4ef8f06cb42027f1ebdf89efbb7faad8c
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 EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_
6 #define EXTENSIONS_BROWSER_APP_WINDOW_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/web_contents_delegate.h"
17 #include "content/public/browser/web_contents_observer.h"
18 #include "extensions/browser/extension_function_dispatcher.h"
19 #include "extensions/browser/extension_icon_image.h"
20 #include "extensions/browser/extension_registry_observer.h"
21 #include "ui/base/ui_base_types.h" // WindowShowState
22 #include "ui/gfx/geometry/rect.h"
23 #include "ui/gfx/image/image.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 {
39 class AppDelegate;
40 class AppWebContentsHelper;
41 class Extension;
42 class NativeAppWindow;
43 class PlatformAppBrowserTest;
45 struct DraggableRegion;
47 // Manages the web contents for app 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 AppWindowContents {
52 public:
53 AppWindowContents() {}
54 virtual ~AppWindowContents() {}
56 // Called to initialize the WebContents, before the app window is created.
57 virtual void Initialize(content::BrowserContext* context,
58 const GURL& url) = 0;
60 // Called to load the contents, after the app window is created.
61 virtual void LoadContents(int32 creator_process_id) = 0;
63 // Called when the native window changes.
64 virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0;
66 // Called when the native window closes.
67 virtual void NativeWindowClosed() = 0;
69 // Called in tests when the window is shown
70 virtual void DispatchWindowShownForTests() const = 0;
72 virtual content::WebContents* GetWebContents() const = 0;
74 virtual extensions::WindowController* GetWindowController() const = 0;
76 private:
77 DISALLOW_COPY_AND_ASSIGN(AppWindowContents);
80 // AppWindow is the type of window used by platform apps. App windows
81 // have a WebContents but none of the chrome of normal browser windows.
82 class AppWindow : public content::WebContentsDelegate,
83 public content::WebContentsObserver,
84 public web_modal::WebContentsModalDialogManagerDelegate,
85 public IconImage::Observer,
86 public ExtensionFunctionDispatcher::Delegate,
87 public ExtensionRegistryObserver {
88 public:
89 enum WindowType {
90 WINDOW_TYPE_DEFAULT = 1 << 0, // Default app window.
91 WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only).
92 WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate
93 // with v1 apps.
96 enum Frame {
97 FRAME_CHROME, // Chrome-style window frame.
98 FRAME_NONE, // Frameless window.
101 enum FullscreenType {
102 // Not fullscreen.
103 FULLSCREEN_TYPE_NONE = 0,
105 // Fullscreen entered by the app.window api.
106 FULLSCREEN_TYPE_WINDOW_API = 1 << 0,
108 // Fullscreen entered by HTML requestFullscreen().
109 FULLSCREEN_TYPE_HTML_API = 1 << 1,
111 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
112 // enter immersive fullscreen when the user hits the <F4> key.
113 FULLSCREEN_TYPE_OS = 1 << 2,
115 // Fullscreen mode that could not be exited by the user. ChromeOS uses
116 // this type of fullscreen to run an app in kiosk mode.
117 FULLSCREEN_TYPE_FORCED = 1 << 3,
120 struct BoundsSpecification {
121 // INT_MIN represents an unspecified position component.
122 static const int kUnspecifiedPosition;
124 BoundsSpecification();
125 ~BoundsSpecification();
127 // INT_MIN designates 'unspecified' for the position components, and 0
128 // designates 'unspecified' for the size components. When unspecified,
129 // they should be replaced with a default value.
130 gfx::Rect bounds;
132 gfx::Size minimum_size;
133 gfx::Size maximum_size;
135 // Reset the bounds fields to their 'unspecified' values. The minimum and
136 // maximum size constraints remain unchanged.
137 void ResetBounds();
140 struct CreateParams {
141 CreateParams();
142 ~CreateParams();
144 WindowType window_type;
145 Frame frame;
147 bool has_frame_color;
148 SkColor active_frame_color;
149 SkColor inactive_frame_color;
150 bool alpha_enabled;
151 bool is_ime_window;
153 // The initial content/inner bounds specification (excluding any window
154 // decorations).
155 BoundsSpecification content_spec;
157 // The initial window/outer bounds specification (including window
158 // decorations).
159 BoundsSpecification window_spec;
161 std::string window_key;
163 // The process ID of the process that requested the create.
164 int32 creator_process_id;
166 // Initial state of the window.
167 ui::WindowShowState state;
169 // If true, don't show the window after creation.
170 bool hidden;
172 // If true, the window will be resizable by the user. Defaults to true.
173 bool resizable;
175 // If true, the window will be focused on creation. Defaults to true.
176 bool focused;
178 // If true, the window will stay on top of other windows that are not
179 // configured to be always on top. Defaults to false.
180 bool always_on_top;
182 // If true, the window will be visible on all workspaces. Defaults to false.
183 bool visible_on_all_workspaces;
185 // The API enables developers to specify content or window bounds. This
186 // function combines them into a single, constrained window size.
187 gfx::Rect GetInitialWindowBounds(const gfx::Insets& frame_insets) const;
189 // The API enables developers to specify content or window size constraints.
190 // These functions combine them so that we can work with one set of
191 // constraints.
192 gfx::Size GetContentMinimumSize(const gfx::Insets& frame_insets) const;
193 gfx::Size GetContentMaximumSize(const gfx::Insets& frame_insets) const;
194 gfx::Size GetWindowMinimumSize(const gfx::Insets& frame_insets) const;
195 gfx::Size GetWindowMaximumSize(const gfx::Insets& frame_insets) const;
198 // Convert draggable regions in raw format to SkRegion format. Caller is
199 // responsible for deleting the returned SkRegion instance.
200 static SkRegion* RawDraggableRegionsToSkRegion(
201 const std::vector<DraggableRegion>& regions);
203 // The constructor and Init methods are public for constructing a AppWindow
204 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
205 // Normally AppWindow::Create should be used.
206 // Takes ownership of |app_delegate| and |delegate|.
207 AppWindow(content::BrowserContext* context,
208 AppDelegate* app_delegate,
209 const Extension* extension);
211 // Initializes the render interface, web contents, and native window.
212 // |app_window_contents| will become owned by AppWindow.
213 void Init(const GURL& url,
214 AppWindowContents* app_window_contents,
215 const CreateParams& params);
217 const std::string& window_key() const { return window_key_; }
218 const SessionID& session_id() const { return session_id_; }
219 const std::string& extension_id() const { return extension_id_; }
220 content::WebContents* web_contents() const;
221 WindowType window_type() const { return window_type_; }
222 bool window_type_is_panel() const {
223 return (window_type_ == WINDOW_TYPE_PANEL ||
224 window_type_ == WINDOW_TYPE_V1_PANEL);
226 content::BrowserContext* browser_context() const { return browser_context_; }
227 const gfx::Image& app_icon() const { return app_icon_; }
228 const GURL& app_icon_url() const { return app_icon_url_; }
229 const GURL& initial_url() const { return initial_url_; }
230 bool is_hidden() const { return is_hidden_; }
232 const Extension* GetExtension() const;
233 NativeAppWindow* GetBaseWindow();
234 gfx::NativeWindow GetNativeWindow();
236 // Returns the bounds that should be reported to the renderer.
237 gfx::Rect GetClientBounds() const;
239 // NativeAppWindows should call this to determine what the window's title
240 // is on startup and from within UpdateWindowTitle().
241 base::string16 GetTitle() const;
243 // Call to notify ShellRegistry and delete the window. Subclasses should
244 // invoke this method instead of using "delete this".
245 void OnNativeClose();
247 // Should be called by native implementations when the window size, position,
248 // or minimized/maximized state has changed.
249 void OnNativeWindowChanged();
251 // Should be called by native implementations when the window is activated.
252 void OnNativeWindowActivated();
254 // Specifies a url for the launcher icon.
255 void SetAppIconUrl(const GURL& icon_url);
257 // Set the window shape. Passing a NULL |region| sets the default shape.
258 void UpdateShape(scoped_ptr<SkRegion> region);
260 // Called from the render interface to modify the draggable regions.
261 void UpdateDraggableRegions(const std::vector<DraggableRegion>& regions);
263 // Updates the app image to |image|. Called internally from the image loader
264 // callback. Also called externally for v1 apps using Ash Panels.
265 void UpdateAppIcon(const gfx::Image& image);
267 // Enable or disable fullscreen mode. |type| specifies which type of
268 // fullscreen mode to change (note that disabling one type of fullscreen may
269 // not exit fullscreen mode because a window may have a different type of
270 // fullscreen enabled). If |type| is not FORCED, checks that the extension has
271 // the required permission.
272 void SetFullscreen(FullscreenType type, bool enable);
274 // Returns true if the app window is in a fullscreen state.
275 bool IsFullscreen() const;
277 // Returns true if the app window is in a forced fullscreen state (one that
278 // cannot be exited by the user).
279 bool IsForcedFullscreen() const;
281 // Returns true if the app window is in a fullscreen state entered from an
282 // HTML API request.
283 bool IsHtmlApiFullscreen() const;
285 // Transitions window into fullscreen, maximized, minimized or restores based
286 // on chrome.app.window API.
287 void Fullscreen();
288 void Maximize();
289 void Minimize();
290 void Restore();
292 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
293 void OSFullscreen();
295 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
296 // details.
297 void ForcedFullscreen();
299 // Set the minimum and maximum size of the content bounds.
300 void SetContentSizeConstraints(const gfx::Size& min_size,
301 const gfx::Size& max_size);
303 enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE };
305 // Shows the window if its contents have been painted; otherwise flags the
306 // window to be shown as soon as its contents are painted for the first time.
307 void Show(ShowType show_type);
309 // Hides the window. If the window was previously flagged to be shown on
310 // first paint, it will be unflagged.
311 void Hide();
313 AppWindowContents* app_window_contents_for_test() {
314 return app_window_contents_.get();
317 int fullscreen_types_for_test() {
318 return fullscreen_types_;
321 // Set whether the window should stay above other windows which are not
322 // configured to be always-on-top.
323 void SetAlwaysOnTop(bool always_on_top);
325 // Whether the always-on-top property has been set by the chrome.app.window
326 // API. Note that the actual value of this property in the native app window
327 // may be false if the bit is silently switched off for security reasons.
328 bool IsAlwaysOnTop() const;
330 // Restores the always-on-top property according to |cached_always_on_top_|.
331 void RestoreAlwaysOnTop();
333 // Set whether the window should get even reserved keys (modulo platform
334 // restrictions).
335 void SetInterceptAllKeys(bool want_all_keys);
337 // Retrieve the current state of the app window as a dictionary, to pass to
338 // the renderer.
339 void GetSerializedState(base::DictionaryValue* properties) const;
341 // Called by the window API when events can be sent to the window for this
342 // app.
343 void WindowEventsReady();
345 // Whether the app window wants to be alpha enabled.
346 bool requested_alpha_enabled() const { return requested_alpha_enabled_; }
348 // Whether the app window is created by IME extensions.
349 // TODO(bshe): rename to hide_app_window_in_launcher if it is not used
350 // anywhere other than app_window_launcher_controller after M45. Otherwise,
351 // remove this TODO.
352 bool is_ime_window() const { return is_ime_window_; }
354 void SetAppWindowContentsForTesting(scoped_ptr<AppWindowContents> contents) {
355 app_window_contents_ = contents.Pass();
358 protected:
359 ~AppWindow() override;
361 private:
362 // PlatformAppBrowserTest needs access to web_contents()
363 friend class PlatformAppBrowserTest;
365 // content::WebContentsDelegate implementation.
366 void CloseContents(content::WebContents* contents) override;
367 bool ShouldSuppressDialogs(content::WebContents* source) override;
368 content::ColorChooser* OpenColorChooser(
369 content::WebContents* web_contents,
370 SkColor color,
371 const std::vector<content::ColorSuggestion>& suggestions) override;
372 void RunFileChooser(content::WebContents* tab,
373 const content::FileChooserParams& params) override;
374 bool IsPopupOrPanel(const content::WebContents* source) const override;
375 void MoveContents(content::WebContents* source,
376 const gfx::Rect& pos) override;
377 void NavigationStateChanged(content::WebContents* source,
378 content::InvalidateTypes changed_flags) override;
379 void EnterFullscreenModeForTab(content::WebContents* source,
380 const GURL& origin) override;
381 void ExitFullscreenModeForTab(content::WebContents* source) override;
382 bool IsFullscreenForTabOrPending(
383 const content::WebContents* source) const override;
384 blink::WebDisplayMode GetDisplayMode(
385 const content::WebContents* source) const override;
386 void RequestMediaAccessPermission(
387 content::WebContents* web_contents,
388 const content::MediaStreamRequest& request,
389 const content::MediaResponseCallback& callback) override;
390 bool CheckMediaAccessPermission(content::WebContents* web_contents,
391 const GURL& security_origin,
392 content::MediaStreamType type) override;
393 content::WebContents* OpenURLFromTab(
394 content::WebContents* source,
395 const content::OpenURLParams& params) override;
396 void AddNewContents(content::WebContents* source,
397 content::WebContents* new_contents,
398 WindowOpenDisposition disposition,
399 const gfx::Rect& initial_rect,
400 bool user_gesture,
401 bool* was_blocked) override;
402 bool PreHandleKeyboardEvent(content::WebContents* source,
403 const content::NativeWebKeyboardEvent& event,
404 bool* is_keyboard_shortcut) override;
405 void HandleKeyboardEvent(
406 content::WebContents* source,
407 const content::NativeWebKeyboardEvent& event) override;
408 void RequestToLockMouse(content::WebContents* web_contents,
409 bool user_gesture,
410 bool last_unlocked_by_target) override;
411 bool PreHandleGestureEvent(content::WebContents* source,
412 const blink::WebGestureEvent& event) override;
414 // content::WebContentsObserver implementation.
415 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
416 void DidFirstVisuallyNonEmptyPaint() override;
418 // ExtensionFunctionDispatcher::Delegate implementation.
419 WindowController* GetExtensionWindowController() const override;
420 content::WebContents* GetAssociatedWebContents() const override;
422 // ExtensionRegistryObserver implementation.
423 void OnExtensionUnloaded(content::BrowserContext* browser_context,
424 const Extension* extension,
425 UnloadedExtensionInfo::Reason reason) override;
426 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
427 const Extension* extension,
428 bool is_update,
429 bool from_ephemeral,
430 const std::string& old_name) override;
432 // web_modal::WebContentsModalDialogManagerDelegate implementation.
433 void SetWebContentsBlocked(content::WebContents* web_contents,
434 bool blocked) override;
435 bool IsWebContentsVisible(content::WebContents* web_contents) override;
437 void ToggleFullscreenModeForTab(content::WebContents* source,
438 bool enter_fullscreen);
440 // Saves the window geometry/position/screen bounds.
441 void SaveWindowPosition();
443 // Helper method to adjust the cached bounds so that we can make sure it can
444 // be visible on the screen. See http://crbug.com/145752.
445 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
446 const gfx::Rect& cached_screen_bounds,
447 const gfx::Rect& current_screen_bounds,
448 const gfx::Size& minimum_size,
449 gfx::Rect* bounds) const;
451 // Loads the appropriate default or cached window bounds. Returns a new
452 // CreateParams that should be used to create the window.
453 CreateParams LoadDefaults(CreateParams params) const;
455 // Load the app's image, firing a load state change when loaded.
456 void UpdateExtensionAppIcon();
458 // Set the fullscreen state in the native app window.
459 void SetNativeWindowFullscreen();
461 // Returns true if there is any overlap between the window and the taskbar
462 // (Windows only).
463 bool IntersectsWithTaskbar() const;
465 // Update the always-on-top bit in the native app window.
466 void UpdateNativeAlwaysOnTop();
468 // Sends the onWindowShown event to the app if the window has been shown. Only
469 // has an effect in tests.
470 void SendOnWindowShownIfShown();
472 // web_modal::WebContentsModalDialogManagerDelegate implementation.
473 web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
474 override;
476 // Callback from web_contents()->DownloadFavicon.
477 void DidDownloadFavicon(int id,
478 int http_status_code,
479 const GURL& image_url,
480 const std::vector<SkBitmap>& bitmaps,
481 const std::vector<gfx::Size>& original_bitmap_sizes);
483 // IconImage::Observer implementation.
484 void OnExtensionIconImageChanged(IconImage* image) override;
486 // The browser context with which this window is associated. AppWindow does
487 // not own this object.
488 content::BrowserContext* browser_context_;
490 const std::string extension_id_;
492 // Identifier that is used when saving and restoring geometry for this
493 // window.
494 std::string window_key_;
496 const SessionID session_id_;
497 WindowType window_type_;
499 // Icon shown in the task bar.
500 gfx::Image app_icon_;
502 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
503 // be fetched and set using this URL.
504 GURL app_icon_url_;
506 // An object to load the app's icon as an extension resource.
507 scoped_ptr<IconImage> app_icon_image_;
509 scoped_ptr<NativeAppWindow> native_app_window_;
510 scoped_ptr<AppWindowContents> app_window_contents_;
511 scoped_ptr<AppDelegate> app_delegate_;
512 scoped_ptr<AppWebContentsHelper> helper_;
514 // The initial url this AppWindow was navigated to.
515 GURL initial_url_;
517 // Manages popup windows (bubbles, tab-modals) visible overlapping the
518 // app window.
519 scoped_ptr<web_modal::PopupManager> popup_manager_;
521 // Bit field of FullscreenType.
522 int fullscreen_types_;
524 // Show has been called, so the window should be shown once the first visually
525 // non-empty paint occurs.
526 bool show_on_first_paint_;
528 // The first visually non-empty paint has completed.
529 bool first_paint_complete_;
531 // Whether the window has been shown or not.
532 bool has_been_shown_;
534 // Whether events can be sent to the window.
535 bool can_send_events_;
537 // Whether the window is hidden or not. Hidden in this context means actively
538 // by the chrome.app.window API, not in an operating system context. For
539 // example windows which are minimized are not hidden, and windows which are
540 // part of a hidden app on OS X are not hidden. Windows which were created
541 // with the |hidden| flag set to true, or which have been programmatically
542 // hidden, are considered hidden.
543 bool is_hidden_;
545 // Whether the delayed Show() call was for an active or inactive window.
546 ShowType delayed_show_type_;
548 // Cache the desired value of the always-on-top property. When windows enter
549 // fullscreen or overlap the Windows taskbar, this property will be
550 // automatically and silently switched off for security reasons. It is
551 // reinstated when the window exits fullscreen and moves away from the
552 // taskbar.
553 bool cached_always_on_top_;
555 // Whether |alpha_enabled| was set in the CreateParams.
556 bool requested_alpha_enabled_;
558 // Whether |is_ime_window| was set in the CreateParams.
559 bool is_ime_window_;
561 base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
563 DISALLOW_COPY_AND_ASSIGN(AppWindow);
566 } // namespace extensions
568 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_