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 "chrome/browser/web_applications/web_app.h" | 5 #include "chrome/browser/web_applications/web_app.h" |
6 | 6 |
7 #include <shlobj.h> | 7 #include <shlobj.h> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 base::StringPrintf(" (%d)", i)); | 122 base::StringPrintf(" (%d)", i)); |
123 } | 123 } |
124 if (file_util::PathExists(shortcut_file) && | 124 if (file_util::PathExists(shortcut_file) && |
125 ShortcutIsForProfile(shortcut_file, profile_path)) { | 125 ShortcutIsForProfile(shortcut_file, profile_path)) { |
126 shortcut_paths.push_back(shortcut_file); | 126 shortcut_paths.push_back(shortcut_file); |
127 } | 127 } |
128 } | 128 } |
129 return shortcut_paths; | 129 return shortcut_paths; |
130 } | 130 } |
131 | 131 |
132 } // namespace | 132 // Creates application shortcuts in a given set of paths. |
133 | 133 // |shortcut_paths| is a list of directories in which shortcuts should be |
134 namespace web_app { | 134 // created. If |pin_to_taskbar| is true, also pins a shortcut to the taskbar. |
koz (OOO until 15th September)
2013/05/17 01:36:19
There is no |pin_to_taskbar| here.
Matt Giuca
2013/05/20 01:02:46
Thanks, good catch. That comment was from an older
| |
135 | 135 // Returns true on success, false on failure. |
136 namespace internals { | 136 // Must be called on the FILE thread. |
137 | 137 bool CreateShortcutsInPaths( |
138 // Saves |image| to |icon_file| if the file is outdated and refresh shell's | |
139 // icon cache to ensure correct icon is displayed. Returns true if icon_file | |
140 // is up to date or successfully updated. | |
141 bool CheckAndSaveIcon(const base::FilePath& icon_file, | |
142 const gfx::ImageFamily& image) { | |
143 if (ShouldUpdateIcon(icon_file, image)) { | |
144 if (SaveIconWithCheckSum(icon_file, image)) { | |
145 // Refresh shell's icon cache. This call is quite disruptive as user would | |
146 // see explorer rebuilding the icon cache. It would be great that we find | |
147 // a better way to achieve this. | |
148 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, | |
149 NULL, NULL); | |
150 } else { | |
151 return false; | |
152 } | |
153 } | |
154 | |
155 return true; | |
156 } | |
157 | |
158 bool CreatePlatformShortcuts( | |
159 const base::FilePath& web_app_path, | 138 const base::FilePath& web_app_path, |
160 const ShellIntegration::ShortcutInfo& shortcut_info, | 139 const ShellIntegration::ShortcutInfo& shortcut_info, |
161 const ShellIntegration::ShortcutLocations& creation_locations) { | 140 const std::vector<base::FilePath>& shortcut_paths) { |
162 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
163 | |
164 // Shortcut paths under which to create shortcuts. | |
165 std::vector<base::FilePath> shortcut_paths = | |
166 GetShortcutPaths(creation_locations); | |
167 | |
168 bool pin_to_taskbar = creation_locations.in_quick_launch_bar && | |
169 (base::win::GetVersion() >= base::win::VERSION_WIN7); | |
170 | |
171 // Create/update the shortcut in the web app path for the "Pin To Taskbar" | |
172 // option in Win7. We use the web app path shortcut because we will overwrite | |
173 // it rather than appending unique numbers if the shortcut already exists. | |
174 // This prevents pinned apps from having unique numbers in their names. | |
175 if (pin_to_taskbar) | |
176 shortcut_paths.push_back(web_app_path); | |
177 | |
178 if (shortcut_paths.empty()) | |
179 return false; | |
180 | |
181 // Ensure web_app_path exists. | 141 // Ensure web_app_path exists. |
182 if (!file_util::PathExists(web_app_path) && | 142 if (!file_util::PathExists(web_app_path) && |
183 !file_util::CreateDirectory(web_app_path)) { | 143 !file_util::CreateDirectory(web_app_path)) { |
184 return false; | 144 return false; |
185 } | 145 } |
186 | 146 |
187 // Generates file name to use with persisted ico and shortcut file. | 147 // Generates file name to use with persisted ico and shortcut file. |
188 base::FilePath file_name = | 148 base::FilePath file_name = |
189 web_app::internals::GetSanitizedFileName(shortcut_info.title); | 149 web_app::internals::GetSanitizedFileName(shortcut_info.title); |
190 | 150 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
250 if (!file_util::PathExists(shortcut_file.DirName()) && | 210 if (!file_util::PathExists(shortcut_file.DirName()) && |
251 !file_util::CreateDirectory(shortcut_file.DirName())) { | 211 !file_util::CreateDirectory(shortcut_file.DirName())) { |
252 NOTREACHED(); | 212 NOTREACHED(); |
253 return false; | 213 return false; |
254 } | 214 } |
255 success = base::win::CreateOrUpdateShortcutLink( | 215 success = base::win::CreateOrUpdateShortcutLink( |
256 shortcut_file, shortcut_properties, | 216 shortcut_file, shortcut_properties, |
257 base::win::SHORTCUT_CREATE_ALWAYS) && success; | 217 base::win::SHORTCUT_CREATE_ALWAYS) && success; |
258 } | 218 } |
259 | 219 |
260 if (success && pin_to_taskbar) { | |
261 // Use the web app path shortcut for pinning to avoid having unique numbers | |
262 // in the application name. | |
263 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). | |
264 AddExtension(installer::kLnkExt); | |
265 success = base::win::TaskbarPinShortcutLink( | |
266 shortcut_to_pin.value().c_str()) && success; | |
267 } | |
268 | |
269 return success; | 220 return success; |
270 } | 221 } |
271 | 222 |
223 // Deletes all shortcuts for an app, recording the paths at they were found. | |
koz (OOO until 15th September)
2013/05/17 01:36:19
"the paths at they were found" -> "the paths they
Matt Giuca
2013/05/20 01:02:46
Done.
| |
224 // This will search for shortcuts named |title| (not necessarily the title found | |
225 // in |shortcut_info|). | |
koz (OOO until 15th September)
2013/05/17 01:36:19
Could you elaborate slightly on why |title| isn't
Matt Giuca
2013/05/20 01:02:46
Done.
| |
226 // |shortcut_paths| will be populated with a list of directories where shortcuts | |
227 // for this app were found (and deleted). It may be NULL. | |
228 void DeleteAndRememberShortcuts( | |
229 const base::FilePath& web_app_path, | |
230 const ShellIntegration::ShortcutInfo& shortcut_info, | |
231 const string16& title, | |
232 bool unpin_from_taskbar, | |
233 std::vector<base::FilePath>* shortcut_paths) { | |
234 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
235 | |
236 // Get all possible locations for shortcuts. | |
237 ShellIntegration::ShortcutLocations all_shortcut_locations; | |
238 all_shortcut_locations.in_applications_menu = true; | |
239 all_shortcut_locations.in_quick_launch_bar = true; | |
240 all_shortcut_locations.on_desktop = true; | |
241 // Delete shortcuts from the Chrome Apps subdirectory. | |
242 // This matches the subdir name set by CreateApplicationShortcutView::Accept | |
243 // for Chrome apps (not URL apps, but this function does not apply for them). | |
244 all_shortcut_locations.applications_menu_subdir = | |
245 web_app::GetAppShortcutsSubdirName(); | |
246 std::vector<base::FilePath> all_paths = web_app::internals::GetShortcutPaths( | |
247 all_shortcut_locations); | |
248 if (base::win::GetVersion() >= base::win::VERSION_WIN7) | |
249 all_paths.push_back(web_app_path); | |
250 | |
251 for (std::vector<base::FilePath>::const_iterator i = all_paths.begin(); | |
252 i != all_paths.end(); ++i) { | |
253 std::vector<base::FilePath> shortcut_files = | |
254 MatchingShortcutsForProfileAndExtension(*i, shortcut_info.profile_path, | |
255 title); | |
256 if (shortcut_paths != NULL && !shortcut_files.empty()) { | |
257 shortcut_paths->push_back(*i); | |
258 } | |
259 for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin(); | |
260 j != shortcut_files.end(); ++j) { | |
261 // Any shortcut could have been pinned, either by chrome or the user, so | |
262 // they are all unpinned. | |
263 if (unpin_from_taskbar) | |
264 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); | |
265 file_util::Delete(*j, false); | |
266 } | |
267 } | |
268 } | |
269 | |
270 } // namespace | |
271 | |
272 namespace web_app { | |
273 | |
274 namespace internals { | |
275 | |
276 // Saves |image| to |icon_file| if the file is outdated and refresh shell's | |
277 // icon cache to ensure correct icon is displayed. Returns true if icon_file | |
278 // is up to date or successfully updated. | |
279 bool CheckAndSaveIcon(const base::FilePath& icon_file, | |
280 const gfx::ImageFamily& image) { | |
281 if (ShouldUpdateIcon(icon_file, image)) { | |
282 if (SaveIconWithCheckSum(icon_file, image)) { | |
283 // Refresh shell's icon cache. This call is quite disruptive as user would | |
284 // see explorer rebuilding the icon cache. It would be great that we find | |
285 // a better way to achieve this. | |
286 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, | |
287 NULL, NULL); | |
288 } else { | |
289 return false; | |
290 } | |
291 } | |
292 | |
293 return true; | |
294 } | |
295 | |
296 bool CreatePlatformShortcuts( | |
297 const base::FilePath& web_app_path, | |
298 const ShellIntegration::ShortcutInfo& shortcut_info, | |
299 const ShellIntegration::ShortcutLocations& creation_locations) { | |
300 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | |
301 | |
302 // Shortcut paths under which to create shortcuts. | |
303 std::vector<base::FilePath> shortcut_paths = | |
304 GetShortcutPaths(creation_locations); | |
305 | |
306 bool pin_to_taskbar = creation_locations.in_quick_launch_bar && | |
307 (base::win::GetVersion() >= base::win::VERSION_WIN7); | |
308 | |
309 // Create/update the shortcut in the web app path for the "Pin To Taskbar" | |
310 // option in Win7. We use the web app path shortcut because we will overwrite | |
311 // it rather than appending unique numbers if the shortcut already exists. | |
312 // This prevents pinned apps from having unique numbers in their names. | |
313 if (pin_to_taskbar) | |
314 shortcut_paths.push_back(web_app_path); | |
315 | |
316 if (shortcut_paths.empty()) | |
317 return false; | |
318 | |
319 if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths)) | |
320 return false; | |
321 | |
322 if (pin_to_taskbar) { | |
323 base::FilePath file_name = | |
324 web_app::internals::GetSanitizedFileName(shortcut_info.title); | |
325 // Use the web app path shortcut for pinning to avoid having unique numbers | |
326 // in the application name. | |
327 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). | |
328 AddExtension(installer::kLnkExt); | |
329 if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) | |
330 return false; | |
331 } | |
332 | |
333 return true; | |
334 } | |
335 | |
272 void UpdatePlatformShortcuts( | 336 void UpdatePlatformShortcuts( |
273 const base::FilePath& web_app_path, | 337 const base::FilePath& web_app_path, |
338 const string16& old_app_title, | |
274 const ShellIntegration::ShortcutInfo& shortcut_info) { | 339 const ShellIntegration::ShortcutInfo& shortcut_info) { |
275 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 340 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
276 | 341 |
277 // Generates file name to use with persisted ico and shortcut file. | 342 // Generates file name to use with persisted ico and shortcut file. |
278 base::FilePath file_name = | 343 base::FilePath file_name = |
279 web_app::internals::GetSanitizedFileName(shortcut_info.title); | 344 web_app::internals::GetSanitizedFileName(shortcut_info.title); |
280 | 345 |
346 if (old_app_title != shortcut_info.title) { | |
347 // The app's title has changed. Delete all existing app shortcuts and | |
348 // recreate them in any locations they already existed (but do not add them | |
349 // to locations where they do not currently exist). | |
350 std::vector<base::FilePath> shortcut_paths; | |
351 // Do not unpin shortcuts from the task bar. If a shortcut is pinned, it | |
352 // will not be renamed, but at least it will continue to work. | |
Matt Giuca
2013/05/16 06:40:23
This is not ideal, but I want to leave it as-is fo
Matt Giuca
2013/05/20 01:02:46
Actually, I think this is important to do now. If
Matt Giuca
2013/05/23 02:36:24
Done.
| |
353 DeleteAndRememberShortcuts(web_app_path, shortcut_info, old_app_title, | |
354 false, &shortcut_paths); | |
355 CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths); | |
356 } | |
357 | |
281 // If an icon file exists, and is out of date, replace it with the new icon | 358 // If an icon file exists, and is out of date, replace it with the new icon |
282 // and let the shell know the icon has been modified. | 359 // and let the shell know the icon has been modified. |
283 base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( | 360 base::FilePath icon_file = web_app_path.Append(file_name).AddExtension( |
284 FILE_PATH_LITERAL(".ico")); | 361 FILE_PATH_LITERAL(".ico")); |
285 if (file_util::PathExists(icon_file)) { | 362 if (file_util::PathExists(icon_file)) { |
286 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); | 363 web_app::internals::CheckAndSaveIcon(icon_file, shortcut_info.favicon); |
287 } | 364 } |
288 } | 365 } |
289 | 366 |
290 void DeletePlatformShortcuts( | 367 void DeletePlatformShortcuts( |
291 const base::FilePath& web_app_path, | 368 const base::FilePath& web_app_path, |
292 const ShellIntegration::ShortcutInfo& shortcut_info) { | 369 const ShellIntegration::ShortcutInfo& shortcut_info) { |
293 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); | 370 DeleteAndRememberShortcuts(web_app_path, shortcut_info, shortcut_info.title, |
294 | 371 true, NULL); |
295 // Get all possible locations for shortcuts. | |
296 ShellIntegration::ShortcutLocations all_shortcut_locations; | |
297 all_shortcut_locations.in_applications_menu = true; | |
298 all_shortcut_locations.in_quick_launch_bar = true; | |
299 all_shortcut_locations.on_desktop = true; | |
300 // Delete shortcuts from the Chrome Apps subdirectory. | |
301 // This matches the subdir name set by CreateApplicationShortcutView::Accept | |
302 // for Chrome apps (not URL apps, but this function does not apply for them). | |
303 string16 start_menu_subdir = GetAppShortcutsSubdirName(); | |
304 all_shortcut_locations.applications_menu_subdir = start_menu_subdir; | |
305 std::vector<base::FilePath> shortcut_paths = GetShortcutPaths( | |
306 all_shortcut_locations); | |
307 if (base::win::GetVersion() >= base::win::VERSION_WIN7) | |
308 shortcut_paths.push_back(web_app_path); | |
309 | |
310 for (std::vector<base::FilePath>::const_iterator i = shortcut_paths.begin(); | |
311 i != shortcut_paths.end(); ++i) { | |
312 std::vector<base::FilePath> shortcut_files = | |
313 MatchingShortcutsForProfileAndExtension(*i, shortcut_info.profile_path, | |
314 shortcut_info.title); | |
315 for (std::vector<base::FilePath>::const_iterator j = shortcut_files.begin(); | |
316 j != shortcut_files.end(); ++j) { | |
317 // Any shortcut could have been pinned, either by chrome or the user, so | |
318 // they are all unpinned. | |
319 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); | |
320 file_util::Delete(*j, false); | |
321 } | |
322 } | |
323 | 372 |
324 // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. | 373 // If there are no more shortcuts in the Chrome Apps subdirectory, remove it. |
325 base::FilePath chrome_apps_dir; | 374 base::FilePath chrome_apps_dir; |
326 if (PathService::Get(base::DIR_START_MENU, &chrome_apps_dir)) { | 375 if (PathService::Get(base::DIR_START_MENU, &chrome_apps_dir)) { |
327 chrome_apps_dir = chrome_apps_dir.Append(start_menu_subdir); | 376 chrome_apps_dir = chrome_apps_dir.Append(GetAppShortcutsSubdirName()); |
328 if (file_util::IsDirectoryEmpty(chrome_apps_dir)) | 377 if (file_util::IsDirectoryEmpty(chrome_apps_dir)) |
329 file_util::Delete(chrome_apps_dir, false); | 378 file_util::Delete(chrome_apps_dir, false); |
330 } | 379 } |
331 } | 380 } |
332 | 381 |
333 std::vector<base::FilePath> GetShortcutPaths( | 382 std::vector<base::FilePath> GetShortcutPaths( |
334 const ShellIntegration::ShortcutLocations& creation_locations) { | 383 const ShellIntegration::ShortcutLocations& creation_locations) { |
335 // Shortcut paths under which to create shortcuts. | 384 // Shortcut paths under which to create shortcuts. |
336 std::vector<base::FilePath> shortcut_paths; | 385 std::vector<base::FilePath> shortcut_paths; |
337 // Locations to add to shortcut_paths. | 386 // Locations to add to shortcut_paths. |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
376 path = path.Append(locations[i].subdir); | 425 path = path.Append(locations[i].subdir); |
377 shortcut_paths.push_back(path); | 426 shortcut_paths.push_back(path); |
378 } | 427 } |
379 } | 428 } |
380 return shortcut_paths; | 429 return shortcut_paths; |
381 } | 430 } |
382 | 431 |
383 } // namespace internals | 432 } // namespace internals |
384 | 433 |
385 } // namespace web_app | 434 } // namespace web_app |
OLD | NEW |