Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / apps / shell_window.cc
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.
4
5 #include "apps/shell_window.h"
6
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"
40
41 #if !defined(OS_MACOSX)
42 #include "apps/pref_names.h"
43 #include "base/prefs/pref_service.h"
44 #endif
45
46 using content::ConsoleMessageLevel;
47 using content::WebContents;
48 using extensions::APIPermission;
49 using web_modal::WebContentsModalDialogHost;
50 using web_modal::WebContentsModalDialogManager;
51
52 namespace {
53
54 const int kDefaultWidth = 512;
55 const int kDefaultHeight = 384;
56
57 bool IsFullscreen(int fullscreen_types) {
58   return fullscreen_types != apps::ShellWindow::FULLSCREEN_TYPE_NONE;
59 }
60
61 }  // namespace
62
63 namespace apps {
64
65 ShellWindow::SizeConstraints::SizeConstraints()
66     : maximum_size_(kUnboundedSize, kUnboundedSize) {
67 }
68
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) {
73 }
74
75 ShellWindow::SizeConstraints::~SizeConstraints() {}
76
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());
84   return size;
85 }
86
87 bool ShellWindow::SizeConstraints::HasMinimumSize() const {
88   return GetMinimumSize().width() != kUnboundedSize ||
89       GetMinimumSize().height() != kUnboundedSize;
90 }
91
92 bool ShellWindow::SizeConstraints::HasMaximumSize() const {
93   const gfx::Size max_size = GetMaximumSize();
94   return max_size.width() != kUnboundedSize ||
95       max_size.height() != kUnboundedSize;
96 }
97
98 bool ShellWindow::SizeConstraints::HasFixedSize() const {
99   return !GetMinimumSize().IsEmpty() && GetMinimumSize() == GetMaximumSize();
100 }
101
102 gfx::Size ShellWindow::SizeConstraints::GetMinimumSize() const {
103   return minimum_size_;
104 }
105
106 gfx::Size ShellWindow::SizeConstraints::GetMaximumSize() const {
107   return gfx::Size(
108       maximum_size_.width() == kUnboundedSize ?
109           kUnboundedSize :
110           std::max(maximum_size_.width(), minimum_size_.width()),
111       maximum_size_.height() == kUnboundedSize ?
112           kUnboundedSize :
113           std::max(maximum_size_.height(), minimum_size_.height()));
114 }
115
116 void ShellWindow::SizeConstraints::set_minimum_size(const gfx::Size& min_size) {
117   minimum_size_ = min_size;
118 }
119
120 void ShellWindow::SizeConstraints::set_maximum_size(const gfx::Size& max_size) {
121   maximum_size_ = max_size;
122 }
123
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),
131     hidden(false),
132     resizable(true),
133     focused(true),
134     always_on_top(false) {}
135
136 ShellWindow::CreateParams::~CreateParams() {}
137
138 ShellWindow::Delegate::~Delegate() {}
139
140 ShellWindow::ShellWindow(Profile* profile,
141                          Delegate* delegate,
142                          const extensions::Extension* extension)
143     : profile_(profile),
144       extension_(extension),
145       extension_id_(extension->id()),
146       window_type_(WINDOW_TYPE_DEFAULT),
147       delegate_(delegate),
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.";
155 }
156
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);
167   }
168   delegate_->InitWebContents(web_contents);
169   WebContentsModalDialogManager::CreateForWebContents(web_contents);
170   extensions::ExtensionWebContentsObserver::CreateForWebContents(web_contents);
171
172   web_contents->SetDelegate(this);
173   WebContentsModalDialogManager::FromWebContents(web_contents)->
174       SetDelegate(this);
175   extensions::SetViewType(web_contents, extensions::VIEW_TYPE_APP_SHELL);
176
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);
183
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;
188
189   native_app_window_.reset(delegate_->CreateNativeAppWindow(this, new_params));
190
191   if (!new_params.hidden) {
192     // Panels are not activated by default.
193     Show(window_type_is_panel() || !new_params.focused ? SHOW_INACTIVE
194                                                        : SHOW_ACTIVE);
195   }
196
197   if (new_params.state == ui::SHOW_STATE_FULLSCREEN)
198     Fullscreen();
199   else if (new_params.state == ui::SHOW_STATE_MAXIMIZED)
200     Maximize();
201   else if (new_params.state == ui::SHOW_STATE_MINIMIZED)
202     Minimize();
203
204   OnNativeWindowChanged();
205
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
209   // to be set.
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());
215
216   shell_window_contents_->LoadContents(new_params.creator_process_id);
217
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());
225   }
226
227   // Prevent the browser process from shutting down while this window is open.
228   chrome::StartKeepAlive();
229
230   UpdateExtensionAppIcon();
231
232   ShellWindowRegistry::Get(profile_)->AddShellWindow(this);
233 }
234
235 ShellWindow::~ShellWindow() {
236   // Unregister now to prevent getting NOTIFICATION_APP_TERMINATING if we're the
237   // last window open.
238   registrar_.RemoveAll();
239
240   // Remove shutdown prevention.
241   chrome::EndKeepAlive();
242 }
243
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,
249                                           extension());
250 }
251
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,
263         base::StringPrintf(
264             "Can't open same-window link to \"%s\"; try target=\"_blank\".",
265             params.url.spec().c_str()));
266     return NULL;
267   }
268
269   // These dispositions aren't really navigations.
270   if (disposition == SUPPRESS_OPEN || disposition == SAVE_TO_DISK ||
271       disposition == IGNORE_ACTION) {
272     return NULL;
273   }
274
275   WebContents* contents = delegate_->OpenURLFromTab(profile_, source,
276                                                     params);
277   if (!contents) {
278     AddMessageToDevToolsConsole(
279         content::CONSOLE_MESSAGE_LEVEL_ERROR,
280         base::StringPrintf(
281             "Can't navigate to \"%s\"; apps do not support navigation.",
282             params.url.spec().c_str()));
283   }
284
285   return contents;
286 }
287
288 void ShellWindow::AddNewContents(WebContents* source,
289                                  WebContents* new_contents,
290                                  WindowOpenDisposition disposition,
291                                  const gfx::Rect& initial_pos,
292                                  bool user_gesture,
293                                  bool* was_blocked) {
294   DCHECK(Profile::FromBrowserContext(new_contents->GetBrowserContext()) ==
295       profile_);
296   delegate_->AddNewContents(profile_, new_contents, disposition,
297                             initial_pos, user_gesture, was_blocked);
298 }
299
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)) {
316     Restore();
317     return true;
318   }
319
320   return false;
321 }
322
323 void ShellWindow::HandleKeyboardEvent(
324     WebContents* source,
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)) {
332     Restore();
333     return;
334   }
335
336   native_app_window_->HandleKeyboardEvent(event);
337 }
338
339 void ShellWindow::RequestToLockMouse(WebContents* web_contents,
340                                      bool user_gesture,
341                                      bool last_unlocked_by_target) {
342   bool has_permission = IsExtensionWithPermissionOrSuggestInConsole(
343       APIPermission::kPointerLock,
344       extension_,
345       web_contents->GetRenderViewHost());
346
347   web_contents->GotResponseToLockMouseRequest(has_permission);
348 }
349
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_);
356   }
357 }
358
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)->
364         SetDelegate(NULL);
365     shell_window_contents_->NativeWindowClosed();
366   }
367   delete this;
368 }
369
370 void ShellWindow::OnNativeWindowChanged() {
371   SaveWindowPosition();
372
373 #if defined(OS_WIN)
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();
380   }
381 #endif
382
383   if (shell_window_contents_ && native_app_window_)
384     shell_window_contents_->NativeWindowChanged(native_app_window_.get());
385 }
386
387 void ShellWindow::OnNativeWindowActivated() {
388   ShellWindowRegistry::Get(profile_)->ShellWindowActivated(this);
389 }
390
391 content::WebContents* ShellWindow::web_contents() const {
392   return shell_window_contents_->GetWebContents();
393 }
394
395 NativeAppWindow* ShellWindow::GetBaseWindow() {
396   return native_app_window_.get();
397 }
398
399 gfx::NativeWindow ShellWindow::GetNativeWindow() {
400   return GetBaseWindow()->GetNativeWindow();
401 }
402
403 gfx::Rect ShellWindow::GetClientBounds() const {
404   gfx::Rect bounds = native_app_window_->GetBounds();
405   bounds.Inset(native_app_window_->GetFrameInsets());
406   return bounds;
407 }
408
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());
418   } else {
419     title = web_contents()->GetTitle();
420   }
421   const base::char16 kBadChars[] = { '\n', 0 };
422   base::RemoveChars(title, kBadChars, &title);
423   return title;
424 }
425
426 void ShellWindow::SetAppIconUrl(const GURL& url) {
427   // Avoid using any previous app icons were are being downloaded.
428   image_loader_ptr_factory_.InvalidateWeakPtrs();
429
430   // Reset |app_icon_image_| to abort pending image load (if any).
431   app_icon_image_.reset();
432
433   app_icon_url_ = url;
434   web_contents()->DownloadImage(
435       url,
436       true,  // is a favicon
437       0,  // no maximum size
438       base::Bind(&ShellWindow::DidDownloadFavicon,
439                  image_loader_ptr_factory_.GetWeakPtr()));
440 }
441
442 void ShellWindow::UpdateShape(scoped_ptr<SkRegion> region) {
443   native_app_window_->UpdateShape(region.Pass());
444 }
445
446 void ShellWindow::UpdateDraggableRegions(
447     const std::vector<extensions::DraggableRegion>& regions) {
448   native_app_window_->UpdateDraggableRegions(regions);
449 }
450
451 void ShellWindow::UpdateAppIcon(const gfx::Image& image) {
452   if (image.IsEmpty())
453     return;
454   app_icon_ = image;
455   native_app_window_->UpdateWindowIcon();
456   ShellWindowRegistry::Get(profile_)->ShellWindowIconChanged(this);
457 }
458
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))
463     return;
464 #endif
465   fullscreen_types_ |= FULLSCREEN_TYPE_WINDOW_API;
466   SetNativeWindowFullscreen();
467 }
468
469 void ShellWindow::Maximize() {
470   GetBaseWindow()->Maximize();
471 }
472
473 void ShellWindow::Minimize() {
474   GetBaseWindow()->Minimize();
475 }
476
477 void ShellWindow::Restore() {
478   if (IsFullscreen(fullscreen_types_)) {
479     fullscreen_types_ = FULLSCREEN_TYPE_NONE;
480     SetNativeWindowFullscreen();
481   } else {
482     GetBaseWindow()->Restore();
483   }
484 }
485
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))
490     return;
491 #endif
492   fullscreen_types_ |= FULLSCREEN_TYPE_OS;
493   SetNativeWindowFullscreen();
494 }
495
496 void ShellWindow::ForcedFullscreen() {
497   fullscreen_types_ |= FULLSCREEN_TYPE_FORCED;
498   SetNativeWindowFullscreen();
499 }
500
501 void ShellWindow::SetMinimumSize(const gfx::Size& min_size) {
502   size_constraints_.set_minimum_size(min_size);
503   OnSizeConstraintsChanged();
504 }
505
506 void ShellWindow::SetMaximumSize(const gfx::Size& max_size) {
507   size_constraints_.set_maximum_size(max_size);
508   OnSizeConstraintsChanged();
509 }
510
511 void ShellWindow::Show(ShowType show_type) {
512   if (CommandLine::ForCurrentProcess()->HasSwitch(
513         switches::kEnableAppsShowOnFirstPaint)) {
514     show_on_first_paint_ = true;
515
516     if (!first_paint_complete_) {
517       delayed_show_type_ = show_type;
518       return;
519     }
520   }
521
522   switch (show_type) {
523     case SHOW_ACTIVE:
524       GetBaseWindow()->Show();
525       break;
526     case SHOW_INACTIVE:
527       GetBaseWindow()->ShowInactive();
528       break;
529   }
530 }
531
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();
539 }
540
541 void ShellWindow::SetAlwaysOnTop(bool always_on_top) {
542   if (cached_always_on_top_ == always_on_top)
543     return;
544
545   cached_always_on_top_ = always_on_top;
546
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);
552
553   OnNativeWindowChanged();
554 }
555
556 bool ShellWindow::IsAlwaysOnTop() const {
557   return cached_always_on_top_;
558 }
559
560 //------------------------------------------------------------------------------
561 // Private methods
562
563 void ShellWindow::DidDownloadFavicon(
564     int id,
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())
570     return;
571
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())
577       break;
578     largest_index = i;
579   }
580   const SkBitmap& largest = bitmaps[largest_index];
581   UpdateAppIcon(gfx::Image::CreateFrom1xBitmap(largest));
582 }
583
584 void ShellWindow::OnExtensionIconImageChanged(extensions::IconImage* image) {
585   DCHECK_EQ(app_icon_image_.get(), image);
586
587   UpdateAppIcon(gfx::Image(app_icon_image_->image_skia()));
588 }
589
590 void ShellWindow::UpdateExtensionAppIcon() {
591   // Avoid using any previous app icons were are being downloaded.
592   image_loader_ptr_factory_.InvalidateWeakPtrs();
593
594   app_icon_image_.reset(new extensions::IconImage(
595       profile(),
596       extension(),
597       extensions::IconsInfo::GetIcons(extension()),
598       delegate_->PreferredIconSize(),
599       extensions::IconsInfo::GetDefaultAppIcon(),
600       this));
601
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);
605 }
606
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);
614   }
615   OnNativeWindowChanged();
616 }
617
618 void ShellWindow::SetNativeWindowFullscreen() {
619   native_app_window_->SetFullscreen(fullscreen_types_);
620
621   if (cached_always_on_top_)
622     UpdateNativeAlwaysOnTop();
623 }
624
625 bool ShellWindow::IntersectsWithTaskbar() const {
626 #if defined(OS_WIN)
627   gfx::Screen* screen = gfx::Screen::GetNativeScreen();
628   gfx::Rect window_bounds = native_app_window_->GetRestoredBounds();
629   std::vector<gfx::Display> displays = screen->GetAllDisplays();
630
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())
636       continue;
637
638     if (window_bounds.Intersects(taskbar_bounds))
639       return true;
640   }
641 #endif
642
643   return false;
644 }
645
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();
651
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
658     // always-on-top.
659     native_app_window_->SetAlwaysOnTop(true);
660   }
661 }
662
663 void ShellWindow::CloseContents(WebContents* contents) {
664   native_app_window_->Close();
665 }
666
667 bool ShellWindow::ShouldSuppressDialogs() {
668   return true;
669 }
670
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);
676 }
677
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.
683     // crbug.com/172502.
684     LOG(WARNING) << "File dialog opened by panel.";
685     return;
686   }
687
688   delegate_->RunFileChooser(tab, params);
689 }
690
691 bool ShellWindow::IsPopupOrPanel(const WebContents* source) const {
692   return true;
693 }
694
695 void ShellWindow::MoveContents(WebContents* source, const gfx::Rect& pos) {
696   native_app_window_->SetBounds(pos);
697 }
698
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();
705 }
706
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)) {
715     return;
716   }
717 #endif
718
719   if (!IsExtensionWithPermissionOrSuggestInConsole(
720       APIPermission::kFullscreen,
721       extension_,
722       source->GetRenderViewHost())) {
723     return;
724   }
725
726   if (enter_fullscreen)
727     fullscreen_types_ |= FULLSCREEN_TYPE_HTML_API;
728   else
729     fullscreen_types_ &= ~FULLSCREEN_TYPE_HTML_API;
730   SetNativeWindowFullscreen();
731 }
732
733 bool ShellWindow::IsFullscreenForTabOrPending(
734     const content::WebContents* source) const {
735   return ((fullscreen_types_ & FULLSCREEN_TYPE_HTML_API) != 0);
736 }
737
738 void ShellWindow::Observe(int type,
739                           const content::NotificationSource& source,
740                           const content::NotificationDetails& details) {
741   switch (type) {
742     case chrome::NOTIFICATION_EXTENSION_UNLOADED: {
743       const extensions::Extension* unloaded_extension =
744           content::Details<extensions::UnloadedExtensionInfo>(
745               details)->extension;
746       if (extension_ == unloaded_extension)
747         native_app_window_->Close();
748       break;
749     }
750     case chrome::NOTIFICATION_APP_TERMINATING:
751       native_app_window_->Close();
752       break;
753     default:
754       NOTREACHED() << "Received unexpected notification";
755   }
756 }
757
758 void ShellWindow::SetWebContentsBlocked(content::WebContents* web_contents,
759                                         bool blocked) {
760   delegate_->SetWebContentsBlocked(web_contents, blocked);
761 }
762
763 bool ShellWindow::IsWebContentsVisible(content::WebContents* web_contents) {
764   return delegate_->IsWebContentsVisible(web_contents);
765 }
766
767 extensions::ActiveTabPermissionGranter*
768     ShellWindow::GetActiveTabPermissionGranter() {
769   // Shell windows don't support the activeTab permission.
770   return NULL;
771 }
772
773 WebContentsModalDialogHost* ShellWindow::GetWebContentsModalDialogHost() {
774   return native_app_window_.get();
775 }
776
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));
782 }
783
784 void ShellWindow::SaveWindowPosition() {
785   if (window_key_.empty())
786     return;
787   if (!native_app_window_)
788     return;
789
790   ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile());
791
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(),
798                       window_key_,
799                       bounds,
800                       screen_bounds,
801                       window_state);
802 }
803
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;
811
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)) {
817     bounds->set_width(
818         std::max(minimum_size.width(),
819                  std::min(bounds->width(), current_screen_bounds.width())));
820     bounds->set_height(
821         std::max(minimum_size.height(),
822                  std::min(bounds->height(), current_screen_bounds.height())));
823     bounds->set_x(
824         std::max(current_screen_bounds.x(),
825                  std::min(bounds->x(),
826                           current_screen_bounds.right() - bounds->width())));
827     bounds->set_y(
828         std::max(current_screen_bounds.y(),
829                  std::min(bounds->y(),
830                           current_screen_bounds.bottom() - bounds->height())));
831   }
832 }
833
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);
840
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.
843
844   // Load cached state if it exists.
845   if (!params.window_key.empty()) {
846     ShellWindowGeometryCache* cache = ShellWindowGeometryCache::Get(profile());
847
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,
853                            &cached_state)) {
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,
862                                       params.minimum_size,
863                                       &params.bounds);
864       params.state = cached_state;
865     }
866   }
867
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();
872
873   return params;
874 }
875
876 // static
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 =
881            regions.begin();
882        iter != regions.end(); ++iter) {
883     const extensions::DraggableRegion& region = *iter;
884     sk_region->op(
885         region.bounds.x(),
886         region.bounds.y(),
887         region.bounds.right(),
888         region.bounds.bottom(),
889         region.draggable ? SkRegion::kUnion_Op : SkRegion::kDifference_Op);
890   }
891   return sk_region;
892 }
893
894 }  // namespace apps