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
;
53 const int kDefaultWidth
= 512;
54 const int kDefaultHeight
= 384;
60 ShellWindow::SizeConstraints::SizeConstraints()
61 : maximum_size_(kUnboundedSize
, kUnboundedSize
) {
64 ShellWindow::SizeConstraints::SizeConstraints(const gfx::Size
& min_size
,
65 const gfx::Size
& max_size
)
66 : minimum_size_(min_size
),
67 maximum_size_(max_size
) {
70 ShellWindow::SizeConstraints::~SizeConstraints() {}
72 gfx::Size
ShellWindow::SizeConstraints::ClampSize(gfx::Size size
) const {
73 const gfx::Size max_size
= GetMaximumSize();
74 if (max_size
.width() != kUnboundedSize
)
75 size
.set_width(std::min(size
.width(), GetMaximumSize().width()));
76 if (max_size
.height() != kUnboundedSize
)
77 size
.set_height(std::min(size
.height(), GetMaximumSize().height()));
78 size
.SetToMax(GetMinimumSize());
82 bool ShellWindow::SizeConstraints::HasMinimumSize() const {
83 return GetMinimumSize().width() != kUnboundedSize
||
84 GetMinimumSize().height() != kUnboundedSize
;
87 bool ShellWindow::SizeConstraints::HasMaximumSize() const {
88 const gfx::Size max_size
= GetMaximumSize();
89 return max_size
.width() != kUnboundedSize
||
90 max_size
.height() != kUnboundedSize
;
93 bool ShellWindow::SizeConstraints::HasFixedSize() const {
94 return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize();
97 gfx::Size
ShellWindow::SizeConstraints::GetMinimumSize() const {
101 gfx::Size
ShellWindow::SizeConstraints::GetMaximumSize() const {
103 maximum_size_
.width() == kUnboundedSize
?
105 std::max(maximum_size_
.width(), minimum_size_
.width()),
106 maximum_size_
.height() == kUnboundedSize
?
108 std::max(maximum_size_
.height(), minimum_size_
.height()));
111 void ShellWindow::SizeConstraints::set_minimum_size(const gfx::Size
& min_size
) {
112 minimum_size_
= min_size
;
115 void ShellWindow::SizeConstraints::set_maximum_size(const gfx::Size
& max_size
) {
116 maximum_size_
= max_size
;
119 ShellWindow::CreateParams::CreateParams()
120 : window_type(ShellWindow::WINDOW_TYPE_DEFAULT
),
121 frame(ShellWindow::FRAME_CHROME
),
122 transparent_background(false),
123 bounds(INT_MIN
, INT_MIN
, 0, 0),
124 creator_process_id(0),
125 state(ui::SHOW_STATE_DEFAULT
),
129 always_on_top(false) {}
131 ShellWindow::CreateParams::~CreateParams() {}
133 ShellWindow::Delegate::~Delegate() {}
135 ShellWindow::ShellWindow(Profile
* profile
,
137 const extensions::Extension
* extension
)
139 extension_(extension
),
140 extension_id_(extension
->id()),
141 window_type_(WINDOW_TYPE_DEFAULT
),
143 image_loader_ptr_factory_(this),
144 fullscreen_types_(FULLSCREEN_TYPE_NONE
),
145 show_on_first_paint_(false),
146 first_paint_complete_(false),
147 cached_always_on_top_(false) {
150 void ShellWindow::Init(const GURL
& url
,
151 ShellWindowContents
* shell_window_contents
,
152 const CreateParams
& params
) {
153 // Initialize the render interface and web contents
154 shell_window_contents_
.reset(shell_window_contents
);
155 shell_window_contents_
->Initialize(profile(), url
);
156 WebContents
* web_contents
= shell_window_contents_
->GetWebContents();
157 if (CommandLine::ForCurrentProcess()->HasSwitch(
158 switches::kEnableAppsShowOnFirstPaint
)) {
159 content::WebContentsObserver::Observe(web_contents
);
161 delegate_
->InitWebContents(web_contents
);
162 WebContentsModalDialogManager::CreateForWebContents(web_contents
);
163 extensions::ExtensionWebContentsObserver::CreateForWebContents(web_contents
);
165 web_contents
->SetDelegate(this);
166 WebContentsModalDialogManager::FromWebContents(web_contents
)->
168 extensions::SetViewType(web_contents
, extensions::VIEW_TYPE_APP_SHELL
);
170 // Initialize the window
171 CreateParams new_params
= LoadDefaultsAndConstrain(params
);
172 window_type_
= new_params
.window_type
;
173 window_key_
= new_params
.window_key
;
174 size_constraints_
= SizeConstraints(new_params
.minimum_size
,
175 new_params
.maximum_size
);
177 // Windows cannot be always-on-top in fullscreen mode for security reasons.
178 cached_always_on_top_
= new_params
.always_on_top
;
179 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
180 new_params
.always_on_top
= false;
182 native_app_window_
.reset(delegate_
->CreateNativeAppWindow(this, new_params
));
184 if (!new_params
.hidden
) {
185 // Panels are not activated by default.
186 Show(window_type_is_panel() || !new_params
.focused
? SHOW_INACTIVE
190 if (new_params
.state
== ui::SHOW_STATE_FULLSCREEN
)
192 else if (new_params
.state
== ui::SHOW_STATE_MAXIMIZED
)
194 else if (new_params
.state
== ui::SHOW_STATE_MINIMIZED
)
197 OnNativeWindowChanged();
199 // When the render view host is changed, the native window needs to know
200 // about it in case it has any setup to do to make the renderer appear
201 // properly. In particular, on Windows, the view's clickthrough region needs
203 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED
,
204 content::Source
<Profile
>(profile_
));
205 // Close when the browser process is exiting.
206 registrar_
.Add(this, chrome::NOTIFICATION_APP_TERMINATING
,
207 content::NotificationService::AllSources());
209 shell_window_contents_
->LoadContents(new_params
.creator_process_id
);
211 if (CommandLine::ForCurrentProcess()->HasSwitch(
212 switches::kEnableAppsShowOnFirstPaint
)) {
213 // We want to show the window only when the content has been painted. For
214 // that to happen, we need to define a size for the content, otherwise the
215 // layout will happen in a 0x0 area.
216 // Note: WebContents::GetView() is guaranteed to be non-null.
217 web_contents
->GetView()->SizeContents(new_params
.bounds
.size());
220 // Prevent the browser process from shutting down while this window is open.
221 chrome::StartKeepAlive();
223 UpdateExtensionAppIcon();
225 ShellWindowRegistry::Get(profile_
)->AddShellWindow(this);
228 ShellWindow::~ShellWindow() {
229 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the
231 registrar_
.RemoveAll();
233 // Remove shutdown prevention.
234 chrome::EndKeepAlive();
237 void ShellWindow::RequestMediaAccessPermission(
238 content::WebContents
* web_contents
,
239 const content::MediaStreamRequest
& request
,
240 const content::MediaResponseCallback
& callback
) {
241 delegate_
->RequestMediaAccessPermission(web_contents
, request
, callback
,
245 WebContents
* ShellWindow::OpenURLFromTab(WebContents
* source
,
246 const content::OpenURLParams
& params
) {
247 // Don't allow the current tab to be navigated. It would be nice to map all
248 // anchor tags (even those without target="_blank") to new tabs, but right
249 // now we can't distinguish between those and <meta> refreshes or window.href
250 // navigations, which we don't want to allow.
251 // TOOD(mihaip): Can we check for user gestures instead?
252 WindowOpenDisposition disposition
= params
.disposition
;
253 if (disposition
== CURRENT_TAB
) {
254 AddMessageToDevToolsConsole(
255 content::CONSOLE_MESSAGE_LEVEL_ERROR
,
257 "Can't open same-window link to \"%s\"; try target=\"_blank\".",
258 params
.url
.spec().c_str()));
262 // These dispositions aren't really navigations.
263 if (disposition
== SUPPRESS_OPEN
|| disposition
== SAVE_TO_DISK
||
264 disposition
== IGNORE_ACTION
) {
268 WebContents
* contents
= delegate_
->OpenURLFromTab(profile_
, source
,
271 AddMessageToDevToolsConsole(
272 content::CONSOLE_MESSAGE_LEVEL_ERROR
,
274 "Can't navigate to \"%s\"; apps do not support navigation.",
275 params
.url
.spec().c_str()));
281 void ShellWindow::AddNewContents(WebContents
* source
,
282 WebContents
* new_contents
,
283 WindowOpenDisposition disposition
,
284 const gfx::Rect
& initial_pos
,
287 DCHECK(Profile::FromBrowserContext(new_contents
->GetBrowserContext()) ==
289 delegate_
->AddNewContents(profile_
, new_contents
, disposition
,
290 initial_pos
, user_gesture
, was_blocked
);
293 bool ShellWindow::PreHandleKeyboardEvent(
294 content::WebContents
* source
,
295 const content::NativeWebKeyboardEvent
& event
,
296 bool* is_keyboard_shortcut
) {
297 // Here, we can handle a key event before the content gets it. When we are
298 // fullscreen, we want to allow the user to leave when ESC is pressed.
299 // However, if the application has the "overrideEscFullscreen" permission, we
300 // should let it override that behavior.
301 // ::HandleKeyboardEvent() will only be called if the KeyEvent's default
302 // action is not prevented.
303 // Thus, we should handle the KeyEvent here only if the permission is not set.
304 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&&
305 (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
) &&
306 !extension_
->HasAPIPermission(APIPermission::kOverrideEscFullscreen
)) {
314 void ShellWindow::HandleKeyboardEvent(
316 const content::NativeWebKeyboardEvent
& event
) {
317 // If the window is currently fullscreen, ESC should leave fullscreen.
318 // If this code is being called for ESC, that means that the KeyEvent's
319 // default behavior was not prevented by the content.
320 if (event
.windowsKeyCode
== ui::VKEY_ESCAPE
&&
321 (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
)) {
326 native_app_window_
->HandleKeyboardEvent(event
);
329 void ShellWindow::RequestToLockMouse(WebContents
* web_contents
,
331 bool last_unlocked_by_target
) {
332 bool has_permission
= IsExtensionWithPermissionOrSuggestInConsole(
333 APIPermission::kPointerLock
,
335 web_contents
->GetRenderViewHost());
337 web_contents
->GotResponseToLockMouseRequest(has_permission
);
340 void ShellWindow::DidFirstVisuallyNonEmptyPaint(int32 page_id
) {
341 first_paint_complete_
= true;
342 if (show_on_first_paint_
) {
343 DCHECK(delayed_show_type_
== SHOW_ACTIVE
||
344 delayed_show_type_
== SHOW_INACTIVE
);
345 Show(delayed_show_type_
);
349 void ShellWindow::OnNativeClose() {
350 ShellWindowRegistry::Get(profile_
)->RemoveShellWindow(this);
351 if (shell_window_contents_
) {
352 WebContents
* web_contents
= shell_window_contents_
->GetWebContents();
353 WebContentsModalDialogManager::FromWebContents(web_contents
)->
355 shell_window_contents_
->NativeWindowClosed();
360 void ShellWindow::OnNativeWindowChanged() {
361 SaveWindowPosition();
362 if (shell_window_contents_
&& native_app_window_
)
363 shell_window_contents_
->NativeWindowChanged(native_app_window_
.get());
366 void ShellWindow::OnNativeWindowActivated() {
367 ShellWindowRegistry::Get(profile_
)->ShellWindowActivated(this);
370 content::WebContents
* ShellWindow::web_contents() const {
371 return shell_window_contents_
->GetWebContents();
374 NativeAppWindow
* ShellWindow::GetBaseWindow() {
375 return native_app_window_
.get();
378 gfx::NativeWindow
ShellWindow::GetNativeWindow() {
379 return GetBaseWindow()->GetNativeWindow();
382 gfx::Rect
ShellWindow::GetClientBounds() const {
383 gfx::Rect bounds
= native_app_window_
->GetBounds();
384 bounds
.Inset(native_app_window_
->GetFrameInsets());
388 base::string16
ShellWindow::GetTitle() const {
389 // WebContents::GetTitle() will return the page's URL if there's no <title>
390 // specified. However, we'd prefer to show the name of the extension in that
391 // case, so we directly inspect the NavigationEntry's title.
392 base::string16 title
;
393 if (!web_contents() ||
394 !web_contents()->GetController().GetActiveEntry() ||
395 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) {
396 title
= UTF8ToUTF16(extension()->name());
398 title
= web_contents()->GetTitle();
400 const base::char16 kBadChars
[] = { '\n', 0 };
401 base::RemoveChars(title
, kBadChars
, &title
);
405 void ShellWindow::SetAppIconUrl(const GURL
& url
) {
406 // Avoid using any previous app icons were are being downloaded.
407 image_loader_ptr_factory_
.InvalidateWeakPtrs();
409 // Reset |app_icon_image_| to abort pending image load (if any).
410 app_icon_image_
.reset();
413 web_contents()->DownloadImage(
415 true, // is a favicon
416 0, // no maximum size
417 base::Bind(&ShellWindow::DidDownloadFavicon
,
418 image_loader_ptr_factory_
.GetWeakPtr()));
421 void ShellWindow::UpdateShape(scoped_ptr
<SkRegion
> region
) {
422 native_app_window_
->UpdateShape(region
.Pass());
425 void ShellWindow::UpdateDraggableRegions(
426 const std::vector
<extensions::DraggableRegion
>& regions
) {
427 native_app_window_
->UpdateDraggableRegions(regions
);
430 void ShellWindow::UpdateAppIcon(const gfx::Image
& image
) {
434 native_app_window_
->UpdateWindowIcon();
435 ShellWindowRegistry::Get(profile_
)->ShellWindowIconChanged(this);
438 void ShellWindow::Fullscreen() {
439 #if !defined(OS_MACOSX)
440 // Do not enter fullscreen mode if disallowed by pref.
441 if (!profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed
))
444 fullscreen_types_
|= FULLSCREEN_TYPE_WINDOW_API
;
445 SetNativeWindowFullscreen(fullscreen_types_
);
448 void ShellWindow::Maximize() {
449 GetBaseWindow()->Maximize();
452 void ShellWindow::Minimize() {
453 GetBaseWindow()->Minimize();
456 void ShellWindow::Restore() {
457 if (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
) {
458 fullscreen_types_
= FULLSCREEN_TYPE_NONE
;
459 SetNativeWindowFullscreen(fullscreen_types_
);
461 GetBaseWindow()->Restore();
465 void ShellWindow::OSFullscreen() {
466 #if !defined(OS_MACOSX)
467 // Do not enter fullscreen mode if disallowed by pref.
468 if (!profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed
))
471 fullscreen_types_
|= FULLSCREEN_TYPE_OS
;
472 SetNativeWindowFullscreen(fullscreen_types_
);
475 void ShellWindow::SetMinimumSize(const gfx::Size
& min_size
) {
476 size_constraints_
.set_minimum_size(min_size
);
477 OnSizeConstraintsChanged();
480 void ShellWindow::SetMaximumSize(const gfx::Size
& max_size
) {
481 size_constraints_
.set_maximum_size(max_size
);
482 OnSizeConstraintsChanged();
485 void ShellWindow::Show(ShowType show_type
) {
486 if (CommandLine::ForCurrentProcess()->HasSwitch(
487 switches::kEnableAppsShowOnFirstPaint
)) {
488 show_on_first_paint_
= true;
490 if (!first_paint_complete_
) {
491 delayed_show_type_
= show_type
;
498 GetBaseWindow()->Show();
501 GetBaseWindow()->ShowInactive();
506 void ShellWindow::Hide() {
507 // This is there to prevent race conditions with Hide() being called before
508 // there was a non-empty paint. It should have no effect in a non-racy
509 // scenario where the application is hiding then showing a window: the second
510 // show will not be delayed.
511 show_on_first_paint_
= false;
512 GetBaseWindow()->Hide();
515 void ShellWindow::SetAlwaysOnTop(bool always_on_top
) {
516 if (cached_always_on_top_
== always_on_top
)
519 cached_always_on_top_
= always_on_top
;
521 // As a security measure, do not allow fullscreen windows to be on top.
522 // The property will be applied when the window exits fullscreen.
523 bool fullscreen
= (fullscreen_types_
!= FULLSCREEN_TYPE_NONE
);
525 native_app_window_
->SetAlwaysOnTop(always_on_top
);
527 OnNativeWindowChanged();
530 bool ShellWindow::IsAlwaysOnTop() const {
531 return cached_always_on_top_
;
534 //------------------------------------------------------------------------------
537 void ShellWindow::DidDownloadFavicon(
539 int http_status_code
,
540 const GURL
& image_url
,
541 const std::vector
<SkBitmap
>& bitmaps
,
542 const std::vector
<gfx::Size
>& original_bitmap_sizes
) {
543 if (image_url
!= app_icon_url_
|| bitmaps
.empty())
546 // Bitmaps are ordered largest to smallest. Choose the smallest bitmap
547 // whose height >= the preferred size.
548 int largest_index
= 0;
549 for (size_t i
= 1; i
< bitmaps
.size(); ++i
) {
550 if (bitmaps
[i
].height() < delegate_
->PreferredIconSize())
554 const SkBitmap
& largest
= bitmaps
[largest_index
];
555 UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest
));
558 void ShellWindow::OnExtensionIconImageChanged(extensions::IconImage
* image
) {
559 DCHECK_EQ(app_icon_image_
.get(), image
);
561 UpdateAppIcon(gfx::Image(app_icon_image_
->image_skia()));
564 void ShellWindow::UpdateExtensionAppIcon() {
565 // Avoid using any previous app icons were are being downloaded.
566 image_loader_ptr_factory_
.InvalidateWeakPtrs();
568 app_icon_image_
.reset(new extensions::IconImage(
571 extensions::IconsInfo::GetIcons(extension()),
572 delegate_
->PreferredIconSize(),
573 extensions::IconsInfo::GetDefaultAppIcon(),
576 // Triggers actual image loading with 1x resources. The 2x resource will
577 // be handled by IconImage class when requested.
578 app_icon_image_
->image_skia().GetRepresentation(1.0f
);
581 void ShellWindow::OnSizeConstraintsChanged() {
582 native_app_window_
->UpdateWindowMinMaxSize();
583 gfx::Rect bounds
= GetClientBounds();
584 gfx::Size constrained_size
= size_constraints_
.ClampSize(bounds
.size());
585 if (bounds
.size() != constrained_size
) {
586 bounds
.set_size(constrained_size
);
587 native_app_window_
->SetBounds(bounds
);
589 OnNativeWindowChanged();
592 void ShellWindow::SetNativeWindowFullscreen(int fullscreen_types
) {
593 native_app_window_
->SetFullscreen(fullscreen_types
);
595 if (!cached_always_on_top_
)
598 bool is_on_top
= native_app_window_
->IsAlwaysOnTop();
599 bool fullscreen
= (fullscreen_types
!= FULLSCREEN_TYPE_NONE
);
600 if (fullscreen
&& is_on_top
) {
601 // When entering fullscreen, ensure windows are not always-on-top.
602 native_app_window_
->SetAlwaysOnTop(false);
603 } else if (!fullscreen
&& !is_on_top
) {
604 // When exiting fullscreen, reinstate always-on-top.
605 native_app_window_
->SetAlwaysOnTop(true);
609 void ShellWindow::CloseContents(WebContents
* contents
) {
610 native_app_window_
->Close();
613 bool ShellWindow::ShouldSuppressDialogs() {
617 content::ColorChooser
* ShellWindow::OpenColorChooser(
618 WebContents
* web_contents
,
619 SkColor initial_color
,
620 const std::vector
<content::ColorSuggestion
>& suggestionss
) {
621 return delegate_
->ShowColorChooser(web_contents
, initial_color
);
624 void ShellWindow::RunFileChooser(WebContents
* tab
,
625 const content::FileChooserParams
& params
) {
626 if (window_type_is_panel()) {
627 // Panels can't host a file dialog, abort. TODO(stevenjb): allow file
628 // dialogs to be unhosted but still close with the owning web contents.
630 LOG(WARNING
) << "File dialog opened by panel.";
634 delegate_
->RunFileChooser(tab
, params
);
637 bool ShellWindow::IsPopupOrPanel(const WebContents
* source
) const {
641 void ShellWindow::MoveContents(WebContents
* source
, const gfx::Rect
& pos
) {
642 native_app_window_
->SetBounds(pos
);
645 void ShellWindow::NavigationStateChanged(
646 const content::WebContents
* source
, unsigned changed_flags
) {
647 if (changed_flags
& content::INVALIDATE_TYPE_TITLE
)
648 native_app_window_
->UpdateWindowTitle();
649 else if (changed_flags
& content::INVALIDATE_TYPE_TAB
)
650 native_app_window_
->UpdateWindowIcon();
653 void ShellWindow::ToggleFullscreenModeForTab(content::WebContents
* source
,
654 bool enter_fullscreen
) {
655 #if !defined(OS_MACOSX)
656 // Do not enter fullscreen mode if disallowed by pref.
657 // TODO(bartfab): Add a test once it becomes possible to simulate a user
658 // gesture. http://crbug.com/174178
659 if (enter_fullscreen
&&
660 !profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed
)) {
665 if (!IsExtensionWithPermissionOrSuggestInConsole(
666 APIPermission::kFullscreen
,
668 source
->GetRenderViewHost())) {
672 if (enter_fullscreen
)
673 fullscreen_types_
|= FULLSCREEN_TYPE_HTML_API
;
675 fullscreen_types_
&= ~FULLSCREEN_TYPE_HTML_API
;
676 SetNativeWindowFullscreen(fullscreen_types_
);
679 bool ShellWindow::IsFullscreenForTabOrPending(
680 const content::WebContents
* source
) const {
681 return ((fullscreen_types_
& FULLSCREEN_TYPE_HTML_API
) != 0);
684 void ShellWindow::Observe(int type
,
685 const content::NotificationSource
& source
,
686 const content::NotificationDetails
& details
) {
688 case chrome::NOTIFICATION_EXTENSION_UNLOADED
: {
689 const extensions::Extension
* unloaded_extension
=
690 content::Details
<extensions::UnloadedExtensionInfo
>(
692 if (extension_
== unloaded_extension
)
693 native_app_window_
->Close();
696 case chrome::NOTIFICATION_APP_TERMINATING
:
697 native_app_window_
->Close();
700 NOTREACHED() << "Received unexpected notification";
704 void ShellWindow::SetWebContentsBlocked(content::WebContents
* web_contents
,
706 delegate_
->SetWebContentsBlocked(web_contents
, blocked
);
709 bool ShellWindow::IsWebContentsVisible(content::WebContents
* web_contents
) {
710 return delegate_
->IsWebContentsVisible(web_contents
);
713 extensions::ActiveTabPermissionGranter
*
714 ShellWindow::GetActiveTabPermissionGranter() {
715 // Shell windows don't support the activeTab permission.
719 WebContentsModalDialogHost
* ShellWindow::GetWebContentsModalDialogHost() {
720 return native_app_window_
.get();
723 void ShellWindow::AddMessageToDevToolsConsole(ConsoleMessageLevel level
,
724 const std::string
& message
) {
725 content::RenderViewHost
* rvh
= web_contents()->GetRenderViewHost();
726 rvh
->Send(new ExtensionMsg_AddMessageToConsole(
727 rvh
->GetRoutingID(), level
, message
));
730 void ShellWindow::SaveWindowPosition() {
731 if (window_key_
.empty())
733 if (!native_app_window_
)
736 ShellWindowGeometryCache
* cache
= ShellWindowGeometryCache::Get(profile());
738 gfx::Rect bounds
= native_app_window_
->GetRestoredBounds();
739 bounds
.Inset(native_app_window_
->GetFrameInsets());
740 gfx::Rect screen_bounds
=
741 gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds
).work_area();
742 ui::WindowShowState window_state
= native_app_window_
->GetRestoredState();
743 cache
->SaveGeometry(extension()->id(),
750 void ShellWindow::AdjustBoundsToBeVisibleOnScreen(
751 const gfx::Rect
& cached_bounds
,
752 const gfx::Rect
& cached_screen_bounds
,
753 const gfx::Rect
& current_screen_bounds
,
754 const gfx::Size
& minimum_size
,
755 gfx::Rect
* bounds
) const {
756 *bounds
= cached_bounds
;
758 // Reposition and resize the bounds if the cached_screen_bounds is different
759 // from the current screen bounds and the current screen bounds doesn't
760 // completely contain the bounds.
761 if (cached_screen_bounds
!= current_screen_bounds
&&
762 !current_screen_bounds
.Contains(cached_bounds
)) {
764 std::max(minimum_size
.width(),
765 std::min(bounds
->width(), current_screen_bounds
.width())));
767 std::max(minimum_size
.height(),
768 std::min(bounds
->height(), current_screen_bounds
.height())));
770 std::max(current_screen_bounds
.x(),
771 std::min(bounds
->x(),
772 current_screen_bounds
.right() - bounds
->width())));
774 std::max(current_screen_bounds
.y(),
775 std::min(bounds
->y(),
776 current_screen_bounds
.bottom() - bounds
->height())));
780 ShellWindow::CreateParams
ShellWindow::LoadDefaultsAndConstrain(
781 CreateParams params
) const {
782 if (params
.bounds
.width() == 0)
783 params
.bounds
.set_width(kDefaultWidth
);
784 if (params
.bounds
.height() == 0)
785 params
.bounds
.set_height(kDefaultHeight
);
787 // If left and top are left undefined, the native shell window will center
788 // the window on the main screen in a platform-defined manner.
790 // Load cached state if it exists.
791 if (!params
.window_key
.empty()) {
792 ShellWindowGeometryCache
* cache
= ShellWindowGeometryCache::Get(profile());
794 gfx::Rect cached_bounds
;
795 gfx::Rect cached_screen_bounds
;
796 ui::WindowShowState cached_state
= ui::SHOW_STATE_DEFAULT
;
797 if (cache
->GetGeometry(extension()->id(), params
.window_key
,
798 &cached_bounds
, &cached_screen_bounds
,
800 // App window has cached screen bounds, make sure it fits on screen in
801 // case the screen resolution changed.
802 gfx::Screen
* screen
= gfx::Screen::GetNativeScreen();
803 gfx::Display display
= screen
->GetDisplayMatching(cached_bounds
);
804 gfx::Rect current_screen_bounds
= display
.work_area();
805 AdjustBoundsToBeVisibleOnScreen(cached_bounds
,
806 cached_screen_bounds
,
807 current_screen_bounds
,
810 params
.state
= cached_state
;
814 SizeConstraints
size_constraints(params
.minimum_size
, params
.maximum_size
);
815 params
.bounds
.set_size(size_constraints
.ClampSize(params
.bounds
.size()));
816 params
.minimum_size
= size_constraints
.GetMinimumSize();
817 params
.maximum_size
= size_constraints
.GetMaximumSize();
823 SkRegion
* ShellWindow::RawDraggableRegionsToSkRegion(
824 const std::vector
<extensions::DraggableRegion
>& regions
) {
825 SkRegion
* sk_region
= new SkRegion
;
826 for (std::vector
<extensions::DraggableRegion
>::const_iterator iter
=
828 iter
!= regions
.end(); ++iter
) {
829 const extensions::DraggableRegion
& region
= *iter
;
833 region
.bounds
.right(),
834 region
.bounds
.bottom(),
835 region
.draggable
? SkRegion::kUnion_Op
: SkRegion::kDifference_Op
);