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 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 } | 130 } |
131 | 131 |
132 // Creates application shortcuts in a given set of paths. | 132 // Creates application shortcuts in a given set of paths. |
133 // |shortcut_paths| is a list of directories in which shortcuts should be | 133 // |shortcut_paths| is a list of directories in which shortcuts should be |
134 // created. | 134 // created. |
135 // Returns true on success, false on failure. | 135 // Returns true on success, false on failure. |
136 // Must be called on the FILE thread. | 136 // Must be called on the FILE thread. |
137 bool CreateShortcutsInPaths( | 137 bool CreateShortcutsInPaths( |
138 const base::FilePath& web_app_path, | 138 const base::FilePath& web_app_path, |
139 const ShellIntegration::ShortcutInfo& shortcut_info, | 139 const ShellIntegration::ShortcutInfo& shortcut_info, |
140 const std::vector<base::FilePath>& shortcut_paths) { | 140 const std::vector<base::FilePath>& shortcut_paths, |
| 141 std::vector<base::FilePath>* out_filenames) { |
141 // Ensure web_app_path exists. | 142 // Ensure web_app_path exists. |
142 if (!file_util::PathExists(web_app_path) && | 143 if (!file_util::PathExists(web_app_path) && |
143 !file_util::CreateDirectory(web_app_path)) { | 144 !file_util::CreateDirectory(web_app_path)) { |
144 return false; | 145 return false; |
145 } | 146 } |
146 | 147 |
147 // Generates file name to use with persisted ico and shortcut file. | 148 // Generates file name to use with persisted ico and shortcut file. |
148 base::FilePath file_name = | 149 base::FilePath file_name = |
149 web_app::internals::GetSanitizedFileName(shortcut_info.title); | 150 web_app::internals::GetSanitizedFileName(shortcut_info.title); |
150 | 151 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 shortcut_properties.set_app_id(app_id); | 209 shortcut_properties.set_app_id(app_id); |
209 shortcut_properties.set_dual_mode(false); | 210 shortcut_properties.set_dual_mode(false); |
210 if (!file_util::PathExists(shortcut_file.DirName()) && | 211 if (!file_util::PathExists(shortcut_file.DirName()) && |
211 !file_util::CreateDirectory(shortcut_file.DirName())) { | 212 !file_util::CreateDirectory(shortcut_file.DirName())) { |
212 NOTREACHED(); | 213 NOTREACHED(); |
213 return false; | 214 return false; |
214 } | 215 } |
215 success = base::win::CreateOrUpdateShortcutLink( | 216 success = base::win::CreateOrUpdateShortcutLink( |
216 shortcut_file, shortcut_properties, | 217 shortcut_file, shortcut_properties, |
217 base::win::SHORTCUT_CREATE_ALWAYS) && success; | 218 base::win::SHORTCUT_CREATE_ALWAYS) && success; |
| 219 if (out_filenames) |
| 220 out_filenames->push_back(shortcut_file); |
218 } | 221 } |
219 | 222 |
220 return success; | 223 return success; |
221 } | 224 } |
222 | 225 |
223 // Gets the directories with shortcuts for an app, and deletes the shortcuts. | 226 // Gets the directories with shortcuts for an app, and deletes the shortcuts. |
224 // This will search the standard locations for shortcuts named |title| that open | 227 // This will search the standard locations for shortcuts named |title| that open |
225 // in the profile with |profile_path|. | 228 // in the profile with |profile_path|. |
226 // |was_pinned_to_taskbar| will be set to true if there was previously a | 229 // |was_pinned_to_taskbar| will be set to true if there was previously a |
227 // shortcut pinned to the taskbar for this app; false otherwise. | 230 // shortcut pinned to the taskbar for this app; false otherwise. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
279 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); | 282 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); |
280 file_util::Delete(*j, false); | 283 file_util::Delete(*j, false); |
281 } | 284 } |
282 } | 285 } |
283 } | 286 } |
284 | 287 |
285 } // namespace | 288 } // namespace |
286 | 289 |
287 namespace web_app { | 290 namespace web_app { |
288 | 291 |
| 292 base::FilePath CreateShortcutInWebAppDir( |
| 293 const base::FilePath& web_app_dir, |
| 294 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 295 std::vector<base::FilePath> paths; |
| 296 paths.push_back(web_app_dir); |
| 297 std::vector<base::FilePath> out_filenames; |
| 298 CreateShortcutsInPaths(web_app_dir, shortcut_info, paths, &out_filenames); |
| 299 DCHECK_EQ(out_filenames.size(), 1u); |
| 300 return out_filenames[0]; |
| 301 } |
| 302 |
289 namespace internals { | 303 namespace internals { |
290 | 304 |
291 // Saves |image| to |icon_file| if the file is outdated and refresh shell's | 305 // Saves |image| to |icon_file| if the file is outdated and refresh shell's |
292 // icon cache to ensure correct icon is displayed. Returns true if icon_file | 306 // icon cache to ensure correct icon is displayed. Returns true if icon_file |
293 // is up to date or successfully updated. | 307 // is up to date or successfully updated. |
294 bool CheckAndSaveIcon(const base::FilePath& icon_file, | 308 bool CheckAndSaveIcon(const base::FilePath& icon_file, |
295 const gfx::ImageFamily& image) { | 309 const gfx::ImageFamily& image) { |
296 if (ShouldUpdateIcon(icon_file, image)) { | 310 if (ShouldUpdateIcon(icon_file, image)) { |
297 if (SaveIconWithCheckSum(icon_file, image)) { | 311 if (SaveIconWithCheckSum(icon_file, image)) { |
298 // Refresh shell's icon cache. This call is quite disruptive as user would | 312 // Refresh shell's icon cache. This call is quite disruptive as user would |
(...skipping 25 matching lines...) Expand all Loading... |
324 // Create/update the shortcut in the web app path for the "Pin To Taskbar" | 338 // Create/update the shortcut in the web app path for the "Pin To Taskbar" |
325 // option in Win7. We use the web app path shortcut because we will overwrite | 339 // option in Win7. We use the web app path shortcut because we will overwrite |
326 // it rather than appending unique numbers if the shortcut already exists. | 340 // it rather than appending unique numbers if the shortcut already exists. |
327 // This prevents pinned apps from having unique numbers in their names. | 341 // This prevents pinned apps from having unique numbers in their names. |
328 if (pin_to_taskbar) | 342 if (pin_to_taskbar) |
329 shortcut_paths.push_back(web_app_path); | 343 shortcut_paths.push_back(web_app_path); |
330 | 344 |
331 if (shortcut_paths.empty()) | 345 if (shortcut_paths.empty()) |
332 return false; | 346 return false; |
333 | 347 |
334 if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths)) | 348 if (!CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths, |
| 349 NULL)) |
335 return false; | 350 return false; |
336 | 351 |
337 if (pin_to_taskbar) { | 352 if (pin_to_taskbar) { |
338 base::FilePath file_name = | 353 base::FilePath file_name = |
339 web_app::internals::GetSanitizedFileName(shortcut_info.title); | 354 web_app::internals::GetSanitizedFileName(shortcut_info.title); |
340 // Use the web app path shortcut for pinning to avoid having unique numbers | 355 // Use the web app path shortcut for pinning to avoid having unique numbers |
341 // in the application name. | 356 // in the application name. |
342 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). | 357 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). |
343 AddExtension(installer::kLnkExt); | 358 AddExtension(installer::kLnkExt); |
344 if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) | 359 if (!base::win::TaskbarPinShortcutLink(shortcut_to_pin.value().c_str())) |
(...skipping 15 matching lines...) Expand all Loading... |
360 | 375 |
361 if (old_app_title != shortcut_info.title) { | 376 if (old_app_title != shortcut_info.title) { |
362 // The app's title has changed. Delete all existing app shortcuts and | 377 // The app's title has changed. Delete all existing app shortcuts and |
363 // recreate them in any locations they already existed (but do not add them | 378 // recreate them in any locations they already existed (but do not add them |
364 // to locations where they do not currently exist). | 379 // to locations where they do not currently exist). |
365 bool was_pinned_to_taskbar; | 380 bool was_pinned_to_taskbar; |
366 std::vector<base::FilePath> shortcut_paths; | 381 std::vector<base::FilePath> shortcut_paths; |
367 GetShortcutLocationsAndDeleteShortcuts( | 382 GetShortcutLocationsAndDeleteShortcuts( |
368 web_app_path, shortcut_info.profile_path, old_app_title, | 383 web_app_path, shortcut_info.profile_path, old_app_title, |
369 &was_pinned_to_taskbar, &shortcut_paths); | 384 &was_pinned_to_taskbar, &shortcut_paths); |
370 CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths); | 385 CreateShortcutsInPaths(web_app_path, shortcut_info, shortcut_paths, NULL); |
371 // If the shortcut was pinned to the taskbar, | 386 // If the shortcut was pinned to the taskbar, |
372 // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that | 387 // GetShortcutLocationsAndDeleteShortcuts will have deleted it. In that |
373 // case, re-pin it. | 388 // case, re-pin it. |
374 if (was_pinned_to_taskbar) { | 389 if (was_pinned_to_taskbar) { |
375 base::FilePath file_name = | 390 base::FilePath file_name = |
376 web_app::internals::GetSanitizedFileName(shortcut_info.title); | 391 web_app::internals::GetSanitizedFileName(shortcut_info.title); |
377 // Use the web app path shortcut for pinning to avoid having unique | 392 // Use the web app path shortcut for pinning to avoid having unique |
378 // numbers in the application name. | 393 // numbers in the application name. |
379 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). | 394 base::FilePath shortcut_to_pin = web_app_path.Append(file_name). |
380 AddExtension(installer::kLnkExt); | 395 AddExtension(installer::kLnkExt); |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
453 path = path.Append(locations[i].subdir); | 468 path = path.Append(locations[i].subdir); |
454 shortcut_paths.push_back(path); | 469 shortcut_paths.push_back(path); |
455 } | 470 } |
456 } | 471 } |
457 return shortcut_paths; | 472 return shortcut_paths; |
458 } | 473 } |
459 | 474 |
460 } // namespace internals | 475 } // namespace internals |
461 | 476 |
462 } // namespace web_app | 477 } // namespace web_app |
OLD | NEW |