Created a <webview> find API.
[chromium-blink-merge.git] / apps / app_window.h
blob8403153d2741abd098210fb20abbb51dbeebc66c
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/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 SkRegion;
26 namespace base {
27 class DictionaryValue;
30 namespace content {
31 class BrowserContext;
32 class WebContents;
35 namespace extensions {
36 class Extension;
37 class PlatformAppBrowserTest;
38 class WindowController;
40 struct DraggableRegion;
43 namespace ui {
44 class BaseWindow;
47 namespace apps {
49 class NativeAppWindow;
51 // Manages the web contents for app windows. The implementation for this
52 // class should create and maintain the WebContents for the window, and handle
53 // any message passing between the web contents and the extension system or
54 // native window.
55 class AppWindowContents {
56 public:
57 AppWindowContents() {}
58 virtual ~AppWindowContents() {}
60 // Called to initialize the WebContents, before the app window is created.
61 virtual void Initialize(content::BrowserContext* context,
62 const GURL& url) = 0;
64 // Called to load the contents, after the app window is created.
65 virtual void LoadContents(int32 creator_process_id) = 0;
67 // Called when the native window changes.
68 virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0;
70 // Called when the native window closes.
71 virtual void NativeWindowClosed() = 0;
73 virtual content::WebContents* GetWebContents() 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::NotificationObserver,
82 public content::WebContentsDelegate,
83 public content::WebContentsObserver,
84 public web_modal::WebContentsModalDialogManagerDelegate,
85 public extensions::ExtensionKeybindingRegistry::Delegate,
86 public extensions::IconImage::Observer {
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 CreateParams {
120 CreateParams();
121 ~CreateParams();
123 WindowType window_type;
124 Frame frame;
126 bool has_frame_color;
127 SkColor frame_color;
128 bool transparent_background; // Only supported on ash.
130 // Specify the initial content bounds of the window (excluding any window
131 // decorations). INT_MIN designates 'unspecified' for the position
132 // components, and 0 for the size components. When unspecified, they should
133 // be replaced with a default value.
134 gfx::Rect bounds;
136 gfx::Size minimum_size;
137 gfx::Size maximum_size;
139 std::string window_key;
141 // The process ID of the process that requested the create.
142 int32 creator_process_id;
144 // Initial state of the window.
145 ui::WindowShowState state;
147 // If true, don't show the window after creation.
148 bool hidden;
150 // If true, the window will be resizable by the user. Defaults to true.
151 bool resizable;
153 // If true, the window will be focused on creation. Defaults to true.
154 bool focused;
156 // If true, the window will stay on top of other windows that are not
157 // configured to be always on top. Defaults to false.
158 bool always_on_top;
161 class Delegate {
162 public:
163 virtual ~Delegate();
165 // General initialization.
166 virtual void InitWebContents(content::WebContents* web_contents) = 0;
167 virtual NativeAppWindow* CreateNativeAppWindow(
168 AppWindow* window,
169 const CreateParams& params) = 0;
171 // Link handling.
172 virtual content::WebContents* OpenURLFromTab(
173 content::BrowserContext* context,
174 content::WebContents* source,
175 const content::OpenURLParams& params) = 0;
176 virtual void AddNewContents(content::BrowserContext* context,
177 content::WebContents* new_contents,
178 WindowOpenDisposition disposition,
179 const gfx::Rect& initial_pos,
180 bool user_gesture,
181 bool* was_blocked) = 0;
183 // Feature support.
184 virtual content::ColorChooser* ShowColorChooser(
185 content::WebContents* web_contents,
186 SkColor initial_color) = 0;
187 virtual void RunFileChooser(content::WebContents* tab,
188 const content::FileChooserParams& params) = 0;
189 virtual void RequestMediaAccessPermission(
190 content::WebContents* web_contents,
191 const content::MediaStreamRequest& request,
192 const content::MediaResponseCallback& callback,
193 const extensions::Extension* extension) = 0;
194 virtual int PreferredIconSize() = 0;
196 // Web contents modal dialog support.
197 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
198 bool blocked) = 0;
199 virtual bool IsWebContentsVisible(content::WebContents* web_contents) = 0;
202 // Convert draggable regions in raw format to SkRegion format. Caller is
203 // responsible for deleting the returned SkRegion instance.
204 static SkRegion* RawDraggableRegionsToSkRegion(
205 const std::vector<extensions::DraggableRegion>& regions);
207 // The constructor and Init methods are public for constructing a AppWindow
208 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
209 // Normally AppWindow::Create should be used.
210 // The constructed app window takes ownership of |delegate|.
211 AppWindow(content::BrowserContext* context,
212 Delegate* delegate,
213 const extensions::Extension* extension);
215 // Initializes the render interface, web contents, and native window.
216 // |app_window_contents| will become owned by AppWindow.
217 void Init(const GURL& url,
218 AppWindowContents* app_window_contents,
219 const CreateParams& params);
221 const std::string& window_key() const { return window_key_; }
222 const SessionID& session_id() const { return session_id_; }
223 const extensions::Extension* extension() const { return extension_; }
224 const std::string& extension_id() const { return extension_id_; }
225 content::WebContents* web_contents() const;
226 WindowType window_type() const { return window_type_; }
227 bool window_type_is_panel() const {
228 return (window_type_ == WINDOW_TYPE_PANEL ||
229 window_type_ == WINDOW_TYPE_V1_PANEL);
231 content::BrowserContext* browser_context() const { return browser_context_; }
232 const gfx::Image& app_icon() const { return app_icon_; }
233 const GURL& app_icon_url() const { return app_icon_url_; }
234 const gfx::Image& badge_icon() const { return badge_icon_; }
235 const GURL& badge_icon_url() const { return badge_icon_url_; }
237 NativeAppWindow* GetBaseWindow();
238 gfx::NativeWindow GetNativeWindow();
240 // Returns the bounds that should be reported to the renderer.
241 gfx::Rect GetClientBounds() const;
243 // NativeAppWindows should call this to determine what the window's title
244 // is on startup and from within UpdateWindowTitle().
245 base::string16 GetTitle() const;
247 // Call to notify ShellRegistry and delete the window. Subclasses should
248 // invoke this method instead of using "delete this".
249 void OnNativeClose();
251 // Should be called by native implementations when the window size, position,
252 // or minimized/maximized state has changed.
253 void OnNativeWindowChanged();
255 // Should be called by native implementations when the window is activated.
256 void OnNativeWindowActivated();
258 // Specifies a url for the launcher icon.
259 void SetAppIconUrl(const GURL& icon_url);
261 // Specifies a url for the window badge.
262 void SetBadgeIconUrl(const GURL& icon_url);
264 // Clear the current badge.
265 void ClearBadge();
267 // Set the window shape. Passing a NULL |region| sets the default shape.
268 void UpdateShape(scoped_ptr<SkRegion> region);
270 // Called from the render interface to modify the draggable regions.
271 void UpdateDraggableRegions(
272 const std::vector<extensions::DraggableRegion>& regions);
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);
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();
285 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
286 void OSFullscreen();
288 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
289 // details.
290 void ForcedFullscreen();
292 // Set the minimum and maximum size that this window is allowed to be.
293 void SetMinimumSize(const gfx::Size& min_size);
294 void SetMaximumSize(const gfx::Size& max_size);
296 enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE };
298 // Shows the window if its contents have been painted; otherwise flags the
299 // window to be shown as soon as its contents are painted for the first time.
300 void Show(ShowType show_type);
302 // Hides the window. If the window was previously flagged to be shown on
303 // first paint, it will be unflagged.
304 void Hide();
306 AppWindowContents* app_window_contents_for_test() {
307 return app_window_contents_.get();
310 // Set whether the window should stay above other windows which are not
311 // configured to be always-on-top.
312 void SetAlwaysOnTop(bool always_on_top);
314 // Whether the always-on-top property has been set by the chrome.app.window
315 // API. Note that the actual value of this property in the native app window
316 // may be false if the bit is silently switched off for security reasons.
317 bool IsAlwaysOnTop() const;
319 // Retrieve the current state of the app window as a dictionary, to pass to
320 // the renderer.
321 void GetSerializedState(base::DictionaryValue* properties) const;
323 protected:
324 virtual ~AppWindow();
326 private:
327 // PlatformAppBrowserTest needs access to web_contents()
328 friend class extensions::PlatformAppBrowserTest;
330 // content::WebContentsDelegate implementation.
331 virtual void CloseContents(content::WebContents* contents) OVERRIDE;
332 virtual bool ShouldSuppressDialogs() OVERRIDE;
333 virtual content::ColorChooser* OpenColorChooser(
334 content::WebContents* web_contents,
335 SkColor color,
336 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE;
337 virtual void RunFileChooser(content::WebContents* tab,
338 const content::FileChooserParams& params)
339 OVERRIDE;
340 virtual bool IsPopupOrPanel(const content::WebContents* source)
341 const OVERRIDE;
342 virtual void MoveContents(content::WebContents* source,
343 const gfx::Rect& pos) OVERRIDE;
344 virtual void NavigationStateChanged(const content::WebContents* source,
345 unsigned changed_flags) OVERRIDE;
346 virtual void ToggleFullscreenModeForTab(content::WebContents* source,
347 bool enter_fullscreen) OVERRIDE;
348 virtual bool IsFullscreenForTabOrPending(const content::WebContents* source)
349 const OVERRIDE;
350 virtual void RequestMediaAccessPermission(
351 content::WebContents* web_contents,
352 const content::MediaStreamRequest& request,
353 const content::MediaResponseCallback& callback) OVERRIDE;
354 virtual content::WebContents* OpenURLFromTab(
355 content::WebContents* source,
356 const content::OpenURLParams& params) OVERRIDE;
357 virtual void AddNewContents(content::WebContents* source,
358 content::WebContents* new_contents,
359 WindowOpenDisposition disposition,
360 const gfx::Rect& initial_pos,
361 bool user_gesture,
362 bool* was_blocked) OVERRIDE;
363 virtual bool PreHandleKeyboardEvent(
364 content::WebContents* source,
365 const content::NativeWebKeyboardEvent& event,
366 bool* is_keyboard_shortcut) OVERRIDE;
367 virtual void HandleKeyboardEvent(content::WebContents* source,
368 const content::NativeWebKeyboardEvent& event)
369 OVERRIDE;
370 virtual void RequestToLockMouse(content::WebContents* web_contents,
371 bool user_gesture,
372 bool last_unlocked_by_target) OVERRIDE;
373 virtual bool PreHandleGestureEvent(content::WebContents* source,
374 const blink::WebGestureEvent& event)
375 OVERRIDE;
377 // content::WebContentsObserver implementation.
378 virtual void DidFirstVisuallyNonEmptyPaint(int32 page_id) OVERRIDE;
380 // content::NotificationObserver implementation.
381 virtual void Observe(int type,
382 const content::NotificationSource& source,
383 const content::NotificationDetails& details) OVERRIDE;
385 // web_modal::WebContentsModalDialogManagerDelegate implementation.
386 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
387 bool blocked) OVERRIDE;
388 virtual bool IsWebContentsVisible(content::WebContents* web_contents)
389 OVERRIDE;
391 // Helper method to add a message to the renderer's DevTools console.
392 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level,
393 const std::string& message);
395 // Saves the window geometry/position/screen bounds.
396 void SaveWindowPosition();
398 // Helper method to adjust the cached bounds so that we can make sure it can
399 // be visible on the screen. See http://crbug.com/145752 .
400 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
401 const gfx::Rect& cached_screen_bounds,
402 const gfx::Rect& current_screen_bounds,
403 const gfx::Size& minimum_size,
404 gfx::Rect* bounds) const;
406 // Loads the appropriate default or cached window bounds and constrains them
407 // based on screen size and minimum/maximum size. Returns a new CreateParams
408 // that should be used to create the window.
409 CreateParams LoadDefaultsAndConstrain(CreateParams params) const;
411 // Load the app's image, firing a load state change when loaded.
412 void UpdateExtensionAppIcon();
414 // Called when size_constraints is changed.
415 void OnSizeConstraintsChanged();
417 // Set the fullscreen state in the native app window.
418 void SetNativeWindowFullscreen();
420 // Returns true if there is any overlap between the window and the taskbar
421 // (Windows only).
422 bool IntersectsWithTaskbar() const;
424 // Update the always-on-top bit in the native app window.
425 void UpdateNativeAlwaysOnTop();
427 // extensions::ExtensionKeybindingRegistry::Delegate implementation.
428 virtual extensions::ActiveTabPermissionGranter*
429 GetActiveTabPermissionGranter() OVERRIDE;
431 // web_modal::WebContentsModalDialogManagerDelegate implementation.
432 virtual web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
433 OVERRIDE;
435 // Updates the badge to |image|. Called internally from the image loader
436 // callback.
437 void UpdateBadgeIcon(const gfx::Image& image);
439 // Callback from web_contents()->DownloadFavicon.
440 void DidDownloadFavicon(int id,
441 int http_status_code,
442 const GURL& image_url,
443 const std::vector<SkBitmap>& bitmaps,
444 const std::vector<gfx::Size>& original_bitmap_sizes);
446 // extensions::IconImage::Observer implementation.
447 virtual void OnExtensionIconImageChanged(extensions::IconImage* image)
448 OVERRIDE;
450 // The browser context with which this window is associated. AppWindow does
451 // not own this object.
452 content::BrowserContext* browser_context_;
454 // weak pointer - owned by ExtensionService.
455 const extensions::Extension* extension_;
456 const std::string extension_id_;
458 // Identifier that is used when saving and restoring geometry for this
459 // window.
460 std::string window_key_;
462 const SessionID session_id_;
463 WindowType window_type_;
464 content::NotificationRegistrar registrar_;
466 // Icon shown in the task bar.
467 gfx::Image app_icon_;
469 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
470 // be fetched and set using this URL.
471 GURL app_icon_url_;
473 // An object to load the app's icon as an extension resource.
474 scoped_ptr<extensions::IconImage> app_icon_image_;
476 // Badge for icon shown in the task bar.
477 gfx::Image badge_icon_;
479 // URL to be used for setting the badge on the app icon.
480 GURL badge_icon_url_;
482 // An object to load the badge as an extension resource.
483 scoped_ptr<extensions::IconImage> badge_icon_image_;
485 scoped_ptr<NativeAppWindow> native_app_window_;
486 scoped_ptr<AppWindowContents> app_window_contents_;
487 scoped_ptr<Delegate> delegate_;
489 base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
491 // Bit field of FullscreenType.
492 int fullscreen_types_;
494 // Show has been called, so the window should be shown once the first visually
495 // non-empty paint occurs.
496 bool show_on_first_paint_;
498 // The first visually non-empty paint has completed.
499 bool first_paint_complete_;
501 // Whether the delayed Show() call was for an active or inactive window.
502 ShowType delayed_show_type_;
504 // Cache the desired value of the always-on-top property. When windows enter
505 // fullscreen or overlap the Windows taskbar, this property will be
506 // automatically and silently switched off for security reasons. It is
507 // reinstated when the window exits fullscreen and moves away from the
508 // taskbar.
509 bool cached_always_on_top_;
511 DISALLOW_COPY_AND_ASSIGN(AppWindow);
514 } // namespace apps
516 #endif // APPS_APP_WINDOW_H_