| 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/string_util.h" |
| 20 #include "base/threading/sequenced_worker_pool.h" | 20 #include "base/threading/sequenced_worker_pool.h" |
| 21 #include "chrome/common/chrome_version_info.h" | 21 #include "chrome/common/chrome_version_info.h" |
| 22 #include "chrome/common/pref_names.h" | 22 #include "chrome/common/pref_names.h" |
| 23 #include "chrome/common/url_constants.h" | 23 #include "chrome/common/url_constants.h" |
| 24 #include "chrome/browser/chromeos/gdata/gdata.pb.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/chromeos/login/user.h" | 27 #include "chrome/browser/chromeos/login/user.h" |
| 27 #include "chrome/browser/chromeos/login/user_manager.h" | 28 #include "chrome/browser/chromeos/login/user_manager.h" |
| 28 #include "chrome/browser/prefs/pref_service.h" | 29 #include "chrome/browser/prefs/pref_service.h" |
| 29 #include "chrome/browser/profiles/profile.h" | 30 #include "chrome/browser/profiles/profile.h" |
| 30 #include "chrome/browser/ui/browser.h" | 31 #include "chrome/browser/ui/browser.h" |
| 31 #include "chrome/browser/ui/browser_finder.h" | 32 #include "chrome/browser/ui/browser_finder.h" |
| 32 #include "content/public/browser/browser_thread.h" | 33 #include "content/public/browser/browser_thread.h" |
| 33 #include "content/public/browser/child_process_security_policy.h" | 34 #include "content/public/browser/child_process_security_policy.h" |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 } | 87 } |
| 87 | 88 |
| 88 void OpenEditURLUIThread(Profile* profile, GURL* edit_url) { | 89 void OpenEditURLUIThread(Profile* profile, GURL* edit_url) { |
| 89 Browser* browser = browser::FindLastActiveWithProfile(profile); | 90 Browser* browser = browser::FindLastActiveWithProfile(profile); |
| 90 if (browser) { | 91 if (browser) { |
| 91 browser->OpenURL(content::OpenURLParams(*edit_url, content::Referrer(), | 92 browser->OpenURL(content::OpenURLParams(*edit_url, content::Referrer(), |
| 92 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); | 93 CURRENT_TAB, content::PAGE_TRANSITION_TYPED, false)); |
| 93 } | 94 } |
| 94 } | 95 } |
| 95 | 96 |
| 97 // Invoked upon completion of GetFileInfoByPathAsync initiated by |
| 98 // InsertGDataCachePathPermissions. |
| 99 void OnGetFileInfoForInsertGDataCachePathsPermissions( |
| 100 Profile* profile, |
| 101 std::vector<std::pair<FilePath, int> >* cache_paths, |
| 102 const base::Closure& callback, |
| 103 base::PlatformFileError error, |
| 104 scoped_ptr<GDataFileProto> file_info) { |
| 105 DCHECK(profile); |
| 106 DCHECK(cache_paths); |
| 107 DCHECK(!callback.is_null()); |
| 108 |
| 109 GDataFileSystem* file_system = GetGDataFileSystem(profile); |
| 110 if (!file_system || error != base::PLATFORM_FILE_OK) { |
| 111 callback.Run(); |
| 112 return; |
| 113 } |
| 114 |
| 115 DCHECK(file_info.get()); |
| 116 std::string resource_id = file_info->gdata_entry().resource_id(); |
| 117 std::string file_md5 = file_info->file_md5(); |
| 118 |
| 119 // We check permissions for raw cache file paths only for read-only |
| 120 // operations (when fileEntry.file() is called), so read only permissions |
| 121 // should be sufficient for all cache paths. For the rest of supported |
| 122 // operations the file access check is done for drive/ paths. |
| 123 cache_paths->push_back(std::make_pair( |
| 124 file_system->GetCacheFilePath(resource_id, file_md5, |
| 125 GDataCache::CACHE_TYPE_PERSISTENT, |
| 126 GDataCache::CACHED_FILE_FROM_SERVER), |
| 127 kReadOnlyFilePermissions)); |
| 128 // TODO(tbarzic): When we start supporting openFile operation, we may have to |
| 129 // change permission for localy modified files to match handler's permissions. |
| 130 cache_paths->push_back(std::make_pair( |
| 131 file_system->GetCacheFilePath(resource_id, file_md5, |
| 132 GDataCache::CACHE_TYPE_PERSISTENT, |
| 133 GDataCache::CACHED_FILE_LOCALLY_MODIFIED), |
| 134 kReadOnlyFilePermissions)); |
| 135 cache_paths->push_back(std::make_pair( |
| 136 file_system->GetCacheFilePath(resource_id, file_md5, |
| 137 GDataCache::CACHE_TYPE_PERSISTENT, |
| 138 GDataCache::CACHED_FILE_MOUNTED), |
| 139 kReadOnlyFilePermissions)); |
| 140 cache_paths->push_back(std::make_pair( |
| 141 file_system->GetCacheFilePath(resource_id, file_md5, |
| 142 GDataCache::CACHE_TYPE_TMP, |
| 143 GDataCache::CACHED_FILE_FROM_SERVER), |
| 144 kReadOnlyFilePermissions)); |
| 145 |
| 146 callback.Run(); |
| 147 } |
| 148 |
| 96 } // namespace | 149 } // namespace |
| 97 | 150 |
| 98 const FilePath& GetGDataMountPointPath() { | 151 const FilePath& GetGDataMountPointPath() { |
| 99 CR_DEFINE_STATIC_LOCAL(FilePath, gdata_mount_path, | 152 CR_DEFINE_STATIC_LOCAL(FilePath, gdata_mount_path, |
| 100 (FilePath::FromUTF8Unsafe(kGDataMountPointPath))); | 153 (FilePath::FromUTF8Unsafe(kGDataMountPointPath))); |
| 101 return gdata_mount_path; | 154 return gdata_mount_path; |
| 102 } | 155 } |
| 103 | 156 |
| 104 const std::string& GetGDataMountPointPathAsString() { | 157 const std::string& GetGDataMountPointPathAsString() { |
| 105 CR_DEFINE_STATIC_LOCAL(std::string, gdata_mount_path_string, | 158 CR_DEFINE_STATIC_LOCAL(std::string, gdata_mount_path_string, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 FilePath extracted; | 277 FilePath extracted; |
| 225 for (size_t i = arraysize(kGDataMountPointPathComponents) - 1; | 278 for (size_t i = arraysize(kGDataMountPointPathComponents) - 1; |
| 226 i < components.size(); ++i) { | 279 i < components.size(); ++i) { |
| 227 extracted = extracted.Append(components[i]); | 280 extracted = extracted.Append(components[i]); |
| 228 } | 281 } |
| 229 return extracted; | 282 return extracted; |
| 230 } | 283 } |
| 231 | 284 |
| 232 void InsertGDataCachePathsPermissions( | 285 void InsertGDataCachePathsPermissions( |
| 233 Profile* profile, | 286 Profile* profile, |
| 234 const FilePath& gdata_path, | 287 scoped_ptr<std::vector<FilePath> > gdata_paths, |
| 235 std::vector<std::pair<FilePath, int> >* cache_paths ) { | 288 std::vector<std::pair<FilePath, int> >* cache_paths, |
| 289 const base::Closure& callback) { |
| 290 DCHECK(profile); |
| 291 DCHECK(gdata_paths.get()); |
| 236 DCHECK(cache_paths); | 292 DCHECK(cache_paths); |
| 293 DCHECK(!callback.is_null()); |
| 237 | 294 |
| 238 GDataFileSystem* file_system = GetGDataFileSystem(profile); | 295 GDataFileSystem* file_system = GetGDataFileSystem(profile); |
| 239 if (!file_system) | 296 if (!file_system || gdata_paths->empty()) { |
| 297 callback.Run(); |
| 240 return; | 298 return; |
| 299 } |
| 241 | 300 |
| 242 GDataFileProperties file_properties; | 301 // Remove one file path entry from the back of the input vector |gdata_paths|. |
| 243 if (!file_system->GetFileInfoByPath(gdata_path, &file_properties)) | 302 FilePath gdata_path = gdata_paths->back(); |
| 244 return; | 303 gdata_paths->pop_back(); |
| 245 | 304 |
| 246 std::string resource_id = file_properties.resource_id; | 305 // Call GetFileInfoByPathAsync() to get file info for |gdata_path| then insert |
| 247 std::string file_md5 = file_properties.file_md5; | 306 // all possible cache paths to the output vector |cache_paths|. |
| 248 | 307 // Note that we can only process one file path at a time. Upon completion |
| 249 // We check permissions for raw cache file paths only for read-only | 308 // of OnGetFileInfoForInsertGDataCachePathsPermissions(), we recursively call |
| 250 // operations (when fileEntry.file() is called), so read only permissions | 309 // InsertGDataCachePathsPermissions() to process the next file path from the |
| 251 // should be sufficient for all cache paths. For the rest of supported | 310 // back of the input vector |gdata_paths| until it is empty. |
| 252 // operations the file access check is done for drive/ paths. | 311 file_system->GetFileInfoByPathAsync( |
| 253 cache_paths->push_back(std::make_pair( | 312 gdata_path, |
| 254 file_system->GetCacheFilePath(resource_id, file_md5, | 313 base::Bind(&OnGetFileInfoForInsertGDataCachePathsPermissions, |
| 255 GDataCache::CACHE_TYPE_PERSISTENT, | 314 profile, |
| 256 GDataCache::CACHED_FILE_FROM_SERVER), | 315 cache_paths, |
| 257 kReadOnlyFilePermissions)); | 316 base::Bind(&InsertGDataCachePathsPermissions, |
| 258 // TODO(tbarzic): When we start supporting openFile operation, we may have to | 317 profile, |
| 259 // change permission for localy modified files to match handler's permissions. | 318 base::Passed(&gdata_paths), |
| 260 cache_paths->push_back(std::make_pair( | 319 cache_paths, |
| 261 file_system->GetCacheFilePath(resource_id, file_md5, | 320 callback))); |
| 262 GDataCache::CACHE_TYPE_PERSISTENT, | |
| 263 GDataCache::CACHED_FILE_LOCALLY_MODIFIED), | |
| 264 kReadOnlyFilePermissions)); | |
| 265 cache_paths->push_back(std::make_pair( | |
| 266 file_system->GetCacheFilePath(resource_id, file_md5, | |
| 267 GDataCache::CACHE_TYPE_PERSISTENT, | |
| 268 GDataCache::CACHED_FILE_MOUNTED), | |
| 269 kReadOnlyFilePermissions)); | |
| 270 cache_paths->push_back(std::make_pair( | |
| 271 file_system->GetCacheFilePath(resource_id, file_md5, | |
| 272 GDataCache::CACHE_TYPE_TMP, | |
| 273 GDataCache::CACHED_FILE_FROM_SERVER), | |
| 274 kReadOnlyFilePermissions)); | |
| 275 } | 321 } |
| 276 | 322 |
| 277 bool IsGDataAvailable(Profile* profile) { | 323 bool IsGDataAvailable(Profile* profile) { |
| 278 if (!chromeos::UserManager::Get()->IsUserLoggedIn() || | 324 if (!chromeos::UserManager::Get()->IsUserLoggedIn() || |
| 279 chromeos::UserManager::Get()->IsLoggedInAsGuest() || | 325 chromeos::UserManager::Get()->IsLoggedInAsGuest() || |
| 280 chromeos::UserManager::Get()->IsLoggedInAsDemoUser()) | 326 chromeos::UserManager::Get()->IsLoggedInAsDemoUser()) |
| 281 return false; | 327 return false; |
| 282 | 328 |
| 283 // Do not allow GData for incognito windows / guest mode. | 329 // Do not allow GData for incognito windows / guest mode. |
| 284 if (profile->IsOffTheRecord()) | 330 if (profile->IsOffTheRecord()) |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 351 // Assign the extracted extensions to md5 and extra_extension. | 397 // Assign the extracted extensions to md5 and extra_extension. |
| 352 int extension_count = extensions.size(); | 398 int extension_count = extensions.size(); |
| 353 *md5 = (extension_count > 0) ? extensions[extension_count - 1] : | 399 *md5 = (extension_count > 0) ? extensions[extension_count - 1] : |
| 354 std::string(); | 400 std::string(); |
| 355 *extra_extension = (extension_count > 1) ? extensions[extension_count - 2] : | 401 *extra_extension = (extension_count > 1) ? extensions[extension_count - 2] : |
| 356 std::string(); | 402 std::string(); |
| 357 } | 403 } |
| 358 | 404 |
| 359 } // namespace util | 405 } // namespace util |
| 360 } // namespace gdata | 406 } // namespace gdata |
| OLD | NEW |