Android WebView: add sfntly to deps whitelist.
[chromium-blink-merge.git] / apps / shell_window.cc
blob2c0b3fac68463847044925154795f0aa9c020271
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"
44 #endif
46 using content::ConsoleMessageLevel;
47 using content::WebContents;
48 using extensions::APIPermission;
49 using web_modal::WebContentsModalDialogHost;
50 using web_modal::WebContentsModalDialogManager;
52 namespace {
53 const int kDefaultWidth = 512;
54 const int kDefaultHeight = 384;
56 } // namespace
58 namespace apps {
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());
79 return size;
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 {
98 return minimum_size_;
101 gfx::Size ShellWindow::SizeConstraints::GetMaximumSize() const {
102 return gfx::Size(
103 maximum_size_.width() == kUnboundedSize ?
104 kUnboundedSize :
105 std::max(maximum_size_.width(), minimum_size_.width()),
106 maximum_size_.height() == kUnboundedSize ?
107 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),
126 hidden(false),
127 resizable(true),
128 focused(true),
129 always_on_top(false) {}
131 ShellWindow::CreateParams::~CreateParams() {}
133 ShellWindow::Delegate::~Delegate() {}
135 ShellWindow::ShellWindow(Profile* profile,
136 Delegate* delegate,
137 const extensions::Extension* extension)
138 : profile_(profile),
139 extension_(extension),
140 extension_id_(extension->id()),
141 window_type_(WINDOW_TYPE_DEFAULT),
142 delegate_(delegate),
143 image_loader_ptr_factory_(this),
144 fullscreen_types_(FULLSCREEN_TYPE_NONE),
145 show_on_first_paint_(false),
146 first_paint_complete_(false) {
149 void ShellWindow::Init(const GURL& url,
150 ShellWindowContents* shell_window_contents,
151 const CreateParams& params) {
152 // Initialize the render interface and web contents
153 shell_window_contents_.reset(shell_window_contents);
154 shell_window_contents_->Initialize(profile(), url);
155 WebContents* web_contents = shell_window_contents_->GetWebContents();
156 if (CommandLine::ForCurrentProcess()->HasSwitch(
157 switches::kEnableAppsShowOnFirstPaint)) {
158 content::WebContentsObserver::Observe(web_contents);
160 delegate_->InitWebContents(web_contents);
161 WebContentsModalDialogManager::CreateForWebContents(web_contents);
162 extensions::ExtensionWebContentsObserver::CreateForWebContents(web_contents);
164 web_contents->SetDelegate(this);
165 WebContentsModalDialogManager::FromWebContents(web_contents)->
166 SetDelegate(this);
167 extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL);
169 // Initialize the window
170 CreateParams new_params = LoadDefaultsAndConstrain(params);
171 window_type_ = new_params.window_type;
172 window_key_ = new_params.window_key;
173 size_constraints_ = SizeConstraints(new_params.minimum_size,
174 new_params.maximum_size);
175 native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params));
177 if (!new_params.hidden) {
178 // Panels are not activated by default.
179 Show(window_type_is_panel() ? SHOW_INACTIVE : SHOW_ACTIVE);
182 if (new_params.state == ui::SHOW_STATE_FULLSCREEN)
183 Fullscreen();
184 else if (new_params.state == ui::SHOW_STATE_MAXIMIZED)
185 Maximize();
186 else if (new_params.state == ui::SHOW_STATE_MINIMIZED)
187 Minimize();
189 OnNativeWindowChanged();
191 // When the render view host is changed, the native window needs to know
192 // about it in case it has any setup to do to make the renderer appear
193 // properly. In particular, on Windows, the view's clickthrough region needs
194 // to be set.
195 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED,
196 content::Source<Profile>(profile_));
197 // Close when the browser process is exiting.
198 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING,
199 content::NotificationService::AllSources());
201 shell_window_contents_->LoadContents(new_params.creator_process_id);
203 if (CommandLine::ForCurrentProcess()->HasSwitch(
204 switches::kEnableAppsShowOnFirstPaint)) {
205 // We want to show the window only when the content has been painted. For
206 // that to happen, we need to define a size for the content, otherwise the
207 // layout will happen in a 0x0 area.
208 // Note: WebContents::GetView() is guaranteed to be non-null.
209 web_contents->GetView()->SizeContents(new_params.bounds.size());
212 // Prevent the browser process from shutting down while this window is open.
213 chrome::StartKeepAlive();
215 UpdateExtensionAppIcon();
217 ShellWindowRegistry::Get(profile_)->AddShellWindow(this);
220 ShellWindow::~ShellWindow() {
221 // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the
222 // last window open.
223 registrar_.RemoveAll();
225 // Remove shutdown prevention.
226 chrome::EndKeepAlive();
229 void ShellWindow::RequestMediaAccessPermission(
230 content::WebContents* web_contents,
231 const content::MediaStreamRequest& request,
232 const content::MediaResponseCallback& callback) {
233 delegate_->RequestMediaAccessPermission(web_contents, request, callback,
234 extension());
237 WebContents* ShellWindow::OpenURLFromTab(WebContents* source,
238 const content::OpenURLParams& params) {
239 // Don't allow the current tab to be navigated. It would be nice to map all
240 // anchor tags (even those without target="_blank") to new tabs, but right
241 // now we can't distinguish between those and <meta> refreshes or window.href
242 // navigations, which we don't want to allow.
243 // TOOD(mihaip): Can we check for user gestures instead?
244 WindowOpenDisposition disposition = params.disposition;
245 if (disposition == CURRENT_TAB) {
246 AddMessageToDevToolsConsole(
247 content::CONSOLE_MESSAGE_LEVEL_ERROR,
248 base::StringPrintf(
249 "Can't open same-window link to \"%s\"; try target=\"_blank\".",
250 params.url.spec().c_str()));
251 return NULL;
254 // These dispositions aren't really navigations.
255 if (disposition == SUPPRESS_OPEN || disposition == SAVE_TO_DISK ||
256 disposition == IGNORE_ACTION) {
257 return NULL;
260 WebContents* contents = delegate_->OpenURLFromTab(profile_, source,
261 params);
262 if (!contents) {
263 AddMessageToDevToolsConsole(
264 content::CONSOLE_MESSAGE_LEVEL_ERROR,
265 base::StringPrintf(
266 "Can't navigate to \"%s\"; apps do not support navigation.",
267 params.url.spec().c_str()));
270 return contents;
273 void ShellWindow::AddNewContents(WebContents* source,
274 WebContents* new_contents,
275 WindowOpenDisposition disposition,
276 const gfx::Rect& initial_pos,
277 bool user_gesture,
278 bool* was_blocked) {
279 DCHECK(Profile::FromBrowserContext(new_contents->GetBrowserContext()) ==
280 profile_);
281 delegate_->AddNewContents(profile_, new_contents, disposition,
282 initial_pos, user_gesture, was_blocked);
285 void ShellWindow::HandleKeyboardEvent(
286 WebContents* source,
287 const content::NativeWebKeyboardEvent& event) {
288 native_app_window_->HandleKeyboardEvent(event);
291 void ShellWindow::RequestToLockMouse(WebContents* web_contents,
292 bool user_gesture,
293 bool last_unlocked_by_target) {
294 bool has_permission = IsExtensionWithPermissionOrSuggestInConsole(
295 APIPermission::kPointerLock,
296 extension_,
297 web_contents->GetRenderViewHost());
299 web_contents->GotResponseToLockMouseRequest(has_permission);
302 void ShellWindow::DidFirstVisuallyNonEmptyPaint(int32 page_id) {
303 first_paint_complete_ = true;
304 if (show_on_first_paint_) {
305 DCHECK(delayed_show_type_ == SHOW_ACTIVE ||
306 delayed_show_type_ == SHOW_INACTIVE);
307 Show(delayed_show_type_);
311 void ShellWindow::OnNativeClose() {
312 ShellWindowRegistry::Get(profile_)->RemoveShellWindow(this);
313 if (shell_window_contents_) {
314 WebContents* web_contents = shell_window_contents_->GetWebContents();
315 WebContentsModalDialogManager::FromWebContents(web_contents)->
316 SetDelegate(NULL);
317 shell_window_contents_->NativeWindowClosed();
319 delete this;
322 void ShellWindow::OnNativeWindowChanged() {
323 SaveWindowPosition();
324 if (shell_window_contents_ && native_app_window_)
325 shell_window_contents_->NativeWindowChanged(native_app_window_.get());
328 void ShellWindow::OnNativeWindowActivated() {
329 ShellWindowRegistry::Get(profile_)->ShellWindowActivated(this);
332 content::WebContents* ShellWindow::web_contents() const {
333 return shell_window_contents_->GetWebContents();
336 NativeAppWindow* ShellWindow::GetBaseWindow() {
337 return native_app_window_.get();
340 gfx::NativeWindow ShellWindow::GetNativeWindow() {
341 return GetBaseWindow()->GetNativeWindow();
344 gfx::Rect ShellWindow::GetClientBounds() const {
345 gfx::Rect bounds = native_app_window_->GetBounds();
346 bounds.Inset(native_app_window_->GetFrameInsets());
347 return bounds;
350 string16 ShellWindow::GetTitle() const {
351 // WebContents::GetTitle() will return the page's URL if there's no <title>
352 // specified. However, we'd prefer to show the name of the extension in that
353 // case, so we directly inspect the NavigationEntry's title.
354 string16 title;
355 if (!web_contents() ||
356 !web_contents()->GetController().GetActiveEntry() ||
357 web_contents()->GetController().GetActiveEntry()->GetTitle().empty()) {
358 title = UTF8ToUTF16(extension()->name());
359 } else {
360 title = web_contents()->GetTitle();
362 const char16 kBadChars[] = { '\n', 0 };
363 RemoveChars(title, kBadChars, &title);
364 return title;
367 void ShellWindow::SetAppIconUrl(const GURL& url) {
368 // Avoid using any previous app icons were are being downloaded.
369 image_loader_ptr_factory_.InvalidateWeakPtrs();
371 // Reset |app_icon_image_| to abort pending image load (if any).
372 app_icon_image_.reset();
374 app_icon_url_ = url;
375 web_contents()->DownloadImage(
376 url,
377 true, // is a favicon
378 0, // no maximum size
379 base::Bind(&ShellWindow::DidDownloadFavicon,
380 image_loader_ptr_factory_.GetWeakPtr()));
383 void ShellWindow::UpdateInputRegion(scoped_ptr<SkRegion> region) {
384 native_app_window_->UpdateInputRegion(region.Pass());
387 void ShellWindow::UpdateDraggableRegions(
388 const std::vector<extensions::DraggableRegion>& regions) {
389 native_app_window_->UpdateDraggableRegions(regions);
392 void ShellWindow::UpdateAppIcon(const gfx::Image& image) {
393 if (image.IsEmpty())
394 return;
395 app_icon_ = image;
396 native_app_window_->UpdateWindowIcon();
397 ShellWindowRegistry::Get(profile_)->ShellWindowIconChanged(this);
400 void ShellWindow::Fullscreen() {
401 #if !defined(OS_MACOSX)
402 // Do not enter fullscreen mode if disallowed by pref.
403 if (!profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed))
404 return;
405 #endif
406 fullscreen_types_ |= FULLSCREEN_TYPE_WINDOW_API;
407 GetBaseWindow()->SetFullscreen(fullscreen_types_);
410 void ShellWindow::Maximize() {
411 GetBaseWindow()->Maximize();
414 void ShellWindow::Minimize() {
415 GetBaseWindow()->Minimize();
418 void ShellWindow::Restore() {
419 if (fullscreen_types_ != FULLSCREEN_TYPE_NONE) {
420 fullscreen_types_ = FULLSCREEN_TYPE_NONE;
421 GetBaseWindow()->SetFullscreen(fullscreen_types_);
422 } else {
423 GetBaseWindow()->Restore();
427 void ShellWindow::OSFullscreen() {
428 #if !defined(OS_MACOSX)
429 // Do not enter fullscreen mode if disallowed by pref.
430 if (!profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed))
431 return;
432 #endif
433 fullscreen_types_ |= FULLSCREEN_TYPE_OS;
434 GetBaseWindow()->SetFullscreen(fullscreen_types_);
437 void ShellWindow::SetMinimumSize(const gfx::Size& min_size) {
438 size_constraints_.set_minimum_size(min_size);
439 OnSizeConstraintsChanged();
442 void ShellWindow::SetMaximumSize(const gfx::Size& max_size) {
443 size_constraints_.set_maximum_size(max_size);
444 OnSizeConstraintsChanged();
447 void ShellWindow::Show(ShowType show_type) {
448 if (CommandLine::ForCurrentProcess()->HasSwitch(
449 switches::kEnableAppsShowOnFirstPaint)) {
450 show_on_first_paint_ = true;
452 if (!first_paint_complete_) {
453 delayed_show_type_ = show_type;
454 return;
458 switch (show_type) {
459 case SHOW_ACTIVE:
460 GetBaseWindow()->Show();
461 break;
462 case SHOW_INACTIVE:
463 GetBaseWindow()->ShowInactive();
464 break;
468 void ShellWindow::Hide() {
469 // This is there to prevent race conditions with Hide() being called before
470 // there was a non-empty paint. It should have no effect in a non-racy
471 // scenario where the application is hiding then showing a window: the second
472 // show will not be delayed.
473 show_on_first_paint_ = false;
474 GetBaseWindow()->Hide();
477 //------------------------------------------------------------------------------
478 // Private methods
480 void ShellWindow::DidDownloadFavicon(
481 int id,
482 int http_status_code,
483 const GURL& image_url,
484 const std::vector<SkBitmap>& bitmaps,
485 const std::vector<gfx::Size>& original_bitmap_sizes) {
486 if (image_url != app_icon_url_ || bitmaps.empty())
487 return;
489 // Bitmaps are ordered largest to smallest. Choose the smallest bitmap
490 // whose height >= the preferred size.
491 int largest_index = 0;
492 for (size_t i = 1; i < bitmaps.size(); ++i) {
493 if (bitmaps[i].height() < delegate_->PreferredIconSize())
494 break;
495 largest_index = i;
497 const SkBitmap& largest = bitmaps[largest_index];
498 UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest));
501 void ShellWindow::OnExtensionIconImageChanged(extensions::IconImage* image) {
502 DCHECK_EQ(app_icon_image_.get(), image);
504 UpdateAppIcon(gfx::Image(app_icon_image_->image_skia()));
507 void ShellWindow::UpdateExtensionAppIcon() {
508 // Avoid using any previous app icons were are being downloaded.
509 image_loader_ptr_factory_.InvalidateWeakPtrs();
511 app_icon_image_.reset(new extensions::IconImage(
512 profile(),
513 extension(),
514 extensions::IconsInfo::GetIcons(extension()),
515 delegate_->PreferredIconSize(),
516 extensions::IconsInfo::GetDefaultAppIcon(),
517 this));
519 // Triggers actual image loading with 1x resources. The 2x resource will
520 // be handled by IconImage class when requested.
521 app_icon_image_->image_skia().GetRepresentation(1.0f);
524 void ShellWindow::OnSizeConstraintsChanged() {
525 native_app_window_->UpdateWindowMinMaxSize();
526 gfx::Rect bounds = GetClientBounds();
527 gfx::Size constrained_size = size_constraints_.ClampSize(bounds.size());
528 if (bounds.size() != constrained_size) {
529 bounds.set_size(constrained_size);
530 native_app_window_->SetBounds(bounds);
532 OnNativeWindowChanged();
535 void ShellWindow::CloseContents(WebContents* contents) {
536 native_app_window_->Close();
539 bool ShellWindow::ShouldSuppressDialogs() {
540 return true;
543 content::ColorChooser* ShellWindow::OpenColorChooser(WebContents* web_contents,
544 SkColor initial_color) {
545 return delegate_->ShowColorChooser(web_contents, initial_color);
548 void ShellWindow::RunFileChooser(WebContents* tab,
549 const content::FileChooserParams& params) {
550 if (window_type_is_panel()) {
551 // Panels can't host a file dialog, abort. TODO(stevenjb): allow file
552 // dialogs to be unhosted but still close with the owning web contents.
553 // crbug.com/172502.
554 LOG(WARNING) << "File dialog opened by panel.";
555 return;
558 delegate_->RunFileChooser(tab, params);
561 bool ShellWindow::IsPopupOrPanel(const WebContents* source) const {
562 return true;
565 void ShellWindow::MoveContents(WebContents* source, const gfx::Rect& pos) {
566 native_app_window_->SetBounds(pos);
569 void ShellWindow::NavigationStateChanged(
570 const content::WebContents* source, unsigned changed_flags) {
571 if (changed_flags & content::INVALIDATE_TYPE_TITLE)
572 native_app_window_->UpdateWindowTitle();
573 else if (changed_flags & content::INVALIDATE_TYPE_TAB)
574 native_app_window_->UpdateWindowIcon();
577 void ShellWindow::ToggleFullscreenModeForTab(content::WebContents* source,
578 bool enter_fullscreen) {
579 #if !defined(OS_MACOSX)
580 // Do not enter fullscreen mode if disallowed by pref.
581 // TODO(bartfab): Add a test once it becomes possible to simulate a user
582 // gesture. http://crbug.com/174178
583 if (enter_fullscreen &&
584 !profile()->GetPrefs()->GetBoolean(prefs::kAppFullscreenAllowed)) {
585 return;
587 #endif
589 if (!IsExtensionWithPermissionOrSuggestInConsole(
590 APIPermission::kFullscreen,
591 extension_,
592 source->GetRenderViewHost())) {
593 return;
596 if (enter_fullscreen)
597 fullscreen_types_ |= FULLSCREEN_TYPE_HTML_API;
598 else
599 fullscreen_types_ &= ~FULLSCREEN_TYPE_HTML_API;
600 GetBaseWindow()->SetFullscreen(fullscreen_types_);
603 bool ShellWindow::IsFullscreenForTabOrPending(
604 const content::WebContents* source) const {
605 return ((fullscreen_types_ & FULLSCREEN_TYPE_HTML_API) != 0);
608 void ShellWindow::Observe(int type,
609 const content::NotificationSource& source,
610 const content::NotificationDetails& details) {
611 switch (type) {
612 case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
613 const extensions::Extension* unloaded_extension =
614 content::Details<extensions::UnloadedExtensionInfo>(
615 details)->extension;
616 if (extension_ == unloaded_extension)
617 native_app_window_->Close();
618 break;
620 case chrome::NOTIFICATION_APP_TERMINATING:
621 native_app_window_->Close();
622 break;
623 default:
624 NOTREACHED() << "Received unexpected notification";
628 void ShellWindow::SetWebContentsBlocked(content::WebContents* web_contents,
629 bool blocked) {
630 delegate_->SetWebContentsBlocked(web_contents, blocked);
633 bool ShellWindow::IsWebContentsVisible(content::WebContents* web_contents) {
634 return delegate_->IsWebContentsVisible(web_contents);
637 extensions::ActiveTabPermissionGranter*
638 ShellWindow::GetActiveTabPermissionGranter() {
639 // Shell windows don't support the activeTab permission.
640 return NULL;
643 WebContentsModalDialogHost* ShellWindow::GetWebContentsModalDialogHost() {
644 return native_app_window_.get();
647 void ShellWindow::AddMessageToDevToolsConsole(ConsoleMessageLevel level,
648 const std::string& message) {
649 content::RenderViewHost* rvh = web_contents()->GetRenderViewHost();
650 rvh->Send(new ExtensionMsg_AddMessageToConsole(
651 rvh->GetRoutingID(), level, message));
654 void ShellWindow::SaveWindowPosition() {
655 if (window_key_.empty())
656 return;
657 if (!native_app_window_)
658 return;
660 ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile());
662 gfx::Rect bounds = native_app_window_->GetRestoredBounds();
663 bounds.Inset(native_app_window_->GetFrameInsets());
664 gfx::Rect screen_bounds =
665 gfx::Screen::GetNativeScreen()->GetDisplayMatching(bounds).work_area();
666 ui::WindowShowState window_state = native_app_window_->GetRestoredState();
667 cache->SaveGeometry(extension()->id(),
668 window_key_,
669 bounds,
670 screen_bounds,
671 window_state);
674 void ShellWindow::AdjustBoundsToBeVisibleOnScreen(
675 const gfx::Rect& cached_bounds,
676 const gfx::Rect& cached_screen_bounds,
677 const gfx::Rect& current_screen_bounds,
678 const gfx::Size& minimum_size,
679 gfx::Rect* bounds) const {
680 *bounds = cached_bounds;
682 // Reposition and resize the bounds if the cached_screen_bounds is different
683 // from the current screen bounds and the current screen bounds doesn't
684 // completely contain the bounds.
685 if (cached_screen_bounds != current_screen_bounds &&
686 !current_screen_bounds.Contains(cached_bounds)) {
687 bounds->set_width(
688 std::max(minimum_size.width(),
689 std::min(bounds->width(), current_screen_bounds.width())));
690 bounds->set_height(
691 std::max(minimum_size.height(),
692 std::min(bounds->height(), current_screen_bounds.height())));
693 bounds->set_x(
694 std::max(current_screen_bounds.x(),
695 std::min(bounds->x(),
696 current_screen_bounds.right() - bounds->width())));
697 bounds->set_y(
698 std::max(current_screen_bounds.y(),
699 std::min(bounds->y(),
700 current_screen_bounds.bottom() - bounds->height())));
704 ShellWindow::CreateParams ShellWindow::LoadDefaultsAndConstrain(
705 CreateParams params) const {
706 if (params.bounds.width() == 0)
707 params.bounds.set_width(kDefaultWidth);
708 if (params.bounds.height() == 0)
709 params.bounds.set_height(kDefaultHeight);
711 // If left and top are left undefined, the native shell window will center
712 // the window on the main screen in a platform-defined manner.
714 // Load cached state if it exists.
715 if (!params.window_key.empty()) {
716 ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile());
718 gfx::Rect cached_bounds;
719 gfx::Rect cached_screen_bounds;
720 ui::WindowShowState cached_state = ui::SHOW_STATE_DEFAULT;
721 if (cache->GetGeometry(extension()->id(), params.window_key,
722 &cached_bounds, &cached_screen_bounds,
723 &cached_state)) {
724 // App window has cached screen bounds, make sure it fits on screen in
725 // case the screen resolution changed.
726 gfx::Screen* screen = gfx::Screen::GetNativeScreen();
727 gfx::Display display = screen->GetDisplayMatching(cached_bounds);
728 gfx::Rect current_screen_bounds = display.work_area();
729 AdjustBoundsToBeVisibleOnScreen(cached_bounds,
730 cached_screen_bounds,
731 current_screen_bounds,
732 params.minimum_size,
733 &params.bounds);
734 params.state = cached_state;
738 SizeConstraints size_constraints(params.minimum_size, params.maximum_size);
739 params.bounds.set_size(size_constraints.ClampSize(params.bounds.size()));
740 params.minimum_size = size_constraints.GetMinimumSize();
741 params.maximum_size = size_constraints.GetMaximumSize();
743 return params;
746 // static
747 SkRegion* ShellWindow::RawDraggableRegionsToSkRegion(
748 const std::vector<extensions::DraggableRegion>& regions) {
749 SkRegion* sk_region = new SkRegion;
750 for (std::vector<extensions::DraggableRegion>::const_iterator iter =
751 regions.begin();
752 iter != regions.end(); ++iter) {
753 const extensions::DraggableRegion& region = *iter;
754 sk_region->op(
755 region.bounds.x(),
756 region.bounds.y(),
757 region.bounds.right(),
758 region.bounds.bottom(),
759 region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
761 return sk_region;
764 } // namespace apps