Revert "Only store leading 13 bits of password hash."
[chromium-blink-merge.git] / chrome / browser / ui / views / apps / chrome_native_app_window_views.cc
blobcb150261b2e9a0bce1ba8018bf80c04f7dc8a2fc
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.
5 #include "chrome/browser/ui/views/apps/chrome_native_app_window_views.h"
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/favicon/favicon_tab_helper.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/host_desktop.h"
14 #include "chrome/browser/ui/views/apps/desktop_keyboard_capture.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 "components/ui/zoom/page_zoom.h"
21 #include "components/ui/zoom/zoom_controller.h"
22 #include "extensions/common/extension.h"
23 #include "ui/aura/window.h"
24 #include "ui/base/hit_test.h"
25 #include "ui/base/models/simple_menu_model.h"
26 #include "ui/gfx/image/image_skia.h"
27 #include "ui/views/controls/menu/menu_runner.h"
28 #include "ui/views/controls/webview/webview.h"
29 #include "ui/views/widget/widget.h"
30 #include "ui/wm/core/easy_resize_window_targeter.h"
31 #include "ui/wm/core/shadow_types.h"
33 #if defined(OS_LINUX)
34 #include "chrome/browser/shell_integration_linux.h"
35 #endif
37 #if defined(USE_ASH)
38 #include "ash/ash_constants.h"
39 #include "ash/ash_switches.h"
40 #include "ash/frame/custom_frame_view_ash.h"
41 #include "ash/screen_util.h"
42 #include "ash/shell.h"
43 #include "ash/wm/immersive_fullscreen_controller.h"
44 #include "ash/wm/panels/panel_frame_view.h"
45 #include "ash/wm/window_properties.h"
46 #include "ash/wm/window_state.h"
47 #include "ash/wm/window_state_delegate.h"
48 #include "ash/wm/window_state_observer.h"
49 #include "chrome/browser/ui/ash/ash_util.h"
50 #include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
51 #include "ui/aura/client/aura_constants.h"
52 #include "ui/aura/client/window_tree_client.h"
53 #include "ui/aura/window_observer.h"
54 #endif
56 #if defined(OS_CHROMEOS)
57 #include "ash/shell_window_ids.h"
58 #endif
60 using extensions::AppWindow;
62 namespace {
64 const int kMinPanelWidth = 100;
65 const int kMinPanelHeight = 100;
66 const int kDefaultPanelWidth = 200;
67 const int kDefaultPanelHeight = 300;
69 struct AcceleratorMapping {
70 ui::KeyboardCode keycode;
71 int modifiers;
72 int command_id;
75 const AcceleratorMapping kAppWindowAcceleratorMap[] = {
76 { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
77 { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
78 { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW },
81 // These accelerators will only be available in kiosk mode. These allow the
82 // user to manually zoom app windows. This is only necessary in kiosk mode
83 // (in normal mode, the user can zoom via the screen magnifier).
84 // TODO(xiyuan): Write a test for kiosk accelerators.
85 const AcceleratorMapping kAppWindowKioskAppModeAcceleratorMap[] = {
86 { ui::VKEY_OEM_MINUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
87 { ui::VKEY_OEM_MINUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN,
88 IDC_ZOOM_MINUS },
89 { ui::VKEY_SUBTRACT, ui::EF_CONTROL_DOWN, IDC_ZOOM_MINUS },
90 { ui::VKEY_OEM_PLUS, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
91 { ui::VKEY_OEM_PLUS, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
92 { ui::VKEY_ADD, ui::EF_CONTROL_DOWN, IDC_ZOOM_PLUS },
93 { ui::VKEY_0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
94 { ui::VKEY_NUMPAD0, ui::EF_CONTROL_DOWN, IDC_ZOOM_NORMAL },
97 void AddAcceleratorsFromMapping(const AcceleratorMapping mapping[],
98 size_t mapping_length,
99 std::map<ui::Accelerator, int>* accelerators) {
100 for (size_t i = 0; i < mapping_length; ++i) {
101 ui::Accelerator accelerator(mapping[i].keycode, mapping[i].modifiers);
102 (*accelerators)[accelerator] = mapping[i].command_id;
106 const std::map<ui::Accelerator, int>& GetAcceleratorTable() {
107 typedef std::map<ui::Accelerator, int> AcceleratorMap;
108 CR_DEFINE_STATIC_LOCAL(AcceleratorMap, accelerators, ());
109 if (accelerators.empty()) {
110 AddAcceleratorsFromMapping(
111 kAppWindowAcceleratorMap,
112 arraysize(kAppWindowAcceleratorMap),
113 &accelerators);
115 // Add accelerators for kiosk mode.
116 if (chrome::IsRunningInForcedAppMode()) {
117 AddAcceleratorsFromMapping(
118 kAppWindowKioskAppModeAcceleratorMap,
119 arraysize(kAppWindowKioskAppModeAcceleratorMap),
120 &accelerators);
123 return accelerators;
126 #if defined(USE_ASH)
127 // This class handles a user's fullscreen request (Shift+F4/F4).
128 class NativeAppWindowStateDelegate : public ash::wm::WindowStateDelegate,
129 public ash::wm::WindowStateObserver,
130 public aura::WindowObserver {
131 public:
132 NativeAppWindowStateDelegate(AppWindow* app_window,
133 extensions::NativeAppWindow* native_app_window)
134 : app_window_(app_window),
135 window_state_(
136 ash::wm::GetWindowState(native_app_window->GetNativeWindow())) {
137 // Add a window state observer to exit fullscreen properly in case
138 // fullscreen is exited without going through AppWindow::Restore(). This
139 // is the case when exiting immersive fullscreen via the "Restore" window
140 // control.
141 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
142 window_state_->AddObserver(this);
143 window_state_->window()->AddObserver(this);
145 ~NativeAppWindowStateDelegate() override {
146 if (window_state_) {
147 window_state_->RemoveObserver(this);
148 window_state_->window()->RemoveObserver(this);
152 private:
153 // Overridden from ash::wm::WindowStateDelegate.
154 bool ToggleFullscreen(ash::wm::WindowState* window_state) override {
155 // Windows which cannot be maximized should not be fullscreened.
156 DCHECK(window_state->IsFullscreen() || window_state->CanMaximize());
157 if (window_state->IsFullscreen())
158 app_window_->Restore();
159 else if (window_state->CanMaximize())
160 app_window_->OSFullscreen();
161 return true;
164 // Overridden from ash::wm::WindowStateObserver:
165 void OnPostWindowStateTypeChange(ash::wm::WindowState* window_state,
166 ash::wm::WindowStateType old_type) override {
167 // Since the window state might get set by a window manager, it is possible
168 // to come here before the application set its |BaseWindow|.
169 if (!window_state->IsFullscreen() && !window_state->IsMinimized() &&
170 app_window_->GetBaseWindow() &&
171 app_window_->GetBaseWindow()->IsFullscreenOrPending()) {
172 app_window_->Restore();
173 // Usually OnNativeWindowChanged() is called when the window bounds are
174 // changed as a result of a state type change. Because the change in state
175 // type has already occurred, we need to call OnNativeWindowChanged()
176 // explicitly.
177 app_window_->OnNativeWindowChanged();
181 // Overridden from aura::WindowObserver:
182 void OnWindowDestroying(aura::Window* window) override {
183 window_state_->RemoveObserver(this);
184 window_state_->window()->RemoveObserver(this);
185 window_state_ = NULL;
188 // Not owned.
189 AppWindow* app_window_;
190 ash::wm::WindowState* window_state_;
192 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowStateDelegate);
194 #endif // USE_ASH
196 } // namespace
198 ChromeNativeAppWindowViews::ChromeNativeAppWindowViews()
199 : is_fullscreen_(false),
200 has_frame_color_(false),
201 active_frame_color_(SK_ColorBLACK),
202 inactive_frame_color_(SK_ColorBLACK) {
205 ChromeNativeAppWindowViews::~ChromeNativeAppWindowViews() {}
207 void ChromeNativeAppWindowViews::OnBeforeWidgetInit(
208 views::Widget::InitParams* init_params,
209 views::Widget* widget) {}
211 void ChromeNativeAppWindowViews::InitializeDefaultWindow(
212 const AppWindow::CreateParams& create_params) {
213 std::string app_name = web_app::GenerateApplicationNameFromExtensionId(
214 app_window()->extension_id());
216 views::Widget::InitParams init_params(views::Widget::InitParams::TYPE_WINDOW);
217 init_params.delegate = this;
218 init_params.remove_standard_frame = IsFrameless() || has_frame_color_;
219 init_params.use_system_default_icon = true;
220 if (create_params.alpha_enabled)
221 init_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
222 init_params.keep_on_top = create_params.always_on_top;
223 init_params.visible_on_all_workspaces =
224 create_params.visible_on_all_workspaces;
226 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
227 // Set up a custom WM_CLASS for app windows. This allows task switchers in
228 // X11 environments to distinguish them from main browser windows.
229 init_params.wm_class_name = web_app::GetWMClassFromAppName(app_name);
230 init_params.wm_class_class = shell_integration_linux::GetProgramClassName();
231 const char kX11WindowRoleApp[] = "app";
232 init_params.wm_role_name = std::string(kX11WindowRoleApp);
233 #endif
235 OnBeforeWidgetInit(&init_params, widget());
236 #if defined(OS_CHROMEOS)
237 if (create_params.is_ime_window) {
238 // Puts ime windows into ime window container.
239 init_params.parent =
240 ash::Shell::GetContainer(ash::Shell::GetPrimaryRootWindow(),
241 ash::kShellWindowId_ImeWindowParentContainer);
243 #endif
244 widget()->Init(init_params);
246 // The frame insets are required to resolve the bounds specifications
247 // correctly. So we set the window bounds and constraints now.
248 gfx::Insets frame_insets = GetFrameInsets();
249 gfx::Rect window_bounds = create_params.GetInitialWindowBounds(frame_insets);
250 SetContentSizeConstraints(create_params.GetContentMinimumSize(frame_insets),
251 create_params.GetContentMaximumSize(frame_insets));
252 if (!window_bounds.IsEmpty()) {
253 typedef AppWindow::BoundsSpecification BoundsSpecification;
254 bool position_specified =
255 window_bounds.x() != BoundsSpecification::kUnspecifiedPosition &&
256 window_bounds.y() != BoundsSpecification::kUnspecifiedPosition;
257 if (!position_specified)
258 widget()->CenterWindow(window_bounds.size());
259 else
260 widget()->SetBounds(window_bounds);
263 if (IsFrameless() &&
264 init_params.opacity == views::Widget::InitParams::TRANSLUCENT_WINDOW) {
265 // The given window is most likely not rectangular since it uses
266 // transparency and has no standard frame, don't show a shadow for it.
267 // TODO(skuhne): If we run into an application which should have a shadow
268 // but does not have, a new attribute has to be added.
269 wm::SetShadowType(widget()->GetNativeWindow(), wm::SHADOW_TYPE_NONE);
272 #if defined(OS_CHROMEOS)
273 if (create_params.is_ime_window)
274 return;
275 #endif
277 // Register accelarators supported by app windows.
278 // TODO(jeremya/stevenjb): should these be registered for panels too?
279 views::FocusManager* focus_manager = GetFocusManager();
280 const std::map<ui::Accelerator, int>& accelerator_table =
281 GetAcceleratorTable();
282 const bool is_kiosk_app_mode = chrome::IsRunningInForcedAppMode();
284 // Ensures that kiosk mode accelerators are enabled when in kiosk mode (to be
285 // future proof). This is needed because GetAcceleratorTable() uses a static
286 // to store data and only checks kiosk mode once. If a platform app is
287 // launched before kiosk mode starts, the kiosk accelerators will not be
288 // registered. This DCHECK catches the case.
289 DCHECK(!is_kiosk_app_mode ||
290 accelerator_table.size() ==
291 arraysize(kAppWindowAcceleratorMap) +
292 arraysize(kAppWindowKioskAppModeAcceleratorMap));
294 // Ensure there is a ZoomController in kiosk mode, otherwise the processing
295 // of the accelerators will cause a crash.
296 DCHECK(!is_kiosk_app_mode || ui_zoom::ZoomController::FromWebContents(
297 web_view()->GetWebContents()));
299 for (std::map<ui::Accelerator, int>::const_iterator iter =
300 accelerator_table.begin();
301 iter != accelerator_table.end(); ++iter) {
302 if (is_kiosk_app_mode && !chrome::IsCommandAllowedInAppMode(iter->second))
303 continue;
305 focus_manager->RegisterAccelerator(
306 iter->first, ui::AcceleratorManager::kNormalPriority, this);
310 void ChromeNativeAppWindowViews::InitializePanelWindow(
311 const AppWindow::CreateParams& create_params) {
312 views::Widget::InitParams params(views::Widget::InitParams::TYPE_PANEL);
313 params.delegate = this;
315 gfx::Rect initial_window_bounds =
316 create_params.GetInitialWindowBounds(gfx::Insets());
317 preferred_size_ = gfx::Size(initial_window_bounds.width(),
318 initial_window_bounds.height());
319 if (preferred_size_.width() == 0)
320 preferred_size_.set_width(kDefaultPanelWidth);
321 else if (preferred_size_.width() < kMinPanelWidth)
322 preferred_size_.set_width(kMinPanelWidth);
324 if (preferred_size_.height() == 0)
325 preferred_size_.set_height(kDefaultPanelHeight);
326 else if (preferred_size_.height() < kMinPanelHeight)
327 preferred_size_.set_height(kMinPanelHeight);
328 #if defined(USE_ASH)
329 if (ash::Shell::HasInstance()) {
330 // Open a new panel on the target root.
331 aura::Window* target = ash::Shell::GetTargetRootWindow();
332 params.bounds = ash::ScreenUtil::ConvertRectToScreen(
333 target, gfx::Rect(preferred_size_));
334 } else {
335 params.bounds = gfx::Rect(preferred_size_);
337 #else
338 params.bounds = gfx::Rect(preferred_size_);
339 #endif
340 widget()->Init(params);
341 widget()->set_focus_on_creation(create_params.focused);
344 views::NonClientFrameView*
345 ChromeNativeAppWindowViews::CreateStandardDesktopAppFrame() {
346 return views::WidgetDelegateView::CreateNonClientFrameView(widget());
349 apps::AppWindowFrameView*
350 ChromeNativeAppWindowViews::CreateNonStandardAppFrame() {
351 apps::AppWindowFrameView* frame =
352 new apps::AppWindowFrameView(widget(),
353 this,
354 has_frame_color_,
355 active_frame_color_,
356 inactive_frame_color_);
357 frame->Init();
358 #if defined(USE_ASH)
359 // For Aura windows on the Ash desktop the sizes are different and the user
360 // can resize the window from slightly outside the bounds as well.
361 if (chrome::IsNativeWindowInAsh(widget()->GetNativeWindow())) {
362 frame->SetResizeSizes(ash::kResizeInsideBoundsSize,
363 ash::kResizeOutsideBoundsSize,
364 ash::kResizeAreaCornerSize);
366 #endif
368 #if !defined(OS_CHROMEOS)
369 // For non-Ash windows, install an easy resize window targeter, which ensures
370 // that the root window (not the app) receives mouse events on the edges.
371 if (chrome::GetHostDesktopTypeForNativeWindow(widget()->GetNativeWindow()) !=
372 chrome::HOST_DESKTOP_TYPE_ASH) {
373 aura::Window* window = widget()->GetNativeWindow();
374 int resize_inside = frame->resize_inside_bounds_size();
375 gfx::Insets inset(
376 resize_inside, resize_inside, resize_inside, resize_inside);
377 // Add the EasyResizeWindowTargeter on the window, not its root window. The
378 // root window does not have a delegate, which is needed to handle the event
379 // in Linux.
380 window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
381 new wm::EasyResizeWindowTargeter(window, inset, inset)));
383 #endif
385 return frame;
388 // ui::BaseWindow implementation.
390 gfx::Rect ChromeNativeAppWindowViews::GetRestoredBounds() const {
391 #if defined(USE_ASH)
392 gfx::Rect* bounds = widget()->GetNativeWindow()->GetProperty(
393 ash::kRestoreBoundsOverrideKey);
394 if (bounds && !bounds->IsEmpty())
395 return *bounds;
396 #endif
397 return widget()->GetRestoredBounds();
400 ui::WindowShowState ChromeNativeAppWindowViews::GetRestoredState() const {
401 #if !defined(USE_ASH)
402 if (IsMaximized())
403 return ui::SHOW_STATE_MAXIMIZED;
404 if (IsFullscreen())
405 return ui::SHOW_STATE_FULLSCREEN;
406 #else
407 // Use kRestoreShowStateKey in case a window is minimized/hidden.
408 ui::WindowShowState restore_state = widget()->GetNativeWindow()->GetProperty(
409 aura::client::kRestoreShowStateKey);
410 if (widget()->GetNativeWindow()->GetProperty(
411 ash::kRestoreBoundsOverrideKey)) {
412 // If an override is given, we use that restore state (after filtering).
413 restore_state = widget()->GetNativeWindow()->GetProperty(
414 ash::kRestoreShowStateOverrideKey);
415 } else {
416 // Otherwise first normal states are checked.
417 if (IsMaximized())
418 return ui::SHOW_STATE_MAXIMIZED;
419 if (IsFullscreen()) {
420 if (immersive_fullscreen_controller_.get() &&
421 immersive_fullscreen_controller_->IsEnabled()) {
422 // Restore windows which were previously in immersive fullscreen to
423 // maximized. Restoring the window to a different fullscreen type
424 // makes for a bad experience.
425 return ui::SHOW_STATE_MAXIMIZED;
427 return ui::SHOW_STATE_FULLSCREEN;
430 // Whitelist states to return so that invalid and transient states
431 // are not saved and used to restore windows when they are recreated.
432 switch (restore_state) {
433 case ui::SHOW_STATE_NORMAL:
434 case ui::SHOW_STATE_MAXIMIZED:
435 case ui::SHOW_STATE_FULLSCREEN:
436 return restore_state;
438 case ui::SHOW_STATE_DEFAULT:
439 case ui::SHOW_STATE_MINIMIZED:
440 case ui::SHOW_STATE_INACTIVE:
441 case ui::SHOW_STATE_END:
442 return ui::SHOW_STATE_NORMAL;
444 #endif // !defined(USE_ASH)
445 return ui::SHOW_STATE_NORMAL;
448 bool ChromeNativeAppWindowViews::IsAlwaysOnTop() const {
449 if (app_window()->window_type_is_panel()) {
450 #if defined(USE_ASH)
451 return ash::wm::GetWindowState(widget()->GetNativeWindow())
452 ->panel_attached();
453 #else
454 return true;
455 #endif
456 } else {
457 return widget()->IsAlwaysOnTop();
461 // views::ContextMenuController implementation.
463 void ChromeNativeAppWindowViews::ShowContextMenuForView(
464 views::View* source,
465 const gfx::Point& p,
466 ui::MenuSourceType source_type) {
467 #if defined(USE_ASH) && defined(OS_CHROMEOS)
468 scoped_ptr<ui::MenuModel> model =
469 CreateMultiUserContextMenu(app_window()->GetNativeWindow());
470 if (!model.get())
471 return;
473 // Only show context menu if point is in caption.
474 gfx::Point point_in_view_coords(p);
475 views::View::ConvertPointFromScreen(widget()->non_client_view(),
476 &point_in_view_coords);
477 int hit_test =
478 widget()->non_client_view()->NonClientHitTest(point_in_view_coords);
479 if (hit_test == HTCAPTION) {
480 menu_runner_.reset(new views::MenuRunner(
481 model.get(),
482 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU));
483 if (menu_runner_->RunMenuAt(source->GetWidget(),
484 NULL,
485 gfx::Rect(p, gfx::Size(0, 0)),
486 views::MENU_ANCHOR_TOPLEFT,
487 source_type) ==
488 views::MenuRunner::MENU_DELETED) {
489 return;
492 #endif
495 // views::WidgetDelegate implementation.
497 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowAppIcon() {
498 gfx::Image app_icon = app_window()->app_icon();
499 if (app_icon.IsEmpty())
500 return GetWindowIcon();
501 else
502 return *app_icon.ToImageSkia();
505 gfx::ImageSkia ChromeNativeAppWindowViews::GetWindowIcon() {
506 content::WebContents* web_contents = app_window()->web_contents();
507 if (web_contents) {
508 FaviconTabHelper* favicon_tab_helper =
509 FaviconTabHelper::FromWebContents(web_contents);
510 gfx::Image app_icon = favicon_tab_helper->GetFavicon();
511 if (!app_icon.IsEmpty())
512 return *app_icon.ToImageSkia();
514 return gfx::ImageSkia();
517 views::NonClientFrameView* ChromeNativeAppWindowViews::CreateNonClientFrameView(
518 views::Widget* widget) {
519 #if defined(USE_ASH)
520 if (chrome::IsNativeViewInAsh(widget->GetNativeView())) {
521 // Set the delegate now because CustomFrameViewAsh sets the
522 // WindowStateDelegate if one is not already set.
523 ash::wm::GetWindowState(GetNativeWindow())->SetDelegate(
524 scoped_ptr<ash::wm::WindowStateDelegate>(
525 new NativeAppWindowStateDelegate(app_window(), this)).Pass());
527 if (IsFrameless())
528 return CreateNonStandardAppFrame();
530 if (app_window()->window_type_is_panel()) {
531 views::NonClientFrameView* frame_view =
532 new ash::PanelFrameView(widget, ash::PanelFrameView::FRAME_ASH);
533 frame_view->set_context_menu_controller(this);
534 return frame_view;
537 ash::CustomFrameViewAsh* custom_frame_view =
538 new ash::CustomFrameViewAsh(widget);
539 // Non-frameless app windows can be put into immersive fullscreen.
540 immersive_fullscreen_controller_.reset(
541 new ash::ImmersiveFullscreenController());
542 custom_frame_view->InitImmersiveFullscreenControllerForView(
543 immersive_fullscreen_controller_.get());
544 custom_frame_view->GetHeaderView()->set_context_menu_controller(this);
546 if (has_frame_color_) {
547 custom_frame_view->SetFrameColors(active_frame_color_,
548 inactive_frame_color_);
551 return custom_frame_view;
553 #endif
554 return (IsFrameless() || has_frame_color_) ?
555 CreateNonStandardAppFrame() : CreateStandardDesktopAppFrame();
558 bool ChromeNativeAppWindowViews::WidgetHasHitTestMask() const {
559 return shape_ != NULL;
562 void ChromeNativeAppWindowViews::GetWidgetHitTestMask(gfx::Path* mask) const {
563 shape_->getBoundaryPath(mask);
566 // views::View implementation.
568 gfx::Size ChromeNativeAppWindowViews::GetPreferredSize() const {
569 if (!preferred_size_.IsEmpty())
570 return preferred_size_;
571 return NativeAppWindowViews::GetPreferredSize();
574 bool ChromeNativeAppWindowViews::AcceleratorPressed(
575 const ui::Accelerator& accelerator) {
576 const std::map<ui::Accelerator, int>& accelerator_table =
577 GetAcceleratorTable();
578 std::map<ui::Accelerator, int>::const_iterator iter =
579 accelerator_table.find(accelerator);
580 DCHECK(iter != accelerator_table.end());
581 int command_id = iter->second;
582 switch (command_id) {
583 case IDC_CLOSE_WINDOW:
584 Close();
585 return true;
586 case IDC_ZOOM_MINUS:
587 ui_zoom::PageZoom::Zoom(web_view()->GetWebContents(),
588 content::PAGE_ZOOM_OUT);
589 return true;
590 case IDC_ZOOM_NORMAL:
591 ui_zoom::PageZoom::Zoom(web_view()->GetWebContents(),
592 content::PAGE_ZOOM_RESET);
593 return true;
594 case IDC_ZOOM_PLUS:
595 ui_zoom::PageZoom::Zoom(web_view()->GetWebContents(),
596 content::PAGE_ZOOM_IN);
597 return true;
598 default:
599 NOTREACHED() << "Unknown accelerator sent to app window.";
601 return NativeAppWindowViews::AcceleratorPressed(accelerator);
604 // NativeAppWindow implementation.
606 void ChromeNativeAppWindowViews::SetFullscreen(int fullscreen_types) {
607 // Fullscreen not supported by panels.
608 if (app_window()->window_type_is_panel())
609 return;
610 is_fullscreen_ = (fullscreen_types != AppWindow::FULLSCREEN_TYPE_NONE);
611 widget()->SetFullscreen(is_fullscreen_);
613 #if defined(USE_ASH)
614 if (immersive_fullscreen_controller_.get()) {
615 // |immersive_fullscreen_controller_| should only be set if immersive
616 // fullscreen is the fullscreen type used by the OS.
617 immersive_fullscreen_controller_->SetEnabled(
618 ash::ImmersiveFullscreenController::WINDOW_TYPE_PACKAGED_APP,
619 (fullscreen_types & AppWindow::FULLSCREEN_TYPE_OS) != 0);
620 // Autohide the shelf instead of hiding the shelf completely when only in
621 // OS fullscreen.
622 ash::wm::WindowState* window_state =
623 ash::wm::GetWindowState(widget()->GetNativeWindow());
624 window_state->set_hide_shelf_when_fullscreen(fullscreen_types !=
625 AppWindow::FULLSCREEN_TYPE_OS);
626 DCHECK(ash::Shell::HasInstance());
627 ash::Shell::GetInstance()->UpdateShelfVisibility();
629 #endif
631 // TODO(jeremya) we need to call RenderViewHost::ExitFullscreen() if we
632 // ever drop the window out of fullscreen in response to something that
633 // wasn't the app calling webkitCancelFullScreen().
636 bool ChromeNativeAppWindowViews::IsFullscreenOrPending() const {
637 return is_fullscreen_;
640 void ChromeNativeAppWindowViews::UpdateBadgeIcon() {
641 const gfx::Image* icon = NULL;
642 if (!app_window()->badge_icon().IsEmpty()) {
643 icon = &app_window()->badge_icon();
644 // chrome::DrawTaskbarDecoration can do interesting things with non-square
645 // bitmaps.
646 // TODO(benwells): Refactor chrome::DrawTaskbarDecoration to not be avatar
647 // specific, and lift this restriction.
648 if (icon->Width() != icon->Height()) {
649 LOG(ERROR) << "Attempt to set a non-square badge; request ignored.";
650 return;
653 chrome::DrawTaskbarDecoration(GetNativeWindow(), icon);
656 void ChromeNativeAppWindowViews::UpdateShape(scoped_ptr<SkRegion> region) {
657 bool had_shape = shape_;
658 shape_ = region.Pass();
660 aura::Window* native_window = widget()->GetNativeWindow();
661 if (shape_) {
662 widget()->SetShape(new SkRegion(*shape_));
663 if (!had_shape) {
664 native_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>(
665 new ShapedAppWindowTargeter(native_window, this)));
667 } else {
668 widget()->SetShape(NULL);
669 if (had_shape)
670 native_window->SetEventTargeter(scoped_ptr<ui::EventTargeter>());
672 widget()->OnSizeConstraintsChanged();
675 bool ChromeNativeAppWindowViews::HasFrameColor() const {
676 return has_frame_color_;
679 SkColor ChromeNativeAppWindowViews::ActiveFrameColor() const {
680 return active_frame_color_;
683 SkColor ChromeNativeAppWindowViews::InactiveFrameColor() const {
684 return inactive_frame_color_;
687 void ChromeNativeAppWindowViews::SetInterceptAllKeys(bool want_all_keys) {
688 if (want_all_keys && (desktop_keyboard_capture_.get() == NULL)) {
689 desktop_keyboard_capture_.reset(new DesktopKeyboardCapture(widget()));
690 } else if (!want_all_keys) {
691 desktop_keyboard_capture_.reset(NULL);
695 // NativeAppWindowViews implementation.
697 void ChromeNativeAppWindowViews::InitializeWindow(
698 AppWindow* app_window,
699 const AppWindow::CreateParams& create_params) {
700 DCHECK(widget());
701 has_frame_color_ = create_params.has_frame_color;
702 active_frame_color_ = create_params.active_frame_color;
703 inactive_frame_color_ = create_params.inactive_frame_color;
704 if (create_params.window_type == AppWindow::WINDOW_TYPE_PANEL ||
705 create_params.window_type == AppWindow::WINDOW_TYPE_V1_PANEL) {
706 InitializePanelWindow(create_params);
707 } else {
708 InitializeDefaultWindow(create_params);
710 extension_keybinding_registry_.reset(new ExtensionKeybindingRegistryViews(
711 Profile::FromBrowserContext(app_window->browser_context()),
712 widget()->GetFocusManager(),
713 extensions::ExtensionKeybindingRegistry::PLATFORM_APPS_ONLY,
714 NULL));