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 "extensions/browser/app_window/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 "components/web_modal/web_contents_modal_dialog_manager.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/invalidate_type.h"
18 #include "content/public/browser/navigation_entry.h"
19 #include "content/public/browser/render_view_host.h"
20 #include "content/public/browser/resource_dispatcher_host.h"
21 #include "content/public/browser/web_contents.h"
22 #include "content/public/common/content_switches.h"
23 #include "content/public/common/media_stream_request.h"
24 #include "extensions/browser/app_window/app_delegate.h"
25 #include "extensions/browser/app_window/app_web_contents_helper.h"
26 #include "extensions/browser/app_window/app_window_client.h"
27 #include "extensions/browser/app_window/app_window_geometry_cache.h"
28 #include "extensions/browser/app_window/app_window_registry.h"
29 #include "extensions/browser/app_window/native_app_window.h"
30 #include "extensions/browser/app_window/size_constraints.h"
31 #include "extensions/browser/extension_registry.h"
32 #include "extensions/browser/extension_system.h"
33 #include "extensions/browser/extensions_browser_client.h"
34 #include "extensions/browser/notification_types.h"
35 #include "extensions/browser/process_manager.h"
36 #include "extensions/browser/suggest_permission_util.h"
37 #include "extensions/browser/view_type_utils.h"
38 #include "extensions/common/draggable_region.h"
39 #include "extensions/common/extension.h"
40 #include "extensions/common/manifest_handlers/icons_handler.h"
41 #include "extensions/common/permissions/permissions_data.h"
42 #include "extensions/common/switches.h"
43 #include "extensions/grit/extensions_browser_resources.h"
44 #include "third_party/skia/include/core/SkRegion.h"
45 #include "ui/base/resource/resource_bundle.h"
46 #include "ui/gfx/screen.h"
48 #if !defined(OS_MACOSX)
49 #include "base/prefs/pref_service.h"
50 #include "extensions/browser/pref_names.h"
53 using content::BrowserContext
;
54 using content::ConsoleMessageLevel
;
55 using content::WebContents
;
56 using web_modal::WebContentsModalDialogHost
;
57 using web_modal::WebContentsModalDialogManager
;
59 namespace extensions
{
63 const int kDefaultWidth
= 512;
64 const int kDefaultHeight
= 384;
66 void SetConstraintProperty(const std::string
& name
,
68 base::DictionaryValue
* bounds_properties
) {
69 if (value
!= SizeConstraints::kUnboundedSize
)
70 bounds_properties
->SetInteger(name
, value
);
72 bounds_properties
->Set(name
, base::Value::CreateNullValue());
75 void SetBoundsProperties(const gfx::Rect
& bounds
,
76 const gfx::Size
& min_size
,
77 const gfx::Size
& max_size
,
78 const std::string
& bounds_name
,
79 base::DictionaryValue
* window_properties
) {
80 scoped_ptr
<base::DictionaryValue
> bounds_properties(
81 new base::DictionaryValue());
83 bounds_properties
->SetInteger("left", bounds
.x());
84 bounds_properties
->SetInteger("top", bounds
.y());
85 bounds_properties
->SetInteger("width", bounds
.width());
86 bounds_properties
->SetInteger("height", bounds
.height());
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());
98 // Combines the constraints of the content and window, and returns constraints
100 gfx::Size
GetCombinedWindowConstraints(const gfx::Size
& window_constraints
,
101 const gfx::Size
& content_constraints
,
102 const gfx::Insets
& frame_insets
) {
103 gfx::Size
combined_constraints(window_constraints
);
104 if (content_constraints
.width() > 0) {
105 combined_constraints
.set_width(
106 content_constraints
.width() + frame_insets
.width());
108 if (content_constraints
.height() > 0) {
109 combined_constraints
.set_height(
110 content_constraints
.height() + frame_insets
.height());
112 return combined_constraints
;
115 // Combines the constraints of the content and window, and returns constraints
117 gfx::Size
GetCombinedContentConstraints(const gfx::Size
& window_constraints
,
118 const gfx::Size
& content_constraints
,
119 const gfx::Insets
& frame_insets
) {
120 gfx::Size
combined_constraints(content_constraints
);
121 if (window_constraints
.width() > 0) {
122 combined_constraints
.set_width(
123 std::max(0, window_constraints
.width() - frame_insets
.width()));
125 if (window_constraints
.height() > 0) {
126 combined_constraints
.set_height(
127 std::max(0, window_constraints
.height() - frame_insets
.height()));
129 return combined_constraints
;
134 // AppWindow::BoundsSpecification
136 const int AppWindow::BoundsSpecification::kUnspecifiedPosition
= INT_MIN
;
138 AppWindow::BoundsSpecification::BoundsSpecification()
139 : bounds(kUnspecifiedPosition
, kUnspecifiedPosition
, 0, 0) {}
141 AppWindow::BoundsSpecification::~BoundsSpecification() {}
143 void AppWindow::BoundsSpecification::ResetBounds() {
144 bounds
.SetRect(kUnspecifiedPosition
, kUnspecifiedPosition
, 0, 0);
147 // AppWindow::CreateParams
149 AppWindow::CreateParams::CreateParams()
150 : window_type(AppWindow::WINDOW_TYPE_DEFAULT
),
151 frame(AppWindow::FRAME_CHROME
),
152 has_frame_color(false),
153 active_frame_color(SK_ColorBLACK
),
154 inactive_frame_color(SK_ColorBLACK
),
155 alpha_enabled(false),
156 is_ime_window(false),
157 creator_process_id(0),
158 state(ui::SHOW_STATE_DEFAULT
),
162 always_on_top(false),
163 visible_on_all_workspaces(false) {
166 AppWindow::CreateParams::~CreateParams() {}
168 gfx::Rect
AppWindow::CreateParams::GetInitialWindowBounds(
169 const gfx::Insets
& frame_insets
) const {
170 // Combine into a single window bounds.
171 gfx::Rect
combined_bounds(window_spec
.bounds
);
172 if (content_spec
.bounds
.x() != BoundsSpecification::kUnspecifiedPosition
)
173 combined_bounds
.set_x(content_spec
.bounds
.x() - frame_insets
.left());
174 if (content_spec
.bounds
.y() != BoundsSpecification::kUnspecifiedPosition
)
175 combined_bounds
.set_y(content_spec
.bounds
.y() - frame_insets
.top());
176 if (content_spec
.bounds
.width() > 0) {
177 combined_bounds
.set_width(
178 content_spec
.bounds
.width() + frame_insets
.width());
180 if (content_spec
.bounds
.height() > 0) {
181 combined_bounds
.set_height(
182 content_spec
.bounds
.height() + frame_insets
.height());
185 // Constrain the bounds.
186 SizeConstraints
constraints(
187 GetCombinedWindowConstraints(
188 window_spec
.minimum_size
, content_spec
.minimum_size
, frame_insets
),
189 GetCombinedWindowConstraints(
190 window_spec
.maximum_size
, content_spec
.maximum_size
, frame_insets
));
191 combined_bounds
.set_size(constraints
.ClampSize(combined_bounds
.size()));
193 return combined_bounds
;
196 gfx::Size
AppWindow::CreateParams::GetContentMinimumSize(
197 const gfx::Insets
& frame_insets
) const {
198 return GetCombinedContentConstraints(window_spec
.minimum_size
,
199 content_spec
.minimum_size
,
203 gfx::Size
AppWindow::CreateParams::GetContentMaximumSize(
204 const gfx::Insets
& frame_insets
) const {
205 return GetCombinedContentConstraints(window_spec
.maximum_size
,
206 content_spec
.maximum_size
,
210 gfx::Size
AppWindow::CreateParams::GetWindowMinimumSize(
211 const gfx::Insets
& frame_insets
) const {
212 return GetCombinedWindowConstraints(window_spec
.minimum_size
,
213 content_spec
.minimum_size
,
217 gfx::Size
AppWindow::CreateParams::GetWindowMaximumSize(
218 const gfx::Insets
& frame_insets
) const {
219 return GetCombinedWindowConstraints(window_spec
.maximum_size
,
220 content_spec
.maximum_size
,
226 AppWindow::AppWindow(BrowserContext
* context
,
227 AppDelegate
* app_delegate
,
228 const Extension
* extension
)
229 : browser_context_(context
),
230 extension_id_(extension
->id()),
231 window_type_(WINDOW_TYPE_DEFAULT
),
232 app_delegate_(app_delegate
),
233 fullscreen_types_(FULLSCREEN_TYPE_NONE
),
234 show_on_first_paint_(false),
235 first_paint_complete_(false),
236 has_been_shown_(false),
237 can_send_events_(false),
239 cached_always_on_top_(false),
240 requested_alpha_enabled_(false),
241 is_ime_window_(false),
242 image_loader_ptr_factory_(this) {
243 ExtensionsBrowserClient
* client
= ExtensionsBrowserClient::Get();
244 CHECK(!client
->IsGuestSession(context
) || context
->IsOffTheRecord())
245 << "Only off the record window may be opened in the guest mode.";
248 void AppWindow::Init(const GURL
& url
,
249 AppWindowContents
* app_window_contents
,
250 const CreateParams
& params
) {
251 // Initialize the render interface and web contents
252 app_window_contents_
.reset(app_window_contents
);
253 app_window_contents_
->Initialize(browser_context(), url
);
255 content::WebContentsObserver::Observe(web_contents());
256 app_delegate_
->InitWebContents(web_contents());
258 WebContentsModalDialogManager::CreateForWebContents(web_contents());
260 web_contents()->SetDelegate(this);
261 WebContentsModalDialogManager::FromWebContents(web_contents())
263 SetViewType(web_contents(), VIEW_TYPE_APP_WINDOW
);
265 // Initialize the window
266 CreateParams new_params
= LoadDefaults(params
);
267 window_type_
= new_params
.window_type
;
268 window_key_
= new_params
.window_key
;
270 // Windows cannot be always-on-top in fullscreen mode for security reasons.
271 cached_always_on_top_
= new_params
.always_on_top
;
272 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
273 new_params
.always_on_top
= false;
275 requested_alpha_enabled_
= new_params
.alpha_enabled
;
277 is_ime_window_
= params
.is_ime_window
;
279 AppWindowClient
* app_window_client
= AppWindowClient::Get();
280 native_app_window_
.reset(
281 app_window_client
->CreateNativeAppWindow(this, &new_params
));
283 helper_
.reset(new AppWebContentsHelper(
284 browser_context_
, extension_id_
, web_contents(), app_delegate_
.get()));
286 popup_manager_
.reset(
287 new web_modal::PopupManager(GetWebContentsModalDialogHost()));
288 popup_manager_
->RegisterWith(web_contents());
290 UpdateExtensionAppIcon();
291 AppWindowRegistry::Get(browser_context_
)->AddAppWindow(this);
293 if (new_params
.hidden
) {
294 // Although the window starts hidden by default, calling Hide() here
295 // notifies observers of the window being hidden.
298 // Panels are not activated by default.
299 Show(window_type_is_panel() || !new_params
.focused
? SHOW_INACTIVE
302 // These states may cause the window to show, so they are ignored if the
303 // window is initially hidden.
304 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
306 else if (new_params
.state
== ui::SHOW_STATE_MAXIMIZED
)
308 else if (new_params
.state
== ui::SHOW_STATE_MINIMIZED
)
312 OnNativeWindowChanged();
314 ExtensionRegistry::Get(browser_context_
)->AddObserver(this);
316 // Close when the browser process is exiting.
317 app_delegate_
->SetTerminatingCallback(
318 base::Bind(&NativeAppWindow::Close
,
319 base::Unretained(native_app_window_
.get())));
321 app_window_contents_
->LoadContents(new_params
.creator_process_id
);
323 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
324 extensions::switches::kEnableAppsShowOnFirstPaint
)) {
325 // We want to show the window only when the content has been painted. For
326 // that to happen, we need to define a size for the content, otherwise the
327 // layout will happen in a 0x0 area.
328 gfx::Insets frame_insets
= native_app_window_
->GetFrameInsets();
329 gfx::Rect initial_bounds
= new_params
.GetInitialWindowBounds(frame_insets
);
330 initial_bounds
.Inset(frame_insets
);
331 app_delegate_
->ResizeWebContents(web_contents(), initial_bounds
.size());
335 AppWindow::~AppWindow() {
336 ExtensionRegistry::Get(browser_context_
)->RemoveObserver(this);
339 void AppWindow::RequestMediaAccessPermission(
340 content::WebContents
* web_contents
,
341 const content::MediaStreamRequest
& request
,
342 const content::MediaResponseCallback
& callback
) {
343 DCHECK_EQ(AppWindow::web_contents(), web_contents
);
344 helper_
->RequestMediaAccessPermission(request
, callback
);
347 bool AppWindow::CheckMediaAccessPermission(content::WebContents
* web_contents
,
348 const GURL
& security_origin
,
349 content::MediaStreamType type
) {
350 DCHECK_EQ(AppWindow::web_contents(), web_contents
);
351 return helper_
->CheckMediaAccessPermission(security_origin
, type
);
354 WebContents
* AppWindow::OpenURLFromTab(WebContents
* source
,
355 const content::OpenURLParams
& params
) {
356 DCHECK_EQ(web_contents(), source
);
357 return helper_
->OpenURLFromTab(params
);
360 void AppWindow::AddNewContents(WebContents
* source
,
361 WebContents
* new_contents
,
362 WindowOpenDisposition disposition
,
363 const gfx::Rect
& initial_rect
,
366 DCHECK(new_contents
->GetBrowserContext() == browser_context_
);
367 app_delegate_
->AddNewContents(browser_context_
,
375 bool AppWindow::PreHandleKeyboardEvent(
376 content::WebContents
* source
,
377 const content::NativeWebKeyboardEvent
& event
,
378 bool* is_keyboard_shortcut
) {
379 const Extension
* extension
= GetExtension();
383 // Here, we can handle a key event before the content gets it. When we are
384 // fullscreen and it is not forced, we want to allow the user to leave
385 // when ESC is pressed.
386 // However, if the application has the "overrideEscFullscreen" permission, we
387 // should let it override that behavior.
388 // ::HandleKeyboardEvent() will only be called if the KeyEvent's default
389 // action is not prevented.
390 // Thus, we should handle the KeyEvent here only if the permission is not set.
391 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&& IsFullscreen() &&
392 !IsForcedFullscreen() &&
393 !extension
->permissions_data()->HasAPIPermission(
394 APIPermission::kOverrideEscFullscreen
)) {
402 void AppWindow::HandleKeyboardEvent(
404 const content::NativeWebKeyboardEvent
& event
) {
405 // If the window is currently fullscreen and not forced, ESC should leave
406 // fullscreen. If this code is being called for ESC, that means that the
407 // KeyEvent's default behavior was not prevented by the content.
408 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&& IsFullscreen() &&
409 !IsForcedFullscreen()) {
414 native_app_window_
->HandleKeyboardEvent(event
);
417 void AppWindow::RequestToLockMouse(WebContents
* web_contents
,
419 bool last_unlocked_by_target
) {
420 DCHECK_EQ(AppWindow::web_contents(), web_contents
);
421 helper_
->RequestToLockMouse();
424 bool AppWindow::PreHandleGestureEvent(WebContents
* source
,
425 const blink::WebGestureEvent
& event
) {
426 return AppWebContentsHelper::ShouldSuppressGestureEvent(event
);
429 void AppWindow::RenderViewCreated(content::RenderViewHost
* render_view_host
) {
430 app_delegate_
->RenderViewCreated(render_view_host
);
433 void AppWindow::DidFirstVisuallyNonEmptyPaint() {
434 first_paint_complete_
= true;
435 if (show_on_first_paint_
) {
436 DCHECK(delayed_show_type_
== SHOW_ACTIVE
||
437 delayed_show_type_
== SHOW_INACTIVE
);
438 Show(delayed_show_type_
);
442 void AppWindow::OnNativeClose() {
443 AppWindowRegistry::Get(browser_context_
)->RemoveAppWindow(this);
444 if (app_window_contents_
) {
445 WebContentsModalDialogManager
* modal_dialog_manager
=
446 WebContentsModalDialogManager::FromWebContents(web_contents());
447 if (modal_dialog_manager
) // May be null in unit tests.
448 modal_dialog_manager
->SetDelegate(nullptr);
449 app_window_contents_
->NativeWindowClosed();
454 void AppWindow::OnNativeWindowChanged() {
455 SaveWindowPosition();
458 if (native_app_window_
&& cached_always_on_top_
&& !IsFullscreen() &&
459 !native_app_window_
->IsMaximized() &&
460 !native_app_window_
->IsMinimized()) {
461 UpdateNativeAlwaysOnTop();
465 if (app_window_contents_
&& native_app_window_
)
466 app_window_contents_
->NativeWindowChanged(native_app_window_
.get());
469 void AppWindow::OnNativeWindowActivated() {
470 AppWindowRegistry::Get(browser_context_
)->AppWindowActivated(this);
473 content::WebContents
* AppWindow::web_contents() const {
474 return app_window_contents_
->GetWebContents();
477 const Extension
* AppWindow::GetExtension() const {
478 return ExtensionRegistry::Get(browser_context_
)
479 ->enabled_extensions()
480 .GetByID(extension_id_
);
483 NativeAppWindow
* AppWindow::GetBaseWindow() { return native_app_window_
.get(); }
485 gfx::NativeWindow
AppWindow::GetNativeWindow() {
486 return GetBaseWindow()->GetNativeWindow();
489 gfx::Rect
AppWindow::GetClientBounds() const {
490 gfx::Rect bounds
= native_app_window_
->GetBounds();
491 bounds
.Inset(native_app_window_
->GetFrameInsets());
495 base::string16
AppWindow::GetTitle() const {
496 const Extension
* extension
= GetExtension();
498 return base::string16();
500 // WebContents::GetTitle() will return the page's URL if there's no <title>
501 // specified. However, we'd prefer to show the name of the extension in that
502 // case, so we directly inspect the NavigationEntry's title.
503 base::string16 title
;
504 if (!web_contents() || !web_contents()->GetController().GetActiveEntry() ||
505 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) {
506 title
= base::UTF8ToUTF16(extension
->name());
508 title
= web_contents()->GetTitle();
510 base::RemoveChars(title
, base::ASCIIToUTF16("\n"), &title
);
514 void AppWindow::SetAppIconUrl(const GURL
& url
) {
515 // If the same url is being used for the badge, ignore it.
516 if (url
== badge_icon_url_
)
519 // Avoid using any previous icons that were being downloaded.
520 image_loader_ptr_factory_
.InvalidateWeakPtrs();
522 // Reset |app_icon_image_| to abort pending image load (if any).
523 app_icon_image_
.reset();
526 web_contents()->DownloadImage(
528 true, // is a favicon
529 0, // no maximum size
530 base::Bind(&AppWindow::DidDownloadFavicon
,
531 image_loader_ptr_factory_
.GetWeakPtr()));
534 void AppWindow::SetBadgeIconUrl(const GURL
& url
) {
535 // Avoid using any previous icons that were being downloaded.
536 image_loader_ptr_factory_
.InvalidateWeakPtrs();
538 // Reset |app_icon_image_| to abort pending image load (if any).
539 badge_icon_image_
.reset();
541 badge_icon_url_
= url
;
542 web_contents()->DownloadImage(
544 true, // is a favicon
545 0, // no maximum size
546 base::Bind(&AppWindow::DidDownloadFavicon
,
547 image_loader_ptr_factory_
.GetWeakPtr()));
550 void AppWindow::ClearBadge() {
551 badge_icon_image_
.reset();
552 badge_icon_url_
= GURL();
553 UpdateBadgeIcon(gfx::Image());
556 void AppWindow::UpdateShape(scoped_ptr
<SkRegion
> region
) {
557 native_app_window_
->UpdateShape(region
.Pass());
560 void AppWindow::UpdateDraggableRegions(
561 const std::vector
<DraggableRegion
>& regions
) {
562 native_app_window_
->UpdateDraggableRegions(regions
);
565 void AppWindow::UpdateAppIcon(const gfx::Image
& image
) {
569 native_app_window_
->UpdateWindowIcon();
570 AppWindowRegistry::Get(browser_context_
)->AppWindowIconChanged(this);
573 void AppWindow::SetFullscreen(FullscreenType type
, bool enable
) {
574 DCHECK_NE(FULLSCREEN_TYPE_NONE
, type
);
577 #if !defined(OS_MACOSX)
578 // Do not enter fullscreen mode if disallowed by pref.
579 // TODO(bartfab): Add a test once it becomes possible to simulate a user
580 // gesture. http://crbug.com/174178
581 if (type
!= FULLSCREEN_TYPE_FORCED
) {
583 ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
585 if (!prefs
->GetBoolean(pref_names::kAppFullscreenAllowed
))
589 fullscreen_types_
|= type
;
591 fullscreen_types_
&= ~type
;
593 SetNativeWindowFullscreen();
596 bool AppWindow::IsFullscreen() const {
597 return fullscreen_types_
!= FULLSCREEN_TYPE_NONE
;
600 bool AppWindow::IsForcedFullscreen() const {
601 return (fullscreen_types_
& FULLSCREEN_TYPE_FORCED
) != 0;
604 bool AppWindow::IsHtmlApiFullscreen() const {
605 return (fullscreen_types_
& FULLSCREEN_TYPE_HTML_API
) != 0;
608 void AppWindow::Fullscreen() {
609 SetFullscreen(FULLSCREEN_TYPE_WINDOW_API
, true);
612 void AppWindow::Maximize() { GetBaseWindow()->Maximize(); }
614 void AppWindow::Minimize() { GetBaseWindow()->Minimize(); }
616 void AppWindow::Restore() {
617 if (IsFullscreen()) {
618 fullscreen_types_
= FULLSCREEN_TYPE_NONE
;
619 SetNativeWindowFullscreen();
621 GetBaseWindow()->Restore();
625 void AppWindow::OSFullscreen() {
626 SetFullscreen(FULLSCREEN_TYPE_OS
, true);
629 void AppWindow::ForcedFullscreen() {
630 SetFullscreen(FULLSCREEN_TYPE_FORCED
, true);
633 void AppWindow::SetContentSizeConstraints(const gfx::Size
& min_size
,
634 const gfx::Size
& max_size
) {
635 SizeConstraints
constraints(min_size
, max_size
);
636 native_app_window_
->SetContentSizeConstraints(constraints
.GetMinimumSize(),
637 constraints
.GetMaximumSize());
639 gfx::Rect bounds
= GetClientBounds();
640 gfx::Size constrained_size
= constraints
.ClampSize(bounds
.size());
641 if (bounds
.size() != constrained_size
) {
642 bounds
.set_size(constrained_size
);
643 bounds
.Inset(-native_app_window_
->GetFrameInsets());
644 native_app_window_
->SetBounds(bounds
);
646 OnNativeWindowChanged();
649 void AppWindow::Show(ShowType show_type
) {
650 app_delegate_
->OnShow();
651 bool was_hidden
= is_hidden_
|| !has_been_shown_
;
654 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
655 switches::kEnableAppsShowOnFirstPaint
)) {
656 show_on_first_paint_
= true;
658 if (!first_paint_complete_
) {
659 delayed_show_type_
= show_type
;
666 GetBaseWindow()->Show();
669 GetBaseWindow()->ShowInactive();
672 AppWindowRegistry::Get(browser_context_
)->AppWindowShown(this, was_hidden
);
674 has_been_shown_
= true;
675 SendOnWindowShownIfShown();
678 void AppWindow::Hide() {
679 // This is there to prevent race conditions with Hide() being called before
680 // there was a non-empty paint. It should have no effect in a non-racy
681 // scenario where the application is hiding then showing a window: the second
682 // show will not be delayed.
684 show_on_first_paint_
= false;
685 GetBaseWindow()->Hide();
686 AppWindowRegistry::Get(browser_context_
)->AppWindowHidden(this);
687 app_delegate_
->OnHide();
690 void AppWindow::SetAlwaysOnTop(bool always_on_top
) {
691 if (cached_always_on_top_
== always_on_top
)
694 cached_always_on_top_
= always_on_top
;
696 // As a security measure, do not allow fullscreen windows or windows that
697 // overlap the taskbar to be on top. The property will be applied when the
698 // window exits fullscreen and moves away from the taskbar.
699 if (!IsFullscreen() && !IntersectsWithTaskbar())
700 native_app_window_
->SetAlwaysOnTop(always_on_top
);
702 OnNativeWindowChanged();
705 bool AppWindow::IsAlwaysOnTop() const { return cached_always_on_top_
; }
707 void AppWindow::SetInterceptAllKeys(bool want_all_keys
) {
708 native_app_window_
->SetInterceptAllKeys(want_all_keys
);
711 void AppWindow::WindowEventsReady() {
712 can_send_events_
= true;
713 SendOnWindowShownIfShown();
716 void AppWindow::GetSerializedState(base::DictionaryValue
* properties
) const {
719 properties
->SetBoolean("fullscreen",
720 native_app_window_
->IsFullscreenOrPending());
721 properties
->SetBoolean("minimized", native_app_window_
->IsMinimized());
722 properties
->SetBoolean("maximized", native_app_window_
->IsMaximized());
723 properties
->SetBoolean("alwaysOnTop", IsAlwaysOnTop());
724 properties
->SetBoolean("hasFrameColor", native_app_window_
->HasFrameColor());
725 properties
->SetBoolean(
727 requested_alpha_enabled_
&& native_app_window_
->CanHaveAlphaEnabled());
729 // These properties are undocumented and are to enable testing. Alpha is
731 // make the values easier to check.
732 SkColor transparent_white
= ~SK_ColorBLACK
;
733 properties
->SetInteger(
735 native_app_window_
->ActiveFrameColor() & transparent_white
);
736 properties
->SetInteger(
737 "inactiveFrameColor",
738 native_app_window_
->InactiveFrameColor() & transparent_white
);
740 gfx::Rect content_bounds
= GetClientBounds();
741 gfx::Size content_min_size
= native_app_window_
->GetContentMinimumSize();
742 gfx::Size content_max_size
= native_app_window_
->GetContentMaximumSize();
743 SetBoundsProperties(content_bounds
,
749 gfx::Insets frame_insets
= native_app_window_
->GetFrameInsets();
750 gfx::Rect frame_bounds
= native_app_window_
->GetBounds();
751 gfx::Size frame_min_size
= SizeConstraints::AddFrameToConstraints(
752 content_min_size
, frame_insets
);
753 gfx::Size frame_max_size
= SizeConstraints::AddFrameToConstraints(
754 content_max_size
, frame_insets
);
755 SetBoundsProperties(frame_bounds
,
762 //------------------------------------------------------------------------------
765 void AppWindow::UpdateBadgeIcon(const gfx::Image
& image
) {
767 native_app_window_
->UpdateBadgeIcon();
770 void AppWindow::DidDownloadFavicon(
772 int http_status_code
,
773 const GURL
& image_url
,
774 const std::vector
<SkBitmap
>& bitmaps
,
775 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
776 if ((image_url
!= app_icon_url_
&& image_url
!= badge_icon_url_
) ||
781 // Bitmaps are ordered largest to smallest. Choose the smallest bitmap
782 // whose height >= the preferred size.
783 int largest_index
= 0;
784 for (size_t i
= 1; i
< bitmaps
.size(); ++i
) {
785 if (bitmaps
[i
].height() < app_delegate_
->PreferredIconSize())
789 const SkBitmap
& largest
= bitmaps
[largest_index
];
790 if (image_url
== app_icon_url_
) {
791 UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest
));
795 UpdateBadgeIcon(gfx::Image::CreateFrom1xBitmap(largest
));
798 void AppWindow::OnExtensionIconImageChanged(IconImage
* image
) {
799 DCHECK_EQ(app_icon_image_
.get(), image
);
801 UpdateAppIcon(gfx::Image(app_icon_image_
->image_skia()));
804 void AppWindow::UpdateExtensionAppIcon() {
805 // Avoid using any previous app icons were being downloaded.
806 image_loader_ptr_factory_
.InvalidateWeakPtrs();
808 const Extension
* extension
= GetExtension();
812 gfx::ImageSkia app_default_icon
=
813 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
814 IDR_APP_DEFAULT_ICON
);
816 app_icon_image_
.reset(new IconImage(browser_context(),
818 IconsInfo::GetIcons(extension
),
819 app_delegate_
->PreferredIconSize(),
823 // Triggers actual image loading with 1x resources. The 2x resource will
824 // be handled by IconImage class when requested.
825 app_icon_image_
->image_skia().GetRepresentation(1.0f
);
828 void AppWindow::SetNativeWindowFullscreen() {
829 native_app_window_
->SetFullscreen(fullscreen_types_
);
831 if (cached_always_on_top_
)
832 UpdateNativeAlwaysOnTop();
835 bool AppWindow::IntersectsWithTaskbar() const {
837 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
838 gfx::Rect window_bounds
= native_app_window_
->GetRestoredBounds();
839 std::vector
<gfx::Display
> displays
= screen
->GetAllDisplays();
841 for (std::vector
<gfx::Display
>::const_iterator it
= displays
.begin();
842 it
!= displays
.end();
844 gfx::Rect taskbar_bounds
= it
->bounds();
845 taskbar_bounds
.Subtract(it
->work_area());
846 if (taskbar_bounds
.IsEmpty())
849 if (window_bounds
.Intersects(taskbar_bounds
))
857 void AppWindow::UpdateNativeAlwaysOnTop() {
858 DCHECK(cached_always_on_top_
);
859 bool is_on_top
= native_app_window_
->IsAlwaysOnTop();
860 bool fullscreen
= IsFullscreen();
861 bool intersects_taskbar
= IntersectsWithTaskbar();
863 if (is_on_top
&& (fullscreen
|| intersects_taskbar
)) {
864 // When entering fullscreen or overlapping the taskbar, ensure windows are
865 // not always-on-top.
866 native_app_window_
->SetAlwaysOnTop(false);
867 } else if (!is_on_top
&& !fullscreen
&& !intersects_taskbar
) {
868 // When exiting fullscreen and moving away from the taskbar, reinstate
870 native_app_window_
->SetAlwaysOnTop(true);
874 void AppWindow::SendOnWindowShownIfShown() {
875 if (!can_send_events_
|| !has_been_shown_
)
878 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
879 ::switches::kTestType
)) {
880 app_window_contents_
->DispatchWindowShownForTests();
884 void AppWindow::CloseContents(WebContents
* contents
) {
885 native_app_window_
->Close();
888 bool AppWindow::ShouldSuppressDialogs(WebContents
* source
) {
892 content::ColorChooser
* AppWindow::OpenColorChooser(
893 WebContents
* web_contents
,
894 SkColor initial_color
,
895 const std::vector
<content::ColorSuggestion
>& suggestions
) {
896 return app_delegate_
->ShowColorChooser(web_contents
, initial_color
);
899 void AppWindow::RunFileChooser(WebContents
* tab
,
900 const content::FileChooserParams
& params
) {
901 if (window_type_is_panel()) {
902 // Panels can't host a file dialog, abort. TODO(stevenjb): allow file
903 // dialogs to be unhosted but still close with the owning web contents.
905 LOG(WARNING
) << "File dialog opened by panel.";
909 app_delegate_
->RunFileChooser(tab
, params
);
912 bool AppWindow::IsPopupOrPanel(const WebContents
* source
) const { return true; }
914 void AppWindow::MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {
915 native_app_window_
->SetBounds(pos
);
918 void AppWindow::NavigationStateChanged(content::WebContents
* source
,
919 content::InvalidateTypes changed_flags
) {
920 if (changed_flags
& content::INVALIDATE_TYPE_TITLE
)
921 native_app_window_
->UpdateWindowTitle();
922 else if (changed_flags
& content::INVALIDATE_TYPE_TAB
)
923 native_app_window_
->UpdateWindowIcon();
926 void AppWindow::EnterFullscreenModeForTab(content::WebContents
* source
,
927 const GURL
& origin
) {
928 ToggleFullscreenModeForTab(source
, true);
931 void AppWindow::ExitFullscreenModeForTab(content::WebContents
* source
) {
932 ToggleFullscreenModeForTab(source
, false);
935 void AppWindow::ToggleFullscreenModeForTab(content::WebContents
* source
,
936 bool enter_fullscreen
) {
937 const Extension
* extension
= GetExtension();
941 if (!IsExtensionWithPermissionOrSuggestInConsole(
942 APIPermission::kFullscreen
, extension
, source
->GetRenderViewHost())) {
946 SetFullscreen(FULLSCREEN_TYPE_HTML_API
, enter_fullscreen
);
949 bool AppWindow::IsFullscreenForTabOrPending(const content::WebContents
* source
)
951 return IsHtmlApiFullscreen();
954 void AppWindow::OnExtensionUnloaded(BrowserContext
* browser_context
,
955 const Extension
* extension
,
956 UnloadedExtensionInfo::Reason reason
) {
957 if (extension_id_
== extension
->id())
958 native_app_window_
->Close();
961 void AppWindow::OnExtensionWillBeInstalled(
962 BrowserContext
* browser_context
,
963 const Extension
* extension
,
966 const std::string
& old_name
) {
967 if (extension
->id() == extension_id())
968 native_app_window_
->UpdateShelfMenu();
970 void AppWindow::SetWebContentsBlocked(content::WebContents
* web_contents
,
972 app_delegate_
->SetWebContentsBlocked(web_contents
, blocked
);
975 bool AppWindow::IsWebContentsVisible(content::WebContents
* web_contents
) {
976 return app_delegate_
->IsWebContentsVisible(web_contents
);
979 WebContentsModalDialogHost
* AppWindow::GetWebContentsModalDialogHost() {
980 return native_app_window_
.get();
983 void AppWindow::SaveWindowPosition() {
984 if (window_key_
.empty())
986 if (!native_app_window_
)
989 AppWindowGeometryCache
* cache
=
990 AppWindowGeometryCache::Get(browser_context());
992 gfx::Rect bounds
= native_app_window_
->GetRestoredBounds();
993 gfx::Rect screen_bounds
=
994 gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds
).work_area();
995 ui::WindowShowState window_state
= native_app_window_
->GetRestoredState();
997 extension_id(), window_key_
, bounds
, screen_bounds
, window_state
);
1000 void AppWindow::AdjustBoundsToBeVisibleOnScreen(
1001 const gfx::Rect
& cached_bounds
,
1002 const gfx::Rect
& cached_screen_bounds
,
1003 const gfx::Rect
& current_screen_bounds
,
1004 const gfx::Size
& minimum_size
,
1005 gfx::Rect
* bounds
) const {
1006 *bounds
= cached_bounds
;
1008 // Reposition and resize the bounds if the cached_screen_bounds is different
1009 // from the current screen bounds and the current screen bounds doesn't
1010 // completely contain the bounds.
1011 if (cached_screen_bounds
!= current_screen_bounds
&&
1012 !current_screen_bounds
.Contains(cached_bounds
)) {
1014 std::max(minimum_size
.width(),
1015 std::min(bounds
->width(), current_screen_bounds
.width())));
1017 std::max(minimum_size
.height(),
1018 std::min(bounds
->height(), current_screen_bounds
.height())));
1020 std::max(current_screen_bounds
.x(),
1021 std::min(bounds
->x(),
1022 current_screen_bounds
.right() - bounds
->width())));
1024 std::max(current_screen_bounds
.y(),
1025 std::min(bounds
->y(),
1026 current_screen_bounds
.bottom() - bounds
->height())));
1030 AppWindow::CreateParams
AppWindow::LoadDefaults(CreateParams params
)
1032 // Ensure width and height are specified.
1033 if (params
.content_spec
.bounds
.width() == 0 &&
1034 params
.window_spec
.bounds
.width() == 0) {
1035 params
.content_spec
.bounds
.set_width(kDefaultWidth
);
1037 if (params
.content_spec
.bounds
.height() == 0 &&
1038 params
.window_spec
.bounds
.height() == 0) {
1039 params
.content_spec
.bounds
.set_height(kDefaultHeight
);
1042 // If left and top are left undefined, the native app window will center
1043 // the window on the main screen in a platform-defined manner.
1045 // Load cached state if it exists.
1046 if (!params
.window_key
.empty()) {
1047 AppWindowGeometryCache
* cache
=
1048 AppWindowGeometryCache::Get(browser_context());
1050 gfx::Rect cached_bounds
;
1051 gfx::Rect cached_screen_bounds
;
1052 ui::WindowShowState cached_state
= ui::SHOW_STATE_DEFAULT
;
1053 if (cache
->GetGeometry(extension_id(),
1056 &cached_screen_bounds
,
1058 // App window has cached screen bounds, make sure it fits on screen in
1059 // case the screen resolution changed.
1060 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
1061 gfx::Display display
= screen
->GetDisplayMatching(cached_bounds
);
1062 gfx::Rect current_screen_bounds
= display
.work_area();
1063 SizeConstraints
constraints(params
.GetWindowMinimumSize(gfx::Insets()),
1064 params
.GetWindowMaximumSize(gfx::Insets()));
1065 AdjustBoundsToBeVisibleOnScreen(cached_bounds
,
1066 cached_screen_bounds
,
1067 current_screen_bounds
,
1068 constraints
.GetMinimumSize(),
1069 ¶ms
.window_spec
.bounds
);
1070 params
.state
= cached_state
;
1072 // Since we are restoring a cached state, reset the content bounds spec to
1073 // ensure it is not used.
1074 params
.content_spec
.ResetBounds();
1082 SkRegion
* AppWindow::RawDraggableRegionsToSkRegion(
1083 const std::vector
<DraggableRegion
>& regions
) {
1084 SkRegion
* sk_region
= new SkRegion
;
1085 for (std::vector
<DraggableRegion
>::const_iterator iter
= regions
.begin();
1086 iter
!= regions
.end();
1088 const DraggableRegion
& region
= *iter
;
1092 region
.bounds
.right(),
1093 region
.bounds
.bottom(),
1094 region
.draggable
? SkRegion::kUnion_Op
: SkRegion::kDifference_Op
);
1099 } // namespace extensions