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/chromeos/gdata/gdata_util.h" | 5 #include "chrome/browser/chromeos/gdata/gdata_util.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/bind_helpers.h" | 13 #include "base/bind_helpers.h" |
14 #include "base/file_path.h" | 14 #include "base/file_path.h" |
15 #include "base/file_util.h" | 15 #include "base/file_util.h" |
16 #include "base/json/json_reader.h" | 16 #include "base/json/json_reader.h" |
17 #include "base/logging.h" | 17 #include "base/logging.h" |
18 #include "base/stringprintf.h" | 18 #include "base/stringprintf.h" |
| 19 #include "base/string_util.h" |
19 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
20 #include "chrome/common/chrome_version_info.h" | 21 #include "chrome/common/chrome_version_info.h" |
21 #include "chrome/common/libxml_utils.h" | 22 #include "chrome/common/libxml_utils.h" |
22 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
23 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
24 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" | 25 #include "chrome/browser/chromeos/gdata/gdata_file_system.h" |
25 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" | 26 #include "chrome/browser/chromeos/gdata/gdata_system_service.h" |
26 #include "chrome/browser/prefs/pref_service.h" | 27 #include "chrome/browser/prefs/pref_service.h" |
27 #include "chrome/browser/profiles/profile.h" | 28 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/ui/browser.h" | 29 #include "chrome/browser/ui/browser.h" |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 kReadOnlyFilePermissions)); | 215 kReadOnlyFilePermissions)); |
215 // TODO(tbarzic): When we start supporting openFile operation, we may have to | 216 // TODO(tbarzic): When we start supporting openFile operation, we may have to |
216 // change permission for localy modified files to match handler's permissions. | 217 // change permission for localy modified files to match handler's permissions. |
217 cache_paths->push_back(std::make_pair( | 218 cache_paths->push_back(std::make_pair( |
218 file_system->GetCacheFilePath(resource_id, file_md5, | 219 file_system->GetCacheFilePath(resource_id, file_md5, |
219 GDataRootDirectory::CACHE_TYPE_PERSISTENT, | 220 GDataRootDirectory::CACHE_TYPE_PERSISTENT, |
220 GDataFileSystem::CACHED_FILE_LOCALLY_MODIFIED), | 221 GDataFileSystem::CACHED_FILE_LOCALLY_MODIFIED), |
221 kReadOnlyFilePermissions)); | 222 kReadOnlyFilePermissions)); |
222 cache_paths->push_back(std::make_pair( | 223 cache_paths->push_back(std::make_pair( |
223 file_system->GetCacheFilePath(resource_id, file_md5, | 224 file_system->GetCacheFilePath(resource_id, file_md5, |
| 225 GDataRootDirectory::CACHE_TYPE_PERSISTENT, |
| 226 GDataFileSystem::CACHED_FILE_MOUNTED), |
| 227 kReadOnlyFilePermissions)); |
| 228 cache_paths->push_back(std::make_pair( |
| 229 file_system->GetCacheFilePath(resource_id, file_md5, |
224 GDataRootDirectory::CACHE_TYPE_TMP, | 230 GDataRootDirectory::CACHE_TYPE_TMP, |
225 GDataFileSystem::CACHED_FILE_FROM_SERVER), | 231 GDataFileSystem::CACHED_FILE_FROM_SERVER), |
226 kReadOnlyFilePermissions)); | 232 kReadOnlyFilePermissions)); |
227 | 233 |
228 } | 234 } |
229 | 235 |
230 void SetPermissionsForGDataCacheFiles(Profile* profile, | 236 void SetPermissionsForGDataCacheFiles(Profile* profile, |
231 int pid, | 237 int pid, |
232 const FilePath& path) { | 238 const FilePath& path) { |
233 std::vector<std::pair<FilePath, int> > cache_paths; | 239 std::vector<std::pair<FilePath, int> > cache_paths; |
(...skipping 18 matching lines...) Expand all Loading... |
252 | 258 |
253 // Disable gdata if preference is set. This can happen with commandline flag | 259 // Disable gdata if preference is set. This can happen with commandline flag |
254 // --disable-gdata or enterprise policy, or probably with user settings too | 260 // --disable-gdata or enterprise policy, or probably with user settings too |
255 // in the future. | 261 // in the future. |
256 if (profile->GetPrefs()->GetBoolean(prefs::kDisableGData)) | 262 if (profile->GetPrefs()->GetBoolean(prefs::kDisableGData)) |
257 return false; | 263 return false; |
258 | 264 |
259 return true; | 265 return true; |
260 } | 266 } |
261 | 267 |
| 268 std::string EscapeCacheFileName(const std::string& filename) { |
| 269 // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape |
| 270 std::string escaped; |
| 271 for (size_t i = 0; i < filename.size(); ++i) { |
| 272 char c = filename[i]; |
| 273 if (c == '%' || c == '.' || c == '/') { |
| 274 base::StringAppendF(&escaped, "%%%02X", c); |
| 275 } else { |
| 276 escaped.push_back(c); |
| 277 } |
| 278 } |
| 279 return escaped; |
| 280 } |
| 281 |
| 282 std::string UnescapeCacheFileName(const std::string& filename) { |
| 283 std::string unescaped; |
| 284 for (size_t i = 0; i < filename.size(); ++i) { |
| 285 char c = filename[i]; |
| 286 if (c == '%' && i + 2 < filename.length()) { |
| 287 c = (HexDigitToInt(filename[i + 1]) << 4) + |
| 288 HexDigitToInt(filename[i + 2]); |
| 289 i += 2; |
| 290 } |
| 291 unescaped.push_back(c); |
| 292 } |
| 293 return unescaped; |
| 294 } |
| 295 |
| 296 void ParseCacheFilePath(const FilePath& path, |
| 297 std::string* resource_id, |
| 298 std::string* md5, |
| 299 std::string* extra_extension) { |
| 300 DCHECK(resource_id); |
| 301 DCHECK(md5); |
| 302 DCHECK(extra_extension); |
| 303 |
| 304 // Extract up to two extensions from the right. |
| 305 FilePath base_name = path.BaseName(); |
| 306 const int kNumExtensionsToExtract = 2; |
| 307 std::vector<FilePath::StringType> extensions; |
| 308 for (int i = 0; i < kNumExtensionsToExtract; ++i) { |
| 309 FilePath::StringType extension = base_name.Extension(); |
| 310 if (!extension.empty()) { |
| 311 // FilePath::Extension returns ".", so strip it. |
| 312 extension = UnescapeCacheFileName(extension.substr(1)); |
| 313 base_name = base_name.RemoveExtension(); |
| 314 extensions.push_back(extension); |
| 315 } else { |
| 316 break; |
| 317 } |
| 318 } |
| 319 |
| 320 // The base_name here is already stripped of extensions in the loop above. |
| 321 *resource_id = UnescapeCacheFileName(base_name.value()); |
| 322 |
| 323 // Assign the extracted extensions to md5 and extra_extension. |
| 324 int extension_count = extensions.size(); |
| 325 *md5 = (extension_count > 0) ? extensions[extension_count - 1] : |
| 326 std::string(); |
| 327 *extra_extension = (extension_count > 1) ? extensions[extension_count - 2] : |
| 328 std::string(); |
| 329 } |
| 330 |
262 } // namespace util | 331 } // namespace util |
263 } // namespace gdata | 332 } // namespace gdata |
OLD | NEW |