Add new certificateProvider extension API.
[chromium-blink-merge.git] / extensions / browser / app_window / app_window.h
blob4a06269bd05b5f04cba88e6d67d26d40586e0e82
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 // Called when the renderer notifies the browser that the window is ready.
72 virtual void OnWindowReady() = 0;
74 virtual content::WebContents* GetWebContents() const = 0;
76 virtual extensions::WindowController* GetWindowController() const = 0;
78 private:
79 DISALLOW_COPY_AND_ASSIGN(AppWindowContents);
82 // AppWindow is the type of window used by platform apps. App windows
83 // have a WebContents but none of the chrome of normal browser windows.
84 class AppWindow : public content::WebContentsDelegate,
85 public content::WebContentsObserver,
86 public web_modal::WebContentsModalDialogManagerDelegate,
87 public IconImage::Observer,
88 public ExtensionFunctionDispatcher::Delegate,
89 public ExtensionRegistryObserver {
90 public:
91 enum WindowType {
92 WINDOW_TYPE_DEFAULT = 1 << 0, // Default app window.
93 WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only).
94 WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate
95 // with v1 apps.
98 enum Frame {
99 FRAME_CHROME, // Chrome-style window frame.
100 FRAME_NONE, // Frameless window.
103 enum FullscreenType {
104 // Not fullscreen.
105 FULLSCREEN_TYPE_NONE = 0,
107 // Fullscreen entered by the app.window api.
108 FULLSCREEN_TYPE_WINDOW_API = 1 << 0,
110 // Fullscreen entered by HTML requestFullscreen().
111 FULLSCREEN_TYPE_HTML_API = 1 << 1,
113 // Fullscreen entered by the OS. ChromeOS uses this type of fullscreen to
114 // enter immersive fullscreen when the user hits the <F4> key.
115 FULLSCREEN_TYPE_OS = 1 << 2,
117 // Fullscreen mode that could not be exited by the user. ChromeOS uses
118 // this type of fullscreen to run an app in kiosk mode.
119 FULLSCREEN_TYPE_FORCED = 1 << 3,
122 struct BoundsSpecification {
123 // INT_MIN represents an unspecified position component.
124 static const int kUnspecifiedPosition;
126 BoundsSpecification();
127 ~BoundsSpecification();
129 // INT_MIN designates 'unspecified' for the position components, and 0
130 // designates 'unspecified' for the size components. When unspecified,
131 // they should be replaced with a default value.
132 gfx::Rect bounds;
134 gfx::Size minimum_size;
135 gfx::Size maximum_size;
137 // Reset the bounds fields to their 'unspecified' values. The minimum and
138 // maximum size constraints remain unchanged.
139 void ResetBounds();
142 struct CreateParams {
143 CreateParams();
144 ~CreateParams();
146 WindowType window_type;
147 Frame frame;
149 bool has_frame_color;
150 SkColor active_frame_color;
151 SkColor inactive_frame_color;
152 bool alpha_enabled;
153 bool is_ime_window;
155 // The initial content/inner bounds specification (excluding any window
156 // decorations).
157 BoundsSpecification content_spec;
159 // The initial window/outer bounds specification (including window
160 // decorations).
161 BoundsSpecification window_spec;
163 std::string window_key;
165 // The process ID of the process that requested the create.
166 int32 creator_process_id;
168 // Initial state of the window.
169 ui::WindowShowState state;
171 // If true, don't show the window after creation.
172 bool hidden;
174 // If true, the window will be resizable by the user. Defaults to true.
175 bool resizable;
177 // If true, the window will be focused on creation. Defaults to true.
178 bool focused;
180 // If true, the window will stay on top of other windows that are not
181 // configured to be always on top. Defaults to false.
182 bool always_on_top;
184 // If true, the window will be visible on all workspaces. Defaults to false.
185 bool visible_on_all_workspaces;
187 // The API enables developers to specify content or window bounds. This
188 // function combines them into a single, constrained window size.
189 gfx::Rect GetInitialWindowBounds(const gfx::Insets& frame_insets) const;
191 // The API enables developers to specify content or window size constraints.
192 // These functions combine them so that we can work with one set of
193 // constraints.
194 gfx::Size GetContentMinimumSize(const gfx::Insets& frame_insets) const;
195 gfx::Size GetContentMaximumSize(const gfx::Insets& frame_insets) const;
196 gfx::Size GetWindowMinimumSize(const gfx::Insets& frame_insets) const;
197 gfx::Size GetWindowMaximumSize(const gfx::Insets& frame_insets) const;
200 // Convert draggable regions in raw format to SkRegion format. Caller is
201 // responsible for deleting the returned SkRegion instance.
202 static SkRegion* RawDraggableRegionsToSkRegion(
203 const std::vector<DraggableRegion>& regions);
205 // The constructor and Init methods are public for constructing a AppWindow
206 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
207 // Normally AppWindow::Create should be used.
208 // Takes ownership of |app_delegate| and |delegate|.
209 AppWindow(content::BrowserContext* context,
210 AppDelegate* app_delegate,
211 const Extension* extension);
213 // Initializes the render interface, web contents, and native window.
214 // |app_window_contents| will become owned by AppWindow.
215 void Init(const GURL& url,
216 AppWindowContents* app_window_contents,
217 const CreateParams& params);
219 const std::string& window_key() const { return window_key_; }
220 const SessionID& session_id() const { return session_id_; }
221 const std::string& extension_id() const { return extension_id_; }
222 content::WebContents* web_contents() const;
223 WindowType window_type() const { return window_type_; }
224 bool window_type_is_panel() const {
225 return (window_type_ == WINDOW_TYPE_PANEL ||
226 window_type_ == WINDOW_TYPE_V1_PANEL);
228 content::BrowserContext* browser_context() const { return browser_context_; }
229 const gfx::Image& app_icon() const { return app_icon_; }
230 const GURL& app_icon_url() const { return app_icon_url_; }
231 const GURL& initial_url() const { return initial_url_; }
232 bool is_hidden() const { return is_hidden_; }
234 const Extension* GetExtension() const;
235 NativeAppWindow* GetBaseWindow();
236 gfx::NativeWindow GetNativeWindow();
238 // Returns the bounds that should be reported to the renderer.
239 gfx::Rect GetClientBounds() const;
241 // NativeAppWindows should call this to determine what the window's title
242 // is on startup and from within UpdateWindowTitle().
243 base::string16 GetTitle() const;
245 // Call to notify ShellRegistry and delete the window. Subclasses should
246 // invoke this method instead of using "delete this".
247 void OnNativeClose();
249 // Should be called by native implementations when the window size, position,
250 // or minimized/maximized state has changed.
251 void OnNativeWindowChanged();
253 // Should be called by native implementations when the window is activated.
254 void OnNativeWindowActivated();
256 // Specifies a url for the launcher icon.
257 void SetAppIconUrl(const GURL& icon_url);
259 // Set the window shape. Passing a NULL |region| sets the default shape.
260 void UpdateShape(scoped_ptr<SkRegion> region);
262 // Called from the render interface to modify the draggable regions.
263 void UpdateDraggableRegions(const std::vector<DraggableRegion>& regions);
265 // Updates the app image to |image|. Called internally from the image loader
266 // callback. Also called externally for v1 apps using Ash Panels.
267 void UpdateAppIcon(const gfx::Image& image);
269 // Enable or disable fullscreen mode. |type| specifies which type of
270 // fullscreen mode to change (note that disabling one type of fullscreen may
271 // not exit fullscreen mode because a window may have a different type of
272 // fullscreen enabled). If |type| is not FORCED, checks that the extension has
273 // the required permission.
274 void SetFullscreen(FullscreenType type, bool enable);
276 // Returns true if the app window is in a fullscreen state.
277 bool IsFullscreen() const;
279 // Returns true if the app window is in a forced fullscreen state (one that
280 // cannot be exited by the user).
281 bool IsForcedFullscreen() const;
283 // Returns true if the app window is in a fullscreen state entered from an
284 // HTML API request.
285 bool IsHtmlApiFullscreen() const;
287 // Transitions window into fullscreen, maximized, minimized or restores based
288 // on chrome.app.window API.
289 void Fullscreen();
290 void Maximize();
291 void Minimize();
292 void Restore();
294 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details.
295 void OSFullscreen();
297 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more
298 // details.
299 void ForcedFullscreen();
301 // Set the minimum and maximum size of the content bounds.
302 void SetContentSizeConstraints(const gfx::Size& min_size,
303 const gfx::Size& max_size);
305 enum ShowType { SHOW_ACTIVE, SHOW_INACTIVE };
307 // Shows the window if its contents have been painted; otherwise flags the
308 // window to be shown as soon as its contents are painted for the first time.
309 void Show(ShowType show_type);
311 // Hides the window. If the window was previously flagged to be shown on
312 // first paint, it will be unflagged.
313 void Hide();
315 AppWindowContents* app_window_contents_for_test() {
316 return app_window_contents_.get();
319 int fullscreen_types_for_test() {
320 return fullscreen_types_;
323 // Set whether the window should stay above other windows which are not
324 // configured to be always-on-top.
325 void SetAlwaysOnTop(bool always_on_top);
327 // Whether the always-on-top property has been set by the chrome.app.window
328 // API. Note that the actual value of this property in the native app window
329 // may be false if the bit is silently switched off for security reasons.
330 bool IsAlwaysOnTop() const;
332 // Restores the always-on-top property according to |cached_always_on_top_|.
333 void RestoreAlwaysOnTop();
335 // Set whether the window should get even reserved keys (modulo platform
336 // restrictions).
337 void SetInterceptAllKeys(bool want_all_keys);
339 // Retrieve the current state of the app window as a dictionary, to pass to
340 // the renderer.
341 void GetSerializedState(base::DictionaryValue* properties) const;
343 // Called by the window API when events can be sent to the window for this
344 // app.
345 void WindowEventsReady();
347 // Notifies the window's contents that the render view is ready and it can
348 // unblock resource requests.
349 void NotifyRenderViewReady();
351 // Whether the app window wants to be alpha enabled.
352 bool requested_alpha_enabled() const { return requested_alpha_enabled_; }
354 // Whether the app window is created by IME extensions.
355 // TODO(bshe): rename to hide_app_window_in_launcher if it is not used
356 // anywhere other than app_window_launcher_controller after M45. Otherwise,
357 // remove this TODO.
358 bool is_ime_window() const { return is_ime_window_; }
360 void SetAppWindowContentsForTesting(scoped_ptr<AppWindowContents> contents) {
361 app_window_contents_ = contents.Pass();
364 protected:
365 ~AppWindow() override;
367 private:
368 // PlatformAppBrowserTest needs access to web_contents()
369 friend class PlatformAppBrowserTest;
371 // content::WebContentsDelegate implementation.
372 void CloseContents(content::WebContents* contents) override;
373 bool ShouldSuppressDialogs(content::WebContents* source) override;
374 content::ColorChooser* OpenColorChooser(
375 content::WebContents* web_contents,
376 SkColor color,
377 const std::vector<content::ColorSuggestion>& suggestions) override;
378 void RunFileChooser(content::WebContents* tab,
379 const content::FileChooserParams& params) override;
380 bool IsPopupOrPanel(const content::WebContents* source) const override;
381 void MoveContents(content::WebContents* source,
382 const gfx::Rect& pos) override;
383 void NavigationStateChanged(content::WebContents* source,
384 content::InvalidateTypes changed_flags) override;
385 void EnterFullscreenModeForTab(content::WebContents* source,
386 const GURL& origin) override;
387 void ExitFullscreenModeForTab(content::WebContents* source) override;
388 bool IsFullscreenForTabOrPending(
389 const content::WebContents* source) const override;
390 blink::WebDisplayMode GetDisplayMode(
391 const content::WebContents* source) const override;
392 void RequestMediaAccessPermission(
393 content::WebContents* web_contents,
394 const content::MediaStreamRequest& request,
395 const content::MediaResponseCallback& callback) override;
396 bool CheckMediaAccessPermission(content::WebContents* web_contents,
397 const GURL& security_origin,
398 content::MediaStreamType type) override;
399 content::WebContents* OpenURLFromTab(
400 content::WebContents* source,
401 const content::OpenURLParams& params) override;
402 void AddNewContents(content::WebContents* source,
403 content::WebContents* new_contents,
404 WindowOpenDisposition disposition,
405 const gfx::Rect& initial_rect,
406 bool user_gesture,
407 bool* was_blocked) override;
408 bool PreHandleKeyboardEvent(content::WebContents* source,
409 const content::NativeWebKeyboardEvent& event,
410 bool* is_keyboard_shortcut) override;
411 void HandleKeyboardEvent(
412 content::WebContents* source,
413 const content::NativeWebKeyboardEvent& event) override;
414 void RequestToLockMouse(content::WebContents* web_contents,
415 bool user_gesture,
416 bool last_unlocked_by_target) override;
417 bool PreHandleGestureEvent(content::WebContents* source,
418 const blink::WebGestureEvent& event) override;
420 // content::WebContentsObserver implementation.
421 void RenderViewCreated(content::RenderViewHost* render_view_host) override;
422 void DidFirstVisuallyNonEmptyPaint() override;
424 // ExtensionFunctionDispatcher::Delegate implementation.
425 WindowController* GetExtensionWindowController() const override;
426 content::WebContents* GetAssociatedWebContents() const override;
428 // ExtensionRegistryObserver implementation.
429 void OnExtensionUnloaded(content::BrowserContext* browser_context,
430 const Extension* extension,
431 UnloadedExtensionInfo::Reason reason) override;
433 // web_modal::WebContentsModalDialogManagerDelegate implementation.
434 void SetWebContentsBlocked(content::WebContents* web_contents,
435 bool blocked) override;
436 bool IsWebContentsVisible(content::WebContents* web_contents) override;
438 void ToggleFullscreenModeForTab(content::WebContents* source,
439 bool enter_fullscreen);
441 // Saves the window geometry/position/screen bounds.
442 void SaveWindowPosition();
444 // Helper method to adjust the cached bounds so that we can make sure it can
445 // be visible on the screen. See http://crbug.com/145752.
446 void AdjustBoundsToBeVisibleOnScreen(const gfx::Rect& cached_bounds,
447 const gfx::Rect& cached_screen_bounds,
448 const gfx::Rect& current_screen_bounds,
449 const gfx::Size& minimum_size,
450 gfx::Rect* bounds) const;
452 // Loads the appropriate default or cached window bounds. Returns a new
453 // CreateParams that should be used to create the window.
454 CreateParams LoadDefaults(CreateParams params) const;
456 // Load the app's image, firing a load state change when loaded.
457 void UpdateExtensionAppIcon();
459 // Set the fullscreen state in the native app window.
460 void SetNativeWindowFullscreen();
462 // Returns true if there is any overlap between the window and the taskbar
463 // (Windows only).
464 bool IntersectsWithTaskbar() const;
466 // Update the always-on-top bit in the native app window.
467 void UpdateNativeAlwaysOnTop();
469 // Sends the onWindowShown event to the app if the window has been shown. Only
470 // has an effect in tests.
471 void SendOnWindowShownIfShown();
473 // web_modal::WebContentsModalDialogManagerDelegate implementation.
474 web_modal::WebContentsModalDialogHost* GetWebContentsModalDialogHost()
475 override;
477 // Callback from web_contents()->DownloadFavicon.
478 void DidDownloadFavicon(int id,
479 int http_status_code,
480 const GURL& image_url,
481 const std::vector<SkBitmap>& bitmaps,
482 const std::vector<gfx::Size>& original_bitmap_sizes);
484 // IconImage::Observer implementation.
485 void OnExtensionIconImageChanged(IconImage* image) override;
487 // The browser context with which this window is associated. AppWindow does
488 // not own this object.
489 content::BrowserContext* browser_context_;
491 const std::string extension_id_;
493 // Identifier that is used when saving and restoring geometry for this
494 // window.
495 std::string window_key_;
497 const SessionID session_id_;
498 WindowType window_type_;
500 // Icon shown in the task bar.
501 gfx::Image app_icon_;
503 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
504 // be fetched and set using this URL.
505 GURL app_icon_url_;
507 // An object to load the app's icon as an extension resource.
508 scoped_ptr<IconImage> app_icon_image_;
510 scoped_ptr<NativeAppWindow> native_app_window_;
511 scoped_ptr<AppWindowContents> app_window_contents_;
512 scoped_ptr<AppDelegate> app_delegate_;
513 scoped_ptr<AppWebContentsHelper> helper_;
515 // The initial url this AppWindow was navigated to.
516 GURL initial_url_;
518 // Bit field of FullscreenType.
519 int fullscreen_types_;
521 // Show has been called, so the window should be shown once the first visually
522 // non-empty paint occurs.
523 bool show_on_first_paint_;
525 // The first visually non-empty paint has completed.
526 bool first_paint_complete_;
528 // Whether the window has been shown or not.
529 bool has_been_shown_;
531 // Whether events can be sent to the window.
532 bool can_send_events_;
534 // Whether the window is hidden or not. Hidden in this context means actively
535 // by the chrome.app.window API, not in an operating system context. For
536 // example windows which are minimized are not hidden, and windows which are
537 // part of a hidden app on OS X are not hidden. Windows which were created
538 // with the |hidden| flag set to true, or which have been programmatically
539 // hidden, are considered hidden.
540 bool is_hidden_;
542 // Whether the delayed Show() call was for an active or inactive window.
543 ShowType delayed_show_type_;
545 // Cache the desired value of the always-on-top property. When windows enter
546 // fullscreen or overlap the Windows taskbar, this property will be
547 // automatically and silently switched off for security reasons. It is
548 // reinstated when the window exits fullscreen and moves away from the
549 // taskbar.
550 bool cached_always_on_top_;
552 // Whether |alpha_enabled| was set in the CreateParams.
553 bool requested_alpha_enabled_;
555 // Whether |is_ime_window| was set in the CreateParams.
556 bool is_ime_window_;
558 base::WeakPtrFactory<AppWindow> image_loader_ptr_factory_;
560 DISALLOW_COPY_AND_ASSIGN(AppWindow);
563 } // namespace extensions
565 #endif // EXTENSIONS_BROWSER_APP_WINDOW_APP_WINDOW_H_