Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / apps / native_app_window_views.cc
blobbb28fc510fe7431e703810bd71d5574d7d10abd7
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 "chrome/browser/ui/views/apps/native_app_window_views.h"
7 #include "apps/shell_window.h"
8 #include "apps/ui/views/shell_window_frame_view.h"
9 #include "base/command_line.h"
10 #include "base/file_util.h"
11 #include "base/path_service.h"
12 #include "base/threading/sequenced_worker_pool.h"
13 #include "chrome/app/chrome_command_ids.h"
14 #include "chrome/browser/extensions/extension_host.h"
15 #include "chrome/browser/favicon/favicon_tab_helper.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/ash/multi_user/multi_user_context_menu.h"
18 #include "chrome/browser/ui/host_desktop.h"
19 #include "chrome/browser/ui/views/extensions/extension_keybinding_registry_views.h"
20 #include "chrome/browser/web_applications/web_app.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "content/public/browser/browser_thread.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/render_widget_host_view.h"
25 #include "content/public/browser/web_contents.h"
26 #include "content/public/browser/web_contents_view.h"
27 #include "extensions/common/draggable_region.h"
28 #include "extensions/common/extension.h"
29 #include "ui/base/hit_test.h"
30 #include "ui/base/models/simple_menu_model.h"
31 #include "ui/views/controls/menu/menu_runner.h"
32 #include "ui/views/controls/webview/webview.h"
33 #include "ui/views/widget/widget.h"
34 #include "ui/views/window/non_client_view.h"
36 #if defined(OS_WIN)
37 #include "base/strings/utf_string_conversions.h"
38 #include "chrome/browser/ui/web_applications/web_app_ui.h"
39 #include "chrome/browser/web_applications/web_app_win.h"
40 #include "ui/base/win/shell.h"
41 #include "ui/views/win/hwnd_util.h"
42 #endif
44 #if defined(OS_LINUX)
45 #include "chrome/browser/shell_integration_linux.h"
46 #endif
48 #if defined(USE_ASH)
49 #include "ash/ash_constants.h"
50 #include "ash/ash_switches.h"
51 #include "ash/screen_util.h"
52 #include "ash/shell.h"
53 #include "ash/wm/custom_frame_view_ash.h"
54 #include "ash/wm/immersive_fullscreen_controller.h"
55 #include "ash/wm/panels/panel_frame_view.h"
56 #include "ash/wm/window_state.h"
57 #include "ash/wm/window_state_delegate.h"
58 #include "ash/wm/window_state_observer.h"
59 #include "chrome/browser/ui/ash/ash_util.h"
60 #include "ui/aura/client/aura_constants.h"
61 #include "ui/aura/client/window_tree_client.h"
62 #include "ui/aura/window.h"
63 #include "ui/aura/window_observer.h"
64 #endif
66 #if defined(USE_AURA)
67 #include "ui/aura/window.h"
68 #include "ui/wm/public/masked_window_targeter.h"
69 #endif
71 using apps::ShellWindow;
73 namespace {
75 const int kMinPanelWidth = 100;
76 const int kMinPanelHeight = 100;
77 const int kDefaultPanelWidth = 200;
78 const int kDefaultPanelHeight = 300;
79 const int kResizeInsideBoundsSize = 5;
80 const int kResizeAreaCornerSize = 16;
82 struct AcceleratorMapping {
83 ui::KeyboardCode keycode;
84 int modifiers;
85 int command_id;
87 const AcceleratorMapping kAppWindowAcceleratorMap[] = {
88 { ui::VKEY_W, ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
89 { ui::VKEY_W, ui::EF_SHIFT_DOWN | ui::EF_CONTROL_DOWN, IDC_CLOSE_WINDOW },
90 { ui::VKEY_F4, ui::EF_ALT_DOWN, IDC_CLOSE_WINDOW },
93 const std::map<ui::Accelerator, int>& GetAcceleratorTable() {
94 typedef std::map<ui::Accelerator, int> AcceleratorMap;
95 CR_DEFINE_STATIC_LOCAL(AcceleratorMap, accelerators, ());
96 if (accelerators.empty()) {
97 for (size_t i = 0; i < arraysize(kAppWindowAcceleratorMap); ++i) {
98 ui::Accelerator accelerator(kAppWindowAcceleratorMap[i].keycode,
99 kAppWindowAcceleratorMap[i].modifiers);
100 accelerators[accelerator] = kAppWindowAcceleratorMap[i].command_id;
103 return accelerators;
106 #if defined(OS_WIN)
107 void CreateIconAndSetRelaunchDetails(
108 const base::FilePath web_app_path,
109 const base::FilePath icon_file,
110 const ShellIntegration::ShortcutInfo& shortcut_info,
111 const HWND hwnd) {
112 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread());
114 // Set the relaunch data so "Pin this program to taskbar" has the app's
115 // information.
116 CommandLine command_line = ShellIntegration::CommandLineArgsForLauncher(
117 shortcut_info.url,
118 shortcut_info.extension_id,
119 shortcut_info.profile_path);
121 base::FilePath chrome_exe;
122 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
123 NOTREACHED();
124 return;
126 command_line.SetProgram(chrome_exe);
127 ui::win::SetRelaunchDetailsForWindow(command_line.GetCommandLineString(),
128 shortcut_info.title, hwnd);
130 if (!base::PathExists(web_app_path) &&
131 !base::CreateDirectory(web_app_path))
132 return;
134 ui::win::SetAppIconForWindow(icon_file.value(), hwnd);
135 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon);
137 #endif
139 #if defined(USE_ASH)
140 // This class handles a user's fullscreen request (Shift+F4/F4).
141 class NativeAppWindowStateDelegate : public ash::wm::WindowStateDelegate,
142 public ash::wm::WindowStateObserver,
143 public aura::WindowObserver {
144 public:
145 NativeAppWindowStateDelegate(ShellWindow* shell_window,
146 apps::NativeAppWindow* native_app_window)
147 : shell_window_(shell_window),
148 window_state_(
149 ash::wm::GetWindowState(native_app_window->GetNativeWindow())) {
150 // Add a window state observer to exit fullscreen properly in case
151 // fullscreen is exited without going through ShellWindow::Restore(). This
152 // is the case when exiting immersive fullscreen via the "Restore" window
153 // control.
154 // TODO(pkotwicz): This is a hack. Remove ASAP. http://crbug.com/319048
155 window_state_->AddObserver(this);
156 window_state_->window()->AddObserver(this);
158 virtual ~NativeAppWindowStateDelegate(){
159 if (window_state_) {
160 window_state_->RemoveObserver(this);
161 window_state_->window()->RemoveObserver(this);
165 private:
166 // Overridden from ash::wm::WindowStateDelegate.
167 virtual bool ToggleFullscreen(ash::wm::WindowState* window_state) OVERRIDE {
168 // Windows which cannot be maximized should not be fullscreened.
169 DCHECK(window_state->IsFullscreen() || window_state->CanMaximize());
170 if (window_state->IsFullscreen())
171 shell_window_->Restore();
172 else if (window_state->CanMaximize())
173 shell_window_->OSFullscreen();
174 return true;
177 // Overridden from ash::wm::WindowStateObserver:
178 virtual void OnWindowShowTypeChanged(
179 ash::wm::WindowState* window_state,
180 ash::wm::WindowShowType old_type) OVERRIDE {
181 if (!window_state->IsFullscreen() &&
182 !window_state->IsMinimized() &&
183 shell_window_->GetBaseWindow()->IsFullscreenOrPending()) {
184 shell_window_->Restore();
185 // Usually OnNativeWindowChanged() is called when the window bounds are
186 // changed as a result of a show type change. Because the change in show
187 // type has already occurred, we need to call OnNativeWindowChanged()
188 // explicitly.
189 shell_window_->OnNativeWindowChanged();
193 // Overridden from aura::WindowObserver:
194 virtual void OnWindowDestroying(aura::Window* window) OVERRIDE {
195 window_state_->RemoveObserver(this);
196 window_state_->window()->RemoveObserver(this);
197 window_state_ = NULL;
200 // Not owned.
201 ShellWindow* shell_window_;
202 ash::wm::WindowState* window_state_;
204 DISALLOW_COPY_AND_ASSIGN(NativeAppWindowStateDelegate);
206 #endif // USE_ASH
208 class ShapedNativeAppWindowTargeter : public wm::MaskedWindowTargeter {
209 public:
210 ShapedNativeAppWindowTargeter(aura::Window* window,
211 NativeAppWindowViews* app_window)
212 : wm::MaskedWindowTargeter(window),
213 app_window_(app_window) {
216 virtual ~ShapedNativeAppWindowTargeter() {}
218 private:
219 // wm::MaskedWindowTargeter:
220 virtual bool GetHitTestMask(aura::Window* window,
221 gfx::Path* mask) const OVERRIDE {
222 SkRegion* shape = app_window_->shape();
223 return shape ? shape->getBoundaryPath(mask) : false;
226 NativeAppWindowViews* app_window_;
228 DISALLOW_COPY_AND_ASSIGN(ShapedNativeAppWindowTargeter);
231 } // namespace
233 NativeAppWindowViews::NativeAppWindowViews()
234 : web_view_(NULL),
235 window_(NULL),
236 is_fullscreen_(false),
237 weak_ptr_factory_(this) {
240 void NativeAppWindowViews::Init(
241 apps::ShellWindow* shell_window,
242 const ShellWindow::CreateParams& create_params) {
243 shell_window_ = shell_window;
244 frameless_ = create_params.frame == ShellWindow::FRAME_NONE;
245 transparent_background_ = create_params.transparent_background;
246 resizable_ = create_params.resizable;
247 Observe(web_contents());
249 window_ = new views::Widget;
250 if (create_params.window_type == ShellWindow::WINDOW_TYPE_PANEL ||
251 create_params.window_type == ShellWindow::WINDOW_TYPE_V1_PANEL) {
252 InitializePanelWindow(create_params);
253 } else {
254 InitializeDefaultWindow(create_params);
256 extension_keybinding_registry_.reset(
257 new ExtensionKeybindingRegistryViews(
258 profile(),
259 window_->GetFocusManager(),
260 extensions::ExtensionKeybindingRegistry::PLATFORM_APPS_ONLY,
261 shell_window_));
263 OnViewWasResized();
264 window_->AddObserver(this);
267 NativeAppWindowViews::~NativeAppWindowViews() {
268 web_view_->SetWebContents(NULL);
271 void NativeAppWindowViews::OnBeforeWidgetInit(
272 views::Widget::InitParams* init_params,
273 views::Widget* widget) {}
275 void NativeAppWindowViews::InitializeDefaultWindow(
276 const ShellWindow::CreateParams& create_params) {
277 std::string app_name =
278 web_app::GenerateApplicationNameFromExtensionId(extension()->id());
280 views::Widget::InitParams init_params(views::Widget::InitParams::TYPE_WINDOW);
281 init_params.delegate = this;
282 init_params.remove_standard_frame = ShouldUseChromeStyleFrame();
283 init_params.use_system_default_icon = true;
284 // TODO(erg): Conceptually, these are toplevel windows, but we theoretically
285 // could plumb context through to here in some cases.
286 init_params.top_level = true;
287 if (create_params.transparent_background)
288 init_params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
289 init_params.keep_on_top = create_params.always_on_top;
290 gfx::Rect window_bounds = create_params.bounds;
291 bool position_specified =
292 window_bounds.x() != INT_MIN && window_bounds.y() != INT_MIN;
293 if (position_specified && !window_bounds.IsEmpty())
294 init_params.bounds = window_bounds;
296 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
297 // Set up a custom WM_CLASS for app windows. This allows task switchers in
298 // X11 environments to distinguish them from main browser windows.
299 init_params.wm_class_name = web_app::GetWMClassFromAppName(app_name);
300 init_params.wm_class_class = ShellIntegrationLinux::GetProgramClassName();
301 const char kX11WindowRoleApp[] = "app";
302 init_params.wm_role_name = std::string(kX11WindowRoleApp);
303 #endif
305 OnBeforeWidgetInit(&init_params, window_);
306 window_->Init(init_params);
308 gfx::Rect adjusted_bounds = window_bounds;
309 adjusted_bounds.Inset(-GetFrameInsets());
310 // Center window if no position was specified.
311 if (!position_specified)
312 window_->CenterWindow(adjusted_bounds.size());
313 else if (!adjusted_bounds.IsEmpty() && adjusted_bounds != window_bounds)
314 window_->SetBounds(adjusted_bounds);
316 // Register accelarators supported by app windows.
317 // TODO(jeremya/stevenjb): should these be registered for panels too?
318 views::FocusManager* focus_manager = GetFocusManager();
319 const std::map<ui::Accelerator, int>& accelerator_table =
320 GetAcceleratorTable();
321 for (std::map<ui::Accelerator, int>::const_iterator iter =
322 accelerator_table.begin();
323 iter != accelerator_table.end(); ++iter) {
324 focus_manager->RegisterAccelerator(
325 iter->first, ui::AcceleratorManager::kNormalPriority, this);
328 #if defined(OS_WIN)
329 base::string16 app_name_wide = base::UTF8ToWide(app_name);
330 HWND hwnd = GetNativeAppWindowHWND();
331 ui::win::SetAppIdForWindow(ShellIntegration::GetAppModelIdForProfile(
332 app_name_wide, profile()->GetPath()), hwnd);
334 web_app::UpdateShortcutInfoAndIconForApp(
335 *extension(), profile(),
336 base::Bind(&NativeAppWindowViews::OnShortcutInfoLoaded,
337 weak_ptr_factory_.GetWeakPtr()));
338 #endif
341 #if defined(OS_WIN)
342 void NativeAppWindowViews::OnShortcutInfoLoaded(
343 const ShellIntegration::ShortcutInfo& shortcut_info) {
344 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
346 HWND hwnd = GetNativeAppWindowHWND();
348 // Set window's icon to the one we're about to create/update in the web app
349 // path. The icon cache will refresh on icon creation.
350 base::FilePath web_app_path = web_app::GetWebAppDataDirectory(
351 shortcut_info.profile_path, shortcut_info.extension_id,
352 shortcut_info.url);
353 base::FilePath icon_file = web_app_path
354 .Append(web_app::internals::GetSanitizedFileName(shortcut_info.title))
355 .ReplaceExtension(FILE_PATH_LITERAL(".ico"));
357 content::BrowserThread::PostBlockingPoolTask(
358 FROM_HERE,
359 base::Bind(&CreateIconAndSetRelaunchDetails,
360 web_app_path, icon_file, shortcut_info, hwnd));
363 HWND NativeAppWindowViews::GetNativeAppWindowHWND() const {
364 return views::HWNDForWidget(window_->GetTopLevelWidget());
366 #endif
368 void NativeAppWindowViews::InitializePanelWindow(
369 const ShellWindow::CreateParams& create_params) {
370 views::Widget::InitParams params(views::Widget::InitParams::TYPE_PANEL);
371 params.delegate = this;
373 preferred_size_ = gfx::Size(create_params.bounds.width(),
374 create_params.bounds.height());
375 if (preferred_size_.width() == 0)
376 preferred_size_.set_width(kDefaultPanelWidth);
377 else if (preferred_size_.width() < kMinPanelWidth)
378 preferred_size_.set_width(kMinPanelWidth);
380 if (preferred_size_.height() == 0)
381 preferred_size_.set_height(kDefaultPanelHeight);
382 else if (preferred_size_.height() < kMinPanelHeight)
383 preferred_size_.set_height(kMinPanelHeight);
384 #if defined(USE_ASH)
385 if (ash::Shell::HasInstance()) {
386 // Open a new panel on the target root.
387 aura::Window* target = ash::Shell::GetTargetRootWindow();
388 params.bounds = ash::ScreenUtil::ConvertRectToScreen(
389 target, gfx::Rect(preferred_size_));
390 } else {
391 params.bounds = gfx::Rect(preferred_size_);
393 #else
394 params.bounds = gfx::Rect(preferred_size_);
395 #endif
396 // TODO(erg): Conceptually, these are toplevel windows, but we theoretically
397 // could plumb context through to here in some cases.
398 params.top_level = true;
399 window_->Init(params);
400 window_->set_focus_on_creation(create_params.focused);
402 #if defined(USE_ASH)
403 if (create_params.state == ui::SHOW_STATE_DETACHED) {
404 gfx::Rect window_bounds(create_params.bounds.x(),
405 create_params.bounds.y(),
406 preferred_size_.width(),
407 preferred_size_.height());
408 aura::Window* native_window = GetNativeWindow();
409 ash::wm::GetWindowState(native_window)->set_panel_attached(false);
410 aura::client::ParentWindowWithContext(native_window,
411 native_window->GetRootWindow(),
412 native_window->GetBoundsInScreen());
413 window_->SetBounds(window_bounds);
415 #else
416 // TODO(stevenjb): NativeAppWindow panels need to be implemented for other
417 // platforms.
418 #endif
421 bool NativeAppWindowViews::ShouldUseChromeStyleFrame() const {
422 if (frameless_)
423 return true;
425 #if defined(OS_LINUX) && !defined(OS_CHROMEOS)
426 // Linux always uses native style frames.
427 return false;
428 #endif
430 return !CommandLine::ForCurrentProcess()->HasSwitch(
431 switches::kAppsUseNativeFrame);
434 apps::ShellWindowFrameView* NativeAppWindowViews::CreateShellWindowFrameView() {
435 // By default the user can resize the window from slightly inside the bounds.
436 int resize_inside_bounds_size = kResizeInsideBoundsSize;
437 int resize_outside_bounds_size = 0;
438 int resize_outside_scale_for_touch = 1;
439 int resize_area_corner_size = kResizeAreaCornerSize;
440 #if defined(USE_ASH)
441 // For Aura windows on the Ash desktop the sizes are different and the user
442 // can resize the window from slightly outside the bounds as well.
443 if (chrome::IsNativeWindowInAsh(window_->GetNativeWindow())) {
444 resize_inside_bounds_size = ash::kResizeInsideBoundsSize;
445 resize_outside_bounds_size = ash::kResizeOutsideBoundsSize;
446 resize_outside_scale_for_touch = ash::kResizeOutsideBoundsScaleForTouch;
447 resize_area_corner_size = ash::kResizeAreaCornerSize;
449 #endif
450 apps::ShellWindowFrameView* frame_view = new apps::ShellWindowFrameView(this);
451 frame_view->Init(window_,
452 resize_inside_bounds_size,
453 resize_outside_bounds_size,
454 resize_outside_scale_for_touch,
455 resize_area_corner_size);
456 return frame_view;
459 // ui::BaseWindow implementation.
461 bool NativeAppWindowViews::IsActive() const {
462 return window_->IsActive();
465 bool NativeAppWindowViews::IsMaximized() const {
466 return window_->IsMaximized();
469 bool NativeAppWindowViews::IsMinimized() const {
470 return window_->IsMinimized();
473 bool NativeAppWindowViews::IsFullscreen() const {
474 return window_->IsFullscreen();
477 gfx::NativeWindow NativeAppWindowViews::GetNativeWindow() {
478 return window_->GetNativeWindow();
481 gfx::Rect NativeAppWindowViews::GetRestoredBounds() const {
482 return window_->GetRestoredBounds();
485 ui::WindowShowState NativeAppWindowViews::GetRestoredState() const {
486 if (IsMaximized())
487 return ui::SHOW_STATE_MAXIMIZED;
488 if (IsFullscreen()) {
489 #if defined(USE_ASH)
490 if (immersive_fullscreen_controller_.get() &&
491 immersive_fullscreen_controller_->IsEnabled()) {
492 // Restore windows which were previously in immersive fullscreen to
493 // maximized. Restoring the window to a different fullscreen type
494 // makes for a bad experience.
495 return ui::SHOW_STATE_MAXIMIZED;
497 #endif
498 return ui::SHOW_STATE_FULLSCREEN;
500 #if defined(USE_ASH)
501 // Use kRestoreShowStateKey in case a window is minimized/hidden.
502 ui::WindowShowState restore_state =
503 window_->GetNativeWindow()->GetProperty(
504 aura::client::kRestoreShowStateKey);
505 // Whitelist states to return so that invalid and transient states
506 // are not saved and used to restore windows when they are recreated.
507 switch (restore_state) {
508 case ui::SHOW_STATE_NORMAL:
509 case ui::SHOW_STATE_MAXIMIZED:
510 case ui::SHOW_STATE_FULLSCREEN:
511 case ui::SHOW_STATE_DETACHED:
512 return restore_state;
514 case ui::SHOW_STATE_DEFAULT:
515 case ui::SHOW_STATE_MINIMIZED:
516 case ui::SHOW_STATE_INACTIVE:
517 case ui::SHOW_STATE_END:
518 return ui::SHOW_STATE_NORMAL;
520 #endif
521 return ui::SHOW_STATE_NORMAL;
524 gfx::Rect NativeAppWindowViews::GetBounds() const {
525 return window_->GetWindowBoundsInScreen();
528 void NativeAppWindowViews::Show() {
529 if (window_->IsVisible()) {
530 window_->Activate();
531 return;
534 window_->Show();
537 void NativeAppWindowViews::ShowInactive() {
538 if (window_->IsVisible())
539 return;
540 window_->ShowInactive();
543 void NativeAppWindowViews::Hide() {
544 window_->Hide();
547 void NativeAppWindowViews::Close() {
548 window_->Close();
551 void NativeAppWindowViews::Activate() {
552 window_->Activate();
555 void NativeAppWindowViews::Deactivate() {
556 window_->Deactivate();
559 void NativeAppWindowViews::Maximize() {
560 window_->Maximize();
563 void NativeAppWindowViews::Minimize() {
564 window_->Minimize();
567 void NativeAppWindowViews::Restore() {
568 window_->Restore();
571 void NativeAppWindowViews::SetBounds(const gfx::Rect& bounds) {
572 window_->SetBounds(bounds);
575 void NativeAppWindowViews::FlashFrame(bool flash) {
576 window_->FlashFrame(flash);
579 bool NativeAppWindowViews::IsAlwaysOnTop() const {
580 if (shell_window_->window_type_is_panel()) {
581 #if defined(USE_ASH)
582 return ash::wm::GetWindowState(window_->GetNativeWindow())->
583 panel_attached();
584 #else
585 return true;
586 #endif
587 } else {
588 return window_->IsAlwaysOnTop();
592 void NativeAppWindowViews::SetAlwaysOnTop(bool always_on_top) {
593 window_->SetAlwaysOnTop(always_on_top);
596 void NativeAppWindowViews::ShowContextMenuForView(
597 views::View* source,
598 const gfx::Point& p,
599 ui::MenuSourceType source_type) {
600 #if defined(USE_ASH)
601 scoped_ptr<ui::MenuModel> model = CreateMultiUserContextMenu(
602 shell_window_->GetNativeWindow());
603 if (!model.get())
604 return;
606 // Only show context menu if point is in caption.
607 gfx::Point point_in_view_coords(p);
608 views::View::ConvertPointFromScreen(window_->non_client_view(),
609 &point_in_view_coords);
610 int hit_test = window_->non_client_view()->NonClientHitTest(
611 point_in_view_coords);
612 if (hit_test == HTCAPTION) {
613 menu_runner_.reset(new views::MenuRunner(model.get()));
614 if (menu_runner_->RunMenuAt(source->GetWidget(), NULL,
615 gfx::Rect(p, gfx::Size(0,0)), views::MenuItemView::TOPLEFT,
616 source_type,
617 views::MenuRunner::HAS_MNEMONICS | views::MenuRunner::CONTEXT_MENU) ==
618 views::MenuRunner::MENU_DELETED)
619 return;
621 #endif
624 gfx::NativeView NativeAppWindowViews::GetHostView() const {
625 return window_->GetNativeView();
628 gfx::Point NativeAppWindowViews::GetDialogPosition(const gfx::Size& size) {
629 gfx::Size shell_window_size = window_->GetWindowBoundsInScreen().size();
630 return gfx::Point(shell_window_size.width() / 2 - size.width() / 2,
631 shell_window_size.height() / 2 - size.height() / 2);
634 gfx::Size NativeAppWindowViews::GetMaximumDialogSize() {
635 return window_->GetWindowBoundsInScreen().size();
638 void NativeAppWindowViews::AddObserver(
639 web_modal::ModalDialogHostObserver* observer) {
640 observer_list_.AddObserver(observer);
642 void NativeAppWindowViews::RemoveObserver(
643 web_modal::ModalDialogHostObserver* observer) {
644 observer_list_.RemoveObserver(observer);
647 // Private method. TODO(stevenjb): Move this below InitializePanelWindow()
648 // to match declaration order.
649 void NativeAppWindowViews::OnViewWasResized() {
650 FOR_EACH_OBSERVER(web_modal::ModalDialogHostObserver,
651 observer_list_,
652 OnPositionRequiresUpdate());
655 // WidgetDelegate implementation.
657 void NativeAppWindowViews::OnWidgetMove() {
658 shell_window_->OnNativeWindowChanged();
661 views::View* NativeAppWindowViews::GetInitiallyFocusedView() {
662 return web_view_;
665 bool NativeAppWindowViews::CanResize() const {
666 return resizable_ && !shell_window_->size_constraints().HasFixedSize();
669 bool NativeAppWindowViews::CanMaximize() const {
670 return resizable_ && !shell_window_->size_constraints().HasMaximumSize() &&
671 !shell_window_->window_type_is_panel();
674 base::string16 NativeAppWindowViews::GetWindowTitle() const {
675 return shell_window_->GetTitle();
678 bool NativeAppWindowViews::ShouldShowWindowTitle() const {
679 return shell_window_->window_type() == ShellWindow::WINDOW_TYPE_V1_PANEL;
682 gfx::ImageSkia NativeAppWindowViews::GetWindowAppIcon() {
683 gfx::Image app_icon = shell_window_->app_icon();
684 if (app_icon.IsEmpty())
685 return GetWindowIcon();
686 else
687 return *app_icon.ToImageSkia();
690 gfx::ImageSkia NativeAppWindowViews::GetWindowIcon() {
691 content::WebContents* web_contents = shell_window_->web_contents();
692 if (web_contents) {
693 FaviconTabHelper* favicon_tab_helper =
694 FaviconTabHelper::FromWebContents(web_contents);
695 gfx::Image app_icon = favicon_tab_helper->GetFavicon();
696 if (!app_icon.IsEmpty())
697 return *app_icon.ToImageSkia();
699 return gfx::ImageSkia();
702 bool NativeAppWindowViews::ShouldShowWindowIcon() const {
703 return shell_window_->window_type() == ShellWindow::WINDOW_TYPE_V1_PANEL;
706 void NativeAppWindowViews::SaveWindowPlacement(const gfx::Rect& bounds,
707 ui::WindowShowState show_state) {
708 views::WidgetDelegate::SaveWindowPlacement(bounds, show_state);
709 shell_window_->OnNativeWindowChanged();
712 void NativeAppWindowViews::DeleteDelegate() {
713 window_->RemoveObserver(this);
714 shell_window_->OnNativeClose();
717 views::Widget* NativeAppWindowViews::GetWidget() {
718 return window_;
721 const views::Widget* NativeAppWindowViews::GetWidget() const {
722 return window_;
725 views::View* NativeAppWindowViews::GetContentsView() {
726 return this;
729 views::NonClientFrameView* NativeAppWindowViews::CreateNonClientFrameView(
730 views::Widget* widget) {
731 #if defined(USE_ASH)
732 if (chrome::IsNativeViewInAsh(widget->GetNativeView())) {
733 // Set the delegate now because CustomFrameViewAsh sets the
734 // WindowStateDelegate if one is not already set.
735 ash::wm::GetWindowState(GetNativeWindow())->SetDelegate(
736 scoped_ptr<ash::wm::WindowStateDelegate>(
737 new NativeAppWindowStateDelegate(shell_window_, this)).Pass());
739 if (shell_window_->window_type_is_panel()) {
740 ash::PanelFrameView::FrameType frame_type = frameless_ ?
741 ash::PanelFrameView::FRAME_NONE : ash::PanelFrameView::FRAME_ASH;
742 views::NonClientFrameView* frame_view =
743 new ash::PanelFrameView(widget, frame_type);
744 frame_view->set_context_menu_controller(this);
745 return frame_view;
748 if (!frameless_) {
749 ash::CustomFrameViewAsh* custom_frame_view =
750 new ash::CustomFrameViewAsh(widget);
751 #if defined(OS_CHROMEOS)
752 // Non-frameless app windows can be put into immersive fullscreen.
753 // TODO(pkotwicz): Investigate if immersive fullscreen can be enabled for
754 // Windows Ash.
755 if (ash::switches::UseImmersiveFullscreenForAllWindows()) {
756 immersive_fullscreen_controller_.reset(
757 new ash::ImmersiveFullscreenController());
758 custom_frame_view->InitImmersiveFullscreenControllerForView(
759 immersive_fullscreen_controller_.get());
761 #endif
762 custom_frame_view->GetHeaderView()->set_context_menu_controller(this);
763 return custom_frame_view;
766 #endif
767 if (ShouldUseChromeStyleFrame())
768 return CreateShellWindowFrameView();
769 return views::WidgetDelegateView::CreateNonClientFrameView(widget);
772 bool NativeAppWindowViews::WidgetHasHitTestMask() const {
773 return shape_ != NULL;
776 void NativeAppWindowViews::GetWidgetHitTestMask(gfx::Path* mask) const {
777 shape_->getBoundaryPath(mask);
780 bool NativeAppWindowViews::ShouldDescendIntoChildForEventHandling(
781 gfx::NativeView child,
782 const gfx::Point& location) {
783 #if defined(USE_AURA)
784 if (child->Contains(web_view_->web_contents()->GetView()->GetNativeView())) {
785 // Shell window should claim mouse events that fall within the draggable
786 // region.
787 return !draggable_region_.get() ||
788 !draggable_region_->contains(location.x(), location.y());
790 #endif
792 return true;
795 // WidgetObserver implementation.
797 void NativeAppWindowViews::OnWidgetVisibilityChanged(views::Widget* widget,
798 bool visible) {
799 shell_window_->OnNativeWindowChanged();
802 void NativeAppWindowViews::OnWidgetActivationChanged(views::Widget* widget,
803 bool active) {
804 shell_window_->OnNativeWindowChanged();
805 if (active)
806 shell_window_->OnNativeWindowActivated();
809 // WebContentsObserver implementation.
811 void NativeAppWindowViews::RenderViewCreated(
812 content::RenderViewHost* render_view_host) {
813 if (transparent_background_) {
814 // Use a background with transparency to trigger transparency in Webkit.
815 SkBitmap background;
816 background.setConfig(SkBitmap::kARGB_8888_Config, 1, 1);
817 background.allocPixels();
818 background.eraseARGB(0x00, 0x00, 0x00, 0x00);
820 content::RenderWidgetHostView* view = render_view_host->GetView();
821 DCHECK(view);
822 view->SetBackground(background);
826 void NativeAppWindowViews::RenderViewHostChanged(
827 content::RenderViewHost* old_host,
828 content::RenderViewHost* new_host) {
829 OnViewWasResized();
832 // views::View implementation.
834 void NativeAppWindowViews::Layout() {
835 DCHECK(web_view_);
836 web_view_->SetBounds(0, 0, width(), height());
837 OnViewWasResized();
840 void NativeAppWindowViews::ViewHierarchyChanged(
841 const ViewHierarchyChangedDetails& details) {
842 if (details.is_add && details.child == this) {
843 web_view_ = new views::WebView(NULL);
844 AddChildView(web_view_);
845 web_view_->SetWebContents(web_contents());
849 gfx::Size NativeAppWindowViews::GetPreferredSize() {
850 if (!preferred_size_.IsEmpty())
851 return preferred_size_;
852 return views::View::GetPreferredSize();
855 gfx::Size NativeAppWindowViews::GetMinimumSize() {
856 return shell_window_->size_constraints().GetMinimumSize();
859 gfx::Size NativeAppWindowViews::GetMaximumSize() {
860 return shell_window_->size_constraints().GetMaximumSize();
863 void NativeAppWindowViews::OnFocus() {
864 web_view_->RequestFocus();
867 bool NativeAppWindowViews::AcceleratorPressed(
868 const ui::Accelerator& accelerator) {
869 const std::map<ui::Accelerator, int>& accelerator_table =
870 GetAcceleratorTable();
871 std::map<ui::Accelerator, int>::const_iterator iter =
872 accelerator_table.find(accelerator);
873 DCHECK(iter != accelerator_table.end());
874 int command_id = iter->second;
875 switch (command_id) {
876 case IDC_CLOSE_WINDOW:
877 Close();
878 return true;
879 default:
880 NOTREACHED() << "Unknown accelerator sent to app window.";
882 return false;
885 // NativeAppWindow implementation.
887 void NativeAppWindowViews::SetFullscreen(int fullscreen_types) {
888 // Fullscreen not supported by panels.
889 if (shell_window_->window_type_is_panel())
890 return;
891 is_fullscreen_ = (fullscreen_types != ShellWindow::FULLSCREEN_TYPE_NONE);
892 window_->SetFullscreen(is_fullscreen_);
894 #if defined(USE_ASH)
895 if (immersive_fullscreen_controller_.get()) {
896 // |immersive_fullscreen_controller_| should only be set if immersive
897 // fullscreen is the fullscreen type used by the OS.
898 immersive_fullscreen_controller_->SetEnabled(
899 ash::ImmersiveFullscreenController::WINDOW_TYPE_PACKAGED_APP,
900 (fullscreen_types & ShellWindow::FULLSCREEN_TYPE_OS) != 0);
901 // Autohide the shelf instead of hiding the shelf completely when only in
902 // OS fullscreen.
903 ash::wm::WindowState* window_state =
904 ash::wm::GetWindowState(window_->GetNativeWindow());
905 window_state->set_hide_shelf_when_fullscreen(
906 fullscreen_types != ShellWindow::FULLSCREEN_TYPE_OS);
907 DCHECK(ash::Shell::HasInstance());
908 ash::Shell::GetInstance()->UpdateShelfVisibility();
910 #endif
912 // TODO(jeremya) we need to call RenderViewHost::ExitFullscreen() if we
913 // ever drop the window out of fullscreen in response to something that
914 // wasn't the app calling webkitCancelFullScreen().
917 bool NativeAppWindowViews::IsFullscreenOrPending() const {
918 return is_fullscreen_;
921 bool NativeAppWindowViews::IsDetached() const {
922 if (!shell_window_->window_type_is_panel())
923 return false;
924 #if defined(USE_ASH)
925 return !ash::wm::GetWindowState(window_->GetNativeWindow())->panel_attached();
926 #else
927 return false;
928 #endif
931 void NativeAppWindowViews::UpdateWindowIcon() {
932 window_->UpdateWindowIcon();
935 void NativeAppWindowViews::UpdateWindowTitle() {
936 window_->UpdateWindowTitle();
939 void NativeAppWindowViews::UpdateDraggableRegions(
940 const std::vector<extensions::DraggableRegion>& regions) {
941 // Draggable region is not supported for non-frameless window.
942 if (!frameless_)
943 return;
945 draggable_region_.reset(ShellWindow::RawDraggableRegionsToSkRegion(regions));
946 OnViewWasResized();
949 SkRegion* NativeAppWindowViews::GetDraggableRegion() {
950 return draggable_region_.get();
953 void NativeAppWindowViews::UpdateShape(scoped_ptr<SkRegion> region) {
954 shape_ = region.Pass();
956 #if defined(USE_AURA)
957 if (shape_)
958 window_->SetShape(new SkRegion(*shape_));
959 else
960 window_->SetShape(NULL);
961 #endif // defined(USE_AURA)
964 void NativeAppWindowViews::HandleKeyboardEvent(
965 const content::NativeWebKeyboardEvent& event) {
966 unhandled_keyboard_event_handler_.HandleKeyboardEvent(event,
967 GetFocusManager());
970 bool NativeAppWindowViews::IsFrameless() const {
971 return frameless_;
974 gfx::Insets NativeAppWindowViews::GetFrameInsets() const {
975 if (frameless_)
976 return gfx::Insets();
978 // The pretend client_bounds passed in need to be large enough to ensure that
979 // GetWindowBoundsForClientBounds() doesn't decide that it needs more than
980 // the specified amount of space to fit the window controls in, and return a
981 // number larger than the real frame insets. Most window controls are smaller
982 // than 1000x1000px, so this should be big enough.
983 gfx::Rect client_bounds = gfx::Rect(1000, 1000);
984 gfx::Rect window_bounds =
985 window_->non_client_view()->GetWindowBoundsForClientBounds(
986 client_bounds);
987 return window_bounds.InsetsFrom(client_bounds);
990 void NativeAppWindowViews::HideWithApp() {}
991 void NativeAppWindowViews::ShowWithApp() {}
992 void NativeAppWindowViews::UpdateWindowMinMaxSize() {}