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" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/md5.h" | 12 #include "base/md5.h" |
13 #include "base/path_service.h" | 13 #include "base/path_service.h" |
14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "base/win/shortcut.h" |
16 #include "base/win/windows_version.h" | 17 #include "base/win/windows_version.h" |
17 #include "chrome/common/chrome_paths.h" | 18 #include "chrome/common/chrome_paths.h" |
18 #include "chrome/common/chrome_switches.h" | 19 #include "chrome/common/chrome_switches.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
20 #include "ui/gfx/icon_util.h" | 21 #include "ui/gfx/icon_util.h" |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 const FilePath::CharType kIconChecksumFileExt[] = FILE_PATH_LITERAL(".ico.md5"); | 25 const FilePath::CharType kIconChecksumFileExt[] = FILE_PATH_LITERAL(".ico.md5"); |
25 | 26 |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 shortcut_paths.push_back(path); | 118 shortcut_paths.push_back(path); |
118 } | 119 } |
119 } | 120 } |
120 | 121 |
121 return shortcut_paths; | 122 return shortcut_paths; |
122 } | 123 } |
123 | 124 |
124 bool ShortcutIsForProfile(const FilePath& shortcut_file_name, | 125 bool ShortcutIsForProfile(const FilePath& shortcut_file_name, |
125 const FilePath& profile_path) { | 126 const FilePath& profile_path) { |
126 string16 cmd_line_string; | 127 string16 cmd_line_string; |
127 if (file_util::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) { | 128 if (base::win::ResolveShortcut(shortcut_file_name, NULL, &cmd_line_string)) { |
128 cmd_line_string = L"program " + cmd_line_string; | 129 cmd_line_string = L"program " + cmd_line_string; |
129 CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string); | 130 CommandLine shortcut_cmd_line = CommandLine::FromString(cmd_line_string); |
130 return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) && | 131 return shortcut_cmd_line.HasSwitch(switches::kProfileDirectory) && |
131 shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) == | 132 shortcut_cmd_line.GetSwitchValuePath(switches::kProfileDirectory) == |
132 profile_path.BaseName(); | 133 profile_path.BaseName(); |
133 } | 134 } |
134 | 135 |
135 return false; | 136 return false; |
136 } | 137 } |
137 | 138 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 if (!web_app::internals::CheckAndSaveIcon(icon_file, | 223 if (!web_app::internals::CheckAndSaveIcon(icon_file, |
223 *shortcut_info.favicon.ToSkBitmap())) { | 224 *shortcut_info.favicon.ToSkBitmap())) { |
224 return false; | 225 return false; |
225 } | 226 } |
226 | 227 |
227 FilePath chrome_exe; | 228 FilePath chrome_exe; |
228 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) | 229 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) |
229 return false; | 230 return false; |
230 | 231 |
231 // Working directory. | 232 // Working directory. |
232 FilePath chrome_folder = chrome_exe.DirName(); | 233 FilePath chrome_folder(chrome_exe.DirName()); |
233 | 234 |
234 CommandLine cmd_line(CommandLine::NO_PROGRAM); | 235 CommandLine cmd_line(CommandLine::NO_PROGRAM); |
235 cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, | 236 cmd_line = ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, |
236 shortcut_info.extension_id, shortcut_info.profile_path); | 237 shortcut_info.extension_id, shortcut_info.profile_path); |
237 | 238 |
238 // TODO(evan): we rely on the fact that command_line_string() is | 239 // TODO(evan): we rely on the fact that command_line_string() is |
239 // properly quoted for a Windows command line. The method on | 240 // properly quoted for a Windows command line. The method on |
240 // CommandLine should probably be renamed to better reflect that | 241 // CommandLine should probably be renamed to better reflect that |
241 // fact. | 242 // fact. |
242 string16 wide_switches(cmd_line.GetCommandLineString()); | 243 string16 wide_switches(cmd_line.GetCommandLineString()); |
243 | 244 |
244 // Sanitize description | 245 // Sanitize description |
245 string16 description = shortcut_info.description; | 246 string16 description = shortcut_info.description; |
246 if (description.length() >= MAX_PATH) | 247 if (description.length() >= MAX_PATH) |
247 description.resize(MAX_PATH - 1); | 248 description.resize(MAX_PATH - 1); |
248 | 249 |
249 // Generates app id from web app url and profile path. | 250 // Generates app id from web app url and profile path. |
250 std::string app_name = | 251 std::string app_name(web_app::GenerateApplicationNameFromInfo(shortcut_info)); |
251 web_app::GenerateApplicationNameFromInfo(shortcut_info); | 252 string16 app_id(ShellIntegration::GetAppModelIdForProfile( |
252 string16 app_id = ShellIntegration::GetAppModelIdForProfile( | 253 UTF8ToUTF16(app_name), shortcut_info.profile_path)); |
253 UTF8ToUTF16(app_name), shortcut_info.profile_path); | |
254 | 254 |
255 FilePath shortcut_to_pin; | 255 FilePath shortcut_to_pin; |
256 bool success = true; | 256 bool success = true; |
257 for (size_t i = 0; i < shortcut_paths.size(); ++i) { | 257 for (size_t i = 0; i < shortcut_paths.size(); ++i) { |
258 FilePath shortcut_file = shortcut_paths[i].Append(file_name). | 258 FilePath shortcut_file = shortcut_paths[i].Append(file_name). |
259 ReplaceExtension(FILE_PATH_LITERAL(".lnk")); | 259 ReplaceExtension(FILE_PATH_LITERAL(".lnk")); |
260 | 260 |
261 int unique_number = | 261 int unique_number = |
262 file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL("")); | 262 file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL("")); |
263 if (unique_number == -1) { | 263 if (unique_number == -1) { |
264 success = false; | 264 success = false; |
265 continue; | 265 continue; |
266 } else if (unique_number > 0) { | 266 } else if (unique_number > 0) { |
267 shortcut_file = shortcut_file.InsertBeforeExtensionASCII( | 267 shortcut_file = shortcut_file.InsertBeforeExtensionASCII( |
268 StringPrintf(" (%d)", unique_number)); | 268 StringPrintf(" (%d)", unique_number)); |
269 } | 269 } |
270 | 270 |
271 success = file_util::CreateOrUpdateShortcutLink( | 271 base::win::ShortcutProperties shortcut_properties; |
272 chrome_exe.value().c_str(), | 272 shortcut_properties.set_target(chrome_exe); |
273 shortcut_file.value().c_str(), | 273 shortcut_properties.set_working_dir(chrome_folder); |
274 chrome_folder.value().c_str(), | 274 shortcut_properties.set_arguments(wide_switches); |
275 wide_switches.c_str(), | 275 shortcut_properties.set_description(description); |
276 description.c_str(), | 276 shortcut_properties.set_icon(icon_file, 0); |
277 icon_file.value().c_str(), | 277 shortcut_properties.set_app_id(app_id); |
278 0, | 278 shortcut_properties.set_dual_mode(false); |
279 app_id.c_str(), | 279 success = base::win::CreateOrUpdateShortcutLink( |
280 file_util::SHORTCUT_CREATE_ALWAYS) && success; | 280 shortcut_file, shortcut_properties, |
| 281 base::win::SHORTCUT_CREATE_ALWAYS) && success; |
281 | 282 |
282 // Any shortcut would work for the pinning. We use the first one. | 283 // Any shortcut would work for the pinning. We use the first one. |
283 if (success && pin_to_taskbar && shortcut_to_pin.empty()) | 284 if (success && pin_to_taskbar && shortcut_to_pin.empty()) |
284 shortcut_to_pin = shortcut_file; | 285 shortcut_to_pin = shortcut_file; |
285 } | 286 } |
286 | 287 |
287 if (success && pin_to_taskbar) { | 288 if (success && pin_to_taskbar) { |
288 if (!shortcut_to_pin.empty()) { | 289 if (!shortcut_to_pin.empty()) { |
289 success &= file_util::TaskbarPinShortcutLink( | 290 success &= base::win::TaskbarPinShortcutLink( |
290 shortcut_to_pin.value().c_str()); | 291 shortcut_to_pin.value().c_str()); |
291 } else { | 292 } else { |
292 success = false; | 293 success = false; |
293 } | 294 } |
294 } | 295 } |
295 | 296 |
296 return success; | 297 return success; |
297 } | 298 } |
298 | 299 |
299 void DeletePlatformShortcuts( | 300 void DeletePlatformShortcuts( |
(...skipping 13 matching lines...) Expand all Loading... |
313 | 314 |
314 for (std::vector<FilePath>::const_iterator i = shortcut_locations.begin(); | 315 for (std::vector<FilePath>::const_iterator i = shortcut_locations.begin(); |
315 i != shortcut_locations.end(); ++i) { | 316 i != shortcut_locations.end(); ++i) { |
316 std::vector<FilePath> shortcut_files = | 317 std::vector<FilePath> shortcut_files = |
317 MatchingShortcutsForProfileAndExtension(*i, shortcut_info.profile_path, | 318 MatchingShortcutsForProfileAndExtension(*i, shortcut_info.profile_path, |
318 shortcut_info.title); | 319 shortcut_info.title); |
319 for (std::vector<FilePath>::const_iterator j = shortcut_files.begin(); | 320 for (std::vector<FilePath>::const_iterator j = shortcut_files.begin(); |
320 j != shortcut_files.end(); ++j) { | 321 j != shortcut_files.end(); ++j) { |
321 // Any shortcut could have been pinned, either by chrome or the user, so | 322 // Any shortcut could have been pinned, either by chrome or the user, so |
322 // they are all unpinned. | 323 // they are all unpinned. |
323 file_util::TaskbarUnpinShortcutLink(j->value().c_str()); | 324 base::win::TaskbarUnpinShortcutLink(j->value().c_str()); |
324 file_util::Delete(*j, false); | 325 file_util::Delete(*j, false); |
325 } | 326 } |
326 } | 327 } |
327 } | 328 } |
328 | 329 |
329 } // namespace internals | 330 } // namespace internals |
330 | 331 |
331 } // namespace web_app | 332 } // namespace web_app |
OLD | NEW |