| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <sstream> | 5 #include <sstream> |
| 6 | 6 |
| 7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
| 8 #include "base/file_util.h" |
| 8 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 9 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| 10 #include "base/time.h" | 11 #include "base/time.h" |
| 11 #include "base/timer.h" | 12 #include "base/timer.h" |
| 12 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
| 14 #include "base/win/shortcut.h" |
| 13 #include "chrome/app/chrome_dll_resource.h" | 15 #include "chrome/app/chrome_dll_resource.h" |
| 14 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 15 #include "chrome/browser/extensions/extension_service.h" | 17 #include "chrome/browser/extensions/extension_service.h" |
| 16 #include "chrome/browser/lifetime/application_lifetime.h" | 18 #include "chrome/browser/lifetime/application_lifetime.h" |
| 17 #include "chrome/browser/profiles/profile.h" | 19 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/profiles/profile_manager.h" | 20 #include "chrome/browser/profiles/profile_manager.h" |
| 19 #include "chrome/browser/shell_integration.h" | 21 #include "chrome/browser/shell_integration.h" |
| 20 #include "chrome/browser/ui/app_list/app_list_controller.h" | 22 #include "chrome/browser/ui/app_list/app_list_controller.h" |
| 21 #include "chrome/browser/ui/app_list/app_list_view_delegate.h" | 23 #include "chrome/browser/ui/app_list/app_list_view_delegate.h" |
| 22 #include "chrome/browser/ui/extensions/application_launch.h" | 24 #include "chrome/browser/ui/extensions/application_launch.h" |
| 23 #include "chrome/browser/ui/views/browser_dialogs.h" | 25 #include "chrome/browser/ui/views/browser_dialogs.h" |
| 24 #include "chrome/common/chrome_constants.h" | 26 #include "chrome/common/chrome_constants.h" |
| 25 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/installer/util/util_constants.h" |
| 29 #include "content/public/browser/browser_thread.h" |
| 30 #include "grit/chromium_strings.h" |
| 26 #include "grit/generated_resources.h" | 31 #include "grit/generated_resources.h" |
| 32 #include "grit/google_chrome_strings.h" |
| 27 #include "ui/app_list/app_list_view.h" | 33 #include "ui/app_list/app_list_view.h" |
| 28 #include "ui/app_list/pagination_model.h" | 34 #include "ui/app_list/pagination_model.h" |
| 29 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 30 #include "ui/base/win/shell.h" | 36 #include "ui/base/win/shell.h" |
| 31 #include "ui/gfx/display.h" | 37 #include "ui/gfx/display.h" |
| 32 #include "ui/gfx/screen.h" | 38 #include "ui/gfx/screen.h" |
| 33 #include "ui/views/bubble/bubble_border.h" | 39 #include "ui/views/bubble/bubble_border.h" |
| 34 #include "ui/views/widget/widget.h" | 40 #include "ui/views/widget/widget.h" |
| 35 | 41 |
| 36 namespace { | 42 namespace { |
| 37 | 43 |
| 38 // Offset from the cursor to the point of the bubble arrow. It looks weird | 44 // Offset from the cursor to the point of the bubble arrow. It looks weird |
| 39 // if the arrow comes up right on top of the cursor, so it is offset by this | 45 // if the arrow comes up right on top of the cursor, so it is offset by this |
| 40 // amount. | 46 // amount. |
| 41 static const int kAnchorOffset = 25; | 47 static const int kAnchorOffset = 25; |
| 42 | 48 |
| 49 // Icons are added to the resources of the DLL using icon names. The icon index |
| 50 // for the app list icon is named IDR_X_APP_LIST. Creating shortcuts needs to |
| 51 // specify a resource index, which are different to icon names. They are 0 |
| 52 // based and contiguous. As Google Chrome builds have extra icons the icon for |
| 53 // Google Chrome builds need to be higher. Unfortunately these indexes are not |
| 54 // in any generated header file. |
| 55 #if defined(GOOGLE_CHROME_BUILD) |
| 56 const int kAppListIconIndex = 5; |
| 57 #else |
| 58 const int kAppListIconIndex = 1; |
| 59 #endif |
| 60 |
| 61 CommandLine GetAppListCommandLine() { |
| 62 const char* const kSwitchesToCopy[] = { switches::kUserDataDir }; |
| 63 CommandLine* current = CommandLine::ForCurrentProcess(); |
| 64 CommandLine command_line(current->GetProgram()); |
| 65 command_line.CopySwitchesFrom(*current, kSwitchesToCopy, |
| 66 arraysize(kSwitchesToCopy)); |
| 67 command_line.AppendSwitch(switches::kShowAppList); |
| 68 return command_line; |
| 69 } |
| 70 |
| 71 string16 GetAppModelId() { |
| 72 // The AppModelId should be the same for all profiles in a user data directory |
| 73 // but different for different user data directories, so base it on the |
| 74 // initial profile in the current user data directory. |
| 75 FilePath initial_profile_path; |
| 76 CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 77 if (command_line->HasSwitch(switches::kUserDataDir)) { |
| 78 initial_profile_path = |
| 79 command_line->GetSwitchValuePath(switches::kUserDataDir).AppendASCII( |
| 80 chrome::kInitialProfile); |
| 81 } |
| 82 return ShellIntegration::GetAppListAppModelIdForProfile(initial_profile_path); |
| 83 } |
| 84 |
| 43 class AppListControllerDelegateWin : public AppListControllerDelegate { | 85 class AppListControllerDelegateWin : public AppListControllerDelegate { |
| 44 public: | 86 public: |
| 45 AppListControllerDelegateWin(); | 87 AppListControllerDelegateWin(); |
| 46 virtual ~AppListControllerDelegateWin(); | 88 virtual ~AppListControllerDelegateWin(); |
| 47 | 89 |
| 48 private: | 90 private: |
| 49 // AppListController overrides: | 91 // AppListController overrides: |
| 50 virtual void CloseView() OVERRIDE; | 92 virtual void CloseView() OVERRIDE; |
| 51 virtual void ViewClosing() OVERRIDE; | 93 virtual void ViewClosing() OVERRIDE; |
| 52 virtual void ViewActivationChanged(bool active) OVERRIDE; | 94 virtual void ViewActivationChanged(bool active) OVERRIDE; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 79 | 121 |
| 80 private: | 122 private: |
| 81 // Utility methods for showing the app list. | 123 // Utility methods for showing the app list. |
| 82 void GetArrowLocationAndUpdateAnchor( | 124 void GetArrowLocationAndUpdateAnchor( |
| 83 const gfx::Rect& work_area, | 125 const gfx::Rect& work_area, |
| 84 int min_space_x, | 126 int min_space_x, |
| 85 int min_space_y, | 127 int min_space_y, |
| 86 views::BubbleBorder::ArrowLocation* arrow, | 128 views::BubbleBorder::ArrowLocation* arrow, |
| 87 gfx::Point* anchor); | 129 gfx::Point* anchor); |
| 88 void UpdateArrowPositionAndAnchorPoint(app_list::AppListView* view); | 130 void UpdateArrowPositionAndAnchorPoint(app_list::AppListView* view); |
| 89 CommandLine GetAppListCommandLine(); | |
| 90 string16 GetAppListIconPath(); | 131 string16 GetAppListIconPath(); |
| 91 string16 GetAppModelId(); | |
| 92 | 132 |
| 93 // Check if the app list or the taskbar has focus. The app list is kept | 133 // Check if the app list or the taskbar has focus. The app list is kept |
| 94 // visible whenever either of these have focus, which allows it to be | 134 // visible whenever either of these have focus, which allows it to be |
| 95 // pinned but will hide it if it otherwise loses focus. This is checked | 135 // pinned but will hide it if it otherwise loses focus. This is checked |
| 96 // periodically whenever the app list does not have focus. | 136 // periodically whenever the app list does not have focus. |
| 97 void CheckTaskbarOrViewHasFocus(); | 137 void CheckTaskbarOrViewHasFocus(); |
| 98 | 138 |
| 99 // Weak pointer. The view manages its own lifetime. | 139 // Weak pointer. The view manages its own lifetime. |
| 100 app_list::AppListView* current_view_; | 140 app_list::AppListView* current_view_; |
| 101 | 141 |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 anchor->Offset(kAnchorOffset, 0); | 297 anchor->Offset(kAnchorOffset, 0); |
| 258 return; | 298 return; |
| 259 } | 299 } |
| 260 | 300 |
| 261 *arrow = views::BubbleBorder::RIGHT_TOP; | 301 *arrow = views::BubbleBorder::RIGHT_TOP; |
| 262 anchor->Offset(-kAnchorOffset, 0); | 302 anchor->Offset(-kAnchorOffset, 0); |
| 263 } | 303 } |
| 264 | 304 |
| 265 void AppListController::UpdateArrowPositionAndAnchorPoint( | 305 void AppListController::UpdateArrowPositionAndAnchorPoint( |
| 266 app_list::AppListView* view) { | 306 app_list::AppListView* view) { |
| 267 static const int kArrowSize = 10; | 307 const int kArrowSize = 10; |
| 268 static const int kPadding = 20; | 308 const int kPadding = 20; |
| 269 | 309 |
| 270 gfx::Size preferred = view->GetPreferredSize(); | 310 gfx::Size preferred = view->GetPreferredSize(); |
| 271 // Add the size of the arrow to the space needed, as the preferred size is | 311 // Add the size of the arrow to the space needed, as the preferred size is |
| 272 // of the view excluding the arrow. | 312 // of the view excluding the arrow. |
| 273 int min_space_x = preferred.width() + kAnchorOffset + kPadding + kArrowSize; | 313 int min_space_x = preferred.width() + kAnchorOffset + kPadding + kArrowSize; |
| 274 int min_space_y = preferred.height() + kAnchorOffset + kPadding + kArrowSize; | 314 int min_space_y = preferred.height() + kAnchorOffset + kPadding + kArrowSize; |
| 275 | 315 |
| 276 gfx::Point anchor = view->anchor_point(); | 316 gfx::Point anchor = view->anchor_point(); |
| 277 gfx::Display display = gfx::Screen::GetScreenFor( | 317 gfx::Display display = gfx::Screen::GetScreenFor( |
| 278 view->GetWidget()->GetNativeView())->GetDisplayNearestPoint(anchor); | 318 view->GetWidget()->GetNativeView())->GetDisplayNearestPoint(anchor); |
| 279 const gfx::Rect& display_rect = display.work_area(); | 319 const gfx::Rect& display_rect = display.work_area(); |
| 280 views::BubbleBorder::ArrowLocation arrow; | 320 views::BubbleBorder::ArrowLocation arrow; |
| 281 GetArrowLocationAndUpdateAnchor(display.work_area(), | 321 GetArrowLocationAndUpdateAnchor(display.work_area(), |
| 282 min_space_x, | 322 min_space_x, |
| 283 min_space_y, | 323 min_space_y, |
| 284 &arrow, | 324 &arrow, |
| 285 &anchor); | 325 &anchor); |
| 286 view->SetBubbleArrowLocation(arrow); | 326 view->SetBubbleArrowLocation(arrow); |
| 287 view->SetAnchorPoint(anchor); | 327 view->SetAnchorPoint(anchor); |
| 288 } | 328 } |
| 289 | 329 |
| 290 CommandLine AppListController::GetAppListCommandLine() { | |
| 291 CommandLine* current = CommandLine::ForCurrentProcess(); | |
| 292 CommandLine command_line(current->GetProgram()); | |
| 293 | |
| 294 if (current->HasSwitch(switches::kUserDataDir)) { | |
| 295 FilePath user_data_dir = current->GetSwitchValuePath( | |
| 296 switches::kUserDataDir); | |
| 297 command_line.AppendSwitchPath(switches::kUserDataDir, user_data_dir); | |
| 298 } | |
| 299 | |
| 300 command_line.AppendSwitch(switches::kShowAppList); | |
| 301 return command_line; | |
| 302 } | |
| 303 | |
| 304 string16 AppListController::GetAppListIconPath() { | 330 string16 AppListController::GetAppListIconPath() { |
| 305 FilePath icon_path; | 331 FilePath icon_path; |
| 306 if (!PathService::Get(base::DIR_MODULE, &icon_path)) | 332 if (!PathService::Get(base::FILE_EXE, &icon_path)) { |
| 333 NOTREACHED(); |
| 307 return string16(); | 334 return string16(); |
| 335 } |
| 308 | 336 |
| 309 icon_path = icon_path.Append(chrome::kBrowserResourcesDll); | |
| 310 std::stringstream ss; | 337 std::stringstream ss; |
| 311 ss << ",-" << IDI_APP_LIST; | 338 ss << "," << kAppListIconIndex; |
| 312 string16 result = icon_path.value(); | 339 string16 result = icon_path.value(); |
| 313 result.append(UTF8ToUTF16(ss.str())); | 340 result.append(UTF8ToUTF16(ss.str())); |
| 314 return result; | 341 return result; |
| 315 } | 342 } |
| 316 | 343 |
| 317 string16 AppListController::GetAppModelId() { | |
| 318 static const wchar_t kAppListId[] = L"ChromeAppList"; | |
| 319 // The AppModelId should be the same for all profiles in a user data directory | |
| 320 // but different for different user data directories, so base it on the | |
| 321 // initial profile in the current user data directory. | |
| 322 FilePath initial_profile_path = | |
| 323 g_browser_process->profile_manager()->GetInitialProfileDir(); | |
| 324 return ShellIntegration::GetAppModelIdForProfile(kAppListId, | |
| 325 initial_profile_path); | |
| 326 } | |
| 327 | |
| 328 void AppListController::CheckTaskbarOrViewHasFocus() { | 344 void AppListController::CheckTaskbarOrViewHasFocus() { |
| 329 #if !defined(USE_AURA) | 345 #if !defined(USE_AURA) |
| 330 // Don't bother checking if the view has been closed. | 346 // Don't bother checking if the view has been closed. |
| 331 if (!current_view_) | 347 if (!current_view_) |
| 332 return; | 348 return; |
| 333 | 349 |
| 334 // First get the taskbar and jump lists windows (the jump list is the | 350 // First get the taskbar and jump lists windows (the jump list is the |
| 335 // context menu which the taskbar uses). | 351 // context menu which the taskbar uses). |
| 336 HWND jump_list_hwnd = FindWindow(L"DV2ControlHost", NULL); | 352 HWND jump_list_hwnd = FindWindow(L"DV2ControlHost", NULL); |
| 337 HWND taskbar_hwnd = FindWindow(L"Shell_TrayWnd", NULL); | 353 HWND taskbar_hwnd = FindWindow(L"Shell_TrayWnd", NULL); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 350 } | 366 } |
| 351 focused_hwnd = GetParent(focused_hwnd); | 367 focused_hwnd = GetParent(focused_hwnd); |
| 352 } | 368 } |
| 353 | 369 |
| 354 // If we get here, the focused window is not the taskbar, it's context menu, | 370 // If we get here, the focused window is not the taskbar, it's context menu, |
| 355 // or the app list, so close the app list. | 371 // or the app list, so close the app list. |
| 356 CloseAppList(); | 372 CloseAppList(); |
| 357 #endif | 373 #endif |
| 358 } | 374 } |
| 359 | 375 |
| 376 // Check that a taskbar shortcut exists if it should, or does not exist if |
| 377 // it should not. A taskbar shortcut should exist if the switch |
| 378 // kShowAppListShortcut is set. The shortcut will be created or deleted in |
| 379 // |user_data_dir| and will use a Windows Application Model Id of |
| 380 // |app_model_id|. |
| 381 // This runs on the FILE thread and not in the blocking IO thread pool as there |
| 382 // are other tasks running (also on the FILE thread) which fiddle with shortcut |
| 383 // icons (ShellIntegration::MigrateWin7ShortcutsOnPath). Having different |
| 384 // threads fiddle with the same shortcuts could cause race issues. |
| 385 void CheckAppListTaskbarShortcutOnFileThread(const FilePath& user_data_dir, |
| 386 const string16& app_model_id) { |
| 387 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 388 |
| 389 const string16 shortcut_name = l10n_util::GetStringUTF16( |
| 390 IDS_APP_LIST_SHORTCUT_NAME); |
| 391 const FilePath shortcut_path(user_data_dir.Append(shortcut_name) |
| 392 .AddExtension(installer::kLnkExt)); |
| 393 const bool should_show = CommandLine::ForCurrentProcess()->HasSwitch( |
| 394 switches::kShowAppListShortcut); |
| 395 |
| 396 // This will not reshow a shortcut if it has been unpinned manually by the |
| 397 // user, as that will not delete the shortcut file. |
| 398 if (should_show && !file_util::PathExists(shortcut_path)) { |
| 399 FilePath chrome_exe; |
| 400 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 401 NOTREACHED(); |
| 402 return; |
| 403 } |
| 404 |
| 405 base::win::ShortcutProperties shortcut_properties; |
| 406 shortcut_properties.set_target(chrome_exe); |
| 407 shortcut_properties.set_working_dir(chrome_exe.DirName()); |
| 408 |
| 409 string16 wide_switches(GetAppListCommandLine().GetArgumentsString()); |
| 410 shortcut_properties.set_arguments(wide_switches); |
| 411 shortcut_properties.set_description(shortcut_name); |
| 412 |
| 413 shortcut_properties.set_icon(chrome_exe, kAppListIconIndex); |
| 414 shortcut_properties.set_app_id(app_model_id); |
| 415 |
| 416 base::win::CreateOrUpdateShortcutLink(shortcut_path, shortcut_properties, |
| 417 base::win::SHORTCUT_CREATE_ALWAYS); |
| 418 base::win::TaskbarPinShortcutLink(shortcut_path.value().c_str()); |
| 419 return; |
| 420 } |
| 421 |
| 422 if (!should_show && file_util::PathExists(shortcut_path)) { |
| 423 base::win::TaskbarUnpinShortcutLink(shortcut_path.value().c_str()); |
| 424 file_util::Delete(shortcut_path, false); |
| 425 } |
| 426 } |
| 427 |
| 360 } // namespace | 428 } // namespace |
| 361 | 429 |
| 362 namespace app_list_controller { | 430 namespace app_list_controller { |
| 363 | 431 |
| 364 void ShowAppList() { | 432 void ShowAppList() { |
| 365 g_app_list_controller.Get().ShowAppList(); | 433 g_app_list_controller.Get().ShowAppList(); |
| 366 } | 434 } |
| 367 | 435 |
| 436 void CheckAppListTaskbarShortcut() { |
| 437 FilePath user_data_dir(g_browser_process->profile_manager()->user_data_dir()); |
| 438 content::BrowserThread::PostTask( |
| 439 content::BrowserThread::FILE, FROM_HERE, |
| 440 base::Bind(&CheckAppListTaskbarShortcutOnFileThread, user_data_dir, |
| 441 GetAppModelId())); |
| 442 } |
| 443 |
| 368 } // namespace app_list_controller | 444 } // namespace app_list_controller |
| OLD | NEW |