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 #include "apps/app_window.h"
7 #include "apps/app_window_geometry_cache.h"
8 #include "apps/app_window_registry.h"
9 #include "apps/apps_client.h"
10 #include "apps/ui/native_app_window.h"
11 #include "base/command_line.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/values.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/extensions/extension_web_contents_observer.h"
17 #include "chrome/browser/extensions/suggest_permission_util.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/extensions/extension_messages.h"
20 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
21 #include "components/web_modal/web_contents_modal_dialog_manager.h"
22 #include "content/public/browser/browser_context.h"
23 #include "content/public/browser/invalidate_type.h"
24 #include "content/public/browser/navigation_entry.h"
25 #include "content/public/browser/notification_details.h"
26 #include "content/public/browser/notification_service.h"
27 #include "content/public/browser/notification_source.h"
28 #include "content/public/browser/notification_types.h"
29 #include "content/public/browser/render_view_host.h"
30 #include "content/public/browser/resource_dispatcher_host.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/browser/web_contents_view.h"
33 #include "content/public/common/media_stream_request.h"
34 #include "extensions/browser/extension_system.h"
35 #include "extensions/browser/extensions_browser_client.h"
36 #include "extensions/browser/process_manager.h"
37 #include "extensions/browser/view_type_utils.h"
38 #include "extensions/common/extension.h"
39 #include "third_party/skia/include/core/SkRegion.h"
40 #include "ui/gfx/screen.h"
42 #if !defined(OS_MACOSX)
43 #include "apps/pref_names.h"
44 #include "base/prefs/pref_service.h"
47 using content::BrowserContext
;
48 using content::ConsoleMessageLevel
;
49 using content::WebContents
;
50 using extensions::APIPermission
;
51 using web_modal::WebContentsModalDialogHost
;
52 using web_modal::WebContentsModalDialogManager
;
58 const int kDefaultWidth
= 512;
59 const int kDefaultHeight
= 384;
61 bool IsFullscreen(int fullscreen_types
) {
62 return fullscreen_types
!= apps::AppWindow::FULLSCREEN_TYPE_NONE
;
65 void SetConstraintProperty(const std::string
& name
,
67 base::DictionaryValue
* bounds_properties
) {
68 if (value
!= AppWindow::SizeConstraints::kUnboundedSize
)
69 bounds_properties
->SetInteger(name
, value
);
71 bounds_properties
->Set(name
, base::Value::CreateNullValue());
74 void SetBoundsProperties(const gfx::Rect
& bounds
,
75 const AppWindow::SizeConstraints
& constraints
,
76 const std::string
& bounds_name
,
77 base::DictionaryValue
* window_properties
) {
78 scoped_ptr
<base::DictionaryValue
> bounds_properties(
79 new base::DictionaryValue());
81 bounds_properties
->SetInteger("left", bounds
.x());
82 bounds_properties
->SetInteger("top", bounds
.y());
83 bounds_properties
->SetInteger("width", bounds
.width());
84 bounds_properties
->SetInteger("height", bounds
.height());
86 gfx::Size min_size
= constraints
.GetMinimumSize();
87 gfx::Size max_size
= constraints
.GetMaximumSize();
88 SetConstraintProperty("minWidth", min_size
.width(), bounds_properties
.get());
89 SetConstraintProperty(
90 "minHeight", min_size
.height(), bounds_properties
.get());
91 SetConstraintProperty("maxWidth", max_size
.width(), bounds_properties
.get());
92 SetConstraintProperty(
93 "maxHeight", max_size
.height(), bounds_properties
.get());
95 window_properties
->Set(bounds_name
, bounds_properties
.release());
100 AppWindow::SizeConstraints::SizeConstraints()
101 : maximum_size_(kUnboundedSize
, kUnboundedSize
) {}
103 AppWindow::SizeConstraints::SizeConstraints(const gfx::Size
& min_size
,
104 const gfx::Size
& max_size
)
105 : minimum_size_(min_size
), maximum_size_(max_size
) {}
107 AppWindow::SizeConstraints::~SizeConstraints() {}
109 gfx::Size
AppWindow::SizeConstraints::ClampSize(gfx::Size size
) const {
110 const gfx::Size max_size
= GetMaximumSize();
111 if (max_size
.width() != kUnboundedSize
)
112 size
.set_width(std::min(size
.width(), GetMaximumSize().width()));
113 if (max_size
.height() != kUnboundedSize
)
114 size
.set_height(std::min(size
.height(), GetMaximumSize().height()));
115 size
.SetToMax(GetMinimumSize());
119 bool AppWindow::SizeConstraints::HasMinimumSize() const {
120 return GetMinimumSize().width() != kUnboundedSize
||
121 GetMinimumSize().height() != kUnboundedSize
;
124 bool AppWindow::SizeConstraints::HasMaximumSize() const {
125 const gfx::Size max_size
= GetMaximumSize();
126 return max_size
.width() != kUnboundedSize
||
127 max_size
.height() != kUnboundedSize
;
130 bool AppWindow::SizeConstraints::HasFixedSize() const {
131 return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize();
134 gfx::Size
AppWindow::SizeConstraints::GetMinimumSize() const {
135 return minimum_size_
;
138 gfx::Size
AppWindow::SizeConstraints::GetMaximumSize() const {
140 maximum_size_
.width() == kUnboundedSize
142 : std::max(maximum_size_
.width(), minimum_size_
.width()),
143 maximum_size_
.height() == kUnboundedSize
145 : std::max(maximum_size_
.height(), minimum_size_
.height()));
148 void AppWindow::SizeConstraints::set_minimum_size(const gfx::Size
& min_size
) {
149 minimum_size_
= min_size
;
152 void AppWindow::SizeConstraints::set_maximum_size(const gfx::Size
& max_size
) {
153 maximum_size_
= max_size
;
156 AppWindow::CreateParams::CreateParams()
157 : window_type(AppWindow::WINDOW_TYPE_DEFAULT
),
158 frame(AppWindow::FRAME_CHROME
),
159 has_frame_color(false),
160 transparent_background(false),
161 bounds(INT_MIN
, INT_MIN
, 0, 0),
162 creator_process_id(0),
163 state(ui::SHOW_STATE_DEFAULT
),
167 always_on_top(false) {}
169 AppWindow::CreateParams::~CreateParams() {}
171 AppWindow::Delegate::~Delegate() {}
173 AppWindow::AppWindow(BrowserContext
* context
,
175 const extensions::Extension
* extension
)
176 : browser_context_(context
),
177 extension_(extension
),
178 extension_id_(extension
->id()),
179 window_type_(WINDOW_TYPE_DEFAULT
),
181 image_loader_ptr_factory_(this),
182 fullscreen_types_(FULLSCREEN_TYPE_NONE
),
183 show_on_first_paint_(false),
184 first_paint_complete_(false),
185 cached_always_on_top_(false) {
186 extensions::ExtensionsBrowserClient
* client
=
187 extensions::ExtensionsBrowserClient::Get();
188 CHECK(!client
->IsGuestSession(context
) || context
->IsOffTheRecord())
189 << "Only off the record window may be opened in the guest mode.";
192 void AppWindow::Init(const GURL
& url
,
193 AppWindowContents
* app_window_contents
,
194 const CreateParams
& params
) {
195 // Initialize the render interface and web contents
196 app_window_contents_
.reset(app_window_contents
);
197 app_window_contents_
->Initialize(browser_context(), url
);
198 WebContents
* web_contents
= app_window_contents_
->GetWebContents();
199 if (CommandLine::ForCurrentProcess()->HasSwitch(
200 switches::kEnableAppsShowOnFirstPaint
)) {
201 content::WebContentsObserver::Observe(web_contents
);
203 delegate_
->InitWebContents(web_contents
);
204 WebContentsModalDialogManager::CreateForWebContents(web_contents
);
205 extensions::ExtensionWebContentsObserver::CreateForWebContents(web_contents
);
207 web_contents
->SetDelegate(this);
208 WebContentsModalDialogManager::FromWebContents(web_contents
)
210 extensions::SetViewType(web_contents
, extensions::VIEW_TYPE_APP_WINDOW
);
212 // Initialize the window
213 CreateParams new_params
= LoadDefaultsAndConstrain(params
);
214 window_type_
= new_params
.window_type
;
215 window_key_
= new_params
.window_key
;
217 SizeConstraints(new_params
.minimum_size
, new_params
.maximum_size
);
219 // Windows cannot be always-on-top in fullscreen mode for security reasons.
220 cached_always_on_top_
= new_params
.always_on_top
;
221 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
222 new_params
.always_on_top
= false;
224 native_app_window_
.reset(delegate_
->CreateNativeAppWindow(this, new_params
));
226 if (!new_params
.hidden
) {
227 // Panels are not activated by default.
228 Show(window_type_is_panel() || !new_params
.focused
? SHOW_INACTIVE
232 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
234 else if (new_params
.state
== ui::SHOW_STATE_MAXIMIZED
)
236 else if (new_params
.state
== ui::SHOW_STATE_MINIMIZED
)
239 OnNativeWindowChanged();
241 // When the render view host is changed, the native window needs to know
242 // about it in case it has any setup to do to make the renderer appear
243 // properly. In particular, on Windows, the view's clickthrough region needs
245 extensions::ExtensionsBrowserClient
* client
=
246 extensions::ExtensionsBrowserClient::Get();
248 chrome::NOTIFICATION_EXTENSION_UNLOADED
,
249 content::Source
<content::BrowserContext
>(
250 client
->GetOriginalContext(browser_context_
)));
251 // Close when the browser process is exiting.
253 chrome::NOTIFICATION_APP_TERMINATING
,
254 content::NotificationService::AllSources());
255 // Update the app menu if an ephemeral app becomes installed.
257 chrome::NOTIFICATION_EXTENSION_INSTALLED
,
258 content::Source
<content::BrowserContext
>(
259 client
->GetOriginalContext(browser_context_
)));
261 app_window_contents_
->LoadContents(new_params
.creator_process_id
);
263 if (CommandLine::ForCurrentProcess()->HasSwitch(
264 switches::kEnableAppsShowOnFirstPaint
)) {
265 // We want to show the window only when the content has been painted. For
266 // that to happen, we need to define a size for the content, otherwise the
267 // layout will happen in a 0x0 area.
268 // Note: WebContents::GetView() is guaranteed to be non-null.
269 web_contents
->GetView()->SizeContents(new_params
.bounds
.size());
272 // Prevent the browser process from shutting down while this window is open.
273 AppsClient::Get()->IncrementKeepAliveCount();
275 UpdateExtensionAppIcon();
277 AppWindowRegistry::Get(browser_context_
)->AddAppWindow(this);
280 AppWindow::~AppWindow() {
281 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the
283 registrar_
.RemoveAll();
285 // Remove shutdown prevention.
286 AppsClient::Get()->DecrementKeepAliveCount();
289 void AppWindow::RequestMediaAccessPermission(
290 content::WebContents
* web_contents
,
291 const content::MediaStreamRequest
& request
,
292 const content::MediaResponseCallback
& callback
) {
293 delegate_
->RequestMediaAccessPermission(
294 web_contents
, request
, callback
, extension());
297 WebContents
* AppWindow::OpenURLFromTab(WebContents
* source
,
298 const content::OpenURLParams
& params
) {
299 // Don't allow the current tab to be navigated. It would be nice to map all
300 // anchor tags (even those without target="_blank") to new tabs, but right
301 // now we can't distinguish between those and <meta> refreshes or window.href
302 // navigations, which we don't want to allow.
303 // TOOD(mihaip): Can we check for user gestures instead?
304 WindowOpenDisposition disposition
= params
.disposition
;
305 if (disposition
== CURRENT_TAB
) {
306 AddMessageToDevToolsConsole(
307 content::CONSOLE_MESSAGE_LEVEL_ERROR
,
309 "Can't open same-window link to \"%s\"; try target=\"_blank\".",
310 params
.url
.spec().c_str()));
314 // These dispositions aren't really navigations.
315 if (disposition
== SUPPRESS_OPEN
|| disposition
== SAVE_TO_DISK
||
316 disposition
== IGNORE_ACTION
) {
320 WebContents
* contents
=
321 delegate_
->OpenURLFromTab(browser_context_
, source
, params
);
323 AddMessageToDevToolsConsole(
324 content::CONSOLE_MESSAGE_LEVEL_ERROR
,
326 "Can't navigate to \"%s\"; apps do not support navigation.",
327 params
.url
.spec().c_str()));
333 void AppWindow::AddNewContents(WebContents
* source
,
334 WebContents
* new_contents
,
335 WindowOpenDisposition disposition
,
336 const gfx::Rect
& initial_pos
,
339 DCHECK(new_contents
->GetBrowserContext() == browser_context_
);
340 delegate_
->AddNewContents(browser_context_
,
348 bool AppWindow::PreHandleKeyboardEvent(
349 content::WebContents
* source
,
350 const content::NativeWebKeyboardEvent
& event
,
351 bool* is_keyboard_shortcut
) {
352 // Here, we can handle a key event before the content gets it. When we are
353 // fullscreen and it is not forced, we want to allow the user to leave
354 // when ESC is pressed.
355 // However, if the application has the "overrideEscFullscreen" permission, we
356 // should let it override that behavior.
357 // ::HandleKeyboardEvent() will only be called if the KeyEvent's default
358 // action is not prevented.
359 // Thus, we should handle the KeyEvent here only if the permission is not set.
360 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&&
361 (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
) &&
362 ((fullscreen_types_
& FULLSCREEN_TYPE_FORCED
) == 0) &&
363 !extension_
->HasAPIPermission(APIPermission::kOverrideEscFullscreen
)) {
371 void AppWindow::HandleKeyboardEvent(
373 const content::NativeWebKeyboardEvent
& event
) {
374 // If the window is currently fullscreen and not forced, ESC should leave
375 // fullscreen. If this code is being called for ESC, that means that the
376 // KeyEvent's default behavior was not prevented by the content.
377 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&&
378 (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
) &&
379 ((fullscreen_types_
& FULLSCREEN_TYPE_FORCED
) == 0)) {
384 native_app_window_
->HandleKeyboardEvent(event
);
387 void AppWindow::RequestToLockMouse(WebContents
* web_contents
,
389 bool last_unlocked_by_target
) {
390 bool has_permission
= IsExtensionWithPermissionOrSuggestInConsole(
391 APIPermission::kPointerLock
,
393 web_contents
->GetRenderViewHost());
395 web_contents
->GotResponseToLockMouseRequest(has_permission
);
398 bool AppWindow::PreHandleGestureEvent(WebContents
* source
,
399 const blink::WebGestureEvent
& event
) {
400 // Disable pinch zooming in app windows.
401 return event
.type
== blink::WebGestureEvent::GesturePinchBegin
||
402 event
.type
== blink::WebGestureEvent::GesturePinchUpdate
||
403 event
.type
== blink::WebGestureEvent::GesturePinchEnd
;
406 void AppWindow::DidFirstVisuallyNonEmptyPaint(int32 page_id
) {
407 first_paint_complete_
= true;
408 if (show_on_first_paint_
) {
409 DCHECK(delayed_show_type_
== SHOW_ACTIVE
||
410 delayed_show_type_
== SHOW_INACTIVE
);
411 Show(delayed_show_type_
);
415 void AppWindow::OnNativeClose() {
416 AppWindowRegistry::Get(browser_context_
)->RemoveAppWindow(this);
417 if (app_window_contents_
) {
418 WebContents
* web_contents
= app_window_contents_
->GetWebContents();
419 WebContentsModalDialogManager::FromWebContents(web_contents
)
421 app_window_contents_
->NativeWindowClosed();
426 void AppWindow::OnNativeWindowChanged() {
427 SaveWindowPosition();
430 if (native_app_window_
&& cached_always_on_top_
&&
431 !IsFullscreen(fullscreen_types_
) && !native_app_window_
->IsMaximized() &&
432 !native_app_window_
->IsMinimized()) {
433 UpdateNativeAlwaysOnTop();
437 if (app_window_contents_
&& native_app_window_
)
438 app_window_contents_
->NativeWindowChanged(native_app_window_
.get());
441 void AppWindow::OnNativeWindowActivated() {
442 AppWindowRegistry::Get(browser_context_
)->AppWindowActivated(this);
445 content::WebContents
* AppWindow::web_contents() const {
446 return app_window_contents_
->GetWebContents();
449 NativeAppWindow
* AppWindow::GetBaseWindow() { return native_app_window_
.get(); }
451 gfx::NativeWindow
AppWindow::GetNativeWindow() {
452 return GetBaseWindow()->GetNativeWindow();
455 gfx::Rect
AppWindow::GetClientBounds() const {
456 gfx::Rect bounds
= native_app_window_
->GetBounds();
457 bounds
.Inset(native_app_window_
->GetFrameInsets());
461 base::string16
AppWindow::GetTitle() const {
462 // WebContents::GetTitle() will return the page's URL if there's no <title>
463 // specified. However, we'd prefer to show the name of the extension in that
464 // case, so we directly inspect the NavigationEntry's title.
465 base::string16 title
;
466 if (!web_contents() || !web_contents()->GetController().GetActiveEntry() ||
467 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) {
468 title
= base::UTF8ToUTF16(extension()->name());
470 title
= web_contents()->GetTitle();
472 const base::char16 kBadChars
[] = {'\n', 0};
473 base::RemoveChars(title
, kBadChars
, &title
);
477 void AppWindow::SetAppIconUrl(const GURL
& url
) {
478 // If the same url is being used for the badge, ignore it.
479 if (url
== badge_icon_url_
)
482 // Avoid using any previous icons that were being downloaded.
483 image_loader_ptr_factory_
.InvalidateWeakPtrs();
485 // Reset |app_icon_image_| to abort pending image load (if any).
486 app_icon_image_
.reset();
489 web_contents()->DownloadImage(
491 true, // is a favicon
492 0, // no maximum size
493 base::Bind(&AppWindow::DidDownloadFavicon
,
494 image_loader_ptr_factory_
.GetWeakPtr()));
497 void AppWindow::SetBadgeIconUrl(const GURL
& url
) {
498 // Avoid using any previous icons that were being downloaded.
499 image_loader_ptr_factory_
.InvalidateWeakPtrs();
501 // Reset |app_icon_image_| to abort pending image load (if any).
502 badge_icon_image_
.reset();
504 badge_icon_url_
= url
;
505 web_contents()->DownloadImage(
507 true, // is a favicon
508 0, // no maximum size
509 base::Bind(&AppWindow::DidDownloadFavicon
,
510 image_loader_ptr_factory_
.GetWeakPtr()));
513 void AppWindow::ClearBadge() {
514 badge_icon_image_
.reset();
515 badge_icon_url_
= GURL();
516 UpdateBadgeIcon(gfx::Image());
519 void AppWindow::UpdateShape(scoped_ptr
<SkRegion
> region
) {
520 native_app_window_
->UpdateShape(region
.Pass());
523 void AppWindow::UpdateDraggableRegions(
524 const std::vector
<extensions::DraggableRegion
>& regions
) {
525 native_app_window_
->UpdateDraggableRegions(regions
);
528 void AppWindow::UpdateAppIcon(const gfx::Image
& image
) {
532 native_app_window_
->UpdateWindowIcon();
533 AppWindowRegistry::Get(browser_context_
)->AppWindowIconChanged(this);
536 void AppWindow::Fullscreen() {
537 #if !defined(OS_MACOSX)
538 // Do not enter fullscreen mode if disallowed by pref.
540 extensions::ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
542 if (!prefs
->GetBoolean(prefs::kAppFullscreenAllowed
))
545 fullscreen_types_
|= FULLSCREEN_TYPE_WINDOW_API
;
546 SetNativeWindowFullscreen();
549 void AppWindow::Maximize() { GetBaseWindow()->Maximize(); }
551 void AppWindow::Minimize() { GetBaseWindow()->Minimize(); }
553 void AppWindow::Restore() {
554 if (IsFullscreen(fullscreen_types_
)) {
555 fullscreen_types_
= FULLSCREEN_TYPE_NONE
;
556 SetNativeWindowFullscreen();
558 GetBaseWindow()->Restore();
562 void AppWindow::OSFullscreen() {
563 #if !defined(OS_MACOSX)
564 // Do not enter fullscreen mode if disallowed by pref.
566 extensions::ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
568 if (!prefs
->GetBoolean(prefs::kAppFullscreenAllowed
))
571 fullscreen_types_
|= FULLSCREEN_TYPE_OS
;
572 SetNativeWindowFullscreen();
575 void AppWindow::ForcedFullscreen() {
576 fullscreen_types_
|= FULLSCREEN_TYPE_FORCED
;
577 SetNativeWindowFullscreen();
580 void AppWindow::SetMinimumSize(const gfx::Size
& min_size
) {
581 size_constraints_
.set_minimum_size(min_size
);
582 OnSizeConstraintsChanged();
585 void AppWindow::SetMaximumSize(const gfx::Size
& max_size
) {
586 size_constraints_
.set_maximum_size(max_size
);
587 OnSizeConstraintsChanged();
590 void AppWindow::Show(ShowType show_type
) {
591 if (CommandLine::ForCurrentProcess()->HasSwitch(
592 switches::kEnableAppsShowOnFirstPaint
)) {
593 show_on_first_paint_
= true;
595 if (!first_paint_complete_
) {
596 delayed_show_type_
= show_type
;
603 GetBaseWindow()->Show();
606 GetBaseWindow()->ShowInactive();
611 void AppWindow::Hide() {
612 // This is there to prevent race conditions with Hide() being called before
613 // there was a non-empty paint. It should have no effect in a non-racy
614 // scenario where the application is hiding then showing a window: the second
615 // show will not be delayed.
616 show_on_first_paint_
= false;
617 GetBaseWindow()->Hide();
620 void AppWindow::SetAlwaysOnTop(bool always_on_top
) {
621 if (cached_always_on_top_
== always_on_top
)
624 cached_always_on_top_
= always_on_top
;
626 // As a security measure, do not allow fullscreen windows or windows that
627 // overlap the taskbar to be on top. The property will be applied when the
628 // window exits fullscreen and moves away from the taskbar.
629 if (!IsFullscreen(fullscreen_types_
) && !IntersectsWithTaskbar())
630 native_app_window_
->SetAlwaysOnTop(always_on_top
);
632 OnNativeWindowChanged();
635 bool AppWindow::IsAlwaysOnTop() const { return cached_always_on_top_
; }
637 void AppWindow::GetSerializedState(base::DictionaryValue
* properties
) const {
640 properties
->SetBoolean("fullscreen",
641 native_app_window_
->IsFullscreenOrPending());
642 properties
->SetBoolean("minimized", native_app_window_
->IsMinimized());
643 properties
->SetBoolean("maximized", native_app_window_
->IsMaximized());
644 properties
->SetBoolean("alwaysOnTop", IsAlwaysOnTop());
645 properties
->SetBoolean("hasFrameColor", native_app_window_
->HasFrameColor());
646 properties
->SetInteger("frameColor", native_app_window_
->FrameColor());
648 gfx::Rect content_bounds
= GetClientBounds();
650 content_bounds
, size_constraints(), "innerBounds", properties
);
652 // TODO(tmdiep): Frame constraints will be implemented in a future patch.
653 gfx::Rect frame_bounds
= native_app_window_
->GetBounds();
654 SizeConstraints frame_constraints
;
656 frame_bounds
, frame_constraints
, "outerBounds", properties
);
659 //------------------------------------------------------------------------------
662 void AppWindow::UpdateBadgeIcon(const gfx::Image
& image
) {
664 native_app_window_
->UpdateBadgeIcon();
667 void AppWindow::DidDownloadFavicon(
669 int http_status_code
,
670 const GURL
& image_url
,
671 const std::vector
<SkBitmap
>& bitmaps
,
672 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
673 if ((image_url
!= app_icon_url_
&& image_url
!= badge_icon_url_
) ||
678 // Bitmaps are ordered largest to smallest. Choose the smallest bitmap
679 // whose height >= the preferred size.
680 int largest_index
= 0;
681 for (size_t i
= 1; i
< bitmaps
.size(); ++i
) {
682 if (bitmaps
[i
].height() < delegate_
->PreferredIconSize())
686 const SkBitmap
& largest
= bitmaps
[largest_index
];
687 if (image_url
== app_icon_url_
) {
688 UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest
));
692 UpdateBadgeIcon(gfx::Image::CreateFrom1xBitmap(largest
));
695 void AppWindow::OnExtensionIconImageChanged(extensions::IconImage
* image
) {
696 DCHECK_EQ(app_icon_image_
.get(), image
);
698 UpdateAppIcon(gfx::Image(app_icon_image_
->image_skia()));
701 void AppWindow::UpdateExtensionAppIcon() {
702 // Avoid using any previous app icons were being downloaded.
703 image_loader_ptr_factory_
.InvalidateWeakPtrs();
705 app_icon_image_
.reset(
706 new extensions::IconImage(browser_context(),
708 extensions::IconsInfo::GetIcons(extension()),
709 delegate_
->PreferredIconSize(),
710 extensions::IconsInfo::GetDefaultAppIcon(),
713 // Triggers actual image loading with 1x resources. The 2x resource will
714 // be handled by IconImage class when requested.
715 app_icon_image_
->image_skia().GetRepresentation(1.0f
);
718 void AppWindow::OnSizeConstraintsChanged() {
719 native_app_window_
->UpdateWindowMinMaxSize();
720 gfx::Rect bounds
= GetClientBounds();
721 gfx::Size constrained_size
= size_constraints_
.ClampSize(bounds
.size());
722 if (bounds
.size() != constrained_size
) {
723 bounds
.set_size(constrained_size
);
724 native_app_window_
->SetBounds(bounds
);
726 OnNativeWindowChanged();
729 void AppWindow::SetNativeWindowFullscreen() {
730 native_app_window_
->SetFullscreen(fullscreen_types_
);
732 if (cached_always_on_top_
)
733 UpdateNativeAlwaysOnTop();
736 bool AppWindow::IntersectsWithTaskbar() const {
738 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
739 gfx::Rect window_bounds
= native_app_window_
->GetRestoredBounds();
740 std::vector
<gfx::Display
> displays
= screen
->GetAllDisplays();
742 for (std::vector
<gfx::Display
>::const_iterator it
= displays
.begin();
743 it
!= displays
.end();
745 gfx::Rect taskbar_bounds
= it
->bounds();
746 taskbar_bounds
.Subtract(it
->work_area());
747 if (taskbar_bounds
.IsEmpty())
750 if (window_bounds
.Intersects(taskbar_bounds
))
758 void AppWindow::UpdateNativeAlwaysOnTop() {
759 DCHECK(cached_always_on_top_
);
760 bool is_on_top
= native_app_window_
->IsAlwaysOnTop();
761 bool fullscreen
= IsFullscreen(fullscreen_types_
);
762 bool intersects_taskbar
= IntersectsWithTaskbar();
764 if (is_on_top
&& (fullscreen
|| intersects_taskbar
)) {
765 // When entering fullscreen or overlapping the taskbar, ensure windows are
766 // not always-on-top.
767 native_app_window_
->SetAlwaysOnTop(false);
768 } else if (!is_on_top
&& !fullscreen
&& !intersects_taskbar
) {
769 // When exiting fullscreen and moving away from the taskbar, reinstate
771 native_app_window_
->SetAlwaysOnTop(true);
775 void AppWindow::CloseContents(WebContents
* contents
) {
776 native_app_window_
->Close();
779 bool AppWindow::ShouldSuppressDialogs() { return true; }
781 content::ColorChooser
* AppWindow::OpenColorChooser(
782 WebContents
* web_contents
,
783 SkColor initial_color
,
784 const std::vector
<content::ColorSuggestion
>& suggestionss
) {
785 return delegate_
->ShowColorChooser(web_contents
, initial_color
);
788 void AppWindow::RunFileChooser(WebContents
* tab
,
789 const content::FileChooserParams
& params
) {
790 if (window_type_is_panel()) {
791 // Panels can't host a file dialog, abort. TODO(stevenjb): allow file
792 // dialogs to be unhosted but still close with the owning web contents.
794 LOG(WARNING
) << "File dialog opened by panel.";
798 delegate_
->RunFileChooser(tab
, params
);
801 bool AppWindow::IsPopupOrPanel(const WebContents
* source
) const { return true; }
803 void AppWindow::MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {
804 native_app_window_
->SetBounds(pos
);
807 void AppWindow::NavigationStateChanged(const content::WebContents
* source
,
808 unsigned changed_flags
) {
809 if (changed_flags
& content::INVALIDATE_TYPE_TITLE
)
810 native_app_window_
->UpdateWindowTitle();
811 else if (changed_flags
& content::INVALIDATE_TYPE_TAB
)
812 native_app_window_
->UpdateWindowIcon();
815 void AppWindow::ToggleFullscreenModeForTab(content::WebContents
* source
,
816 bool enter_fullscreen
) {
817 #if !defined(OS_MACOSX)
818 // Do not enter fullscreen mode if disallowed by pref.
819 // TODO(bartfab): Add a test once it becomes possible to simulate a user
820 // gesture. http://crbug.com/174178
822 extensions::ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
824 if (enter_fullscreen
&& !prefs
->GetBoolean(prefs::kAppFullscreenAllowed
)) {
829 if (!IsExtensionWithPermissionOrSuggestInConsole(
830 APIPermission::kFullscreen
,
832 source
->GetRenderViewHost())) {
836 if (enter_fullscreen
)
837 fullscreen_types_
|= FULLSCREEN_TYPE_HTML_API
;
839 fullscreen_types_
&= ~FULLSCREEN_TYPE_HTML_API
;
840 SetNativeWindowFullscreen();
843 bool AppWindow::IsFullscreenForTabOrPending(const content::WebContents
* source
)
845 return ((fullscreen_types_
& FULLSCREEN_TYPE_HTML_API
) != 0);
848 void AppWindow::Observe(int type
,
849 const content::NotificationSource
& source
,
850 const content::NotificationDetails
& details
) {
852 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
853 const extensions::Extension
* unloaded_extension
=
854 content::Details
<extensions::UnloadedExtensionInfo
>(details
)
856 if (extension_
== unloaded_extension
)
857 native_app_window_
->Close();
860 case chrome::NOTIFICATION_EXTENSION_INSTALLED
: {
861 const extensions::Extension
* installed_extension
=
862 content::Details
<const extensions::InstalledExtensionInfo
>(details
)
864 DCHECK(installed_extension
);
865 if (installed_extension
->id() == extension_
->id())
866 native_app_window_
->UpdateShelfMenu();
869 case chrome::NOTIFICATION_APP_TERMINATING
:
870 native_app_window_
->Close();
873 NOTREACHED() << "Received unexpected notification";
877 void AppWindow::SetWebContentsBlocked(content::WebContents
* web_contents
,
879 delegate_
->SetWebContentsBlocked(web_contents
, blocked
);
882 bool AppWindow::IsWebContentsVisible(content::WebContents
* web_contents
) {
883 return delegate_
->IsWebContentsVisible(web_contents
);
886 extensions::ActiveTabPermissionGranter
*
887 AppWindow::GetActiveTabPermissionGranter() {
888 // App windows don't support the activeTab permission.
892 WebContentsModalDialogHost
* AppWindow::GetWebContentsModalDialogHost() {
893 return native_app_window_
.get();
896 void AppWindow::AddMessageToDevToolsConsole(ConsoleMessageLevel level
,
897 const std::string
& message
) {
898 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
899 rvh
->Send(new ExtensionMsg_AddMessageToConsole(
900 rvh
->GetRoutingID(), level
, message
));
903 void AppWindow::SaveWindowPosition() {
904 if (window_key_
.empty())
906 if (!native_app_window_
)
909 AppWindowGeometryCache
* cache
=
910 AppWindowGeometryCache::Get(browser_context());
912 gfx::Rect bounds
= native_app_window_
->GetRestoredBounds();
913 bounds
.Inset(native_app_window_
->GetFrameInsets());
914 gfx::Rect screen_bounds
=
915 gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds
).work_area();
916 ui::WindowShowState window_state
= native_app_window_
->GetRestoredState();
918 extension()->id(), window_key_
, bounds
, screen_bounds
, window_state
);
921 void AppWindow::AdjustBoundsToBeVisibleOnScreen(
922 const gfx::Rect
& cached_bounds
,
923 const gfx::Rect
& cached_screen_bounds
,
924 const gfx::Rect
& current_screen_bounds
,
925 const gfx::Size
& minimum_size
,
926 gfx::Rect
* bounds
) const {
927 *bounds
= cached_bounds
;
929 // Reposition and resize the bounds if the cached_screen_bounds is different
930 // from the current screen bounds and the current screen bounds doesn't
931 // completely contain the bounds.
932 if (cached_screen_bounds
!= current_screen_bounds
&&
933 !current_screen_bounds
.Contains(cached_bounds
)) {
935 std::max(minimum_size
.width(),
936 std::min(bounds
->width(), current_screen_bounds
.width())));
938 std::max(minimum_size
.height(),
939 std::min(bounds
->height(), current_screen_bounds
.height())));
941 std::max(current_screen_bounds
.x(),
942 std::min(bounds
->x(),
943 current_screen_bounds
.right() - bounds
->width())));
945 std::max(current_screen_bounds
.y(),
946 std::min(bounds
->y(),
947 current_screen_bounds
.bottom() - bounds
->height())));
951 AppWindow::CreateParams
AppWindow::LoadDefaultsAndConstrain(CreateParams params
)
953 if (params
.bounds
.width() == 0)
954 params
.bounds
.set_width(kDefaultWidth
);
955 if (params
.bounds
.height() == 0)
956 params
.bounds
.set_height(kDefaultHeight
);
958 // If left and top are left undefined, the native app window will center
959 // the window on the main screen in a platform-defined manner.
961 // Load cached state if it exists.
962 if (!params
.window_key
.empty()) {
963 AppWindowGeometryCache
* cache
=
964 AppWindowGeometryCache::Get(browser_context());
966 gfx::Rect cached_bounds
;
967 gfx::Rect cached_screen_bounds
;
968 ui::WindowShowState cached_state
= ui::SHOW_STATE_DEFAULT
;
969 if (cache
->GetGeometry(extension()->id(),
972 &cached_screen_bounds
,
974 // App window has cached screen bounds, make sure it fits on screen in
975 // case the screen resolution changed.
976 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
977 gfx::Display display
= screen
->GetDisplayMatching(cached_bounds
);
978 gfx::Rect current_screen_bounds
= display
.work_area();
979 AdjustBoundsToBeVisibleOnScreen(cached_bounds
,
980 cached_screen_bounds
,
981 current_screen_bounds
,
984 params
.state
= cached_state
;
988 SizeConstraints
size_constraints(params
.minimum_size
, params
.maximum_size
);
989 params
.bounds
.set_size(size_constraints
.ClampSize(params
.bounds
.size()));
990 params
.minimum_size
= size_constraints
.GetMinimumSize();
991 params
.maximum_size
= size_constraints
.GetMaximumSize();
997 SkRegion
* AppWindow::RawDraggableRegionsToSkRegion(
998 const std::vector
<extensions::DraggableRegion
>& regions
) {
999 SkRegion
* sk_region
= new SkRegion
;
1000 for (std::vector
<extensions::DraggableRegion
>::const_iterator iter
=
1002 iter
!= regions
.end();
1004 const extensions::DraggableRegion
& region
= *iter
;
1008 region
.bounds
.right(),
1009 region
.bounds
.bottom(),
1010 region
.draggable
? SkRegion::kUnion_Op
: SkRegion::kDifference_Op
);