fa942ef80c7d16308b6413f62d38b0f4cfb57557
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / views / apps / chrome_native_app_window_views.cc
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/views/apps/chrome_native_app_window_views.h"
6
7 #include "apps/ui/views/app_window_frame_view.h"
8 #include "base/command_line.h"
9 #include "chrome/app/chrome_command_ids.h"
10 #include "chrome/browser/app_mode/app_mode_utils.h"
11 #include "chrome/browser/chrome_page_zoom.h"
12 #include "chrome/browser/favicon/favicon_tab_helper.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/host_desktop.h"
15 #include "chrome/browser/ui/views/apps/shaped_app_window_targeter.h"
16 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
17 #include "chrome/browser/ui/views/frame/taskbar_decorator.h"
18 #include "chrome/browser/web_applications/web_app.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "extensions/common/extension.h"
21 #include "ui/base/hit_test.h"
22 #include "ui/base/models/simple_menu_model.h"
23 #include "ui/gfx/image/image_skia.h"
24 #include "ui/views/controls/menu/menu_runner.h"
25 #include "ui/views/controls/webview/webview.h"
26 #include "ui/views/widget/widget.h"
27 #include "ui/wm/core/easy_resize_window_targeter.h"
28
29 #if defined(OS_LINUX)
30 #include "chrome/browser/shell_integration_linux.h"
31 #endif
32
33 #if defined(USE_ASH)
34 #include "ash/ash_constants.h"
35 #include "ash/ash_switches.h"
36 #include "ash/frame/custom_frame_view_ash.h"
37 #include "ash/screen_util.h"
38 #include "ash/shell.h"
39 #include "ash/wm/immersive_fullscreen_controller.h"
40 #include "ash/wm/panels/panel_frame_view.h"
41 #include "ash/wm/window_state.h"
42 #include "ash/wm/window_state_delegate.h"
43 #include "ash/wm/window_state_observer.h"
44 #include "chrome/browser/ui/ash/ash_util.h"
45 #include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
46 #include "ui/aura/client/aura_constants.h"
47 #include "ui/aura/client/window_tree_client.h"
48 #include "ui/aura/window.h"
49 #include "ui/aura/window_observer.h"
50 #endif
51
52 #if defined(USE_AURA)
53 #include "ui/aura/window.h"
54 #endif
55
56 using apps::AppWindow;
57
58 namespace {
59
60 const int kMinPanelWidth = 100;
61 const int kMinPanelHeight = 100;
62 const int kDefaultPanelWidth = 200;
63 const int kDefaultPanelHeight = 300;
64 const int kResizeInsideBoundsSize = 5;
65 const int kResizeAreaCornerSize = 16;
66
67 struct AcceleratorMapping {
68   ui::KeyboardCode keycode;
69   int modifiers;
70   int command_id;
71 };
72
73 const AcceleratorMapping kAppWindowAcceleratorMap[] = {
74   { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
75   { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
76   { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW },
77 };
78
79 // These accelerators will only be available in kiosk mode. These allow the
80 // user to manually zoom app windows. This is only necessary in kiosk mode
81 // (in normal mode, the user can zoom via the screen magnifier).
82 // TODO(xiyuan): Write a test for kiosk accelerators.
83 const AcceleratorMapping kAppWindowKioskAppModeAcceleratorMap[] = {
84   { ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
85   { ui::VKEY_OEM_MINUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
86     IDC_ZOOM_MINUS },
87   { ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
88   { ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
89   { ui::VKEY_OEM_PLUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
90   { ui::VKEY_ADD, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
91   { ui::VKEY_0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
92   { ui::VKEY_NUMPAD0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
93 };
94
95 void AddAcceleratorsFromMapping(const AcceleratorMapping mapping[],
96                                 size_t mapping_length,
97                                 std::map<ui::Accelerator, int>* accelerators) {
98   for (size_t i = 0; i < mapping_length; ++i) {
99     ui::Accelerator accelerator(mapping[i].keycode, mapping[i].modifiers);
100     (*accelerators)[accelerator] = mapping[i].command_id;
101   }
102 }
103
104 const std::map<ui::Accelerator, int>& GetAcceleratorTable() {
105   typedef std::map<ui::Accelerator, int> AcceleratorMap;
106   CR_DEFINE_STATIC_LOCAL(AcceleratorMap, accelerators, ());
107   if (accelerators.empty()) {
108     AddAcceleratorsFromMapping(
109         kAppWindowAcceleratorMap,
110         arraysize(kAppWindowAcceleratorMap),
111         &accelerators);
112
113     // Add accelerators for kiosk mode.
114     if (chrome::IsRunningInForcedAppMode()) {
115       AddAcceleratorsFromMapping(
116           kAppWindowKioskAppModeAcceleratorMap,
117           arraysize(kAppWindowKioskAppModeAcceleratorMap),
118           &accelerators);
119     }
120   }
121   return accelerators;
122 }
123
124 #if defined(USE_ASH)
125 // This class handles a user's fullscreen request (Shift+F4/F4).
126 class NativeAppWindowStateDelegate : public ash::wm::WindowStateDelegate,
127                                      public ash::wm::WindowStateObserver,
128                                      public aura::WindowObserver {
129  public:
130   NativeAppWindowStateDelegate(AppWindow* app_window,
131                                apps::NativeAppWindow* native_app_window)
132       : app_window_(app_window),
133         window_state_(
134             ash::wm::GetWindowState(native_app_window->GetNativeWindow())) {
135     // Add a window state observer to exit fullscreen properly in case
136     // fullscreen is exited without going through AppWindow::Restore(). This
137     // is the case when exiting immersive fullscreen via the "Restore" window
138     // control.
139     // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
140     window_state_->AddObserver(this);
141     window_state_->window()->AddObserver(this);
142   }
143   virtual ~NativeAppWindowStateDelegate(){
144     if (window_state_) {
145       window_state_->RemoveObserver(this);
146       window_state_->window()->RemoveObserver(this);
147     }
148   }
149
150  private:
151   // Overridden from ash::wm::WindowStateDelegate.
152   virtual bool ToggleFullscreen(ash::wm::WindowState* window_state) OVERRIDE {
153     // Windows which cannot be maximized should not be fullscreened.
154     DCHECK(window_state->IsFullscreen() || window_state->CanMaximize());
155     if (window_state->IsFullscreen())
156       app_window_->Restore();
157     else if (window_state->CanMaximize())
158       app_window_->OSFullscreen();
159     return true;
160   }
161
162   // Overridden from ash::wm::WindowStateObserver:
163   virtual void OnPostWindowStateTypeChange(
164       ash::wm::WindowState* window_state,
165       ash::wm::WindowStateType old_type) OVERRIDE {
166     if (!window_state->IsFullscreen() && !window_state->IsMinimized() &&
167         app_window_->GetBaseWindow()->IsFullscreenOrPending()) {
168       app_window_->Restore();
169       // Usually OnNativeWindowChanged() is called when the window bounds are
170       // changed as a result of a state type change. Because the change in state
171       // type has already occurred, we need to call OnNativeWindowChanged()
172       // explicitly.
173       app_window_->OnNativeWindowChanged();
174     }
175   }
176
177   // Overridden from aura::WindowObserver:
178   virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
179     window_state_->RemoveObserver(this);
180     window_state_->window()->RemoveObserver(this);
181     window_state_ = NULL;
182   }
183
184   // Not owned.
185   AppWindow* app_window_;
186   ash::wm::WindowState* window_state_;
187
188   DISALLOW_COPY_AND_ASSIGN(NativeAppWindowStateDelegate);
189 };
190 #endif  // USE_ASH
191
192 }  // namespace
193
194 ChromeNativeAppWindowViews::ChromeNativeAppWindowViews()
195     : is_fullscreen_(false),
196       has_frame_color_(false),
197       frame_color_(SK_ColorBLACK) {}
198
199 ChromeNativeAppWindowViews::~ChromeNativeAppWindowViews() {}
200
201 void ChromeNativeAppWindowViews::OnBeforeWidgetInit(
202     views::Widget::InitParams* init_params,
203     views::Widget* widget) {}
204
205 void ChromeNativeAppWindowViews::InitializeDefaultWindow(
206     const AppWindow::CreateParams& create_params) {
207   std::string app_name =
208       web_app::GenerateApplicationNameFromExtensionId(
209           app_window()->extension()->id());
210
211   views::Widget::InitParams init_params(views::Widget::InitParams::TYPE_WINDOW);
212   init_params.delegate = this;
213   init_params.remove_standard_frame = IsFrameless() || has_frame_color_;
214   init_params.use_system_default_icon = true;
215   // TODO(erg): Conceptually, these are toplevel windows, but we theoretically
216   // could plumb context through to here in some cases.
217   init_params.top_level = true;
218   if (create_params.transparent_background)
219     init_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
220   init_params.keep_on_top = create_params.always_on_top;
221
222 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
223   // Set up a custom WM_CLASS for app windows. This allows task switchers in
224   // X11 environments to distinguish them from main browser windows.
225   init_params.wm_class_name = web_app::GetWMClassFromAppName(app_name);
226   init_params.wm_class_class = ShellIntegrationLinux::GetProgramClassName();
227   const char kX11WindowRoleApp[] = "app";
228   init_params.wm_role_name = std::string(kX11WindowRoleApp);
229 #endif
230
231   OnBeforeWidgetInit(&init_params, widget());
232   widget()->Init(init_params);
233
234   // The frame insets are required to resolve the bounds specifications
235   // correctly. So we set the window bounds and constraints now.
236   gfx::Insets frame_insets = GetFrameInsets();
237   gfx::Rect window_bounds = create_params.GetInitialWindowBounds(frame_insets);
238   SetContentSizeConstraints(create_params.GetContentMinimumSize(frame_insets),
239                             create_params.GetContentMaximumSize(frame_insets));
240   if (!window_bounds.IsEmpty()) {
241     typedef apps::AppWindow::BoundsSpecification BoundsSpecification;
242     bool position_specified =
243         window_bounds.x() != BoundsSpecification::kUnspecifiedPosition &&
244         window_bounds.y() != BoundsSpecification::kUnspecifiedPosition;
245     if (!position_specified)
246       widget()->CenterWindow(window_bounds.size());
247     else
248       widget()->SetBounds(window_bounds);
249   }
250
251   // Register accelarators supported by app windows.
252   // TODO(jeremya/stevenjb): should these be registered for panels too?
253   views::FocusManager* focus_manager = GetFocusManager();
254   const std::map<ui::Accelerator, int>& accelerator_table =
255       GetAcceleratorTable();
256   const bool is_kiosk_app_mode = chrome::IsRunningInForcedAppMode();
257
258   // Ensures that kiosk mode accelerators are enabled when in kiosk mode (to be
259   // future proof). This is needed because GetAcceleratorTable() uses a static
260   // to store data and only checks kiosk mode once. If a platform app is
261   // launched before kiosk mode starts, the kiosk accelerators will not be
262   // registered. This DCHECK catches the case.
263   DCHECK(!is_kiosk_app_mode ||
264          accelerator_table.size() ==
265              arraysize(kAppWindowAcceleratorMap) +
266                  arraysize(kAppWindowKioskAppModeAcceleratorMap));
267
268   for (std::map<ui::Accelerator, int>::const_iterator iter =
269            accelerator_table.begin();
270        iter != accelerator_table.end(); ++iter) {
271     if (is_kiosk_app_mode && !chrome::IsCommandAllowedInAppMode(iter->second))
272       continue;
273
274     focus_manager->RegisterAccelerator(
275         iter->first, ui::AcceleratorManager::kNormalPriority, this);
276   }
277 }
278
279 void ChromeNativeAppWindowViews::InitializePanelWindow(
280     const AppWindow::CreateParams& create_params) {
281   views::Widget::InitParams params(views::Widget::InitParams::TYPE_PANEL);
282   params.delegate = this;
283
284   gfx::Rect initial_window_bounds =
285       create_params.GetInitialWindowBounds(gfx::Insets());
286   preferred_size_ = gfx::Size(initial_window_bounds.width(),
287                               initial_window_bounds.height());
288   if (preferred_size_.width() == 0)
289     preferred_size_.set_width(kDefaultPanelWidth);
290   else if (preferred_size_.width() < kMinPanelWidth)
291     preferred_size_.set_width(kMinPanelWidth);
292
293   if (preferred_size_.height() == 0)
294     preferred_size_.set_height(kDefaultPanelHeight);
295   else if (preferred_size_.height() < kMinPanelHeight)
296     preferred_size_.set_height(kMinPanelHeight);
297 #if defined(USE_ASH)
298   if (ash::Shell::HasInstance()) {
299     // Open a new panel on the target root.
300     aura::Window* target = ash::Shell::GetTargetRootWindow();
301     params.bounds = ash::ScreenUtil::ConvertRectToScreen(
302         target, gfx::Rect(preferred_size_));
303   } else {
304     params.bounds = gfx::Rect(preferred_size_);
305   }
306 #else
307   params.bounds = gfx::Rect(preferred_size_);
308 #endif
309   // TODO(erg): Conceptually, these are toplevel windows, but we theoretically
310   // could plumb context through to here in some cases.
311   params.top_level = true;
312   widget()->Init(params);
313   widget()->set_focus_on_creation(create_params.focused);
314
315 #if defined(USE_ASH)
316   if (create_params.state == ui::SHOW_STATE_DETACHED) {
317     gfx::Rect window_bounds(initial_window_bounds.x(),
318                             initial_window_bounds.y(),
319                             preferred_size_.width(),
320                             preferred_size_.height());
321     aura::Window* native_window = GetNativeWindow();
322     ash::wm::GetWindowState(native_window)->set_panel_attached(false);
323     aura::client::ParentWindowWithContext(native_window,
324                                           native_window->GetRootWindow(),
325                                           native_window->GetBoundsInScreen());
326     widget()->SetBounds(window_bounds);
327   }
328 #else
329   // TODO(stevenjb): NativeAppWindow panels need to be implemented for other
330   // platforms.
331 #endif
332 }
333
334 void ChromeNativeAppWindowViews::InstallEasyResizeTargeterOnContainer() const {
335   aura::Window* root_window = widget()->GetNativeWindow()->GetRootWindow();
336   gfx::Insets inset(kResizeInsideBoundsSize, kResizeInsideBoundsSize,
337                     kResizeInsideBoundsSize, kResizeInsideBoundsSize);
338   root_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
339       new wm::EasyResizeWindowTargeter(root_window, inset, inset)));
340 }
341
342 apps::AppWindowFrameView*
343 ChromeNativeAppWindowViews::CreateAppWindowFrameView() {
344   // By default the user can resize the window from slightly inside the bounds.
345   int resize_inside_bounds_size = kResizeInsideBoundsSize;
346   int resize_outside_bounds_size = 0;
347   int resize_outside_scale_for_touch = 1;
348   int resize_area_corner_size = kResizeAreaCornerSize;
349 #if defined(USE_ASH)
350   // For Aura windows on the Ash desktop the sizes are different and the user
351   // can resize the window from slightly outside the bounds as well.
352   if (chrome::IsNativeWindowInAsh(widget()->GetNativeWindow())) {
353     resize_inside_bounds_size = ash::kResizeInsideBoundsSize;
354     resize_outside_bounds_size = ash::kResizeOutsideBoundsSize;
355     resize_outside_scale_for_touch = ash::kResizeOutsideBoundsScaleForTouch;
356     resize_area_corner_size = ash::kResizeAreaCornerSize;
357   }
358 #endif
359   apps::AppWindowFrameView* frame_view = new apps::AppWindowFrameView();
360   frame_view->Init(widget(),
361                    has_frame_color_,
362                    frame_color_,
363                    GetDraggableRegion(),
364                    resize_inside_bounds_size,
365                    resize_outside_bounds_size,
366                    resize_outside_scale_for_touch,
367                    resize_area_corner_size);
368   return frame_view;
369 }
370
371 // ui::BaseWindow implementation.
372
373 ui::WindowShowState ChromeNativeAppWindowViews::GetRestoredState() const {
374   if (IsMaximized())
375     return ui::SHOW_STATE_MAXIMIZED;
376   if (IsFullscreen()) {
377 #if defined(USE_ASH)
378     if (immersive_fullscreen_controller_.get() &&
379         immersive_fullscreen_controller_->IsEnabled()) {
380       // Restore windows which were previously in immersive fullscreen to
381       // maximized. Restoring the window to a different fullscreen type
382       // makes for a bad experience.
383       return ui::SHOW_STATE_MAXIMIZED;
384     }
385 #endif
386     return ui::SHOW_STATE_FULLSCREEN;
387   }
388 #if defined(USE_ASH)
389   // Use kRestoreShowStateKey in case a window is minimized/hidden.
390   ui::WindowShowState restore_state = widget()->GetNativeWindow()->GetProperty(
391       aura::client::kRestoreShowStateKey);
392   // Whitelist states to return so that invalid and transient states
393   // are not saved and used to restore windows when they are recreated.
394   switch (restore_state) {
395     case ui::SHOW_STATE_NORMAL:
396     case ui::SHOW_STATE_MAXIMIZED:
397     case ui::SHOW_STATE_FULLSCREEN:
398     case ui::SHOW_STATE_DETACHED:
399       return restore_state;
400
401     case ui::SHOW_STATE_DEFAULT:
402     case ui::SHOW_STATE_MINIMIZED:
403     case ui::SHOW_STATE_INACTIVE:
404     case ui::SHOW_STATE_END:
405       return ui::SHOW_STATE_NORMAL;
406   }
407 #endif
408   return ui::SHOW_STATE_NORMAL;
409 }
410
411 bool ChromeNativeAppWindowViews::IsAlwaysOnTop() const {
412   if (app_window()->window_type_is_panel()) {
413 #if defined(USE_ASH)
414     return ash::wm::GetWindowState(widget()->GetNativeWindow())
415         ->panel_attached();
416 #else
417     return true;
418 #endif
419   } else {
420     return widget()->IsAlwaysOnTop();
421   }
422 }
423
424 // views::ContextMenuController implementation.
425
426 void ChromeNativeAppWindowViews::ShowContextMenuForView(
427     views::View* source,
428     const gfx::Point& p,
429     ui::MenuSourceType source_type) {
430 #if defined(USE_ASH) && defined(OS_CHROMEOS)
431   scoped_ptr<ui::MenuModel> model =
432       CreateMultiUserContextMenu(app_window()->GetNativeWindow());
433   if (!model.get())
434     return;
435
436   // Only show context menu if point is in caption.
437   gfx::Point point_in_view_coords(p);
438   views::View::ConvertPointFromScreen(widget()->non_client_view(),
439                                       &point_in_view_coords);
440   int hit_test =
441       widget()->non_client_view()->NonClientHitTest(point_in_view_coords);
442   if (hit_test == HTCAPTION) {
443     menu_runner_.reset(new views::MenuRunner(model.get()));
444     if (menu_runner_->RunMenuAt(source->GetWidget(), NULL,
445           gfx::Rect(p, gfx::Size(0,0)), views::MenuItemView::TOPLEFT,
446           source_type,
447           views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) ==
448         views::MenuRunner::MENU_DELETED)
449       return;
450   }
451 #endif
452 }
453
454 // views::WidgetDelegate implementation.
455
456 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowAppIcon() {
457   gfx::Image app_icon = app_window()->app_icon();
458   if (app_icon.IsEmpty())
459     return GetWindowIcon();
460   else
461     return *app_icon.ToImageSkia();
462 }
463
464 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowIcon() {
465   content::WebContents* web_contents = app_window()->web_contents();
466   if (web_contents) {
467     FaviconTabHelper* favicon_tab_helper =
468         FaviconTabHelper::FromWebContents(web_contents);
469     gfx::Image app_icon = favicon_tab_helper->GetFavicon();
470     if (!app_icon.IsEmpty())
471       return *app_icon.ToImageSkia();
472   }
473   return gfx::ImageSkia();
474 }
475
476 views::NonClientFrameView* ChromeNativeAppWindowViews::CreateNonClientFrameView(
477     views::Widget* widget) {
478 #if defined(USE_ASH)
479   if (chrome::IsNativeViewInAsh(widget->GetNativeView())) {
480     // Set the delegate now because CustomFrameViewAsh sets the
481     // WindowStateDelegate if one is not already set.
482     ash::wm::GetWindowState(GetNativeWindow())->SetDelegate(
483         scoped_ptr<ash::wm::WindowStateDelegate>(
484             new NativeAppWindowStateDelegate(app_window(), this)).Pass());
485
486     if (app_window()->window_type_is_panel()) {
487       ash::PanelFrameView::FrameType frame_type = IsFrameless() ?
488           ash::PanelFrameView::FRAME_NONE : ash::PanelFrameView::FRAME_ASH;
489       views::NonClientFrameView* frame_view =
490           new ash::PanelFrameView(widget, frame_type);
491       frame_view->set_context_menu_controller(this);
492       return frame_view;
493     }
494
495     if (!IsFrameless()) {
496       ash::CustomFrameViewAsh* custom_frame_view =
497           new ash::CustomFrameViewAsh(widget);
498 #if defined(OS_CHROMEOS)
499       // Non-frameless app windows can be put into immersive fullscreen.
500       // TODO(pkotwicz): Investigate if immersive fullscreen can be enabled for
501       // Windows Ash.
502       immersive_fullscreen_controller_.reset(
503           new ash::ImmersiveFullscreenController());
504       custom_frame_view->InitImmersiveFullscreenControllerForView(
505           immersive_fullscreen_controller_.get());
506 #endif
507       custom_frame_view->GetHeaderView()->set_context_menu_controller(this);
508       return custom_frame_view;
509     }
510   }
511 #endif
512 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
513   return CreateAppWindowFrameView();
514 #else
515   if (IsFrameless() || has_frame_color_)
516     return CreateAppWindowFrameView();
517 #endif
518   return views::WidgetDelegateView::CreateNonClientFrameView(widget);
519 }
520
521 bool ChromeNativeAppWindowViews::WidgetHasHitTestMask() const {
522   return shape_ != NULL;
523 }
524
525 void ChromeNativeAppWindowViews::GetWidgetHitTestMask(gfx::Path* mask) const {
526   shape_->getBoundaryPath(mask);
527 }
528
529 // views::View implementation.
530
531 gfx::Size ChromeNativeAppWindowViews::GetPreferredSize() {
532   if (!preferred_size_.IsEmpty())
533     return preferred_size_;
534   return NativeAppWindowViews::GetPreferredSize();
535 }
536
537 bool ChromeNativeAppWindowViews::AcceleratorPressed(
538     const ui::Accelerator& accelerator) {
539   const std::map<ui::Accelerator, int>& accelerator_table =
540       GetAcceleratorTable();
541   std::map<ui::Accelerator, int>::const_iterator iter =
542       accelerator_table.find(accelerator);
543   DCHECK(iter != accelerator_table.end());
544   int command_id = iter->second;
545   switch (command_id) {
546     case IDC_CLOSE_WINDOW:
547       Close();
548       return true;
549     case IDC_ZOOM_MINUS:
550       chrome_page_zoom::Zoom(web_view()->GetWebContents(),
551                              content::PAGE_ZOOM_OUT);
552       return true;
553     case IDC_ZOOM_NORMAL:
554       chrome_page_zoom::Zoom(web_view()->GetWebContents(),
555                              content::PAGE_ZOOM_RESET);
556       return true;
557     case IDC_ZOOM_PLUS:
558       chrome_page_zoom::Zoom(web_view()->GetWebContents(),
559                              content::PAGE_ZOOM_IN);
560       return true;
561     default:
562       NOTREACHED() << "Unknown accelerator sent to app window.";
563   }
564   return NativeAppWindowViews::AcceleratorPressed(accelerator);
565 }
566
567 // NativeAppWindow implementation.
568
569 void ChromeNativeAppWindowViews::SetFullscreen(int fullscreen_types) {
570   // Fullscreen not supported by panels.
571   if (app_window()->window_type_is_panel())
572     return;
573   is_fullscreen_ = (fullscreen_types != AppWindow::FULLSCREEN_TYPE_NONE);
574   widget()->SetFullscreen(is_fullscreen_);
575
576 #if defined(USE_ASH)
577   if (immersive_fullscreen_controller_.get()) {
578     // |immersive_fullscreen_controller_| should only be set if immersive
579     // fullscreen is the fullscreen type used by the OS.
580     immersive_fullscreen_controller_->SetEnabled(
581         ash::ImmersiveFullscreenController::WINDOW_TYPE_PACKAGED_APP,
582         (fullscreen_types & AppWindow::FULLSCREEN_TYPE_OS) != 0);
583     // Autohide the shelf instead of hiding the shelf completely when only in
584     // OS fullscreen.
585     ash::wm::WindowState* window_state =
586         ash::wm::GetWindowState(widget()->GetNativeWindow());
587     window_state->set_hide_shelf_when_fullscreen(fullscreen_types !=
588                                                  AppWindow::FULLSCREEN_TYPE_OS);
589     DCHECK(ash::Shell::HasInstance());
590     ash::Shell::GetInstance()->UpdateShelfVisibility();
591   }
592 #endif
593
594   // TODO(jeremya) we need to call RenderViewHost::ExitFullscreen() if we
595   // ever drop the window out of fullscreen in response to something that
596   // wasn't the app calling webkitCancelFullScreen().
597 }
598
599 bool ChromeNativeAppWindowViews::IsFullscreenOrPending() const {
600   return is_fullscreen_;
601 }
602
603 bool ChromeNativeAppWindowViews::IsDetached() const {
604   if (!app_window()->window_type_is_panel())
605     return false;
606 #if defined(USE_ASH)
607   return !ash::wm::GetWindowState(widget()->GetNativeWindow())
608               ->panel_attached();
609 #else
610   return false;
611 #endif
612 }
613
614 void ChromeNativeAppWindowViews::UpdateBadgeIcon() {
615   const gfx::Image* icon = NULL;
616   if (!app_window()->badge_icon().IsEmpty()) {
617     icon = &app_window()->badge_icon();
618     // chrome::DrawTaskbarDecoration can do interesting things with non-square
619     // bitmaps.
620     // TODO(benwells): Refactor chrome::DrawTaskbarDecoration to not be avatar
621     // specific, and lift this restriction.
622     if (icon->Width() != icon->Height()) {
623       LOG(ERROR) << "Attempt to set a non-square badge; request ignored.";
624       return;
625     }
626   }
627   chrome::DrawTaskbarDecoration(GetNativeWindow(), icon);
628 }
629
630 void ChromeNativeAppWindowViews::UpdateShape(scoped_ptr<SkRegion> region) {
631   bool had_shape = shape_;
632   shape_ = region.Pass();
633
634   aura::Window* native_window = widget()->GetNativeWindow();
635   if (shape_) {
636     widget()->SetShape(new SkRegion(*shape_));
637     if (!had_shape) {
638       native_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
639           new ShapedAppWindowTargeter(native_window, this)));
640     }
641   } else {
642     widget()->SetShape(NULL);
643     if (had_shape)
644       native_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>());
645   }
646 }
647
648 bool ChromeNativeAppWindowViews::HasFrameColor() const {
649   return has_frame_color_;
650 }
651
652 SkColor ChromeNativeAppWindowViews::FrameColor() const { return frame_color_; }
653
654 // NativeAppWindowViews implementation.
655
656 void ChromeNativeAppWindowViews::InitializeWindow(
657     AppWindow* app_window,
658     const AppWindow::CreateParams& create_params) {
659   DCHECK(widget());
660   has_frame_color_ = create_params.has_frame_color;
661   frame_color_ = create_params.frame_color;
662   if (create_params.window_type == AppWindow::WINDOW_TYPE_PANEL ||
663       create_params.window_type == AppWindow::WINDOW_TYPE_V1_PANEL) {
664     InitializePanelWindow(create_params);
665   } else {
666     InitializeDefaultWindow(create_params);
667   }
668   extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
669       Profile::FromBrowserContext(app_window->browser_context()),
670       widget()->GetFocusManager(),
671       extensions::ExtensionKeybindingRegistry::PLATFORM_APPS_ONLY,
672       NULL));
673
674 #if defined(OS_WIN)
675   if ((IsFrameless() || has_frame_color_) &&
676       chrome::GetHostDesktopTypeForNativeWindow(widget()->GetNativeWindow()) !=
677           chrome::HOST_DESKTOP_TYPE_ASH) {
678     InstallEasyResizeTargeterOnContainer();
679   }
680 #endif
681 }