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 "content/public/browser/browser_thread.h" | |
29 #include "grit/chromium_strings.h" | |
26 #include "grit/generated_resources.h" | 30 #include "grit/generated_resources.h" |
31 #include "grit/google_chrome_strings.h" | |
27 #include "ui/app_list/app_list_view.h" | 32 #include "ui/app_list/app_list_view.h" |
28 #include "ui/app_list/pagination_model.h" | 33 #include "ui/app_list/pagination_model.h" |
29 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
30 #include "ui/base/win/shell.h" | 35 #include "ui/base/win/shell.h" |
31 #include "ui/gfx/display.h" | 36 #include "ui/gfx/display.h" |
32 #include "ui/gfx/screen.h" | 37 #include "ui/gfx/screen.h" |
33 #include "ui/views/bubble/bubble_border.h" | 38 #include "ui/views/bubble/bubble_border.h" |
34 #include "ui/views/widget/widget.h" | 39 #include "ui/views/widget/widget.h" |
35 | 40 |
36 namespace { | 41 namespace { |
37 | 42 |
38 // Offset from the cursor to the point of the bubble arrow. It looks weird | 43 // 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 | 44 // if the arrow comes up right on top of the cursor, so it is offset by this |
40 // amount. | 45 // amount. |
41 static const int kAnchorOffset = 25; | 46 static const int kAnchorOffset = 25; |
42 | 47 |
48 CommandLine GetAppListCommandLine() { | |
49 const char* const kSwitchesToCopy[] = { switches::kUserDataDir }; | |
50 CommandLine* current = CommandLine::ForCurrentProcess(); | |
51 CommandLine command_line(current->GetProgram()); | |
52 command_line.CopySwitchesFrom(*current, kSwitchesToCopy, | |
53 arraysize(kSwitchesToCopy)); | |
54 command_line.AppendSwitch(switches::kShowAppList); | |
55 return command_line; | |
56 } | |
57 | |
58 string16 GetAppModelId() { | |
59 // The AppModelId should be the same for all profiles in a user data directory | |
60 // but different for different user data directories, so base it on the | |
61 // initial profile in the current user data directory. | |
62 FilePath initial_profile_path; | |
63 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
64 if (command_line->HasSwitch(switches::kUserDataDir)) { | |
65 initial_profile_path = | |
66 command_line->GetSwitchValuePath(switches::kUserDataDir).AppendASCII( | |
67 chrome::kInitialProfile); | |
68 } | |
69 return ShellIntegration::GetAppListAppModelIdForProfile(initial_profile_path); | |
70 } | |
71 | |
43 class AppListControllerDelegateWin : public AppListControllerDelegate { | 72 class AppListControllerDelegateWin : public AppListControllerDelegate { |
44 public: | 73 public: |
45 AppListControllerDelegateWin(); | 74 AppListControllerDelegateWin(); |
46 virtual ~AppListControllerDelegateWin(); | 75 virtual ~AppListControllerDelegateWin(); |
47 | 76 |
48 private: | 77 private: |
49 // AppListController overrides: | 78 // AppListController overrides: |
50 virtual void CloseView() OVERRIDE; | 79 virtual void CloseView() OVERRIDE; |
51 virtual void ViewClosing() OVERRIDE; | 80 virtual void ViewClosing() OVERRIDE; |
52 virtual void ViewActivationChanged(bool active) OVERRIDE; | 81 virtual void ViewActivationChanged(bool active) OVERRIDE; |
(...skipping 26 matching lines...) Expand all Loading... | |
79 | 108 |
80 private: | 109 private: |
81 // Utility methods for showing the app list. | 110 // Utility methods for showing the app list. |
82 void GetArrowLocationAndUpdateAnchor( | 111 void GetArrowLocationAndUpdateAnchor( |
83 const gfx::Rect& work_area, | 112 const gfx::Rect& work_area, |
84 int min_space_x, | 113 int min_space_x, |
85 int min_space_y, | 114 int min_space_y, |
86 views::BubbleBorder::ArrowLocation* arrow, | 115 views::BubbleBorder::ArrowLocation* arrow, |
87 gfx::Point* anchor); | 116 gfx::Point* anchor); |
88 void UpdateArrowPositionAndAnchorPoint(app_list::AppListView* view); | 117 void UpdateArrowPositionAndAnchorPoint(app_list::AppListView* view); |
89 CommandLine GetAppListCommandLine(); | |
90 string16 GetAppListIconPath(); | 118 string16 GetAppListIconPath(); |
91 string16 GetAppModelId(); | |
92 | 119 |
93 // Check if the app list or the taskbar has focus. The app list is kept | 120 // 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 | 121 // 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 | 122 // pinned but will hide it if it otherwise loses focus. This is checked |
96 // periodically whenever the app list does not have focus. | 123 // periodically whenever the app list does not have focus. |
97 void CheckTaskbarOrViewHasFocus(); | 124 void CheckTaskbarOrViewHasFocus(); |
98 | 125 |
99 // Weak pointer. The view manages its own lifetime. | 126 // Weak pointer. The view manages its own lifetime. |
100 app_list::AppListView* current_view_; | 127 app_list::AppListView* current_view_; |
101 | 128 |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
257 anchor->Offset(kAnchorOffset, 0); | 284 anchor->Offset(kAnchorOffset, 0); |
258 return; | 285 return; |
259 } | 286 } |
260 | 287 |
261 *arrow = views::BubbleBorder::RIGHT_TOP; | 288 *arrow = views::BubbleBorder::RIGHT_TOP; |
262 anchor->Offset(-kAnchorOffset, 0); | 289 anchor->Offset(-kAnchorOffset, 0); |
263 } | 290 } |
264 | 291 |
265 void AppListController::UpdateArrowPositionAndAnchorPoint( | 292 void AppListController::UpdateArrowPositionAndAnchorPoint( |
266 app_list::AppListView* view) { | 293 app_list::AppListView* view) { |
267 static const int kArrowSize = 10; | 294 const int kArrowSize = 10; |
268 static const int kPadding = 20; | 295 const int kPadding = 20; |
269 | 296 |
270 gfx::Size preferred = view->GetPreferredSize(); | 297 gfx::Size preferred = view->GetPreferredSize(); |
271 // Add the size of the arrow to the space needed, as the preferred size is | 298 // Add the size of the arrow to the space needed, as the preferred size is |
272 // of the view excluding the arrow. | 299 // of the view excluding the arrow. |
273 int min_space_x = preferred.width() + kAnchorOffset + kPadding + kArrowSize; | 300 int min_space_x = preferred.width() + kAnchorOffset + kPadding + kArrowSize; |
274 int min_space_y = preferred.height() + kAnchorOffset + kPadding + kArrowSize; | 301 int min_space_y = preferred.height() + kAnchorOffset + kPadding + kArrowSize; |
275 | 302 |
276 gfx::Point anchor = view->anchor_point(); | 303 gfx::Point anchor = view->anchor_point(); |
277 gfx::Display display = gfx::Screen::GetScreenFor( | 304 gfx::Display display = gfx::Screen::GetScreenFor( |
278 view->GetWidget()->GetNativeView())->GetDisplayNearestPoint(anchor); | 305 view->GetWidget()->GetNativeView())->GetDisplayNearestPoint(anchor); |
279 const gfx::Rect& display_rect = display.work_area(); | 306 const gfx::Rect& display_rect = display.work_area(); |
280 views::BubbleBorder::ArrowLocation arrow; | 307 views::BubbleBorder::ArrowLocation arrow; |
281 GetArrowLocationAndUpdateAnchor(display.work_area(), | 308 GetArrowLocationAndUpdateAnchor(display.work_area(), |
282 min_space_x, | 309 min_space_x, |
283 min_space_y, | 310 min_space_y, |
284 &arrow, | 311 &arrow, |
285 &anchor); | 312 &anchor); |
286 view->SetBubbleArrowLocation(arrow); | 313 view->SetBubbleArrowLocation(arrow); |
287 view->SetAnchorPoint(anchor); | 314 view->SetAnchorPoint(anchor); |
288 } | 315 } |
289 | 316 |
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() { | 317 string16 AppListController::GetAppListIconPath() { |
305 FilePath icon_path; | 318 FilePath icon_path; |
306 if (!PathService::Get(base::DIR_MODULE, &icon_path)) | 319 if (!PathService::Get(base::DIR_EXE, &icon_path)) |
307 return string16(); | 320 return string16(); |
308 | 321 |
309 icon_path = icon_path.Append(chrome::kBrowserResourcesDll); | |
310 std::stringstream ss; | 322 std::stringstream ss; |
311 ss << ",-" << IDI_APP_LIST; | 323 ss << ",-" << IDR_APP_LIST; |
grt (UTC plus 2)
2012/11/02 19:39:38
i suspect this doesn't work; see my comment in the
benwells
2012/11/03 00:54:18
Thanks for that, it was broken. Fixed now, and tes
| |
312 string16 result = icon_path.value(); | 324 string16 result = icon_path.value(); |
313 result.append(UTF8ToUTF16(ss.str())); | 325 result.append(UTF8ToUTF16(ss.str())); |
314 return result; | 326 return result; |
315 } | 327 } |
316 | 328 |
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() { | 329 void AppListController::CheckTaskbarOrViewHasFocus() { |
329 #if !defined(USE_AURA) | 330 #if !defined(USE_AURA) |
330 // Don't bother checking if the view has been closed. | 331 // Don't bother checking if the view has been closed. |
331 if (!current_view_) | 332 if (!current_view_) |
332 return; | 333 return; |
333 | 334 |
334 // First get the taskbar and jump lists windows (the jump list is the | 335 // First get the taskbar and jump lists windows (the jump list is the |
335 // context menu which the taskbar uses). | 336 // context menu which the taskbar uses). |
336 HWND jump_list_hwnd = FindWindow(L"DV2ControlHost", NULL); | 337 HWND jump_list_hwnd = FindWindow(L"DV2ControlHost", NULL); |
337 HWND taskbar_hwnd = FindWindow(L"Shell_TrayWnd", NULL); | 338 HWND taskbar_hwnd = FindWindow(L"Shell_TrayWnd", NULL); |
(...skipping 12 matching lines...) Expand all Loading... | |
350 } | 351 } |
351 focused_hwnd = GetParent(focused_hwnd); | 352 focused_hwnd = GetParent(focused_hwnd); |
352 } | 353 } |
353 | 354 |
354 // If we get here, the focused window is not the taskbar, it's context menu, | 355 // 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. | 356 // or the app list, so close the app list. |
356 CloseAppList(); | 357 CloseAppList(); |
357 #endif | 358 #endif |
358 } | 359 } |
359 | 360 |
361 // Check that a taskbar shortcut exists if it should, or does not exist if | |
362 // it should not. A taskbar shortcut should exist if the switch | |
363 // kShowAppListShortcut is set. The shortcut will be created or deleted in | |
364 // |user_data_dir| and will use a Windows Application Model Id of | |
365 // |app_model_id|. | |
366 // This runs on the FILE thread and not in the blocking IO thread pool as there | |
367 // are other tasks running (also on the FILE thread) which fiddle with shortcut | |
368 // icons (ShellIntegration::MigrateWin7ShortcutsOnPath). Having different | |
369 // threads fiddle with the same threads could cause race issues. | |
grt (UTC plus 2)
2012/11/02 19:39:38
"same threads" -> "same shortcuts"
benwells
2012/11/03 00:54:18
Done.
| |
370 void CheckAppListTaskbarShortcutOnFileThread(const FilePath& user_data_dir, | |
371 const string16& app_model_id) { | |
372 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
373 | |
374 const string16 shortcut_name = l10n_util::GetStringUTF16( | |
375 IDS_APP_LIST_SHORTCUT_NAME); | |
376 const FilePath shortcut_path(user_data_dir.Append(shortcut_name) | |
377 .AddExtension(L"lnk")); | |
gab
2012/11/02 12:37:28
optional: There is a kLnkExt constant in chrome\in
benwells
2012/11/03 00:54:18
If it wasn't Saturday maybe I'd do it, I'll just a
gab
2012/11/04 16:50:34
It's open to "anyone at google.com" maybe I should
| |
378 const bool should_show = CommandLine::ForCurrentProcess()->HasSwitch( | |
379 switches::kShowAppListShortcut); | |
380 | |
381 // This will not reshow a shortcut if it has been unpinned manually by the | |
382 // user, as that will not delete the shortcut file. | |
383 if (should_show && !file_util::PathExists(shortcut_path)) { | |
384 FilePath chrome_exe; | |
385 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) | |
386 return; | |
387 | |
388 base::win::ShortcutProperties shortcut_properties; | |
389 shortcut_properties.set_target(chrome_exe); | |
390 shortcut_properties.set_working_dir(chrome_exe.DirName()); | |
391 | |
392 string16 wide_switches(GetAppListCommandLine().GetArgumentsString()); | |
393 shortcut_properties.set_arguments(wide_switches); | |
394 shortcut_properties.set_description(shortcut_name); | |
395 | |
396 FilePath icon_path; | |
397 if (!PathService::Get(base::FILE_EXE, &icon_path)) | |
398 return; | |
gab
2012/11/02 12:37:28
Add NOTREACHED() before return;.
benwells
2012/11/03 00:54:18
Done.
| |
399 | |
400 // Icons are added to the resources of the DLL using icon indexes. The | |
401 // icon index for the app list icon is in IDR_APP_LIST. Creating shortcuts | |
402 // needs to specify a resource index, which are different to icon indexes. | |
403 // They are 0 based and contiguous. As Google Chrome builds have an extra | |
404 // icon (SXS) the icon for Google Chrome builds need to be one higher. | |
grt (UTC plus 2)
2012/11/02 19:39:38
the "one higher" portion of this is out of date
benwells
2012/11/03 00:54:18
Done.
| |
405 #if defined(GOOGLE_CHROME_BUILD) | |
406 const int kIconIndex = 5; | |
407 #else | |
408 const int kIconIndex = 1; | |
409 #endif | |
410 shortcut_properties.set_icon(icon_path, kIconIndex); | |
grt (UTC plus 2)
2012/11/02 19:39:38
icon_path -> chrome_exe and remove lines 396-399.
benwells
2012/11/03 00:54:18
Done.
| |
411 shortcut_properties.set_app_id(app_model_id); | |
412 | |
413 base::win::CreateOrUpdateShortcutLink(shortcut_path, shortcut_properties, | |
414 base::win::SHORTCUT_CREATE_ALWAYS); | |
415 base::win::TaskbarPinShortcutLink(shortcut_path.value().c_str()); | |
416 return; | |
417 } | |
418 | |
419 if (!should_show && file_util::PathExists(shortcut_path)) { | |
420 base::win::TaskbarUnpinShortcutLink(shortcut_path.value().c_str()); | |
421 file_util::Delete(shortcut_path, false); | |
422 } | |
423 } | |
424 | |
360 } // namespace | 425 } // namespace |
361 | 426 |
362 namespace app_list_controller { | 427 namespace app_list_controller { |
363 | 428 |
364 void ShowAppList() { | 429 void ShowAppList() { |
365 g_app_list_controller.Get().ShowAppList(); | 430 g_app_list_controller.Get().ShowAppList(); |
366 } | 431 } |
367 | 432 |
433 void CheckAppListTaskbarShortcut() { | |
434 FilePath user_data_dir(g_browser_process->profile_manager()->user_data_dir()); | |
435 content::BrowserThread::PostTask( | |
436 content::BrowserThread::FILE, FROM_HERE, | |
437 base::Bind(&CheckAppListTaskbarShortcutOnFileThread, user_data_dir, | |
438 GetAppModelId())); | |
439 } | |
440 | |
368 } // namespace app_list_controller | 441 } // namespace app_list_controller |
OLD | NEW |