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* kSwitchesToCopy[] = {switches::kUserDataDir}; | |
grt (UTC plus 2)
2012/11/02 04:00:36
"const char* const " so the whole thing is truly c
benwells
2012/11/02 04:54:43
Done.
| |
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 | |
gab
2012/11/02 04:36:28
Are you sure this is what you want? i.e. you will
benwells
2012/11/02 04:54:43
Yes that's correct. It will get profile switching
| |
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_MODULE, &icon_path)) |
307 return string16(); | 320 return string16(); |
308 | 321 |
309 icon_path = icon_path.Append(chrome::kBrowserResourcesDll); | 322 icon_path = icon_path.Append(chrome::kBrowserResourcesDll); |
310 std::stringstream ss; | 323 std::stringstream ss; |
311 ss << ",-" << IDI_APP_LIST; | 324 ss << ",-" << IDI_APP_LIST; |
312 string16 result = icon_path.value(); | 325 string16 result = icon_path.value(); |
313 result.append(UTF8ToUTF16(ss.str())); | 326 result.append(UTF8ToUTF16(ss.str())); |
314 return result; | 327 return result; |
315 } | 328 } |
316 | 329 |
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() { | 330 void AppListController::CheckTaskbarOrViewHasFocus() { |
329 #if !defined(USE_AURA) | 331 #if !defined(USE_AURA) |
330 // Don't bother checking if the view has been closed. | 332 // Don't bother checking if the view has been closed. |
331 if (!current_view_) | 333 if (!current_view_) |
332 return; | 334 return; |
333 | 335 |
334 // First get the taskbar and jump lists windows (the jump list is the | 336 // First get the taskbar and jump lists windows (the jump list is the |
335 // context menu which the taskbar uses). | 337 // context menu which the taskbar uses). |
336 HWND jump_list_hwnd = FindWindow(L"DV2ControlHost", NULL); | 338 HWND jump_list_hwnd = FindWindow(L"DV2ControlHost", NULL); |
337 HWND taskbar_hwnd = FindWindow(L"Shell_TrayWnd", NULL); | 339 HWND taskbar_hwnd = FindWindow(L"Shell_TrayWnd", NULL); |
(...skipping 12 matching lines...) Expand all Loading... | |
350 } | 352 } |
351 focused_hwnd = GetParent(focused_hwnd); | 353 focused_hwnd = GetParent(focused_hwnd); |
352 } | 354 } |
353 | 355 |
354 // If we get here, the focused window is not the taskbar, it's context menu, | 356 // 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. | 357 // or the app list, so close the app list. |
356 CloseAppList(); | 358 CloseAppList(); |
357 #endif | 359 #endif |
358 } | 360 } |
359 | 361 |
362 // Check that a taskbar shortcut exists if it should, or does not exist if | |
363 // it should not. A taskbar shortcut should exist if the switch | |
364 // kShowAppListShortcut is set. The shortcut will be created or deleted in | |
365 // |user_data_dir| and will use a Windows Application Model Id of | |
366 // |app_model_id|. | |
367 void CheckAppListTaskbarShortcutOnFileThread(const FilePath user_data_dir, | |
gab
2012/11/02 04:36:28
nit: Should this be const FilePath&?
gab
2012/11/02 12:37:28
Ping.
| |
368 const string16& app_model_id) { | |
369 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
370 | |
371 const string16 shortcut_name = l10n_util::GetStringUTF16( | |
372 IDS_APP_LIST_SHORTCUT_NAME); | |
373 const FilePath shortcut_path(user_data_dir.Append(shortcut_name). | |
374 AddExtension(L"lnk")); | |
gab
2012/11/02 04:36:28
nit: put '.' on this line to make it more obvious
benwells
2012/11/02 04:54:43
Done.
| |
375 const bool should_show = CommandLine::ForCurrentProcess()->HasSwitch( | |
376 switches::kShowAppListShortcut); | |
377 | |
378 // This will not reshow a shortcut if it has been unpinned manually by the | |
379 // user, as that will not delete the shortcut file. | |
380 if (should_show && !file_util::PathExists(shortcut_path)) { | |
381 FilePath chrome_exe; | |
382 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) | |
383 return; | |
384 | |
385 base::win::ShortcutProperties shortcut_properties; | |
gab
2012/11/02 04:36:28
It will probably be easier to use the ShellUtil AP
benwells
2012/11/02 04:54:43
Yes this is temporary, and will be removed when th
| |
386 shortcut_properties.set_target(chrome_exe); | |
387 shortcut_properties.set_working_dir(chrome_exe.DirName()); | |
388 | |
389 string16 wide_switches(GetAppListCommandLine().GetArgumentsString()); | |
390 shortcut_properties.set_arguments(wide_switches); | |
391 shortcut_properties.set_description(shortcut_name); | |
392 | |
393 FilePath icon_path; | |
394 if (!PathService::Get(base::DIR_MODULE, &icon_path)) | |
395 return; | |
396 | |
397 icon_path = icon_path.Append(chrome::kBrowserResourcesDll); | |
398 | |
399 // Icons are added to the resources of the DLL using icon indexes. The | |
400 // icon index for the app list icon is in IDI_APP_LIST. Creating shortcuts | |
401 // needs to specify a resource index, which are different to icon indexes. | |
402 // They are 0 based and contiguous. As Google Chrome builds have an extra | |
403 // icon (SXS) the icon for Google Chrome builds need to be one higher. | |
404 #if defined(GOOGLE_CHROME_BUILD) | |
405 const int kIconIndex = 26; | |
406 #else | |
407 const int kIconIndex = 25; | |
408 #endif | |
409 shortcut_properties.set_icon(icon_path, kIconIndex); | |
410 shortcut_properties.set_app_id(app_model_id); | |
411 shortcut_properties.set_dual_mode(false); | |
gab
2012/11/02 03:47:15
You don't need to explicitly set dual_mode to fals
benwells
2012/11/02 04:54:43
Done.
| |
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 string16 app_model_id = GetAppModelId(); | |
gab
2012/11/02 03:47:15
nit: Also use () initialization here instead of '=
benwells
2012/11/02 04:54:43
Done.
| |
436 content::BrowserThread::PostTask( | |
437 content::BrowserThread::FILE, FROM_HERE, | |
438 base::Bind(&CheckAppListTaskbarShortcutOnFileThread, user_data_dir, | |
439 app_model_id)); | |
440 } | |
441 | |
368 } // namespace app_list_controller | 442 } // namespace app_list_controller |
OLD | NEW |