[chromedriver] Explicitly set page loading state when document.readyState != "complete".
[chromium-blink-merge.git] / extensions / browser / app_window / app_window.h
blobe92c55347079263ebd35cd7cbdd2e625a61a8c58
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/web_contents_modal_dialog_manager_delegate.h"
15 #include "content/public/browser/web_contents_delegate.h"
16 #include "content/public/browser/web_contents_observer.h"
17 #include "extensions/browser/extension_function_dispatcher.h"
18 #include "extensions/browser/extension_icon_image.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "ui/base/ui_base_types.h" // WindowShowState
21 #include "ui/gfx/geometry/rect.h"
22 #include "ui/gfx/image/image.h"
24 class GURL;
25 class SkRegion;
27 namespace base {
28 class DictionaryValue;
31 namespace content {
32 class BrowserContext;
33 class WebContents;
36 namespace extensions {
38 class AppDelegate;
39 class AppWebContentsHelper;
40 class Extension;
41 class NativeAppWindow;
42 class PlatformAppBrowserTest;
44 struct DraggableRegion;
46 // Manages the web contents for app 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 AppWindowContents {
51 public:
52 AppWindowContents() {}
53 virtual ~AppWindowContents() {}
55 // Called to initialize the WebContents, before the app window is created.
56 virtual void Initialize(content::BrowserContext* context,
57 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 // Called in tests when the window is shown
69 virtual void DispatchWindowShownForTests() const = 0;
71 virtual content::WebContents* GetWebContents() const = 0;
73 virtual extensions::WindowController* GetWindowController() const = 0;
75 private:
76 DISALLOW_COPY_AND_ASSIGN(AppWindowContents);
79 // AppWindow is the type of window used by platform apps. App windows
80 // have a WebContents but none of the chrome of normal browser windows.
81 class AppWindow : public content::WebContentsDelegate,
82 public content::WebContentsObserver,
83 public web_modal::WebContentsModalDialogManagerDelegate,
84 public IconImage::Observer,
85 public ExtensionFunctionDispatcher::Delegate,
86 public ExtensionRegistryObserver {
87 public:
88 enum WindowType {
89 WINDOW_TYPE_DEFAULT = 1 << 0, // Default app window.
90 WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only).
91 WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate
92 // with v1 apps.
95 enum Frame {
96 FRAME_CHROME, // Chrome-style window frame.
97 FRAME_NONE, // Frameless window.
100 enum FullscreenType {
101 // Not fullscreen.
102 FULLSCREEN_TYPE_NONE = 0,
104 // Fullscreen entered by the app.window api.
105 FULLSCREEN_TYPE_WINDOW_API = 1 << 0,
107 // Fullscreen entered by HTML requestFullscreen().
108 FULLSCREEN_TYPE_HTML_API = 1 << 1,
110 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
111 // enter immersive fullscreen when the user hits the <F4> key.
112 FULLSCREEN_TYPE_OS = 1 << 2,
114 // Fullscreen mode that could not be exited by the user. ChromeOS uses
115 // this type of fullscreen to run an app in kiosk mode.
116 FULLSCREEN_TYPE_FORCED = 1 << 3,
119 struct BoundsSpecification {
120 // INT_MIN represents an unspecified position component.
121 static const int kUnspecifiedPosition;
123 BoundsSpecification();
124 ~BoundsSpecification();
126 // INT_MIN designates 'unspecified' for the position components, and 0
127 // designates 'unspecified' for the size components. When unspecified,
128 // they should be replaced with a default value.
129 gfx::Rect bounds;
131 gfx::Size minimum_size;
132 gfx::Size maximum_size;
134 // Reset the bounds fields to their 'unspecified' values. The minimum and
135 // maximum size constraints remain unchanged.
136 void ResetBounds();
139 struct CreateParams {
140 CreateParams();
141 ~CreateParams();
143 WindowType window_type;
144 Frame frame;
146 bool has_frame_color;
147 SkColor active_frame_color;
148 SkColor inactive_frame_color;
149 bool alpha_enabled;
150 bool is_ime_window;
152 // The initial content/inner bounds specification (excluding any window
153 // decorations).
154 BoundsSpecification content_spec;
156 // The initial window/outer bounds specification (including window
157 // decorations).
158 BoundsSpecification window_spec;
160 std::string window_key;
162 // The process ID of the process that requested the create.
163 int32 creator_process_id;
165 // Initial state of the window.
166 ui::WindowShowState state;
168 // If true, don't show the window after creation.
169 bool hidden;
171 // If true, the window will be resizable by the user. Defaults to true.
172 bool resizable;
174 // If true, the window will be focused on creation. Defaults to true.
175 bool focused;
177 // If true, the window will stay on top of other windows that are not
178 // configured to be always on top. Defaults to false.
179 bool always_on_top;
181 // If true, the window will be visible on all workspaces. Defaults to false.
182 bool visible_on_all_workspaces;
184 // The API enables developers to specify content or window bounds. This
185 // function combines them into a single, constrained window size.
186 gfx::Rect GetInitialWindowBounds(const gfx::Insets& frame_insets) const;
188 // The API enables developers to specify content or window size constraints.
189 // These functions combine them so that we can work with one set of
190 // constraints.
191 gfx::Size GetContentMinimumSize(const gfx::Insets& frame_insets) const;
192 gfx::Size GetContentMaximumSize(const gfx::Insets& frame_insets) const;
193 gfx::Size GetWindowMinimumSize(const gfx::Insets& frame_insets) const;
194 gfx::Size GetWindowMaximumSize(const gfx::Insets& frame_insets) const;
197 // Convert draggable regions in raw format to SkRegion format. Caller is
198 // responsible for deleting the returned SkRegion instance.
199 static SkRegion* RawDraggableRegionsToSkRegion(
200 const std::vector<DraggableRegion>& regions);
202 // The constructor and Init methods are public for constructing a AppWindow
203 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
204 // Normally AppWindow::Create should be used.
205 // Takes ownership of |app_delegate| and |delegate|.
206 AppWindow(content::BrowserContext* context,
207 AppDelegate* app_delegate,
208 const Extension* extension);
210 // Initializes the render interface, web contents, and native window.
211 // |app_window_contents| will become owned by AppWindow.
212 void Init(const GURL& url,
213 AppWindowContents* app_window_contents,
214 const CreateParams& params);
216 const std::string& window_key() const { return window_key_; }
217 const SessionID& session_id() const { return session_id_; }
218 const std::string& extension_id() const { return extension_id_; }
219 content::WebContents* web_contents() const;
220 WindowType window_type() const { return window_type_; }
221 bool window_type_is_panel() const {
222 return (window_type_ == WINDOW_TYPE_PANEL ||
223 window_type_ == WINDOW_TYPE_V1_PANEL);
225 content::BrowserContext* browser_context() const { return browser_context_; }
226 const gfx::Image& app_icon() const { return app_icon_; }
227 const GURL& app_icon_url() const { return app_icon_url_; }
228 const GURL& initial_url() const { return initial_url_; }
229 bool is_hidden() const { return is_hidden_; }
231 const Extension* GetExtension() const;
232 NativeAppWindow* GetBaseWindow();
233 gfx::NativeWindow GetNativeWindow();
235 // Returns the bounds that should be reported to the renderer.
236 gfx::Rect GetClientBounds() const;
238 // NativeAppWindows should call this to determine what the window's title
239 // is on startup and from within UpdateWindowTitle().
240 base::string16 GetTitle() const;
242 // Call to notify ShellRegistry and delete the window. Subclasses should
243 // invoke this method instead of using "delete this".
244 void OnNativeClose();
246 // Should be called by native implementations when the window size, position,
247 // or minimized/maximized state has changed.
248 void OnNativeWindowChanged();
250 // Should be called by native implementations when the window is activated.
251 void OnNativeWindowActivated();
253 // Specifies a url for the launcher icon.
254 void SetAppIconUrl(const GURL& icon_url);
256 // Set the window shape. Passing a NULL |region| sets the default shape.
257 void UpdateShape(scoped_ptr<SkRegion> region);
259 // Called from the render interface to modify the draggable regions.
260 void UpdateDraggableRegions(const std::vector<DraggableRegion>& regions);
262 // Updates the app image to |image|. Called internally from the image loader
263 // callback. Also called externally for v1 apps using Ash Panels.
264 void UpdateAppIcon(const gfx::Image& image);
266 // Enable or disable fullscreen mode. |type| specifies which type of
267 // fullscreen mode to change (note that disabling one type of fullscreen may
268 // not exit fullscreen mode because a window may have a different type of
269 // fullscreen enabled). If |type| is not FORCED, checks that the extension has
270 // the required permission.
271 void SetFullscreen(FullscreenType type, bool enable);
273 // Returns true if the app window is in a fullscreen state.
274 bool IsFullscreen() const;
276 // Returns true if the app window is in a forced fullscreen state (one that
277 // cannot be exited by the user).
278 bool IsForcedFullscreen() const;
280 // Returns true if the app window is in a fullscreen state entered from an
281 // HTML API request.
282 bool IsHtmlApiFullscreen() const;
284 // Transitions window into fullscreen, maximized, minimized or restores based
285 // on chrome.app.window API.
286 void Fullscreen();
287 void Maximize();
288 void Minimize();
289 void Restore();
291 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
292 void OSFullscreen();
294 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
295 // details.
296 void ForcedFullscreen();
298 // Set the minimum and maximum size of the content bounds.
299 void SetContentSizeConstraints(const gfx::Size& min_size,
300 const gfx::Size& max_size);
302 enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE };
304 // Shows the window if its contents have been painted; otherwise flags the
305 // window to be shown as soon as its contents are painted for the first time.
306 void Show(ShowType show_type);
308 // Hides the window. If the window was previously flagged to be shown on
309 // first paint, it will be unflagged.
310 void Hide();
312 AppWindowContents* app_window_contents_for_test() {
313 return app_window_contents_.get();
316 int fullscreen_types_for_test() {
317 return fullscreen_types_;
320 // Set whether the window should stay above other windows which are not
321 // configured to be always-on-top.
322 void SetAlwaysOnTop(bool always_on_top);
324 // Whether the always-on-top property has been set by the chrome.app.window
325 // API. Note that the actual value of this property in the native app window
326 // may be false if the bit is silently switched off for security reasons.
327 bool IsAlwaysOnTop() const;
329 // Restores the always-on-top property according to |cached_always_on_top_|.
330 void RestoreAlwaysOnTop();
332 // Set whether the window should get even reserved keys (modulo platform
333 // restrictions).
334 void SetInterceptAllKeys(bool want_all_keys);
336 // Retrieve the current state of the app window as a dictionary, to pass to
337 // the renderer.
338 void GetSerializedState(base::DictionaryValue* properties) const;
340 // Called by the window API when events can be sent to the window for this
341 // app.
342 void WindowEventsReady();
344 // Whether the app window wants to be alpha enabled.
345 bool requested_alpha_enabled() const { return requested_alpha_enabled_; }
347 // Whether the app window is created by IME extensions.
348 // TODO(bshe): rename to hide_app_window_in_launcher if it is not used
349 // anywhere other than app_window_launcher_controller after M45. Otherwise,
350 // remove this TODO.
351 bool is_ime_window() const { return is_ime_window_; }
353 void SetAppWindowContentsForTesting(scoped_ptr<AppWindowContents> contents) {
354 app_window_contents_ = contents.Pass();
357 protected:
358 ~AppWindow() override;
360 private:
361 // PlatformAppBrowserTest needs access to web_contents()
362 friend class PlatformAppBrowserTest;
364 // content::WebContentsDelegate implementation.
365 void CloseContents(content::WebContents* contents) override;
366 bool ShouldSuppressDialogs(content::WebContents* source) override;
367 content::ColorChooser* OpenColorChooser(
368 content::WebContents* web_contents,
369 SkColor color,
370 const std::vector<content::ColorSuggestion>& suggestions) override;
371 void RunFileChooser(content::WebContents* tab,
372 const content::FileChooserParams& params) override;
373 bool IsPopupOrPanel(const content::WebContents* source) const override;
374 void MoveContents(content::WebContents* source,
375 const gfx::Rect& pos) override;
376 void NavigationStateChanged(content::WebContents* source,
377 content::InvalidateTypes changed_flags) override;
378 void EnterFullscreenModeForTab(content::WebContents* source,
379 const GURL& origin) override;
380 void ExitFullscreenModeForTab(content::WebContents* source) override;
381 bool IsFullscreenForTabOrPending(
382 const content::WebContents* source) const override;
383 blink::WebDisplayMode GetDisplayMode(
384 const content::WebContents* source) const override;
385 void RequestMediaAccessPermission(
386 content::WebContents* web_contents,
387 const content::MediaStreamRequest& request,
388 const content::MediaResponseCallback& callback) override;
389 bool CheckMediaAccessPermission(content::WebContents* web_contents,
390 const GURL& security_origin,
391 content::MediaStreamType type) override;
392 content::WebContents* OpenURLFromTab(
393 content::WebContents* source,
394 const content::OpenURLParams& params) override;
395 void AddNewContents(content::WebContents* source,
396 content::WebContents* new_contents,
397 WindowOpenDisposition disposition,
398 const gfx::Rect& initial_rect,
399 bool user_gesture,
400 bool* was_blocked) override;
401 bool PreHandleKeyboardEvent(content::WebContents* source,
402 const content::NativeWebKeyboardEvent& event,
403 bool* is_keyboard_shortcut) override;
404 void HandleKeyboardEvent(
405 content::WebContents* source,
406 const content::NativeWebKeyboardEvent& event) override;
407 void RequestToLockMouse(content::WebContents* web_contents,
408 bool user_gesture,
409 bool last_unlocked_by_target) override;
410 bool PreHandleGestureEvent(content::WebContents* source,
411 const blink::WebGestureEvent& event) override;
413 // content::WebContentsObserver implementation.
414 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
415 void DidFirstVisuallyNonEmptyPaint() override;
417 // ExtensionFunctionDispatcher::Delegate implementation.
418 WindowController* GetExtensionWindowController() const override;
419 content::WebContents* GetAssociatedWebContents() const override;
421 // ExtensionRegistryObserver implementation.
422 void OnExtensionUnloaded(content::BrowserContext* browser_context,
423 const Extension* extension,
424 UnloadedExtensionInfo::Reason reason) override;
425 void OnExtensionWillBeInstalled(content::BrowserContext* browser_context,
426 const Extension* extension,
427 bool is_update,
428 bool from_ephemeral,
429 const std::string& old_name) override;
431 // web_modal::WebContentsModalDialogManagerDelegate implementation.
432 void SetWebContentsBlocked(content::WebContents* web_contents,
433 bool blocked) override;
434 bool IsWebContentsVisible(content::WebContents* web_contents) override;
436 void ToggleFullscreenModeForTab(content::WebContents* source,
437 bool enter_fullscreen);
439 // Saves the window geometry/position/screen bounds.
440 void SaveWindowPosition();
442 // Helper method to adjust the cached bounds so that we can make sure it can
443 // be visible on the screen. See http://crbug.com/145752.
444 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
445 const gfx::Rect& cached_screen_bounds,
446 const gfx::Rect& current_screen_bounds,
447 const gfx::Size& minimum_size,
448 gfx::Rect* bounds) const;
450 // Loads the appropriate default or cached window bounds. Returns a new
451 // CreateParams that should be used to create the window.
452 CreateParams LoadDefaults(CreateParams params) const;
454 // Load the app's image, firing a load state change when loaded.
455 void UpdateExtensionAppIcon();
457 // Set the fullscreen state in the native app window.
458 void SetNativeWindowFullscreen();
460 // Returns true if there is any overlap between the window and the taskbar
461 // (Windows only).
462 bool IntersectsWithTaskbar() const;
464 // Update the always-on-top bit in the native app window.
465 void UpdateNativeAlwaysOnTop();
467 // Sends the onWindowShown event to the app if the window has been shown. Only
468 // has an effect in tests.
469 void SendOnWindowShownIfShown();
471 // web_modal::WebContentsModalDialogManagerDelegate implementation.
472 web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
473 override;
475 // Callback from web_contents()->DownloadFavicon.
476 void DidDownloadFavicon(int id,
477 int http_status_code,
478 const GURL& image_url,
479 const std::vector<SkBitmap>& bitmaps,
480 const std::vector<gfx::Size>& original_bitmap_sizes);
482 // IconImage::Observer implementation.
483 void OnExtensionIconImageChanged(IconImage* image) override;
485 // The browser context with which this window is associated. AppWindow does
486 // not own this object.
487 content::BrowserContext* browser_context_;
489 const std::string extension_id_;
491 // Identifier that is used when saving and restoring geometry for this
492 // window.
493 std::string window_key_;
495 const SessionID session_id_;
496 WindowType window_type_;
498 // Icon shown in the task bar.
499 gfx::Image app_icon_;
501 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
502 // be fetched and set using this URL.
503 GURL app_icon_url_;
505 // An object to load the app's icon as an extension resource.
506 scoped_ptr<IconImage> app_icon_image_;
508 scoped_ptr<NativeAppWindow> native_app_window_;
509 scoped_ptr<AppWindowContents> app_window_contents_;
510 scoped_ptr<AppDelegate> app_delegate_;
511 scoped_ptr<AppWebContentsHelper> helper_;
513 // The initial url this AppWindow was navigated to.
514 GURL initial_url_;
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 has been shown or not.
527 bool has_been_shown_;
529 // Whether events can be sent to the window.
530 bool can_send_events_;
532 // Whether the window is hidden or not. Hidden in this context means actively
533 // by the chrome.app.window API, not in an operating system context. For
534 // example windows which are minimized are not hidden, and windows which are
535 // part of a hidden app on OS X are not hidden. Windows which were created
536 // with the |hidden| flag set to true, or which have been programmatically
537 // hidden, are considered hidden.
538 bool is_hidden_;
540 // Whether the delayed Show() call was for an active or inactive window.
541 ShowType delayed_show_type_;
543 // Cache the desired value of the always-on-top property. When windows enter
544 // fullscreen or overlap the Windows taskbar, this property will be
545 // automatically and silently switched off for security reasons. It is
546 // reinstated when the window exits fullscreen and moves away from the
547 // taskbar.
548 bool cached_always_on_top_;
550 // Whether |alpha_enabled| was set in the CreateParams.
551 bool requested_alpha_enabled_;
553 // Whether |is_ime_window| was set in the CreateParams.
554 bool is_ime_window_;
556 base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
558 DISALLOW_COPY_AND_ASSIGN(AppWindow);
561 } // namespace extensions
563 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_