OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef APPS_SHELL_WINDOW_H_ | |
6 #define APPS_SHELL_WINDOW_H_ | |
7 | |
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" | |
22 | |
23 class GURL; | |
24 class SkRegion; | |
25 | |
26 namespace base { | |
27 class DictionaryValue; | |
28 } | |
29 | |
30 namespace content { | |
31 class BrowserContext; | |
32 class WebContents; | |
33 } | |
34 | |
35 namespace extensions { | |
36 class Extension; | |
37 class PlatformAppBrowserTest; | |
38 class WindowController; | |
39 | |
40 struct DraggableRegion; | |
41 } | |
42 | |
43 namespace ui { | |
44 class BaseWindow; | |
45 } | |
46 | |
47 namespace apps { | |
48 | |
49 class NativeAppWindow; | |
50 | |
51 // Manages the web contents for Shell 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 ShellWindowContents { | |
56 public: | |
57 ShellWindowContents() {} | |
58 virtual ~ShellWindowContents() {} | |
59 | |
60 // Called to initialize the WebContents, before the app window is created. | |
61 virtual void Initialize(content::BrowserContext* context, | |
62 const GURL& url) = 0; | |
63 | |
64 // Called to load the contents, after the app window is created. | |
65 virtual void LoadContents(int32 creator_process_id) = 0; | |
66 | |
67 // Called when the native window changes. | |
68 virtual void NativeWindowChanged(NativeAppWindow* native_app_window) = 0; | |
69 | |
70 // Called when the native window closes. | |
71 virtual void NativeWindowClosed() = 0; | |
72 | |
73 virtual content::WebContents* GetWebContents() const = 0; | |
74 | |
75 private: | |
76 DISALLOW_COPY_AND_ASSIGN(ShellWindowContents); | |
77 }; | |
78 | |
79 // ShellWindow is the type of window used by platform apps. Shell windows | |
80 // have a WebContents but none of the chrome of normal browser windows. | |
81 class ShellWindow : 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 shell 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. | |
93 }; | |
94 | |
95 enum Frame { | |
96 FRAME_CHROME, // Chrome-style window frame. | |
97 FRAME_NONE, // Frameless window. | |
98 }; | |
99 | |
100 enum FullscreenType { | |
101 // Not fullscreen. | |
102 FULLSCREEN_TYPE_NONE = 0, | |
103 | |
104 // Fullscreen entered by the app.window api. | |
105 FULLSCREEN_TYPE_WINDOW_API = 1 << 0, | |
106 | |
107 // Fullscreen entered by HTML requestFullscreen(). | |
108 FULLSCREEN_TYPE_HTML_API = 1 << 1, | |
109 | |
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, | |
113 | |
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, | |
117 }; | |
118 | |
119 class SizeConstraints { | |
120 public: | |
121 // The value SizeConstraints uses to represent an unbounded width or height. | |
122 // This is an enum so that it can be declared inline here. | |
123 enum { kUnboundedSize = 0 }; | |
124 | |
125 SizeConstraints(); | |
126 SizeConstraints(const gfx::Size& min_size, const gfx::Size& max_size); | |
127 ~SizeConstraints(); | |
128 | |
129 // Returns the bounds with its size clamped to the min/max size. | |
130 gfx::Size ClampSize(gfx::Size size) const; | |
131 | |
132 // When gfx::Size is used as a min/max size, a zero represents an unbounded | |
133 // component. This method checks whether either component is specified. | |
134 // Note we can't use gfx::Size::IsEmpty as it returns true if either width | |
135 // or height is zero. | |
136 bool HasMinimumSize() const; | |
137 bool HasMaximumSize() const; | |
138 | |
139 // This returns true if all components are specified, and min and max are | |
140 // equal. | |
141 bool HasFixedSize() const; | |
142 | |
143 gfx::Size GetMaximumSize() const; | |
144 gfx::Size GetMinimumSize() const; | |
145 | |
146 void set_minimum_size(const gfx::Size& min_size); | |
147 void set_maximum_size(const gfx::Size& max_size); | |
148 | |
149 private: | |
150 gfx::Size minimum_size_; | |
151 gfx::Size maximum_size_; | |
152 }; | |
153 | |
154 struct CreateParams { | |
155 CreateParams(); | |
156 ~CreateParams(); | |
157 | |
158 WindowType window_type; | |
159 Frame frame; | |
160 bool transparent_background; // Only supported on ash. | |
161 | |
162 // Specify the initial content bounds of the window (excluding any window | |
163 // decorations). INT_MIN designates 'unspecified' for the position | |
164 // components, and 0 for the size components. When unspecified, they should | |
165 // be replaced with a default value. | |
166 gfx::Rect bounds; | |
167 | |
168 gfx::Size minimum_size; | |
169 gfx::Size maximum_size; | |
170 | |
171 std::string window_key; | |
172 | |
173 // The process ID of the process that requested the create. | |
174 int32 creator_process_id; | |
175 | |
176 // Initial state of the window. | |
177 ui::WindowShowState state; | |
178 | |
179 // If true, don't show the window after creation. | |
180 bool hidden; | |
181 | |
182 // If true, the window will be resizable by the user. Defaults to true. | |
183 bool resizable; | |
184 | |
185 // If true, the window will be focused on creation. Defaults to true. | |
186 bool focused; | |
187 | |
188 // If true, the window will stay on top of other windows that are not | |
189 // configured to be always on top. Defaults to false. | |
190 bool always_on_top; | |
191 }; | |
192 | |
193 class Delegate { | |
194 public: | |
195 virtual ~Delegate(); | |
196 | |
197 // General initialization. | |
198 virtual void InitWebContents(content::WebContents* web_contents) = 0; | |
199 virtual NativeAppWindow* CreateNativeAppWindow( | |
200 ShellWindow* window, | |
201 const CreateParams& params) = 0; | |
202 | |
203 // Link handling. | |
204 virtual content::WebContents* OpenURLFromTab( | |
205 content::BrowserContext* context, | |
206 content::WebContents* source, | |
207 const content::OpenURLParams& params) = 0; | |
208 virtual void AddNewContents(content::BrowserContext* context, | |
209 content::WebContents* new_contents, | |
210 WindowOpenDisposition disposition, | |
211 const gfx::Rect& initial_pos, | |
212 bool user_gesture, | |
213 bool* was_blocked) = 0; | |
214 | |
215 // Feature support. | |
216 virtual content::ColorChooser* ShowColorChooser( | |
217 content::WebContents* web_contents, | |
218 SkColor initial_color) = 0; | |
219 virtual void RunFileChooser(content::WebContents* tab, | |
220 const content::FileChooserParams& params) = 0; | |
221 virtual void RequestMediaAccessPermission( | |
222 content::WebContents* web_contents, | |
223 const content::MediaStreamRequest& request, | |
224 const content::MediaResponseCallback& callback, | |
225 const extensions::Extension* extension) = 0; | |
226 virtual int PreferredIconSize() = 0; | |
227 | |
228 // Web contents modal dialog support. | |
229 virtual void SetWebContentsBlocked(content::WebContents* web_contents, | |
230 bool blocked) = 0; | |
231 virtual bool IsWebContentsVisible(content::WebContents* web_contents) = 0; | |
232 }; | |
233 | |
234 // Convert draggable regions in raw format to SkRegion format. Caller is | |
235 // responsible for deleting the returned SkRegion instance. | |
236 static SkRegion* RawDraggableRegionsToSkRegion( | |
237 const std::vector<extensions::DraggableRegion>& regions); | |
238 | |
239 // The constructor and Init methods are public for constructing a ShellWindow | |
240 // with a non-standard render interface (e.g. v1 apps using Ash Panels). | |
241 // Normally ShellWindow::Create should be used. | |
242 // The constructed shell window takes ownership of |delegate|. | |
243 ShellWindow(content::BrowserContext* context, | |
244 Delegate* delegate, | |
245 const extensions::Extension* extension); | |
246 | |
247 // Initializes the render interface, web contents, and native window. | |
248 // |shell_window_contents| will become owned by ShellWindow. | |
249 void Init(const GURL& url, | |
250 ShellWindowContents* shell_window_contents, | |
251 const CreateParams& params); | |
252 | |
253 | |
254 const std::string& window_key() const { return window_key_; } | |
255 const SessionID& session_id() const { return session_id_; } | |
256 const extensions::Extension* extension() const { return extension_; } | |
257 const std::string& extension_id() const { return extension_id_; } | |
258 content::WebContents* web_contents() const; | |
259 WindowType window_type() const { return window_type_; } | |
260 bool window_type_is_panel() const { | |
261 return (window_type_ == WINDOW_TYPE_PANEL || | |
262 window_type_ == WINDOW_TYPE_V1_PANEL); | |
263 } | |
264 content::BrowserContext* browser_context() const { return browser_context_; } | |
265 const gfx::Image& app_icon() const { return app_icon_; } | |
266 const GURL& app_icon_url() const { return app_icon_url_; } | |
267 const gfx::Image& badge_icon() const { return badge_icon_; } | |
268 const GURL& badge_icon_url() const { return badge_icon_url_; } | |
269 | |
270 NativeAppWindow* GetBaseWindow(); | |
271 gfx::NativeWindow GetNativeWindow(); | |
272 | |
273 // Returns the bounds that should be reported to the renderer. | |
274 gfx::Rect GetClientBounds() const; | |
275 | |
276 // NativeAppWindows should call this to determine what the window's title | |
277 // is on startup and from within UpdateWindowTitle(). | |
278 base::string16 GetTitle() const; | |
279 | |
280 // Call to notify ShellRegistry and delete the window. Subclasses should | |
281 // invoke this method instead of using "delete this". | |
282 void OnNativeClose(); | |
283 | |
284 // Should be called by native implementations when the window size, position, | |
285 // or minimized/maximized state has changed. | |
286 void OnNativeWindowChanged(); | |
287 | |
288 // Should be called by native implementations when the window is activated. | |
289 void OnNativeWindowActivated(); | |
290 | |
291 // Specifies a url for the launcher icon. | |
292 void SetAppIconUrl(const GURL& icon_url); | |
293 | |
294 // Specifies a url for the window badge. | |
295 void SetBadgeIconUrl(const GURL& icon_url); | |
296 | |
297 // Clear the current badge. | |
298 void ClearBadge(); | |
299 | |
300 // Set the window shape. Passing a NULL |region| sets the default shape. | |
301 void UpdateShape(scoped_ptr<SkRegion> region); | |
302 | |
303 // Called from the render interface to modify the draggable regions. | |
304 void UpdateDraggableRegions( | |
305 const std::vector<extensions::DraggableRegion>& regions); | |
306 | |
307 // Updates the app image to |image|. Called internally from the image loader | |
308 // callback. Also called externally for v1 apps using Ash Panels. | |
309 void UpdateAppIcon(const gfx::Image& image); | |
310 | |
311 // Transitions window into fullscreen, maximized, minimized or restores based | |
312 // on chrome.app.window API. | |
313 void Fullscreen(); | |
314 void Maximize(); | |
315 void Minimize(); | |
316 void Restore(); | |
317 | |
318 // Transitions to OS fullscreen. See FULLSCREEN_TYPE_OS for more details. | |
319 void OSFullscreen(); | |
320 | |
321 // Transitions to forced fullscreen. See FULLSCREEN_TYPE_FORCED for more | |
322 // details. | |
323 void ForcedFullscreen(); | |
324 | |
325 // Set the minimum and maximum size that this window is allowed to be. | |
326 void SetMinimumSize(const gfx::Size& min_size); | |
327 void SetMaximumSize(const gfx::Size& max_size); | |
328 | |
329 enum ShowType { | |
330 SHOW_ACTIVE, | |
331 SHOW_INACTIVE | |
332 }; | |
333 | |
334 // Shows the window if its contents have been painted; otherwise flags the | |
335 // window to be shown as soon as its contents are painted for the first time. | |
336 void Show(ShowType show_type); | |
337 | |
338 // Hides the window. If the window was previously flagged to be shown on | |
339 // first paint, it will be unflagged. | |
340 void Hide(); | |
341 | |
342 ShellWindowContents* shell_window_contents_for_test() { | |
343 return shell_window_contents_.get(); | |
344 } | |
345 | |
346 // Get the size constraints. | |
347 const SizeConstraints& size_constraints() const { | |
348 return size_constraints_; | |
349 } | |
350 | |
351 // Set whether the window should stay above other windows which are not | |
352 // configured to be always-on-top. | |
353 void SetAlwaysOnTop(bool always_on_top); | |
354 | |
355 // Whether the always-on-top property has been set by the chrome.app.window | |
356 // API. Note that the actual value of this property in the native app window | |
357 // may be false if the bit is silently switched off for security reasons. | |
358 bool IsAlwaysOnTop() const; | |
359 | |
360 // Retrieve the current state of the app window as a dictionary, to pass to | |
361 // the renderer. | |
362 void GetSerializedState(base::DictionaryValue* properties) const; | |
363 | |
364 protected: | |
365 virtual ~ShellWindow(); | |
366 | |
367 private: | |
368 // PlatformAppBrowserTest needs access to web_contents() | |
369 friend class extensions::PlatformAppBrowserTest; | |
370 | |
371 // content::WebContentsDelegate implementation. | |
372 virtual void CloseContents(content::WebContents* contents) OVERRIDE; | |
373 virtual bool ShouldSuppressDialogs() OVERRIDE; | |
374 virtual content::ColorChooser* OpenColorChooser( | |
375 content::WebContents* web_contents, | |
376 SkColor color, | |
377 const std::vector<content::ColorSuggestion>& suggestions) OVERRIDE; | |
378 virtual void RunFileChooser( | |
379 content::WebContents* tab, | |
380 const content::FileChooserParams& params) OVERRIDE; | |
381 virtual bool IsPopupOrPanel( | |
382 const content::WebContents* source) const OVERRIDE; | |
383 virtual void MoveContents( | |
384 content::WebContents* source, const gfx::Rect& pos) OVERRIDE; | |
385 virtual void NavigationStateChanged(const content::WebContents* source, | |
386 unsigned changed_flags) OVERRIDE; | |
387 virtual void ToggleFullscreenModeForTab(content::WebContents* source, | |
388 bool enter_fullscreen) OVERRIDE; | |
389 virtual bool IsFullscreenForTabOrPending( | |
390 const content::WebContents* source) const OVERRIDE; | |
391 virtual void RequestMediaAccessPermission( | |
392 content::WebContents* web_contents, | |
393 const content::MediaStreamRequest& request, | |
394 const content::MediaResponseCallback& callback) OVERRIDE; | |
395 virtual content::WebContents* OpenURLFromTab( | |
396 content::WebContents* source, | |
397 const content::OpenURLParams& params) OVERRIDE; | |
398 virtual void AddNewContents(content::WebContents* source, | |
399 content::WebContents* new_contents, | |
400 WindowOpenDisposition disposition, | |
401 const gfx::Rect& initial_pos, | |
402 bool user_gesture, | |
403 bool* was_blocked) OVERRIDE; | |
404 virtual bool PreHandleKeyboardEvent( | |
405 content::WebContents* source, | |
406 const content::NativeWebKeyboardEvent& event, | |
407 bool* is_keyboard_shortcut) OVERRIDE; | |
408 virtual void HandleKeyboardEvent( | |
409 content::WebContents* source, | |
410 const content::NativeWebKeyboardEvent& event) OVERRIDE; | |
411 virtual void RequestToLockMouse(content::WebContents* web_contents, | |
412 bool user_gesture, | |
413 bool last_unlocked_by_target) OVERRIDE; | |
414 virtual bool PreHandleGestureEvent( | |
415 content::WebContents* source, | |
416 const blink::WebGestureEvent& event) OVERRIDE; | |
417 | |
418 // content::WebContentsObserver implementation. | |
419 virtual void DidFirstVisuallyNonEmptyPaint(int32 page_id) OVERRIDE; | |
420 | |
421 // content::NotificationObserver implementation. | |
422 virtual void Observe(int type, | |
423 const content::NotificationSource& source, | |
424 const content::NotificationDetails& details) OVERRIDE; | |
425 | |
426 // web_modal::WebContentsModalDialogManagerDelegate implementation. | |
427 virtual void SetWebContentsBlocked(content::WebContents* web_contents, | |
428 bool blocked) OVERRIDE; | |
429 virtual bool IsWebContentsVisible( | |
430 content::WebContents* web_contents) OVERRIDE; | |
431 | |
432 // Helper method to add a message to the renderer's DevTools console. | |
433 void AddMessageToDevToolsConsole(content::ConsoleMessageLevel level, | |
434 const std::string& message); | |
435 | |
436 // Saves the window geometry/position/screen bounds. | |
437 void SaveWindowPosition(); | |
438 | |
439 // Helper method to adjust the cached bounds so that we can make sure it can | |
440 // be visible on the screen. See http://crbug.com/145752 . | |
441 void AdjustBoundsToBeVisibleOnScreen( | |
442 const gfx::Rect& cached_bounds, | |
443 const gfx::Rect& cached_screen_bounds, | |
444 const gfx::Rect& current_screen_bounds, | |
445 const gfx::Size& minimum_size, | |
446 gfx::Rect* bounds) const; | |
447 | |
448 // Loads the appropriate default or cached window bounds and constrains them | |
449 // based on screen size and minimum/maximum size. Returns a new CreateParams | |
450 // that should be used to create the window. | |
451 CreateParams LoadDefaultsAndConstrain(CreateParams params) const; | |
452 | |
453 // Load the app's image, firing a load state change when loaded. | |
454 void UpdateExtensionAppIcon(); | |
455 | |
456 // Called when size_constraints is changed. | |
457 void OnSizeConstraintsChanged(); | |
458 | |
459 // Set the fullscreen state in the native app window. | |
460 void SetNativeWindowFullscreen(); | |
461 | |
462 // Returns true if there is any overlap between the window and the taskbar | |
463 // (Windows only). | |
464 bool IntersectsWithTaskbar() const; | |
465 | |
466 // Update the always-on-top bit in the native app window. | |
467 void UpdateNativeAlwaysOnTop(); | |
468 | |
469 // extensions::ExtensionKeybindingRegistry::Delegate implementation. | |
470 virtual extensions::ActiveTabPermissionGranter* | |
471 GetActiveTabPermissionGranter() OVERRIDE; | |
472 | |
473 // web_modal::WebContentsModalDialogManagerDelegate implementation. | |
474 virtual web_modal::WebContentsModalDialogHost* | |
475 GetWebContentsModalDialogHost() OVERRIDE; | |
476 | |
477 // Updates the badge to |image|. Called internally from the image loader | |
478 // callback. | |
479 void UpdateBadgeIcon(const gfx::Image& image); | |
480 | |
481 // Callback from web_contents()->DownloadFavicon. | |
482 void DidDownloadFavicon(int id, | |
483 int http_status_code, | |
484 const GURL& image_url, | |
485 const std::vector<SkBitmap>& bitmaps, | |
486 const std::vector<gfx::Size>& original_bitmap_sizes); | |
487 | |
488 // extensions::IconImage::Observer implementation. | |
489 virtual void OnExtensionIconImageChanged( | |
490 extensions::IconImage* image) OVERRIDE; | |
491 | |
492 // The browser context with which this window is associated. ShellWindow does | |
493 // not own this object. | |
494 content::BrowserContext* browser_context_; | |
495 | |
496 // weak pointer - owned by ExtensionService. | |
497 const extensions::Extension* extension_; | |
498 const std::string extension_id_; | |
499 | |
500 // Identifier that is used when saving and restoring geometry for this | |
501 // window. | |
502 std::string window_key_; | |
503 | |
504 const SessionID session_id_; | |
505 WindowType window_type_; | |
506 content::NotificationRegistrar registrar_; | |
507 | |
508 // Icon shown in the task bar. | |
509 gfx::Image app_icon_; | |
510 | |
511 // Icon URL to be used for setting the app icon. If not empty, app_icon_ will | |
512 // be fetched and set using this URL. | |
513 GURL app_icon_url_; | |
514 | |
515 // An object to load the app's icon as an extension resource. | |
516 scoped_ptr<extensions::IconImage> app_icon_image_; | |
517 | |
518 // Badge for icon shown in the task bar. | |
519 gfx::Image badge_icon_; | |
520 | |
521 // URL to be used for setting the badge on the app icon. | |
522 GURL badge_icon_url_; | |
523 | |
524 // An object to load the badge as an extension resource. | |
525 scoped_ptr<extensions::IconImage> badge_icon_image_; | |
526 | |
527 scoped_ptr<NativeAppWindow> native_app_window_; | |
528 scoped_ptr<ShellWindowContents> shell_window_contents_; | |
529 scoped_ptr<Delegate> delegate_; | |
530 | |
531 base::WeakPtrFactory<ShellWindow> image_loader_ptr_factory_; | |
532 | |
533 // Bit field of FullscreenType. | |
534 int fullscreen_types_; | |
535 | |
536 // Size constraints on the window. | |
537 SizeConstraints size_constraints_; | |
538 | |
539 // Show has been called, so the window should be shown once the first visually | |
540 // non-empty paint occurs. | |
541 bool show_on_first_paint_; | |
542 | |
543 // The first visually non-empty paint has completed. | |
544 bool first_paint_complete_; | |
545 | |
546 // Whether the delayed Show() call was for an active or inactive window. | |
547 ShowType delayed_show_type_; | |
548 | |
549 // Cache the desired value of the always-on-top property. When windows enter | |
550 // fullscreen or overlap the Windows taskbar, this property will be | |
551 // automatically and silently switched off for security reasons. It is | |
552 // reinstated when the window exits fullscreen and moves away from the | |
553 // taskbar. | |
554 bool cached_always_on_top_; | |
555 | |
556 DISALLOW_COPY_AND_ASSIGN(ShellWindow); | |
557 }; | |
558 | |
559 } // namespace apps | |
560 | |
561 #endif // APPS_SHELL_WINDOW_H_ | |
OLD | NEW |