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/extension_web_contents_observer.h"
34 #include "extensions/browser/extensions_browser_client.h"
35 #include "extensions/browser/notification_types.h"
36 #include "extensions/browser/process_manager.h"
37 #include "extensions/browser/suggest_permission_util.h"
38 #include "extensions/browser/view_type_utils.h"
39 #include "extensions/common/draggable_region.h"
40 #include "extensions/common/extension.h"
41 #include "extensions/common/manifest_handlers/icons_handler.h"
42 #include "extensions/common/permissions/permissions_data.h"
43 #include "extensions/common/switches.h"
44 #include "extensions/grit/extensions_browser_resources.h"
45 #include "third_party/skia/include/core/SkRegion.h"
46 #include "ui/base/resource/resource_bundle.h"
47 #include "ui/gfx/screen.h"
49 #if !defined(OS_MACOSX)
50 #include "base/prefs/pref_service.h"
51 #include "extensions/browser/pref_names.h"
54 using content::BrowserContext
;
55 using content::ConsoleMessageLevel
;
56 using content::WebContents
;
57 using web_modal::WebContentsModalDialogHost
;
58 using web_modal::WebContentsModalDialogManager
;
60 namespace extensions
{
64 const int kDefaultWidth
= 512;
65 const int kDefaultHeight
= 384;
67 void SetConstraintProperty(const std::string
& name
,
69 base::DictionaryValue
* bounds_properties
) {
70 if (value
!= SizeConstraints::kUnboundedSize
)
71 bounds_properties
->SetInteger(name
, value
);
73 bounds_properties
->Set(name
, base::Value::CreateNullValue());
76 void SetBoundsProperties(const gfx::Rect
& bounds
,
77 const gfx::Size
& min_size
,
78 const gfx::Size
& max_size
,
79 const std::string
& bounds_name
,
80 base::DictionaryValue
* window_properties
) {
81 scoped_ptr
<base::DictionaryValue
> bounds_properties(
82 new base::DictionaryValue());
84 bounds_properties
->SetInteger("left", bounds
.x());
85 bounds_properties
->SetInteger("top", bounds
.y());
86 bounds_properties
->SetInteger("width", bounds
.width());
87 bounds_properties
->SetInteger("height", bounds
.height());
89 SetConstraintProperty("minWidth", min_size
.width(), bounds_properties
.get());
90 SetConstraintProperty(
91 "minHeight", min_size
.height(), bounds_properties
.get());
92 SetConstraintProperty("maxWidth", max_size
.width(), bounds_properties
.get());
93 SetConstraintProperty(
94 "maxHeight", max_size
.height(), bounds_properties
.get());
96 window_properties
->Set(bounds_name
, bounds_properties
.release());
99 // Combines the constraints of the content and window, and returns constraints
101 gfx::Size
GetCombinedWindowConstraints(const gfx::Size
& window_constraints
,
102 const gfx::Size
& content_constraints
,
103 const gfx::Insets
& frame_insets
) {
104 gfx::Size
combined_constraints(window_constraints
);
105 if (content_constraints
.width() > 0) {
106 combined_constraints
.set_width(
107 content_constraints
.width() + frame_insets
.width());
109 if (content_constraints
.height() > 0) {
110 combined_constraints
.set_height(
111 content_constraints
.height() + frame_insets
.height());
113 return combined_constraints
;
116 // Combines the constraints of the content and window, and returns constraints
118 gfx::Size
GetCombinedContentConstraints(const gfx::Size
& window_constraints
,
119 const gfx::Size
& content_constraints
,
120 const gfx::Insets
& frame_insets
) {
121 gfx::Size
combined_constraints(content_constraints
);
122 if (window_constraints
.width() > 0) {
123 combined_constraints
.set_width(
124 std::max(0, window_constraints
.width() - frame_insets
.width()));
126 if (window_constraints
.height() > 0) {
127 combined_constraints
.set_height(
128 std::max(0, window_constraints
.height() - frame_insets
.height()));
130 return combined_constraints
;
135 // AppWindow::BoundsSpecification
137 const int AppWindow::BoundsSpecification::kUnspecifiedPosition
= INT_MIN
;
139 AppWindow::BoundsSpecification::BoundsSpecification()
140 : bounds(kUnspecifiedPosition
, kUnspecifiedPosition
, 0, 0) {}
142 AppWindow::BoundsSpecification::~BoundsSpecification() {}
144 void AppWindow::BoundsSpecification::ResetBounds() {
145 bounds
.SetRect(kUnspecifiedPosition
, kUnspecifiedPosition
, 0, 0);
148 // AppWindow::CreateParams
150 AppWindow::CreateParams::CreateParams()
151 : window_type(AppWindow::WINDOW_TYPE_DEFAULT
),
152 frame(AppWindow::FRAME_CHROME
),
153 has_frame_color(false),
154 active_frame_color(SK_ColorBLACK
),
155 inactive_frame_color(SK_ColorBLACK
),
156 alpha_enabled(false),
157 is_ime_window(false),
158 creator_process_id(0),
159 state(ui::SHOW_STATE_DEFAULT
),
163 always_on_top(false),
164 visible_on_all_workspaces(false) {
167 AppWindow::CreateParams::~CreateParams() {}
169 gfx::Rect
AppWindow::CreateParams::GetInitialWindowBounds(
170 const gfx::Insets
& frame_insets
) const {
171 // Combine into a single window bounds.
172 gfx::Rect
combined_bounds(window_spec
.bounds
);
173 if (content_spec
.bounds
.x() != BoundsSpecification::kUnspecifiedPosition
)
174 combined_bounds
.set_x(content_spec
.bounds
.x() - frame_insets
.left());
175 if (content_spec
.bounds
.y() != BoundsSpecification::kUnspecifiedPosition
)
176 combined_bounds
.set_y(content_spec
.bounds
.y() - frame_insets
.top());
177 if (content_spec
.bounds
.width() > 0) {
178 combined_bounds
.set_width(
179 content_spec
.bounds
.width() + frame_insets
.width());
181 if (content_spec
.bounds
.height() > 0) {
182 combined_bounds
.set_height(
183 content_spec
.bounds
.height() + frame_insets
.height());
186 // Constrain the bounds.
187 SizeConstraints
constraints(
188 GetCombinedWindowConstraints(
189 window_spec
.minimum_size
, content_spec
.minimum_size
, frame_insets
),
190 GetCombinedWindowConstraints(
191 window_spec
.maximum_size
, content_spec
.maximum_size
, frame_insets
));
192 combined_bounds
.set_size(constraints
.ClampSize(combined_bounds
.size()));
194 return combined_bounds
;
197 gfx::Size
AppWindow::CreateParams::GetContentMinimumSize(
198 const gfx::Insets
& frame_insets
) const {
199 return GetCombinedContentConstraints(window_spec
.minimum_size
,
200 content_spec
.minimum_size
,
204 gfx::Size
AppWindow::CreateParams::GetContentMaximumSize(
205 const gfx::Insets
& frame_insets
) const {
206 return GetCombinedContentConstraints(window_spec
.maximum_size
,
207 content_spec
.maximum_size
,
211 gfx::Size
AppWindow::CreateParams::GetWindowMinimumSize(
212 const gfx::Insets
& frame_insets
) const {
213 return GetCombinedWindowConstraints(window_spec
.minimum_size
,
214 content_spec
.minimum_size
,
218 gfx::Size
AppWindow::CreateParams::GetWindowMaximumSize(
219 const gfx::Insets
& frame_insets
) const {
220 return GetCombinedWindowConstraints(window_spec
.maximum_size
,
221 content_spec
.maximum_size
,
227 AppWindow::AppWindow(BrowserContext
* context
,
228 AppDelegate
* app_delegate
,
229 const Extension
* extension
)
230 : browser_context_(context
),
231 extension_id_(extension
->id()),
232 window_type_(WINDOW_TYPE_DEFAULT
),
233 app_delegate_(app_delegate
),
234 fullscreen_types_(FULLSCREEN_TYPE_NONE
),
235 show_on_first_paint_(false),
236 first_paint_complete_(false),
237 has_been_shown_(false),
238 can_send_events_(false),
240 delayed_show_type_(SHOW_ACTIVE
),
241 cached_always_on_top_(false),
242 requested_alpha_enabled_(false),
243 is_ime_window_(false),
244 image_loader_ptr_factory_(this) {
245 ExtensionsBrowserClient
* client
= ExtensionsBrowserClient::Get();
246 CHECK(!client
->IsGuestSession(context
) || context
->IsOffTheRecord())
247 << "Only off the record window may be opened in the guest mode.";
250 void AppWindow::Init(const GURL
& url
,
251 AppWindowContents
* app_window_contents
,
252 const CreateParams
& params
) {
253 // Initialize the render interface and web contents
254 app_window_contents_
.reset(app_window_contents
);
255 app_window_contents_
->Initialize(browser_context(), url
);
259 content::WebContentsObserver::Observe(web_contents());
260 SetViewType(web_contents(), VIEW_TYPE_APP_WINDOW
);
261 app_delegate_
->InitWebContents(web_contents());
263 ExtensionWebContentsObserver::GetForWebContents(web_contents())->
264 dispatcher()->set_delegate(this);
266 WebContentsModalDialogManager::CreateForWebContents(web_contents());
268 web_contents()->SetDelegate(this);
269 WebContentsModalDialogManager::FromWebContents(web_contents())
272 // Initialize the window
273 CreateParams new_params
= LoadDefaults(params
);
274 window_type_
= new_params
.window_type
;
275 window_key_
= new_params
.window_key
;
277 // Windows cannot be always-on-top in fullscreen mode for security reasons.
278 cached_always_on_top_
= new_params
.always_on_top
;
279 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
280 new_params
.always_on_top
= false;
282 requested_alpha_enabled_
= new_params
.alpha_enabled
;
284 is_ime_window_
= params
.is_ime_window
;
286 AppWindowClient
* app_window_client
= AppWindowClient::Get();
287 native_app_window_
.reset(
288 app_window_client
->CreateNativeAppWindow(this, &new_params
));
290 helper_
.reset(new AppWebContentsHelper(
291 browser_context_
, extension_id_
, web_contents(), app_delegate_
.get()));
293 UpdateExtensionAppIcon();
294 AppWindowRegistry::Get(browser_context_
)->AddAppWindow(this);
296 if (new_params
.hidden
) {
297 // Although the window starts hidden by default, calling Hide() here
298 // notifies observers of the window being hidden.
301 // Panels are not activated by default.
302 Show(window_type_is_panel() || !new_params
.focused
? SHOW_INACTIVE
305 // These states may cause the window to show, so they are ignored if the
306 // window is initially hidden.
307 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
309 else if (new_params
.state
== ui::SHOW_STATE_MAXIMIZED
)
311 else if (new_params
.state
== ui::SHOW_STATE_MINIMIZED
)
315 OnNativeWindowChanged();
317 ExtensionRegistry::Get(browser_context_
)->AddObserver(this);
319 // Close when the browser process is exiting.
320 app_delegate_
->SetTerminatingCallback(
321 base::Bind(&NativeAppWindow::Close
,
322 base::Unretained(native_app_window_
.get())));
324 app_window_contents_
->LoadContents(new_params
.creator_process_id
);
326 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
327 extensions::switches::kEnableAppsShowOnFirstPaint
)) {
328 // We want to show the window only when the content has been painted. For
329 // that to happen, we need to define a size for the content, otherwise the
330 // layout will happen in a 0x0 area.
331 gfx::Insets frame_insets
= native_app_window_
->GetFrameInsets();
332 gfx::Rect initial_bounds
= new_params
.GetInitialWindowBounds(frame_insets
);
333 initial_bounds
.Inset(frame_insets
);
334 app_delegate_
->ResizeWebContents(web_contents(), initial_bounds
.size());
338 AppWindow::~AppWindow() {
339 ExtensionRegistry::Get(browser_context_
)->RemoveObserver(this);
342 void AppWindow::RequestMediaAccessPermission(
343 content::WebContents
* web_contents
,
344 const content::MediaStreamRequest
& request
,
345 const content::MediaResponseCallback
& callback
) {
346 DCHECK_EQ(AppWindow::web_contents(), web_contents
);
347 helper_
->RequestMediaAccessPermission(request
, callback
);
350 bool AppWindow::CheckMediaAccessPermission(content::WebContents
* web_contents
,
351 const GURL
& security_origin
,
352 content::MediaStreamType type
) {
353 DCHECK_EQ(AppWindow::web_contents(), web_contents
);
354 return helper_
->CheckMediaAccessPermission(security_origin
, type
);
357 WebContents
* AppWindow::OpenURLFromTab(WebContents
* source
,
358 const content::OpenURLParams
& params
) {
359 DCHECK_EQ(web_contents(), source
);
360 return helper_
->OpenURLFromTab(params
);
363 void AppWindow::AddNewContents(WebContents
* source
,
364 WebContents
* new_contents
,
365 WindowOpenDisposition disposition
,
366 const gfx::Rect
& initial_rect
,
369 DCHECK(new_contents
->GetBrowserContext() == browser_context_
);
370 app_delegate_
->AddNewContents(browser_context_
,
378 bool AppWindow::PreHandleKeyboardEvent(
379 content::WebContents
* source
,
380 const content::NativeWebKeyboardEvent
& event
,
381 bool* is_keyboard_shortcut
) {
382 const Extension
* extension
= GetExtension();
386 // Here, we can handle a key event before the content gets it. When we are
387 // fullscreen and it is not forced, we want to allow the user to leave
388 // when ESC is pressed.
389 // However, if the application has the "overrideEscFullscreen" permission, we
390 // should let it override that behavior.
391 // ::HandleKeyboardEvent() will only be called if the KeyEvent's default
392 // action is not prevented.
393 // Thus, we should handle the KeyEvent here only if the permission is not set.
394 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&& IsFullscreen() &&
395 !IsForcedFullscreen() &&
396 !extension
->permissions_data()->HasAPIPermission(
397 APIPermission::kOverrideEscFullscreen
)) {
405 void AppWindow::HandleKeyboardEvent(
407 const content::NativeWebKeyboardEvent
& event
) {
408 // If the window is currently fullscreen and not forced, ESC should leave
409 // fullscreen. If this code is being called for ESC, that means that the
410 // KeyEvent's default behavior was not prevented by the content.
411 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&& IsFullscreen() &&
412 !IsForcedFullscreen()) {
417 native_app_window_
->HandleKeyboardEvent(event
);
420 void AppWindow::RequestToLockMouse(WebContents
* web_contents
,
422 bool last_unlocked_by_target
) {
423 DCHECK_EQ(AppWindow::web_contents(), web_contents
);
424 helper_
->RequestToLockMouse();
427 bool AppWindow::PreHandleGestureEvent(WebContents
* source
,
428 const blink::WebGestureEvent
& event
) {
429 return AppWebContentsHelper::ShouldSuppressGestureEvent(event
);
432 void AppWindow::RenderViewCreated(content::RenderViewHost
* render_view_host
) {
433 app_delegate_
->RenderViewCreated(render_view_host
);
436 void AppWindow::DidFirstVisuallyNonEmptyPaint() {
437 first_paint_complete_
= true;
438 if (show_on_first_paint_
) {
439 DCHECK(delayed_show_type_
== SHOW_ACTIVE
||
440 delayed_show_type_
== SHOW_INACTIVE
);
441 Show(delayed_show_type_
);
445 void AppWindow::OnNativeClose() {
446 AppWindowRegistry::Get(browser_context_
)->RemoveAppWindow(this);
447 if (app_window_contents_
) {
448 WebContentsModalDialogManager
* modal_dialog_manager
=
449 WebContentsModalDialogManager::FromWebContents(web_contents());
450 if (modal_dialog_manager
) // May be null in unit tests.
451 modal_dialog_manager
->SetDelegate(nullptr);
452 app_window_contents_
->NativeWindowClosed();
457 void AppWindow::OnNativeWindowChanged() {
458 // This may be called during Init before |native_app_window_| is set.
459 if (!native_app_window_
)
462 #if defined(OS_MACOSX)
463 // On Mac the user can change the window's fullscreen state. If that has
464 // happened, update AppWindow's internal state.
465 if (native_app_window_
->IsFullscreen()) {
467 fullscreen_types_
= FULLSCREEN_TYPE_OS
;
469 fullscreen_types_
= FULLSCREEN_TYPE_NONE
;
472 RestoreAlwaysOnTop(); // Same as in SetNativeWindowFullscreen.
475 SaveWindowPosition();
478 if (cached_always_on_top_
&& !IsFullscreen() &&
479 !native_app_window_
->IsMaximized() &&
480 !native_app_window_
->IsMinimized()) {
481 UpdateNativeAlwaysOnTop();
485 if (app_window_contents_
)
486 app_window_contents_
->NativeWindowChanged(native_app_window_
.get());
489 void AppWindow::OnNativeWindowActivated() {
490 AppWindowRegistry::Get(browser_context_
)->AppWindowActivated(this);
493 content::WebContents
* AppWindow::web_contents() const {
494 return app_window_contents_
->GetWebContents();
497 const Extension
* AppWindow::GetExtension() const {
498 return ExtensionRegistry::Get(browser_context_
)
499 ->enabled_extensions()
500 .GetByID(extension_id_
);
503 NativeAppWindow
* AppWindow::GetBaseWindow() { return native_app_window_
.get(); }
505 gfx::NativeWindow
AppWindow::GetNativeWindow() {
506 return GetBaseWindow()->GetNativeWindow();
509 gfx::Rect
AppWindow::GetClientBounds() const {
510 gfx::Rect bounds
= native_app_window_
->GetBounds();
511 bounds
.Inset(native_app_window_
->GetFrameInsets());
515 base::string16
AppWindow::GetTitle() const {
516 const Extension
* extension
= GetExtension();
518 return base::string16();
520 // WebContents::GetTitle() will return the page's URL if there's no <title>
521 // specified. However, we'd prefer to show the name of the extension in that
522 // case, so we directly inspect the NavigationEntry's title.
523 base::string16 title
;
524 content::NavigationEntry
* entry
= web_contents() ?
525 web_contents()->GetController().GetLastCommittedEntry() : nullptr;
526 if (!entry
|| entry
->GetTitle().empty()) {
527 title
= base::UTF8ToUTF16(extension
->name());
529 title
= web_contents()->GetTitle();
531 base::RemoveChars(title
, base::ASCIIToUTF16("\n"), &title
);
535 void AppWindow::SetAppIconUrl(const GURL
& url
) {
536 // Avoid using any previous icons that were being downloaded.
537 image_loader_ptr_factory_
.InvalidateWeakPtrs();
539 // Reset |app_icon_image_| to abort pending image load (if any).
540 app_icon_image_
.reset();
543 web_contents()->DownloadImage(
545 true, // is a favicon
546 0, // no maximum size
547 false, // normal cache policy
548 base::Bind(&AppWindow::DidDownloadFavicon
,
549 image_loader_ptr_factory_
.GetWeakPtr()));
552 void AppWindow::UpdateShape(scoped_ptr
<SkRegion
> region
) {
553 native_app_window_
->UpdateShape(region
.Pass());
556 void AppWindow::UpdateDraggableRegions(
557 const std::vector
<DraggableRegion
>& regions
) {
558 native_app_window_
->UpdateDraggableRegions(regions
);
561 void AppWindow::UpdateAppIcon(const gfx::Image
& image
) {
565 native_app_window_
->UpdateWindowIcon();
566 AppWindowRegistry::Get(browser_context_
)->AppWindowIconChanged(this);
569 void AppWindow::SetFullscreen(FullscreenType type
, bool enable
) {
570 DCHECK_NE(FULLSCREEN_TYPE_NONE
, type
);
573 #if !defined(OS_MACOSX)
574 // Do not enter fullscreen mode if disallowed by pref.
575 // TODO(bartfab): Add a test once it becomes possible to simulate a user
576 // gesture. http://crbug.com/174178
577 if (type
!= FULLSCREEN_TYPE_FORCED
) {
579 ExtensionsBrowserClient::Get()->GetPrefServiceForContext(
581 if (!prefs
->GetBoolean(pref_names::kAppFullscreenAllowed
))
585 fullscreen_types_
|= type
;
587 fullscreen_types_
&= ~type
;
589 SetNativeWindowFullscreen();
592 bool AppWindow::IsFullscreen() const {
593 return fullscreen_types_
!= FULLSCREEN_TYPE_NONE
;
596 bool AppWindow::IsForcedFullscreen() const {
597 return (fullscreen_types_
& FULLSCREEN_TYPE_FORCED
) != 0;
600 bool AppWindow::IsHtmlApiFullscreen() const {
601 return (fullscreen_types_
& FULLSCREEN_TYPE_HTML_API
) != 0;
604 void AppWindow::Fullscreen() {
605 SetFullscreen(FULLSCREEN_TYPE_WINDOW_API
, true);
608 void AppWindow::Maximize() { GetBaseWindow()->Maximize(); }
610 void AppWindow::Minimize() { GetBaseWindow()->Minimize(); }
612 void AppWindow::Restore() {
613 if (IsFullscreen()) {
614 fullscreen_types_
= FULLSCREEN_TYPE_NONE
;
615 SetNativeWindowFullscreen();
617 GetBaseWindow()->Restore();
621 void AppWindow::OSFullscreen() {
622 SetFullscreen(FULLSCREEN_TYPE_OS
, true);
625 void AppWindow::ForcedFullscreen() {
626 SetFullscreen(FULLSCREEN_TYPE_FORCED
, true);
629 void AppWindow::SetContentSizeConstraints(const gfx::Size
& min_size
,
630 const gfx::Size
& max_size
) {
631 SizeConstraints
constraints(min_size
, max_size
);
632 native_app_window_
->SetContentSizeConstraints(constraints
.GetMinimumSize(),
633 constraints
.GetMaximumSize());
635 gfx::Rect bounds
= GetClientBounds();
636 gfx::Size constrained_size
= constraints
.ClampSize(bounds
.size());
637 if (bounds
.size() != constrained_size
) {
638 bounds
.set_size(constrained_size
);
639 bounds
.Inset(-native_app_window_
->GetFrameInsets());
640 native_app_window_
->SetBounds(bounds
);
642 OnNativeWindowChanged();
645 void AppWindow::Show(ShowType show_type
) {
646 app_delegate_
->OnShow();
647 bool was_hidden
= is_hidden_
|| !has_been_shown_
;
650 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
651 switches::kEnableAppsShowOnFirstPaint
)) {
652 show_on_first_paint_
= true;
654 if (!first_paint_complete_
) {
655 delayed_show_type_
= show_type
;
662 GetBaseWindow()->Show();
665 GetBaseWindow()->ShowInactive();
668 AppWindowRegistry::Get(browser_context_
)->AppWindowShown(this, was_hidden
);
670 has_been_shown_
= true;
671 SendOnWindowShownIfShown();
674 void AppWindow::Hide() {
675 // This is there to prevent race conditions with Hide() being called before
676 // there was a non-empty paint. It should have no effect in a non-racy
677 // scenario where the application is hiding then showing a window: the second
678 // show will not be delayed.
680 show_on_first_paint_
= false;
681 GetBaseWindow()->Hide();
682 AppWindowRegistry::Get(browser_context_
)->AppWindowHidden(this);
683 app_delegate_
->OnHide();
686 void AppWindow::SetAlwaysOnTop(bool always_on_top
) {
687 if (cached_always_on_top_
== always_on_top
)
690 cached_always_on_top_
= always_on_top
;
692 // As a security measure, do not allow fullscreen windows or windows that
693 // overlap the taskbar to be on top. The property will be applied when the
694 // window exits fullscreen and moves away from the taskbar.
695 if (!IsFullscreen() && !IntersectsWithTaskbar())
696 native_app_window_
->SetAlwaysOnTop(always_on_top
);
698 OnNativeWindowChanged();
701 bool AppWindow::IsAlwaysOnTop() const { return cached_always_on_top_
; }
703 void AppWindow::RestoreAlwaysOnTop() {
704 if (cached_always_on_top_
)
705 UpdateNativeAlwaysOnTop();
708 void AppWindow::SetInterceptAllKeys(bool want_all_keys
) {
709 native_app_window_
->SetInterceptAllKeys(want_all_keys
);
712 void AppWindow::WindowEventsReady() {
713 can_send_events_
= true;
714 SendOnWindowShownIfShown();
717 void AppWindow::NotifyRenderViewReady() {
718 if (app_window_contents_
)
719 app_window_contents_
->OnWindowReady();
722 void AppWindow::GetSerializedState(base::DictionaryValue
* properties
) const {
725 properties
->SetBoolean("fullscreen",
726 native_app_window_
->IsFullscreenOrPending());
727 properties
->SetBoolean("minimized", native_app_window_
->IsMinimized());
728 properties
->SetBoolean("maximized", native_app_window_
->IsMaximized());
729 properties
->SetBoolean("alwaysOnTop", IsAlwaysOnTop());
730 properties
->SetBoolean("hasFrameColor", native_app_window_
->HasFrameColor());
731 properties
->SetBoolean(
733 requested_alpha_enabled_
&& native_app_window_
->CanHaveAlphaEnabled());
735 // These properties are undocumented and are to enable testing. Alpha is
737 // make the values easier to check.
738 SkColor transparent_white
= ~SK_ColorBLACK
;
739 properties
->SetInteger(
741 native_app_window_
->ActiveFrameColor() & transparent_white
);
742 properties
->SetInteger(
743 "inactiveFrameColor",
744 native_app_window_
->InactiveFrameColor() & transparent_white
);
746 gfx::Rect content_bounds
= GetClientBounds();
747 gfx::Size content_min_size
= native_app_window_
->GetContentMinimumSize();
748 gfx::Size content_max_size
= native_app_window_
->GetContentMaximumSize();
749 SetBoundsProperties(content_bounds
,
755 gfx::Insets frame_insets
= native_app_window_
->GetFrameInsets();
756 gfx::Rect frame_bounds
= native_app_window_
->GetBounds();
757 gfx::Size frame_min_size
= SizeConstraints::AddFrameToConstraints(
758 content_min_size
, frame_insets
);
759 gfx::Size frame_max_size
= SizeConstraints::AddFrameToConstraints(
760 content_max_size
, frame_insets
);
761 SetBoundsProperties(frame_bounds
,
768 //------------------------------------------------------------------------------
771 void AppWindow::DidDownloadFavicon(
773 int http_status_code
,
774 const GURL
& image_url
,
775 const std::vector
<SkBitmap
>& bitmaps
,
776 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
777 if (image_url
!= app_icon_url_
|| bitmaps
.empty())
780 // Bitmaps are ordered largest to smallest. Choose the smallest bitmap
781 // whose height >= the preferred size.
782 int largest_index
= 0;
783 for (size_t i
= 1; i
< bitmaps
.size(); ++i
) {
784 if (bitmaps
[i
].height() < app_delegate_
->PreferredIconSize())
788 const SkBitmap
& largest
= bitmaps
[largest_index
];
789 UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest
));
792 void AppWindow::OnExtensionIconImageChanged(IconImage
* image
) {
793 DCHECK_EQ(app_icon_image_
.get(), image
);
795 UpdateAppIcon(gfx::Image(app_icon_image_
->image_skia()));
798 void AppWindow::UpdateExtensionAppIcon() {
799 // Avoid using any previous app icons were being downloaded.
800 image_loader_ptr_factory_
.InvalidateWeakPtrs();
802 const Extension
* extension
= GetExtension();
806 gfx::ImageSkia app_default_icon
=
807 *ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
808 IDR_APP_DEFAULT_ICON
);
810 app_icon_image_
.reset(new IconImage(browser_context(),
812 IconsInfo::GetIcons(extension
),
813 app_delegate_
->PreferredIconSize(),
817 // Triggers actual image loading with 1x resources. The 2x resource will
818 // be handled by IconImage class when requested.
819 app_icon_image_
->image_skia().GetRepresentation(1.0f
);
822 void AppWindow::SetNativeWindowFullscreen() {
823 native_app_window_
->SetFullscreen(fullscreen_types_
);
825 RestoreAlwaysOnTop();
828 bool AppWindow::IntersectsWithTaskbar() const {
830 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
831 gfx::Rect window_bounds
= native_app_window_
->GetRestoredBounds();
832 std::vector
<gfx::Display
> displays
= screen
->GetAllDisplays();
834 for (std::vector
<gfx::Display
>::const_iterator it
= displays
.begin();
835 it
!= displays
.end();
837 gfx::Rect taskbar_bounds
= it
->bounds();
838 taskbar_bounds
.Subtract(it
->work_area());
839 if (taskbar_bounds
.IsEmpty())
842 if (window_bounds
.Intersects(taskbar_bounds
))
850 void AppWindow::UpdateNativeAlwaysOnTop() {
851 DCHECK(cached_always_on_top_
);
852 bool is_on_top
= native_app_window_
->IsAlwaysOnTop();
853 bool fullscreen
= IsFullscreen();
854 bool intersects_taskbar
= IntersectsWithTaskbar();
856 if (is_on_top
&& (fullscreen
|| intersects_taskbar
)) {
857 // When entering fullscreen or overlapping the taskbar, ensure windows are
858 // not always-on-top.
859 native_app_window_
->SetAlwaysOnTop(false);
860 } else if (!is_on_top
&& !fullscreen
&& !intersects_taskbar
) {
861 // When exiting fullscreen and moving away from the taskbar, reinstate
863 native_app_window_
->SetAlwaysOnTop(true);
867 void AppWindow::SendOnWindowShownIfShown() {
868 if (!can_send_events_
|| !has_been_shown_
)
871 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
872 ::switches::kTestType
)) {
873 app_window_contents_
->DispatchWindowShownForTests();
877 void AppWindow::CloseContents(WebContents
* contents
) {
878 native_app_window_
->Close();
881 bool AppWindow::ShouldSuppressDialogs(WebContents
* source
) {
885 content::ColorChooser
* AppWindow::OpenColorChooser(
886 WebContents
* web_contents
,
887 SkColor initial_color
,
888 const std::vector
<content::ColorSuggestion
>& suggestions
) {
889 return app_delegate_
->ShowColorChooser(web_contents
, initial_color
);
892 void AppWindow::RunFileChooser(WebContents
* tab
,
893 const content::FileChooserParams
& params
) {
894 if (window_type_is_panel()) {
895 // Panels can't host a file dialog, abort. TODO(stevenjb): allow file
896 // dialogs to be unhosted but still close with the owning web contents.
898 LOG(WARNING
) << "File dialog opened by panel.";
902 app_delegate_
->RunFileChooser(tab
, params
);
905 bool AppWindow::IsPopupOrPanel(const WebContents
* source
) const { return true; }
907 void AppWindow::MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {
908 native_app_window_
->SetBounds(pos
);
911 void AppWindow::NavigationStateChanged(content::WebContents
* source
,
912 content::InvalidateTypes changed_flags
) {
913 if (changed_flags
& content::INVALIDATE_TYPE_TITLE
)
914 native_app_window_
->UpdateWindowTitle();
915 else if (changed_flags
& content::INVALIDATE_TYPE_TAB
)
916 native_app_window_
->UpdateWindowIcon();
919 void AppWindow::EnterFullscreenModeForTab(content::WebContents
* source
,
920 const GURL
& origin
) {
921 ToggleFullscreenModeForTab(source
, true);
924 void AppWindow::ExitFullscreenModeForTab(content::WebContents
* source
) {
925 ToggleFullscreenModeForTab(source
, false);
928 void AppWindow::ToggleFullscreenModeForTab(content::WebContents
* source
,
929 bool enter_fullscreen
) {
930 const Extension
* extension
= GetExtension();
934 if (!IsExtensionWithPermissionOrSuggestInConsole(
935 APIPermission::kFullscreen
, extension
, source
->GetMainFrame())) {
939 SetFullscreen(FULLSCREEN_TYPE_HTML_API
, enter_fullscreen
);
942 bool AppWindow::IsFullscreenForTabOrPending(const content::WebContents
* source
)
944 return IsHtmlApiFullscreen();
947 blink::WebDisplayMode
AppWindow::GetDisplayMode(
948 const content::WebContents
* source
) const {
949 return IsFullscreen() ? blink::WebDisplayModeFullscreen
950 : blink::WebDisplayModeStandalone
;
953 WindowController
* AppWindow::GetExtensionWindowController() const {
954 return app_window_contents_
->GetWindowController();
957 content::WebContents
* AppWindow::GetAssociatedWebContents() const {
958 return web_contents();
961 void AppWindow::OnExtensionUnloaded(BrowserContext
* browser_context
,
962 const Extension
* extension
,
963 UnloadedExtensionInfo::Reason reason
) {
964 if (extension_id_
== extension
->id())
965 native_app_window_
->Close();
968 void AppWindow::SetWebContentsBlocked(content::WebContents
* web_contents
,
970 app_delegate_
->SetWebContentsBlocked(web_contents
, blocked
);
973 bool AppWindow::IsWebContentsVisible(content::WebContents
* web_contents
) {
974 return app_delegate_
->IsWebContentsVisible(web_contents
);
977 WebContentsModalDialogHost
* AppWindow::GetWebContentsModalDialogHost() {
978 return native_app_window_
.get();
981 void AppWindow::SaveWindowPosition() {
982 DCHECK(native_app_window_
);
983 if (window_key_
.empty())
986 AppWindowGeometryCache
* cache
=
987 AppWindowGeometryCache::Get(browser_context());
989 gfx::Rect bounds
= native_app_window_
->GetRestoredBounds();
990 gfx::Rect screen_bounds
=
991 gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds
).work_area();
992 ui::WindowShowState window_state
= native_app_window_
->GetRestoredState();
994 extension_id(), window_key_
, bounds
, screen_bounds
, window_state
);
997 void AppWindow::AdjustBoundsToBeVisibleOnScreen(
998 const gfx::Rect
& cached_bounds
,
999 const gfx::Rect
& cached_screen_bounds
,
1000 const gfx::Rect
& current_screen_bounds
,
1001 const gfx::Size
& minimum_size
,
1002 gfx::Rect
* bounds
) const {
1003 *bounds
= cached_bounds
;
1005 // Reposition and resize the bounds if the cached_screen_bounds is different
1006 // from the current screen bounds and the current screen bounds doesn't
1007 // completely contain the bounds.
1008 if (cached_screen_bounds
!= current_screen_bounds
&&
1009 !current_screen_bounds
.Contains(cached_bounds
)) {
1011 std::max(minimum_size
.width(),
1012 std::min(bounds
->width(), current_screen_bounds
.width())));
1014 std::max(minimum_size
.height(),
1015 std::min(bounds
->height(), current_screen_bounds
.height())));
1017 std::max(current_screen_bounds
.x(),
1018 std::min(bounds
->x(),
1019 current_screen_bounds
.right() - bounds
->width())));
1021 std::max(current_screen_bounds
.y(),
1022 std::min(bounds
->y(),
1023 current_screen_bounds
.bottom() - bounds
->height())));
1027 AppWindow::CreateParams
AppWindow::LoadDefaults(CreateParams params
)
1029 // Ensure width and height are specified.
1030 if (params
.content_spec
.bounds
.width() == 0 &&
1031 params
.window_spec
.bounds
.width() == 0) {
1032 params
.content_spec
.bounds
.set_width(kDefaultWidth
);
1034 if (params
.content_spec
.bounds
.height() == 0 &&
1035 params
.window_spec
.bounds
.height() == 0) {
1036 params
.content_spec
.bounds
.set_height(kDefaultHeight
);
1039 // If left and top are left undefined, the native app window will center
1040 // the window on the main screen in a platform-defined manner.
1042 // Load cached state if it exists.
1043 if (!params
.window_key
.empty()) {
1044 AppWindowGeometryCache
* cache
=
1045 AppWindowGeometryCache::Get(browser_context());
1047 gfx::Rect cached_bounds
;
1048 gfx::Rect cached_screen_bounds
;
1049 ui::WindowShowState cached_state
= ui::SHOW_STATE_DEFAULT
;
1050 if (cache
->GetGeometry(extension_id(),
1053 &cached_screen_bounds
,
1055 // App window has cached screen bounds, make sure it fits on screen in
1056 // case the screen resolution changed.
1057 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
1058 gfx::Display display
= screen
->GetDisplayMatching(cached_bounds
);
1059 gfx::Rect current_screen_bounds
= display
.work_area();
1060 SizeConstraints
constraints(params
.GetWindowMinimumSize(gfx::Insets()),
1061 params
.GetWindowMaximumSize(gfx::Insets()));
1062 AdjustBoundsToBeVisibleOnScreen(cached_bounds
,
1063 cached_screen_bounds
,
1064 current_screen_bounds
,
1065 constraints
.GetMinimumSize(),
1066 ¶ms
.window_spec
.bounds
);
1067 params
.state
= cached_state
;
1069 // Since we are restoring a cached state, reset the content bounds spec to
1070 // ensure it is not used.
1071 params
.content_spec
.ResetBounds();
1079 SkRegion
* AppWindow::RawDraggableRegionsToSkRegion(
1080 const std::vector
<DraggableRegion
>& regions
) {
1081 SkRegion
* sk_region
= new SkRegion
;
1082 for (std::vector
<DraggableRegion
>::const_iterator iter
= regions
.begin();
1083 iter
!= regions
.end();
1085 const DraggableRegion
& region
= *iter
;
1089 region
.bounds
.right(),
1090 region
.bounds
.bottom(),
1091 region
.draggable
? SkRegion::kUnion_Op
: SkRegion::kDifference_Op
);
1096 } // namespace extensions