| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/web_applications/web_app.h" |
| 6 |
| 7 #include <shlobj.h> |
| 8 |
| 9 #include "base/command_line.h" |
| 10 #include "base/file_util.h" |
| 11 #include "base/md5.h" |
| 12 #include "base/path_service.h" |
| 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/win/windows_version.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "content/public/browser/browser_thread.h" |
| 18 #include "ui/gfx/icon_util.h" |
| 19 |
| 20 namespace { |
| 21 |
| 22 const FilePath::CharType kIconChecksumFileExt[] = FILE_PATH_LITERAL(".ico.md5"); |
| 23 |
| 24 // Calculates image checksum using MD5. |
| 25 void GetImageCheckSum(const SkBitmap& image, base::MD5Digest* digest) { |
| 26 DCHECK(digest); |
| 27 |
| 28 SkAutoLockPixels image_lock(image); |
| 29 MD5Sum(image.getPixels(), image.getSize(), digest); |
| 30 } |
| 31 |
| 32 // Saves |image| as an |icon_file| with the checksum. |
| 33 bool SaveIconWithCheckSum(const FilePath& icon_file, const SkBitmap& image) { |
| 34 if (!IconUtil::CreateIconFileFromSkBitmap(image, icon_file)) |
| 35 return false; |
| 36 |
| 37 base::MD5Digest digest; |
| 38 GetImageCheckSum(image, &digest); |
| 39 |
| 40 FilePath cheksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt)); |
| 41 return file_util::WriteFile(cheksum_file, |
| 42 reinterpret_cast<const char*>(&digest), |
| 43 sizeof(digest)) == sizeof(digest); |
| 44 } |
| 45 |
| 46 // Returns true if |icon_file| is missing or different from |image|. |
| 47 bool ShouldUpdateIcon(const FilePath& icon_file, const SkBitmap& image) { |
| 48 FilePath checksum_file(icon_file.ReplaceExtension(kIconChecksumFileExt)); |
| 49 |
| 50 // Returns true if icon_file or checksum file is missing. |
| 51 if (!file_util::PathExists(icon_file) || |
| 52 !file_util::PathExists(checksum_file)) |
| 53 return true; |
| 54 |
| 55 base::MD5Digest persisted_image_checksum; |
| 56 if (sizeof(persisted_image_checksum) != file_util::ReadFile(checksum_file, |
| 57 reinterpret_cast<char*>(&persisted_image_checksum), |
| 58 sizeof(persisted_image_checksum))) |
| 59 return true; |
| 60 |
| 61 base::MD5Digest downloaded_image_checksum; |
| 62 GetImageCheckSum(image, &downloaded_image_checksum); |
| 63 |
| 64 // Update icon if checksums are not equal. |
| 65 return memcmp(&persisted_image_checksum, &downloaded_image_checksum, |
| 66 sizeof(base::MD5Digest)) != 0; |
| 67 } |
| 68 |
| 69 } // namespace |
| 70 |
| 71 namespace web_app { |
| 72 |
| 73 namespace internals { |
| 74 |
| 75 // Saves |image| to |icon_file| if the file is outdated and refresh shell's |
| 76 // icon cache to ensure correct icon is displayed. Returns true if icon_file |
| 77 // is up to date or successfully updated. |
| 78 bool CheckAndSaveIcon(const FilePath& icon_file, const SkBitmap& image) { |
| 79 if (ShouldUpdateIcon(icon_file, image)) { |
| 80 if (SaveIconWithCheckSum(icon_file, image)) { |
| 81 // Refresh shell's icon cache. This call is quite disruptive as user would |
| 82 // see explorer rebuilding the icon cache. It would be great that we find |
| 83 // a better way to achieve this. |
| 84 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST | SHCNF_FLUSHNOWAIT, |
| 85 NULL, NULL); |
| 86 } else { |
| 87 return false; |
| 88 } |
| 89 } |
| 90 |
| 91 return true; |
| 92 } |
| 93 |
| 94 void CreateShortcutTask(const FilePath& web_app_path, |
| 95 const FilePath& profile_path, |
| 96 const ShellIntegration::ShortcutInfo& shortcut_info) { |
| 97 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::FILE)); |
| 98 |
| 99 // Shortcut paths under which to create shortcuts. |
| 100 std::vector<FilePath> shortcut_paths; |
| 101 |
| 102 // Locations to add to shortcut_paths. |
| 103 struct { |
| 104 const bool& use_this_location; |
| 105 int location_id; |
| 106 const wchar_t* sub_dir; |
| 107 } locations[] = { |
| 108 { |
| 109 shortcut_info.create_on_desktop, |
| 110 chrome::DIR_USER_DESKTOP, |
| 111 NULL |
| 112 }, { |
| 113 shortcut_info.create_in_applications_menu, |
| 114 base::DIR_START_MENU, |
| 115 NULL |
| 116 }, { |
| 117 shortcut_info.create_in_quick_launch_bar, |
| 118 // For Win7, create_in_quick_launch_bar means pinning to taskbar. Use |
| 119 // base::PATH_START as a flag for this case. |
| 120 (base::win::GetVersion() >= base::win::VERSION_WIN7) ? |
| 121 base::PATH_START : base::DIR_APP_DATA, |
| 122 (base::win::GetVersion() >= base::win::VERSION_WIN7) ? |
| 123 NULL : L"Microsoft\\Internet Explorer\\Quick Launch" |
| 124 } |
| 125 }; |
| 126 |
| 127 // Populate shortcut_paths. |
| 128 for (int i = 0; i < arraysize(locations); ++i) { |
| 129 if (locations[i].use_this_location) { |
| 130 FilePath path; |
| 131 |
| 132 // Skip the Win7 case. |
| 133 if (locations[i].location_id == base::PATH_START) |
| 134 continue; |
| 135 |
| 136 if (!PathService::Get(locations[i].location_id, &path)) { |
| 137 return; |
| 138 } |
| 139 |
| 140 if (locations[i].sub_dir != NULL) |
| 141 path = path.Append(locations[i].sub_dir); |
| 142 |
| 143 shortcut_paths.push_back(path); |
| 144 } |
| 145 } |
| 146 |
| 147 bool pin_to_taskbar = |
| 148 shortcut_info.create_in_quick_launch_bar && |
| 149 (base::win::GetVersion() >= base::win::VERSION_WIN7); |
| 150 |
| 151 // For Win7's pinning support, any shortcut could be used. So we only create |
| 152 // the shortcut file when there is no shortcut file will be created. That is, |
| 153 // user only selects "Pin to taskbar". |
| 154 if (pin_to_taskbar && shortcut_paths.empty()) { |
| 155 // Creates the shortcut in web_app_path in this case. |
| 156 shortcut_paths.push_back(web_app_path); |
| 157 } |
| 158 |
| 159 if (shortcut_paths.empty()) { |
| 160 return; |
| 161 } |
| 162 |
| 163 // Ensure web_app_path exists. |
| 164 if (!file_util::PathExists(web_app_path) && |
| 165 !file_util::CreateDirectory(web_app_path)) { |
| 166 return; |
| 167 } |
| 168 |
| 169 // Generates file name to use with persisted ico and shortcut file. |
| 170 FilePath file_name = |
| 171 web_app::internals::GetSanitizedFileName(shortcut_info.title); |
| 172 |
| 173 // Creates an ico file to use with shortcut. |
| 174 FilePath icon_file = web_app_path.Append(file_name).ReplaceExtension( |
| 175 FILE_PATH_LITERAL(".ico")); |
| 176 if (!web_app::internals::CheckAndSaveIcon(icon_file, |
| 177 shortcut_info.favicon)) { |
| 178 return; |
| 179 } |
| 180 |
| 181 FilePath chrome_exe; |
| 182 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) { |
| 183 return; |
| 184 } |
| 185 |
| 186 // Working directory. |
| 187 FilePath chrome_folder = chrome_exe.DirName(); |
| 188 |
| 189 CommandLine cmd_line = |
| 190 ShellIntegration::CommandLineArgsForLauncher(shortcut_info.url, |
| 191 shortcut_info.extension_id); |
| 192 // TODO(evan): we rely on the fact that command_line_string() is |
| 193 // properly quoted for a Windows command line. The method on |
| 194 // CommandLine should probably be renamed to better reflect that |
| 195 // fact. |
| 196 string16 wide_switches(cmd_line.GetCommandLineString()); |
| 197 |
| 198 // Sanitize description |
| 199 string16 description = shortcut_info.description; |
| 200 if (description.length() >= MAX_PATH) |
| 201 description.resize(MAX_PATH - 1); |
| 202 |
| 203 // Generates app id from web app url and profile path. |
| 204 std::string app_name = |
| 205 web_app::GenerateApplicationNameFromInfo(shortcut_info); |
| 206 string16 app_id = ShellIntegration::GetAppId( |
| 207 UTF8ToUTF16(app_name), profile_path); |
| 208 |
| 209 FilePath shortcut_to_pin; |
| 210 |
| 211 bool success = true; |
| 212 for (size_t i = 0; i < shortcut_paths.size(); ++i) { |
| 213 FilePath shortcut_file = shortcut_paths[i].Append(file_name). |
| 214 ReplaceExtension(FILE_PATH_LITERAL(".lnk")); |
| 215 |
| 216 int unique_number = |
| 217 file_util::GetUniquePathNumber(shortcut_file, FILE_PATH_LITERAL("")); |
| 218 if (unique_number == -1) { |
| 219 success = false; |
| 220 continue; |
| 221 } else if (unique_number > 0) { |
| 222 shortcut_file = shortcut_file.InsertBeforeExtensionASCII( |
| 223 StringPrintf(" (%d)", unique_number)); |
| 224 } |
| 225 |
| 226 success &= file_util::CreateShortcutLink(chrome_exe.value().c_str(), |
| 227 shortcut_file.value().c_str(), |
| 228 chrome_folder.value().c_str(), |
| 229 wide_switches.c_str(), |
| 230 description.c_str(), |
| 231 icon_file.value().c_str(), |
| 232 0, |
| 233 app_id.c_str()); |
| 234 |
| 235 // Any shortcut would work for the pinning. We use the first one. |
| 236 if (success && pin_to_taskbar && shortcut_to_pin.empty()) |
| 237 shortcut_to_pin = shortcut_file; |
| 238 } |
| 239 |
| 240 if (success && pin_to_taskbar) { |
| 241 if (!shortcut_to_pin.empty()) { |
| 242 success &= file_util::TaskbarPinShortcutLink( |
| 243 shortcut_to_pin.value().c_str()); |
| 244 } else { |
| 245 success = false; |
| 246 } |
| 247 } |
| 248 } |
| 249 |
| 250 } // namespace internals |
| 251 |
| 252 } // namespace web_app |
| OLD | NEW |