1 // Copyright 2013 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_SHELL_WINDOW_H_
6 #define APPS_SHELL_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/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"
30 namespace extensions {
32 class PlatformAppBrowserTest;
33 class WindowController;
35 struct DraggableRegion;
44 class NativeAppWindow;
46 // Manages the web contents for Shell 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
50 class ShellWindowContents {
52 ShellWindowContents() {}
53 virtual ~ShellWindowContents() {}
55 // Called to initialize the WebContents, before the app window is created.
56 virtual void Initialize(Profile* profile, const GURL& url) = 0;
58 // Called to load the contents, after the app window is created.
59 virtual void LoadContents(int32 creator_process_id) = 0;
61 // Called when the native window changes.
62 virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0;
64 // Called when the native window closes.
65 virtual void NativeWindowClosed() = 0;
67 virtual content::WebContents* GetWebContents() const = 0;
70 DISALLOW_COPY_AND_ASSIGN(ShellWindowContents);
73 // ShellWindow is the type of window used by platform apps. Shell windows
74 // have a WebContents but none of the chrome of normal browser windows.
75 class ShellWindow : public content::NotificationObserver,
76 public content::WebContentsDelegate,
77 public web_modal::WebContentsModalDialogManagerDelegate,
78 public extensions::ExtensionKeybindingRegistry::Delegate,
79 public extensions::IconImage::Observer {
82 WINDOW_TYPE_DEFAULT = 1 << 0, // Default shell window.
83 WINDOW_TYPE_PANEL = 1 << 1, // OS controlled panel window (Ash only).
84 WINDOW_TYPE_V1_PANEL = 1 << 2, // For apps v1 support in Ash; deprecate
89 FRAME_CHROME, // Chrome-style window frame.
90 FRAME_NONE, // Frameless window.
93 class SizeConstraints {
95 // The value SizeConstraints uses to represent an unbounded width or height.
96 // This is an enum so that it can be declared inline here.
97 enum { kUnboundedSize = 0 };
100 SizeConstraints(const gfx::Size& min_size, const gfx::Size& max_size);
103 // Returns the bounds with its size clamped to the min/max size.
104 gfx::Size ClampSize(gfx::Size size) const;
106 // When gfx::Size is used as a min/max size, a zero represents an unbounded
107 // component. This method checks whether either component is specified.
108 // Note we can't use gfx::Size::IsEmpty as it returns true if either width
109 // or height is zero.
110 bool HasMinimumSize() const;
111 bool HasMaximumSize() const;
113 // This returns true if all components are specified, and min and max are
115 bool HasFixedSize() const;
117 gfx::Size GetMaximumSize() const;
118 gfx::Size GetMinimumSize() const;
120 void set_minimum_size(const gfx::Size& min_size);
121 void set_maximum_size(const gfx::Size& max_size);
124 gfx::Size minimum_size_;
125 gfx::Size maximum_size_;
128 struct CreateParams {
132 WindowType window_type;
134 bool transparent_background; // Only supported on ash.
136 // Specify the initial content bounds of the window (excluding any window
137 // decorations). INT_MIN designates 'unspecified' for the position
138 // components, and 0 for the size components. When unspecified, they should
139 // be replaced with a default value.
142 gfx::Size minimum_size;
143 gfx::Size maximum_size;
145 std::string window_key;
147 // The process ID of the process that requested the create.
148 int32 creator_process_id;
150 // Initial state of the window.
151 ui::WindowShowState state;
153 // If true, don't show the window after creation.
156 // If true, the window will be resizable by the user. Defaults to true.
159 // If true, the window will be focused on creation. Defaults to true.
162 // If true, the window will stay on top of other windows that are not
163 // configured to be always on top. Defaults to false.
171 // General initialization.
172 virtual void InitWebContents(content::WebContents* web_contents) = 0;
173 virtual NativeAppWindow* CreateNativeAppWindow(
175 const CreateParams& params) = 0;
178 virtual content::WebContents* OpenURLFromTab(
180 content::WebContents* source,
181 const content::OpenURLParams& params) = 0;
182 virtual void AddNewContents(Profile* profile,
183 content::WebContents* new_contents,
184 WindowOpenDisposition disposition,
185 const gfx::Rect& initial_pos,
187 bool* was_blocked) = 0;
190 virtual content::ColorChooser* ShowColorChooser(
191 content::WebContents* web_contents,
192 SkColor initial_color) = 0;
193 virtual void RunFileChooser(content::WebContents* tab,
194 const content::FileChooserParams& params) = 0;
195 virtual void RequestMediaAccessPermission(
196 content::WebContents* web_contents,
197 const content::MediaStreamRequest& request,
198 const content::MediaResponseCallback& callback,
199 const extensions::Extension* extension) = 0;
200 virtual int PreferredIconSize() = 0;
202 // Web contents modal dialog support.
203 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
205 virtual bool IsWebContentsVisible(content::WebContents* web_contents) = 0;
208 // Convert draggable regions in raw format to SkRegion format. Caller is
209 // responsible for deleting the returned SkRegion instance.
210 static SkRegion* RawDraggableRegionsToSkRegion(
211 const std::vector<extensions::DraggableRegion>& regions);
213 // The constructor and Init methods are public for constructing a ShellWindow
214 // with a non-standard render interface (e.g. v1 apps using Ash Panels).
215 // Normally ShellWindow::Create should be used.
216 // The constructed shell window takes ownership of |delegate|.
217 ShellWindow(Profile* profile,
219 const extensions::Extension* extension);
221 // Initializes the render interface, web contents, and native window.
222 // |shell_window_contents| will become owned by ShellWindow.
223 void Init(const GURL& url,
224 ShellWindowContents* shell_window_contents,
225 const CreateParams& params);
228 const std::string& window_key() const { return window_key_; }
229 const SessionID& session_id() const { return session_id_; }
230 const extensions::Extension* extension() const { return extension_; }
231 const std::string& extension_id() const { return extension_id_; }
232 content::WebContents* web_contents() const;
233 WindowType window_type() const { return window_type_; }
234 bool window_type_is_panel() const {
235 return (window_type_ == WINDOW_TYPE_PANEL ||
236 window_type_ == WINDOW_TYPE_V1_PANEL);
238 Profile* profile() const { return profile_; }
239 const gfx::Image& app_icon() const { return app_icon_; }
240 const GURL& app_icon_url() { return app_icon_url_; }
242 NativeAppWindow* GetBaseWindow();
243 gfx::NativeWindow GetNativeWindow();
245 // Returns the bounds that should be reported to the renderer.
246 gfx::Rect GetClientBounds() const;
248 // NativeAppWindows should call this to determine what the window's title
249 // is on startup and from within UpdateWindowTitle().
250 string16 GetTitle() const;
252 // Call to notify ShellRegistry and delete the window. Subclasses should
253 // invoke this method instead of using "delete this".
254 void OnNativeClose();
256 // Should be called by native implementations when the window size, position,
257 // or minimized/maximized state has changed.
258 void OnNativeWindowChanged();
260 // Should be called by native implementations when the window is activated.
261 void OnNativeWindowActivated();
263 // Specifies a url for the launcher icon.
264 void SetAppIconUrl(const GURL& icon_url);
266 // Set the region in the window that will accept input events.
267 // If |region| is NULL, then the entire window will accept input events.
268 void UpdateInputRegion(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.
285 // Set the minimum and maximum size that this window is allowed to be.
286 void SetMinimumSize(const gfx::Size& min_size);
287 void SetMaximumSize(const gfx::Size& max_size);
289 ShellWindowContents* shell_window_contents_for_test() {
290 return shell_window_contents_.get();
293 // Get the size constraints.
294 const SizeConstraints& size_constraints() const {
295 return size_constraints_;
299 virtual ~ShellWindow();
302 // PlatformAppBrowserTest needs access to web_contents()
303 friend class extensions::PlatformAppBrowserTest;
305 // content::WebContentsDelegate implementation.
306 virtual void CloseContents(content::WebContents* contents) OVERRIDE;
307 virtual bool ShouldSuppressDialogs() OVERRIDE;
308 virtual content::ColorChooser* OpenColorChooser(
309 content::WebContents* web_contents, SkColor color) OVERRIDE;
310 virtual void RunFileChooser(
311 content::WebContents* tab,
312 const content::FileChooserParams& params) OVERRIDE;
313 virtual bool IsPopupOrPanel(
314 const content::WebContents* source) const OVERRIDE;
315 virtual void MoveContents(
316 content::WebContents* source, const gfx::Rect& pos) OVERRIDE;
317 virtual void NavigationStateChanged(const content::WebContents* source,
318 unsigned changed_flags) OVERRIDE;
319 virtual void ToggleFullscreenModeForTab(content::WebContents* source,
320 bool enter_fullscreen) OVERRIDE;
321 virtual bool IsFullscreenForTabOrPending(
322 const content::WebContents* source) const OVERRIDE;
323 virtual void RequestMediaAccessPermission(
324 content::WebContents* web_contents,
325 const content::MediaStreamRequest& request,
326 const content::MediaResponseCallback& callback) OVERRIDE;
327 virtual content::WebContents* OpenURLFromTab(
328 content::WebContents* source,
329 const content::OpenURLParams& params) OVERRIDE;
330 virtual void AddNewContents(content::WebContents* source,
331 content::WebContents* new_contents,
332 WindowOpenDisposition disposition,
333 const gfx::Rect& initial_pos,
335 bool* was_blocked) OVERRIDE;
336 virtual void HandleKeyboardEvent(
337 content::WebContents* source,
338 const content::NativeWebKeyboardEvent& event) OVERRIDE;
339 virtual void RequestToLockMouse(content::WebContents* web_contents,
341 bool last_unlocked_by_target) OVERRIDE;
343 // content::NotificationObserver implementation.
344 virtual void Observe(int type,
345 const content::NotificationSource& source,
346 const content::NotificationDetails& details) OVERRIDE;
348 // web_modal::WebContentsModalDialogManagerDelegate implementation.
349 virtual void SetWebContentsBlocked(content::WebContents* web_contents,
350 bool blocked) OVERRIDE;
351 virtual bool IsWebContentsVisible(
352 content::WebContents* web_contents) OVERRIDE;
354 // Helper method to add a message to the renderer's DevTools console.
355 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level,
356 const std::string& message);
358 // Saves the window geometry/position/screen bounds.
359 void SaveWindowPosition();
361 // Helper method to adjust the cached bounds so that we can make sure it can
362 // be visible on the screen. See http://crbug.com/145752 .
363 void AdjustBoundsToBeVisibleOnScreen(
364 const gfx::Rect& cached_bounds,
365 const gfx::Rect& cached_screen_bounds,
366 const gfx::Rect& current_screen_bounds,
367 const gfx::Size& minimum_size,
368 gfx::Rect* bounds) const;
370 // Loads the appropriate default or cached window bounds and constrains them
371 // based on screen size and minimum/maximum size. Returns a new CreateParams
372 // that should be used to create the window.
373 CreateParams LoadDefaultsAndConstrain(CreateParams params) const;
375 // Load the app's image, firing a load state change when loaded.
376 void UpdateExtensionAppIcon();
378 // Called when size_constraints is changed.
379 void OnSizeConstraintsChanged();
381 // extensions::ExtensionKeybindingRegistry::Delegate implementation.
382 virtual extensions::ActiveTabPermissionGranter*
383 GetActiveTabPermissionGranter() OVERRIDE;
385 // web_modal::WebContentsModalDialogManagerDelegate implementation.
386 virtual web_modal::WebContentsModalDialogHost*
387 GetWebContentsModalDialogHost() OVERRIDE;
389 // Callback from web_contents()->DownloadFavicon.
390 void DidDownloadFavicon(int id,
391 int http_status_code,
392 const GURL& image_url,
393 const std::vector<SkBitmap>& bitmaps,
394 const std::vector<gfx::Size>& original_bitmap_sizes);
396 // extensions::IconImage::Observer implementation.
397 virtual void OnExtensionIconImageChanged(
398 extensions::IconImage* image) OVERRIDE;
400 Profile* profile_; // weak pointer - owned by ProfileManager.
401 // weak pointer - owned by ExtensionService.
402 const extensions::Extension* extension_;
403 const std::string extension_id_;
405 // Identifier that is used when saving and restoring geometry for this
407 std::string window_key_;
409 const SessionID session_id_;
410 WindowType window_type_;
411 content::NotificationRegistrar registrar_;
413 // Icon shown in the task bar.
414 gfx::Image app_icon_;
416 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will
417 // be fetched and set using this URL.
420 // An object to load the app's icon as an extension resource.
421 scoped_ptr<extensions::IconImage> app_icon_image_;
423 scoped_ptr<NativeAppWindow> native_app_window_;
424 scoped_ptr<ShellWindowContents> shell_window_contents_;
425 scoped_ptr<Delegate> delegate_;
427 base::WeakPtrFactory<ShellWindow> image_loader_ptr_factory_;
429 // Fullscreen entered by app.window api.
430 bool fullscreen_for_window_api_;
431 // Fullscreen entered by HTML requestFullscreen.
432 bool fullscreen_for_tab_;
434 // Size constraints on the window.
435 SizeConstraints size_constraints_;
437 DISALLOW_COPY_AND_ASSIGN(ShellWindow);
442 #endif // APPS_SHELL_WINDOW_H_