Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(183)

Side by Side Diff: chrome/browser/shell_integration_win.cc

Issue 11712003: [Fixit-Dec-2012] Also add dual_mode to Start Menu shortcuts in MigrateChromiumShortcuts. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: grt++ Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/browser/shell_integration.h" 5 #include "chrome/browser/shell_integration.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <shobjidl.h> 8 #include <shobjidl.h>
9 #include <propkey.h> 9 #include <propkey.h>
10 #include <propvarutil.h> 10 #include <propvarutil.h>
11 #include <tchar.h>
12 #include <strsafe.h>
11 13
12 #include "base/bind.h" 14 #include "base/bind.h"
13 #include "base/command_line.h" 15 #include "base/command_line.h"
14 #include "base/file_util.h" 16 #include "base/file_util.h"
15 #include "base/message_loop.h" 17 #include "base/message_loop.h"
16 #include "base/path_service.h" 18 #include "base/path_service.h"
17 #include "base/string_number_conversions.h" 19 #include "base/string_number_conversions.h"
18 #include "base/string_util.h" 20 #include "base/string_util.h"
19 #include "base/stringprintf.h" 21 #include "base/stringprintf.h"
20 #include "base/utf_string_conversions.h" 22 #include "base/utf_string_conversions.h"
21 #include "base/win/registry.h" 23 #include "base/win/registry.h"
22 #include "base/win/scoped_comptr.h" 24 #include "base/win/scoped_comptr.h"
23 #include "base/win/shortcut.h" 25 #include "base/win/shortcut.h"
24 #include "base/win/windows_version.h" 26 #include "base/win/windows_version.h"
25 #include "chrome/browser/web_applications/web_app.h" 27 #include "chrome/browser/web_applications/web_app.h"
26 #include "chrome/common/chrome_constants.h" 28 #include "chrome/common/chrome_constants.h"
27 #include "chrome/common/chrome_paths_internal.h" 29 #include "chrome/common/chrome_paths_internal.h"
28 #include "chrome/common/chrome_switches.h" 30 #include "chrome/common/chrome_switches.h"
29 #include "chrome/installer/setup/setup_util.h" 31 #include "chrome/installer/setup/setup_util.h"
30 #include "chrome/installer/util/browser_distribution.h" 32 #include "chrome/installer/util/browser_distribution.h"
31 #include "chrome/installer/util/create_reg_key_work_item.h" 33 #include "chrome/installer/util/create_reg_key_work_item.h"
32 #include "chrome/installer/util/install_util.h" 34 #include "chrome/installer/util/install_util.h"
33 #include "chrome/installer/util/set_reg_value_work_item.h" 35 #include "chrome/installer/util/set_reg_value_work_item.h"
34 #include "chrome/installer/util/shell_util.h" 36 #include "chrome/installer/util/shell_util.h"
35 #include "chrome/installer/util/util_constants.h" 37 #include "chrome/installer/util/util_constants.h"
36 #include "chrome/installer/util/work_item.h" 38 #include "chrome/installer/util/work_item.h"
37 #include "chrome/installer/util/work_item_list.h" 39 #include "chrome/installer/util/work_item_list.h"
38 #include "content/public/browser/browser_thread.h" 40 #include "content/public/browser/browser_thread.h"
39 41
42 // propsys.lib is required for PropvariantTo*().
43 #pragma comment(lib, "propsys.lib")
44
40 using content::BrowserThread; 45 using content::BrowserThread;
41 46
42 namespace { 47 namespace {
43 48
44 #if defined(GOOGLE_CHROME_BUILD) 49 #if defined(GOOGLE_CHROME_BUILD)
45 const wchar_t kAppListAppName[] = L"ChromeAppList"; 50 const wchar_t kAppListAppName[] = L"ChromeAppList";
46 #else 51 #else
47 const wchar_t kAppListAppName[] = L"ChromiumAppList"; 52 const wchar_t kAppListAppName[] = L"ChromiumAppList";
48 #endif 53 #endif
49 54
(...skipping 26 matching lines...) Expand all
76 for (size_t i = 0; i < basenames.length(); ++i) { 81 for (size_t i = 0; i < basenames.length(); ++i) {
77 if (IsAsciiAlpha(basenames[i]) || 82 if (IsAsciiAlpha(basenames[i]) ||
78 IsAsciiDigit(basenames[i]) || 83 IsAsciiDigit(basenames[i]) ||
79 basenames[i] == L'.') 84 basenames[i] == L'.')
80 profile_id += basenames[i]; 85 profile_id += basenames[i];
81 } 86 }
82 87
83 return profile_id; 88 return profile_id;
84 } 89 }
85 90
86 bool GetShortcutAppId(IShellLink* shell_link, string16* app_id) { 91 // Gets expected app id for given Chrome (based on |command_line| and
87 DCHECK(shell_link); 92 // |is_per_user_install|).
88 DCHECK(app_id); 93 string16 GetExpectedAppId(const CommandLine& command_line,
89 94 bool is_per_user_install) {
90 app_id->clear();
91
92 base::win::ScopedComPtr<IPropertyStore> property_store;
93 if (FAILED(property_store.QueryFrom(shell_link)))
94 return false;
95
96 PROPVARIANT appid_value;
97 PropVariantInit(&appid_value);
98 if (FAILED(property_store->GetValue(PKEY_AppUserModel_ID, &appid_value)))
99 return false;
100
101 if (appid_value.vt == VT_LPWSTR || appid_value.vt == VT_BSTR)
102 app_id->assign(appid_value.pwszVal);
103
104 PropVariantClear(&appid_value);
105 return true;
106 }
107
108 // Gets expected app id for given chrome shortcut. Returns true if the shortcut
109 // points to chrome and expected app id is successfully derived.
110 bool GetExpectedAppId(const FilePath& chrome_exe,
111 IShellLink* shell_link,
112 string16* expected_app_id) {
113 DCHECK(shell_link);
114 DCHECK(expected_app_id);
115
116 expected_app_id->clear();
117
118 // Check if the shortcut points to chrome_exe.
119 string16 source;
120 if (FAILED(shell_link->GetPath(WriteInto(&source, MAX_PATH), MAX_PATH, NULL,
121 SLGP_RAWPATH)) ||
122 lstrcmpi(chrome_exe.value().c_str(), source.c_str()))
123 return false;
124
125 string16 arguments;
126 if (FAILED(shell_link->GetArguments(WriteInto(&arguments, MAX_PATH),
127 MAX_PATH)))
128 return false;
129
130 // Get expected app id from shortcut command line.
131 CommandLine command_line = CommandLine::FromString(base::StringPrintf(
132 L"\"%ls\" %ls", source.c_str(), arguments.c_str()));
133
134 FilePath profile_path; 95 FilePath profile_path;
135 if (command_line.HasSwitch(switches::kUserDataDir)) { 96 if (command_line.HasSwitch(switches::kUserDataDir)) {
136 profile_path = 97 profile_path =
137 command_line.GetSwitchValuePath(switches::kUserDataDir).AppendASCII( 98 command_line.GetSwitchValuePath(switches::kUserDataDir).AppendASCII(
138 chrome::kInitialProfile); 99 chrome::kInitialProfile);
139 } 100 }
140 101
141 string16 app_name; 102 string16 app_name;
142 if (command_line.HasSwitch(switches::kApp)) { 103 if (command_line.HasSwitch(switches::kApp)) {
143 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromURL( 104 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
144 GURL(command_line.GetSwitchValueASCII(switches::kApp)))); 105 GURL(command_line.GetSwitchValueASCII(switches::kApp))));
145 } else if (command_line.HasSwitch(switches::kAppId)) { 106 } else if (command_line.HasSwitch(switches::kAppId)) {
146 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromExtensionId( 107 app_name = UTF8ToUTF16(web_app::GenerateApplicationNameFromExtensionId(
147 command_line.GetSwitchValueASCII(switches::kAppId))); 108 command_line.GetSwitchValueASCII(switches::kAppId)));
148 } else if (command_line.HasSwitch(switches::kShowAppList)) { 109 } else if (command_line.HasSwitch(switches::kShowAppList)) {
149 app_name = kAppListAppName; 110 app_name = kAppListAppName;
150 } else { 111 } else {
151 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 112 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
152 app_name = ShellUtil::GetBrowserModelId( 113 app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
153 dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()));
154 } 114 }
155 115
156 expected_app_id->assign( 116 return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
157 ShellIntegration::GetAppModelIdForProfile(app_name, profile_path));
158 return true;
159 }
160
161 void MigrateWin7ShortcutsInPath(
162 const FilePath& chrome_exe, const FilePath& path) {
163 // Enumerate all pinned shortcuts in the given path directly.
164 file_util::FileEnumerator shortcuts_enum(
165 path, false, // not recursive
166 file_util::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
167
168 for (FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
169 shortcut = shortcuts_enum.Next()) {
170 // Load the shortcut.
171 base::win::ScopedComPtr<IShellLink> shell_link;
172 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
173 CLSCTX_INPROC_SERVER))) {
174 NOTREACHED();
175 return;
176 }
177
178 base::win::ScopedComPtr<IPersistFile> persist_file;
179 if (FAILED(persist_file.QueryFrom(shell_link)) ||
180 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
181 NOTREACHED();
182 return;
183 }
184
185 // Get expected app id from shortcut.
186 string16 expected_app_id;
187 if (!GetExpectedAppId(chrome_exe, shell_link, &expected_app_id) ||
188 expected_app_id.empty())
189 continue;
190
191 // Get existing app id from shortcut if any.
192 string16 existing_app_id;
193 GetShortcutAppId(shell_link, &existing_app_id);
194
195 if (expected_app_id != existing_app_id) {
196 base::win::ShortcutProperties properties_app_id_only;
197 properties_app_id_only.set_app_id(expected_app_id);
198 base::win::CreateOrUpdateShortcutLink(
199 shortcut, properties_app_id_only,
200 base::win::SHORTCUT_UPDATE_EXISTING);
201 }
202 }
203 } 117 }
204 118
205 void MigrateChromiumShortcutsCallback() { 119 void MigrateChromiumShortcutsCallback() {
206 // This should run on the file thread. 120 // This should run on the file thread.
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); 121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
208 122
209 // Get full path of chrome. 123 // Get full path of chrome.
210 FilePath chrome_exe; 124 FilePath chrome_exe;
211 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) 125 if (!PathService::Get(base::FILE_EXE, &chrome_exe))
212 return; 126 return;
(...skipping 21 matching lines...) Expand all
234 for (int i = 0; i < arraysize(kLocations); ++i) { 148 for (int i = 0; i < arraysize(kLocations); ++i) {
235 FilePath path; 149 FilePath path;
236 if (!PathService::Get(kLocations[i].location_id, &path)) { 150 if (!PathService::Get(kLocations[i].location_id, &path)) {
237 NOTREACHED(); 151 NOTREACHED();
238 continue; 152 continue;
239 } 153 }
240 154
241 if (kLocations[i].sub_dir) 155 if (kLocations[i].sub_dir)
242 path = path.Append(kLocations[i].sub_dir); 156 path = path.Append(kLocations[i].sub_dir);
243 157
244 MigrateWin7ShortcutsInPath(chrome_exe, path); 158 bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU);
159 ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
160 check_dual_mode);
245 } 161 }
246 } 162 }
247 163
248 ShellIntegration::DefaultWebClientState 164 ShellIntegration::DefaultWebClientState
249 GetDefaultWebClientStateFromShellUtilDefaultState( 165 GetDefaultWebClientStateFromShellUtilDefaultState(
250 ShellUtil::DefaultState default_state) { 166 ShellUtil::DefaultState default_state) {
251 switch (default_state) { 167 switch (default_state) {
252 case ShellUtil::NOT_DEFAULT: 168 case ShellUtil::NOT_DEFAULT:
253 return ShellIntegration::NOT_DEFAULT; 169 return ShellIntegration::NOT_DEFAULT;
254 case ShellUtil::IS_DEFAULT: 170 case ShellUtil::IS_DEFAULT:
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after
446 // This needs to happen eventually (e.g. so that the appid is fixed and the 362 // This needs to happen eventually (e.g. so that the appid is fixed and the
447 // run-time Chrome icon is merged with the taskbar shortcut), but this is not 363 // run-time Chrome icon is merged with the taskbar shortcut), but this is not
448 // urgent and shouldn't delay Chrome startup. 364 // urgent and shouldn't delay Chrome startup.
449 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15; 365 static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
450 BrowserThread::PostDelayedTask( 366 BrowserThread::PostDelayedTask(
451 BrowserThread::FILE, FROM_HERE, 367 BrowserThread::FILE, FROM_HERE,
452 base::Bind(&MigrateChromiumShortcutsCallback), 368 base::Bind(&MigrateChromiumShortcutsCallback),
453 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds)); 369 base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
454 } 370 }
455 371
372 int ShellIntegration::MigrateShortcutsInPathInternal(
373 const FilePath& chrome_exe, const FilePath& path, bool check_dual_mode) {
374 DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
375
376 // Enumerate all pinned shortcuts in the given path directly.
377 file_util::FileEnumerator shortcuts_enum(
378 path, false, // not recursive
379 file_util::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
380
381 bool is_per_user_install =
382 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
383
384 int shortcuts_migrated = 0;
385 FilePath target_path;
386 string16 arguments;
387 string16 existing_app_id;
388 for (FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
389 shortcut = shortcuts_enum.Next()) {
390 // TODO(gab): Use ProgramCompare instead of comparing FilePaths below once
391 // it is fixed to work with FilePaths with spaces.
392 if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
393 chrome_exe != target_path) {
394 continue;
395 }
396 CommandLine command_line(CommandLine::FromString(base::StringPrintf(
397 L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
robertshield 2013/01/03 22:41:59 Just to double check, is there any way that target
gab 2013/01/03 23:12:43 Good question, this code was already there, I find
robertshield 2013/01/04 01:38:14 For peace of mind, could you edit the shortcut in
gab 2013/01/04 16:41:44 I just tried and this is not even doable in Explor
398
399 // Get the expected AppId for this Chrome shortcut.
400 string16 expected_app_id(
401 GetExpectedAppId(command_line, is_per_user_install));
402 if (expected_app_id.empty())
403 continue;
404
405 // Load the shortcut.
406 base::win::ScopedComPtr<IShellLink> shell_link;
407 base::win::ScopedComPtr<IPersistFile> persist_file;
408 if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
409 CLSCTX_INPROC_SERVER)) ||
410 FAILED(persist_file.QueryFrom(shell_link)) ||
411 FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
412 DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
413 continue;
414 }
415
416 // Any properties that need to be updated on the shortcut will be stored in
417 // |updated_properties|.
418 base::win::ShortcutProperties updated_properties;
419
420 // Validate the existing app id for the shortcut.
421 base::win::ScopedComPtr<IPropertyStore> property_store;
422 PROPVARIANT pv_app_id;
423 PropVariantInit(&pv_app_id);
424 if (FAILED(property_store.QueryFrom(shell_link)) ||
425 property_store->GetValue(PKEY_AppUserModel_ID, &pv_app_id) != S_OK) {
426 // When in doubt, prefer not updating the shortcut.
427 NOTREACHED();
428 continue;
429 } else if (pv_app_id.vt == VT_EMPTY) {
430 // If there is no app_id set, set our app_id if one is expected.
431 if (!expected_app_id.empty())
432 updated_properties.set_app_id(expected_app_id);
433 } else {
434 // Validate that the existing app_id is the expected app_id; if not, set
435 // the expected app_id on the shortcut.
436 size_t expected_size = expected_app_id.size() + 1;
437 HRESULT result = PropVariantToString(
438 pv_app_id, WriteInto(&existing_app_id, expected_size), expected_size);
439 PropVariantClear(&pv_app_id);
440 if (result != S_OK && result != STRSAFE_E_INSUFFICIENT_BUFFER) {
441 // Accept the STRSAFE_E_INSUFFICIENT_BUFFER error state as it means the
442 // existing appid is longer than |expected_app_id| and thus we will
443 // simply assume inequality.
444 NOTREACHED();
445 continue;
446 } else if (result == STRSAFE_E_INSUFFICIENT_BUFFER ||
447 expected_app_id != existing_app_id) {
448 updated_properties.set_app_id(expected_app_id);
449 }
450 }
451
452 if (check_dual_mode) {
453 BOOL existing_dual_mode;
454 PROPVARIANT pv_dual_mode;
455 PropVariantInit(&pv_dual_mode);
456 if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
457 &pv_dual_mode) != S_OK) {
458 // When in doubt, prefer to not update the shortcut.
459 NOTREACHED();
460 continue;
461 } else if (pv_dual_mode.vt == VT_EMPTY) {
462 // If dual_mode is not set at all, make sure it gets set to true.
463 updated_properties.set_dual_mode(true);
464 } else {
465 // If it is set to false, make sure it gets set to true as well.
466 if (PropVariantToBoolean(pv_dual_mode, &existing_dual_mode) != S_OK) {
467 NOTREACHED();
468 continue;
469 }
470 PropVariantClear(&pv_dual_mode);
robertshield 2013/01/03 22:41:59 Should we not clear the PropVariant in the if bran
gab 2013/01/03 23:12:43 Right, MSDN is not clear about when clearing is re
robertshield 2013/01/04 01:38:14 sgtm
471 if (!existing_dual_mode)
472 updated_properties.set_dual_mode(true);
473 }
474 }
475
476 persist_file.Release();
477 shell_link.Release();
478
479 // Update the shortcut if some of its properties need to be updated.
480 if (updated_properties.options &&
481 base::win::CreateOrUpdateShortcutLink(
robertshield 2013/01/03 22:41:59 minor nit: DoFooToShortcutLink reads a little bit
gab 2013/01/03 23:12:43 I agree that this name is redundant, added this to
482 shortcut, updated_properties,
483 base::win::SHORTCUT_UPDATE_EXISTING)) {
484 ++shortcuts_migrated;
485 }
486 }
487 return shortcuts_migrated;
488 }
489
456 FilePath ShellIntegration::GetStartMenuShortcut(const FilePath& chrome_exe) { 490 FilePath ShellIntegration::GetStartMenuShortcut(const FilePath& chrome_exe) {
457 static const int kFolderIds[] = { 491 static const int kFolderIds[] = {
458 base::DIR_COMMON_START_MENU, 492 base::DIR_COMMON_START_MENU,
459 base::DIR_START_MENU, 493 base::DIR_START_MENU,
460 }; 494 };
461 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 495 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
462 string16 shortcut_name(dist->GetAppShortCutName()); 496 string16 shortcut_name(dist->GetAppShortCutName());
463 FilePath shortcut; 497 FilePath shortcut;
464 498
465 // Check both the common and the per-user Start Menu folders for system-level 499 // Check both the common and the per-user Start Menu folders for system-level
466 // installs. 500 // installs.
467 size_t folder = 501 size_t folder =
468 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0; 502 InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0;
469 for (; folder < arraysize(kFolderIds); ++folder) { 503 for (; folder < arraysize(kFolderIds); ++folder) {
470 if (!PathService::Get(kFolderIds[folder], &shortcut)) { 504 if (!PathService::Get(kFolderIds[folder], &shortcut)) {
471 NOTREACHED(); 505 NOTREACHED();
472 continue; 506 continue;
473 } 507 }
474 508
475 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name + 509 shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
476 installer::kLnkExt); 510 installer::kLnkExt);
477 if (file_util::PathExists(shortcut)) 511 if (file_util::PathExists(shortcut))
478 return shortcut; 512 return shortcut;
479 } 513 }
480 514
481 return FilePath(); 515 return FilePath();
482 } 516 }
OLDNEW
« no previous file with comments | « chrome/browser/shell_integration_unittest.cc ('k') | chrome/browser/shell_integration_win_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698