1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "apps/shell_window.h"
7 #include "apps/shell_window_geometry_cache.h"
8 #include "apps/shell_window_registry.h"
9 #include "apps/ui/native_app_window.h"
10 #include "base/command_line.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/extensions/extension_system.h"
16 #include "chrome/browser/extensions/extension_web_contents_observer.h"
17 #include "chrome/browser/extensions/suggest_permission_util.h"
18 #include "chrome/browser/lifetime/application_lifetime.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/chrome_switches.h"
21 #include "chrome/common/extensions/extension_messages.h"
22 #include "chrome/common/extensions/manifest_handlers/icons_handler.h"
23 #include "components/web_modal/web_contents_modal_dialog_manager.h"
24 #include "content/public/browser/invalidate_type.h"
25 #include "content/public/browser/navigation_entry.h"
26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_service.h"
28 #include "content/public/browser/notification_source.h"
29 #include "content/public/browser/notification_types.h"
30 #include "content/public/browser/render_view_host.h"
31 #include "content/public/browser/resource_dispatcher_host.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/browser/web_contents_view.h"
34 #include "content/public/common/media_stream_request.h"
35 #include "extensions/browser/process_manager.h"
36 #include "extensions/browser/view_type_utils.h"
37 #include "extensions/common/extension.h"
38 #include "third_party/skia/include/core/SkRegion.h"
39 #include "ui/gfx/screen.h"
41 #if !defined(OS_MACOSX)
42 #include "apps/pref_names.h"
43 #include "base/prefs/pref_service.h"
46 using content::ConsoleMessageLevel
;
47 using content::WebContents
;
48 using extensions::APIPermission
;
49 using web_modal::WebContentsModalDialogHost
;
50 using web_modal::WebContentsModalDialogManager
;
54 const int kDefaultWidth
= 512;
55 const int kDefaultHeight
= 384;
57 bool IsFullscreen(int fullscreen_types
) {
58 return fullscreen_types
!= apps::ShellWindow::FULLSCREEN_TYPE_NONE
;
65 ShellWindow::SizeConstraints::SizeConstraints()
66 : maximum_size_(kUnboundedSize
, kUnboundedSize
) {
69 ShellWindow::SizeConstraints::SizeConstraints(const gfx::Size
& min_size
,
70 const gfx::Size
& max_size
)
71 : minimum_size_(min_size
),
72 maximum_size_(max_size
) {
75 ShellWindow::SizeConstraints::~SizeConstraints() {}
77 gfx::Size
ShellWindow::SizeConstraints::ClampSize(gfx::Size size
) const {
78 const gfx::Size max_size
= GetMaximumSize();
79 if (max_size
.width() != kUnboundedSize
)
80 size
.set_width(std::min(size
.width(), GetMaximumSize().width()));
81 if (max_size
.height() != kUnboundedSize
)
82 size
.set_height(std::min(size
.height(), GetMaximumSize().height()));
83 size
.SetToMax(GetMinimumSize());
87 bool ShellWindow::SizeConstraints::HasMinimumSize() const {
88 return GetMinimumSize().width() != kUnboundedSize
||
89 GetMinimumSize().height() != kUnboundedSize
;
92 bool ShellWindow::SizeConstraints::HasMaximumSize() const {
93 const gfx::Size max_size
= GetMaximumSize();
94 return max_size
.width() != kUnboundedSize
||
95 max_size
.height() != kUnboundedSize
;
98 bool ShellWindow::SizeConstraints::HasFixedSize() const {
99 return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize();
102 gfx::Size
ShellWindow::SizeConstraints::GetMinimumSize() const {
103 return minimum_size_
;
106 gfx::Size
ShellWindow::SizeConstraints::GetMaximumSize() const {
108 maximum_size_
.width() == kUnboundedSize
?
110 std::max(maximum_size_
.width(), minimum_size_
.width()),
111 maximum_size_
.height() == kUnboundedSize
?
113 std::max(maximum_size_
.height(), minimum_size_
.height()));
116 void ShellWindow::SizeConstraints::set_minimum_size(const gfx::Size
& min_size
) {
117 minimum_size_
= min_size
;
120 void ShellWindow::SizeConstraints::set_maximum_size(const gfx::Size
& max_size
) {
121 maximum_size_
= max_size
;
124 ShellWindow::CreateParams::CreateParams()
125 : window_type(ShellWindow::WINDOW_TYPE_DEFAULT
),
126 frame(ShellWindow::FRAME_CHROME
),
127 transparent_background(false),
128 bounds(INT_MIN
, INT_MIN
, 0, 0),
129 creator_process_id(0),
130 state(ui::SHOW_STATE_DEFAULT
),
134 always_on_top(false) {}
136 ShellWindow::CreateParams::~CreateParams() {}
138 ShellWindow::Delegate::~Delegate() {}
140 ShellWindow::ShellWindow(Profile
* profile
,
142 const extensions::Extension
* extension
)
144 extension_(extension
),
145 extension_id_(extension
->id()),
146 window_type_(WINDOW_TYPE_DEFAULT
),
148 image_loader_ptr_factory_(this),
149 fullscreen_types_(FULLSCREEN_TYPE_NONE
),
150 show_on_first_paint_(false),
151 first_paint_complete_(false),
152 cached_always_on_top_(false) {
153 CHECK(!profile
->IsGuestSession() || profile
->IsOffTheRecord())
154 << "Only off the record window may be opened in the guest mode.";
157 void ShellWindow::Init(const GURL
& url
,
158 ShellWindowContents
* shell_window_contents
,
159 const CreateParams
& params
) {
160 // Initialize the render interface and web contents
161 shell_window_contents_
.reset(shell_window_contents
);
162 shell_window_contents_
->Initialize(profile(), url
);
163 WebContents
* web_contents
= shell_window_contents_
->GetWebContents();
164 if (CommandLine::ForCurrentProcess()->HasSwitch(
165 switches::kEnableAppsShowOnFirstPaint
)) {
166 content::WebContentsObserver::Observe(web_contents
);
168 delegate_
->InitWebContents(web_contents
);
169 WebContentsModalDialogManager::CreateForWebContents(web_contents
);
170 extensions::ExtensionWebContentsObserver::CreateForWebContents(web_contents
);
172 web_contents
->SetDelegate(this);
173 WebContentsModalDialogManager::FromWebContents(web_contents
)->
175 extensions::SetViewType(web_contents
, extensions::VIEW_TYPE_APP_SHELL
);
177 // Initialize the window
178 CreateParams new_params
= LoadDefaultsAndConstrain(params
);
179 window_type_
= new_params
.window_type
;
180 window_key_
= new_params
.window_key
;
181 size_constraints_
= SizeConstraints(new_params
.minimum_size
,
182 new_params
.maximum_size
);
184 // Windows cannot be always-on-top in fullscreen mode for security reasons.
185 cached_always_on_top_
= new_params
.always_on_top
;
186 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
187 new_params
.always_on_top
= false;
189 native_app_window_
.reset(delegate_
->CreateNativeAppWindow(this, new_params
));
191 if (!new_params
.hidden
) {
192 // Panels are not activated by default.
193 Show(window_type_is_panel() || !new_params
.focused
? SHOW_INACTIVE
197 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
199 else if (new_params
.state
== ui::SHOW_STATE_MAXIMIZED
)
201 else if (new_params
.state
== ui::SHOW_STATE_MINIMIZED
)
204 OnNativeWindowChanged();
206 // When the render view host is changed, the native window needs to know
207 // about it in case it has any setup to do to make the renderer appear
208 // properly. In particular, on Windows, the view's clickthrough region needs
210 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
211 content::Source
<Profile
>(profile_
));
212 // Close when the browser process is exiting.
213 registrar_
.Add(this, chrome::NOTIFICATION_APP_TERMINATING
,
214 content::NotificationService::AllSources());
216 shell_window_contents_
->LoadContents(new_params
.creator_process_id
);
218 if (CommandLine::ForCurrentProcess()->HasSwitch(
219 switches::kEnableAppsShowOnFirstPaint
)) {
220 // We want to show the window only when the content has been painted. For
221 // that to happen, we need to define a size for the content, otherwise the
222 // layout will happen in a 0x0 area.
223 // Note: WebContents::GetView() is guaranteed to be non-null.
224 web_contents
->GetView()->SizeContents(new_params
.bounds
.size());
227 // Prevent the browser process from shutting down while this window is open.
228 chrome::StartKeepAlive();
230 UpdateExtensionAppIcon();
232 ShellWindowRegistry::Get(profile_
)->AddShellWindow(this);
235 ShellWindow::~ShellWindow() {
236 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the
238 registrar_
.RemoveAll();
240 // Remove shutdown prevention.
241 chrome::EndKeepAlive();
244 void ShellWindow::RequestMediaAccessPermission(
245 content::WebContents
* web_contents
,
246 const content::MediaStreamRequest
& request
,
247 const content::MediaResponseCallback
& callback
) {
248 delegate_
->RequestMediaAccessPermission(web_contents
, request
, callback
,
252 WebContents
* ShellWindow::OpenURLFromTab(WebContents
* source
,
253 const content::OpenURLParams
& params
) {
254 // Don't allow the current tab to be navigated. It would be nice to map all
255 // anchor tags (even those without target="_blank") to new tabs, but right
256 // now we can't distinguish between those and <meta> refreshes or window.href
257 // navigations, which we don't want to allow.
258 // TOOD(mihaip): Can we check for user gestures instead?
259 WindowOpenDisposition disposition
= params
.disposition
;
260 if (disposition
== CURRENT_TAB
) {
261 AddMessageToDevToolsConsole(
262 content::CONSOLE_MESSAGE_LEVEL_ERROR
,
264 "Can't open same-window link to \"%s\"; try target=\"_blank\".",
265 params
.url
.spec().c_str()));
269 // These dispositions aren't really navigations.
270 if (disposition
== SUPPRESS_OPEN
|| disposition
== SAVE_TO_DISK
||
271 disposition
== IGNORE_ACTION
) {
275 WebContents
* contents
= delegate_
->OpenURLFromTab(profile_
, source
,
278 AddMessageToDevToolsConsole(
279 content::CONSOLE_MESSAGE_LEVEL_ERROR
,
281 "Can't navigate to \"%s\"; apps do not support navigation.",
282 params
.url
.spec().c_str()));
288 void ShellWindow::AddNewContents(WebContents
* source
,
289 WebContents
* new_contents
,
290 WindowOpenDisposition disposition
,
291 const gfx::Rect
& initial_pos
,
294 DCHECK(Profile::FromBrowserContext(new_contents
->GetBrowserContext()) ==
296 delegate_
->AddNewContents(profile_
, new_contents
, disposition
,
297 initial_pos
, user_gesture
, was_blocked
);
300 bool ShellWindow::PreHandleKeyboardEvent(
301 content::WebContents
* source
,
302 const content::NativeWebKeyboardEvent
& event
,
303 bool* is_keyboard_shortcut
) {
304 // Here, we can handle a key event before the content gets it. When we are
305 // fullscreen and it is not forced, we want to allow the user to leave
306 // when ESC is pressed.
307 // However, if the application has the "overrideEscFullscreen" permission, we
308 // should let it override that behavior.
309 // ::HandleKeyboardEvent() will only be called if the KeyEvent's default
310 // action is not prevented.
311 // Thus, we should handle the KeyEvent here only if the permission is not set.
312 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&&
313 (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
) &&
314 ((fullscreen_types_
& FULLSCREEN_TYPE_FORCED
) == 0) &&
315 !extension_
->HasAPIPermission(APIPermission::kOverrideEscFullscreen
)) {
323 void ShellWindow::HandleKeyboardEvent(
325 const content::NativeWebKeyboardEvent
& event
) {
326 // If the window is currently fullscreen and not forced, ESC should leave
327 // fullscreen. If this code is being called for ESC, that means that the
328 // KeyEvent's default behavior was not prevented by the content.
329 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&&
330 (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
) &&
331 ((fullscreen_types_
& FULLSCREEN_TYPE_FORCED
) == 0)) {
336 native_app_window_
->HandleKeyboardEvent(event
);
339 void ShellWindow::RequestToLockMouse(WebContents
* web_contents
,
341 bool last_unlocked_by_target
) {
342 bool has_permission
= IsExtensionWithPermissionOrSuggestInConsole(
343 APIPermission::kPointerLock
,
345 web_contents
->GetRenderViewHost());
347 web_contents
->GotResponseToLockMouseRequest(has_permission
);
350 void ShellWindow::DidFirstVisuallyNonEmptyPaint(int32 page_id
) {
351 first_paint_complete_
= true;
352 if (show_on_first_paint_
) {
353 DCHECK(delayed_show_type_
== SHOW_ACTIVE
||
354 delayed_show_type_
== SHOW_INACTIVE
);
355 Show(delayed_show_type_
);
359 void ShellWindow::OnNativeClose() {
360 ShellWindowRegistry::Get(profile_
)->RemoveShellWindow(this);
361 if (shell_window_contents_
) {
362 WebContents
* web_contents
= shell_window_contents_
->GetWebContents();
363 WebContentsModalDialogManager::FromWebContents(web_contents
)->
365 shell_window_contents_
->NativeWindowClosed();
370 void ShellWindow::OnNativeWindowChanged() {
371 SaveWindowPosition();
374 if (native_app_window_
&&
375 cached_always_on_top_
&&
376 !IsFullscreen(fullscreen_types_
) &&
377 !native_app_window_
->IsMaximized() &&
378 !native_app_window_
->IsMinimized()) {
379 UpdateNativeAlwaysOnTop();
383 if (shell_window_contents_
&& native_app_window_
)
384 shell_window_contents_
->NativeWindowChanged(native_app_window_
.get());
387 void ShellWindow::OnNativeWindowActivated() {
388 ShellWindowRegistry::Get(profile_
)->ShellWindowActivated(this);
391 content::WebContents
* ShellWindow::web_contents() const {
392 return shell_window_contents_
->GetWebContents();
395 NativeAppWindow
* ShellWindow::GetBaseWindow() {
396 return native_app_window_
.get();
399 gfx::NativeWindow
ShellWindow::GetNativeWindow() {
400 return GetBaseWindow()->GetNativeWindow();
403 gfx::Rect
ShellWindow::GetClientBounds() const {
404 gfx::Rect bounds
= native_app_window_
->GetBounds();
405 bounds
.Inset(native_app_window_
->GetFrameInsets());
409 base::string16
ShellWindow::GetTitle() const {
410 // WebContents::GetTitle() will return the page's URL if there's no <title>
411 // specified. However, we'd prefer to show the name of the extension in that
412 // case, so we directly inspect the NavigationEntry's title.
413 base::string16 title
;
414 if (!web_contents() ||
415 !web_contents()->GetController().GetActiveEntry() ||
416 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) {
417 title
= base::UTF8ToUTF16(extension()->name());
419 title
= web_contents()->GetTitle();
421 const base::char16 kBadChars
[] = { '\n', 0 };
422 base::RemoveChars(title
, kBadChars
, &title
);
426 void ShellWindow::SetAppIconUrl(const GURL
& url
) {
427 // Avoid using any previous app icons were are being downloaded.
428 image_loader_ptr_factory_
.InvalidateWeakPtrs();
430 // Reset |app_icon_image_| to abort pending image load (if any).
431 app_icon_image_
.reset();
434 web_contents()->DownloadImage(
436 true, // is a favicon
437 0, // no maximum size
438 base::Bind(&ShellWindow::DidDownloadFavicon
,
439 image_loader_ptr_factory_
.GetWeakPtr()));
442 void ShellWindow::UpdateShape(scoped_ptr
<SkRegion
> region
) {
443 native_app_window_
->UpdateShape(region
.Pass());
446 void ShellWindow::UpdateDraggableRegions(
447 const std::vector
<extensions::DraggableRegion
>& regions
) {
448 native_app_window_
->UpdateDraggableRegions(regions
);
451 void ShellWindow::UpdateAppIcon(const gfx::Image
& image
) {
455 native_app_window_
->UpdateWindowIcon();
456 ShellWindowRegistry::Get(profile_
)->ShellWindowIconChanged(this);
459 void ShellWindow::Fullscreen() {
460 #if !defined(OS_MACOSX)
461 // Do not enter fullscreen mode if disallowed by pref.
462 if (!profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed
))
465 fullscreen_types_
|= FULLSCREEN_TYPE_WINDOW_API
;
466 SetNativeWindowFullscreen();
469 void ShellWindow::Maximize() {
470 GetBaseWindow()->Maximize();
473 void ShellWindow::Minimize() {
474 GetBaseWindow()->Minimize();
477 void ShellWindow::Restore() {
478 if (IsFullscreen(fullscreen_types_
)) {
479 fullscreen_types_
= FULLSCREEN_TYPE_NONE
;
480 SetNativeWindowFullscreen();
482 GetBaseWindow()->Restore();
486 void ShellWindow::OSFullscreen() {
487 #if !defined(OS_MACOSX)
488 // Do not enter fullscreen mode if disallowed by pref.
489 if (!profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed
))
492 fullscreen_types_
|= FULLSCREEN_TYPE_OS
;
493 SetNativeWindowFullscreen();
496 void ShellWindow::ForcedFullscreen() {
497 fullscreen_types_
|= FULLSCREEN_TYPE_FORCED
;
498 SetNativeWindowFullscreen();
501 void ShellWindow::SetMinimumSize(const gfx::Size
& min_size
) {
502 size_constraints_
.set_minimum_size(min_size
);
503 OnSizeConstraintsChanged();
506 void ShellWindow::SetMaximumSize(const gfx::Size
& max_size
) {
507 size_constraints_
.set_maximum_size(max_size
);
508 OnSizeConstraintsChanged();
511 void ShellWindow::Show(ShowType show_type
) {
512 if (CommandLine::ForCurrentProcess()->HasSwitch(
513 switches::kEnableAppsShowOnFirstPaint
)) {
514 show_on_first_paint_
= true;
516 if (!first_paint_complete_
) {
517 delayed_show_type_
= show_type
;
524 GetBaseWindow()->Show();
527 GetBaseWindow()->ShowInactive();
532 void ShellWindow::Hide() {
533 // This is there to prevent race conditions with Hide() being called before
534 // there was a non-empty paint. It should have no effect in a non-racy
535 // scenario where the application is hiding then showing a window: the second
536 // show will not be delayed.
537 show_on_first_paint_
= false;
538 GetBaseWindow()->Hide();
541 void ShellWindow::SetAlwaysOnTop(bool always_on_top
) {
542 if (cached_always_on_top_
== always_on_top
)
545 cached_always_on_top_
= always_on_top
;
547 // As a security measure, do not allow fullscreen windows or windows that
548 // overlap the taskbar to be on top. The property will be applied when the
549 // window exits fullscreen and moves away from the taskbar.
550 if (!IsFullscreen(fullscreen_types_
) && !IntersectsWithTaskbar())
551 native_app_window_
->SetAlwaysOnTop(always_on_top
);
553 OnNativeWindowChanged();
556 bool ShellWindow::IsAlwaysOnTop() const {
557 return cached_always_on_top_
;
560 //------------------------------------------------------------------------------
563 void ShellWindow::DidDownloadFavicon(
565 int http_status_code
,
566 const GURL
& image_url
,
567 const std::vector
<SkBitmap
>& bitmaps
,
568 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
569 if (image_url
!= app_icon_url_
|| bitmaps
.empty())
572 // Bitmaps are ordered largest to smallest. Choose the smallest bitmap
573 // whose height >= the preferred size.
574 int largest_index
= 0;
575 for (size_t i
= 1; i
< bitmaps
.size(); ++i
) {
576 if (bitmaps
[i
].height() < delegate_
->PreferredIconSize())
580 const SkBitmap
& largest
= bitmaps
[largest_index
];
581 UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest
));
584 void ShellWindow::OnExtensionIconImageChanged(extensions::IconImage
* image
) {
585 DCHECK_EQ(app_icon_image_
.get(), image
);
587 UpdateAppIcon(gfx::Image(app_icon_image_
->image_skia()));
590 void ShellWindow::UpdateExtensionAppIcon() {
591 // Avoid using any previous app icons were are being downloaded.
592 image_loader_ptr_factory_
.InvalidateWeakPtrs();
594 app_icon_image_
.reset(new extensions::IconImage(
597 extensions::IconsInfo::GetIcons(extension()),
598 delegate_
->PreferredIconSize(),
599 extensions::IconsInfo::GetDefaultAppIcon(),
602 // Triggers actual image loading with 1x resources. The 2x resource will
603 // be handled by IconImage class when requested.
604 app_icon_image_
->image_skia().GetRepresentation(1.0f
);
607 void ShellWindow::OnSizeConstraintsChanged() {
608 native_app_window_
->UpdateWindowMinMaxSize();
609 gfx::Rect bounds
= GetClientBounds();
610 gfx::Size constrained_size
= size_constraints_
.ClampSize(bounds
.size());
611 if (bounds
.size() != constrained_size
) {
612 bounds
.set_size(constrained_size
);
613 native_app_window_
->SetBounds(bounds
);
615 OnNativeWindowChanged();
618 void ShellWindow::SetNativeWindowFullscreen() {
619 native_app_window_
->SetFullscreen(fullscreen_types_
);
621 if (cached_always_on_top_
)
622 UpdateNativeAlwaysOnTop();
625 bool ShellWindow::IntersectsWithTaskbar() const {
627 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
628 gfx::Rect window_bounds
= native_app_window_
->GetRestoredBounds();
629 std::vector
<gfx::Display
> displays
= screen
->GetAllDisplays();
631 for (std::vector
<gfx::Display
>::const_iterator it
= displays
.begin();
632 it
!= displays
.end(); ++it
) {
633 gfx::Rect taskbar_bounds
= it
->bounds();
634 taskbar_bounds
.Subtract(it
->work_area());
635 if (taskbar_bounds
.IsEmpty())
638 if (window_bounds
.Intersects(taskbar_bounds
))
646 void ShellWindow::UpdateNativeAlwaysOnTop() {
647 DCHECK(cached_always_on_top_
);
648 bool is_on_top
= native_app_window_
->IsAlwaysOnTop();
649 bool fullscreen
= IsFullscreen(fullscreen_types_
);
650 bool intersects_taskbar
= IntersectsWithTaskbar();
652 if (is_on_top
&& (fullscreen
|| intersects_taskbar
)) {
653 // When entering fullscreen or overlapping the taskbar, ensure windows are
654 // not always-on-top.
655 native_app_window_
->SetAlwaysOnTop(false);
656 } else if (!is_on_top
&& !fullscreen
&& !intersects_taskbar
) {
657 // When exiting fullscreen and moving away from the taskbar, reinstate
659 native_app_window_
->SetAlwaysOnTop(true);
663 void ShellWindow::CloseContents(WebContents
* contents
) {
664 native_app_window_
->Close();
667 bool ShellWindow::ShouldSuppressDialogs() {
671 content::ColorChooser
* ShellWindow::OpenColorChooser(
672 WebContents
* web_contents
,
673 SkColor initial_color
,
674 const std::vector
<content::ColorSuggestion
>& suggestionss
) {
675 return delegate_
->ShowColorChooser(web_contents
, initial_color
);
678 void ShellWindow::RunFileChooser(WebContents
* tab
,
679 const content::FileChooserParams
& params
) {
680 if (window_type_is_panel()) {
681 // Panels can't host a file dialog, abort. TODO(stevenjb): allow file
682 // dialogs to be unhosted but still close with the owning web contents.
684 LOG(WARNING
) << "File dialog opened by panel.";
688 delegate_
->RunFileChooser(tab
, params
);
691 bool ShellWindow::IsPopupOrPanel(const WebContents
* source
) const {
695 void ShellWindow::MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {
696 native_app_window_
->SetBounds(pos
);
699 void ShellWindow::NavigationStateChanged(
700 const content::WebContents
* source
, unsigned changed_flags
) {
701 if (changed_flags
& content::INVALIDATE_TYPE_TITLE
)
702 native_app_window_
->UpdateWindowTitle();
703 else if (changed_flags
& content::INVALIDATE_TYPE_TAB
)
704 native_app_window_
->UpdateWindowIcon();
707 void ShellWindow::ToggleFullscreenModeForTab(content::WebContents
* source
,
708 bool enter_fullscreen
) {
709 #if !defined(OS_MACOSX)
710 // Do not enter fullscreen mode if disallowed by pref.
711 // TODO(bartfab): Add a test once it becomes possible to simulate a user
712 // gesture. http://crbug.com/174178
713 if (enter_fullscreen
&&
714 !profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed
)) {
719 if (!IsExtensionWithPermissionOrSuggestInConsole(
720 APIPermission::kFullscreen
,
722 source
->GetRenderViewHost())) {
726 if (enter_fullscreen
)
727 fullscreen_types_
|= FULLSCREEN_TYPE_HTML_API
;
729 fullscreen_types_
&= ~FULLSCREEN_TYPE_HTML_API
;
730 SetNativeWindowFullscreen();
733 bool ShellWindow::IsFullscreenForTabOrPending(
734 const content::WebContents
* source
) const {
735 return ((fullscreen_types_
& FULLSCREEN_TYPE_HTML_API
) != 0);
738 void ShellWindow::Observe(int type
,
739 const content::NotificationSource
& source
,
740 const content::NotificationDetails
& details
) {
742 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
743 const extensions::Extension
* unloaded_extension
=
744 content::Details
<extensions::UnloadedExtensionInfo
>(
746 if (extension_
== unloaded_extension
)
747 native_app_window_
->Close();
750 case chrome::NOTIFICATION_APP_TERMINATING
:
751 native_app_window_
->Close();
754 NOTREACHED() << "Received unexpected notification";
758 void ShellWindow::SetWebContentsBlocked(content::WebContents
* web_contents
,
760 delegate_
->SetWebContentsBlocked(web_contents
, blocked
);
763 bool ShellWindow::IsWebContentsVisible(content::WebContents
* web_contents
) {
764 return delegate_
->IsWebContentsVisible(web_contents
);
767 extensions::ActiveTabPermissionGranter
*
768 ShellWindow::GetActiveTabPermissionGranter() {
769 // Shell windows don't support the activeTab permission.
773 WebContentsModalDialogHost
* ShellWindow::GetWebContentsModalDialogHost() {
774 return native_app_window_
.get();
777 void ShellWindow::AddMessageToDevToolsConsole(ConsoleMessageLevel level
,
778 const std::string
& message
) {
779 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
780 rvh
->Send(new ExtensionMsg_AddMessageToConsole(
781 rvh
->GetRoutingID(), level
, message
));
784 void ShellWindow::SaveWindowPosition() {
785 if (window_key_
.empty())
787 if (!native_app_window_
)
790 ShellWindowGeometryCache
* cache
= ShellWindowGeometryCache::Get(profile());
792 gfx::Rect bounds
= native_app_window_
->GetRestoredBounds();
793 bounds
.Inset(native_app_window_
->GetFrameInsets());
794 gfx::Rect screen_bounds
=
795 gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds
).work_area();
796 ui::WindowShowState window_state
= native_app_window_
->GetRestoredState();
797 cache
->SaveGeometry(extension()->id(),
804 void ShellWindow::AdjustBoundsToBeVisibleOnScreen(
805 const gfx::Rect
& cached_bounds
,
806 const gfx::Rect
& cached_screen_bounds
,
807 const gfx::Rect
& current_screen_bounds
,
808 const gfx::Size
& minimum_size
,
809 gfx::Rect
* bounds
) const {
810 *bounds
= cached_bounds
;
812 // Reposition and resize the bounds if the cached_screen_bounds is different
813 // from the current screen bounds and the current screen bounds doesn't
814 // completely contain the bounds.
815 if (cached_screen_bounds
!= current_screen_bounds
&&
816 !current_screen_bounds
.Contains(cached_bounds
)) {
818 std::max(minimum_size
.width(),
819 std::min(bounds
->width(), current_screen_bounds
.width())));
821 std::max(minimum_size
.height(),
822 std::min(bounds
->height(), current_screen_bounds
.height())));
824 std::max(current_screen_bounds
.x(),
825 std::min(bounds
->x(),
826 current_screen_bounds
.right() - bounds
->width())));
828 std::max(current_screen_bounds
.y(),
829 std::min(bounds
->y(),
830 current_screen_bounds
.bottom() - bounds
->height())));
834 ShellWindow::CreateParams
ShellWindow::LoadDefaultsAndConstrain(
835 CreateParams params
) const {
836 if (params
.bounds
.width() == 0)
837 params
.bounds
.set_width(kDefaultWidth
);
838 if (params
.bounds
.height() == 0)
839 params
.bounds
.set_height(kDefaultHeight
);
841 // If left and top are left undefined, the native shell window will center
842 // the window on the main screen in a platform-defined manner.
844 // Load cached state if it exists.
845 if (!params
.window_key
.empty()) {
846 ShellWindowGeometryCache
* cache
= ShellWindowGeometryCache::Get(profile());
848 gfx::Rect cached_bounds
;
849 gfx::Rect cached_screen_bounds
;
850 ui::WindowShowState cached_state
= ui::SHOW_STATE_DEFAULT
;
851 if (cache
->GetGeometry(extension()->id(), params
.window_key
,
852 &cached_bounds
, &cached_screen_bounds
,
854 // App window has cached screen bounds, make sure it fits on screen in
855 // case the screen resolution changed.
856 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
857 gfx::Display display
= screen
->GetDisplayMatching(cached_bounds
);
858 gfx::Rect current_screen_bounds
= display
.work_area();
859 AdjustBoundsToBeVisibleOnScreen(cached_bounds
,
860 cached_screen_bounds
,
861 current_screen_bounds
,
864 params
.state
= cached_state
;
868 SizeConstraints
size_constraints(params
.minimum_size
, params
.maximum_size
);
869 params
.bounds
.set_size(size_constraints
.ClampSize(params
.bounds
.size()));
870 params
.minimum_size
= size_constraints
.GetMinimumSize();
871 params
.maximum_size
= size_constraints
.GetMaximumSize();
877 SkRegion
* ShellWindow::RawDraggableRegionsToSkRegion(
878 const std::vector
<extensions::DraggableRegion
>& regions
) {
879 SkRegion
* sk_region
= new SkRegion
;
880 for (std::vector
<extensions::DraggableRegion
>::const_iterator iter
=
882 iter
!= regions
.end(); ++iter
) {
883 const extensions::DraggableRegion
& region
= *iter
;
887 region
.bounds
.right(),
888 region
.bounds
.bottom(),
889 region
.draggable
? SkRegion::kUnion_Op
: SkRegion::kDifference_Op
);