| 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 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 base::PLATFORM_FILE_READ | | 60 base::PLATFORM_FILE_READ | |
| 61 base::PLATFORM_FILE_EXCLUSIVE_READ | | 61 base::PLATFORM_FILE_EXCLUSIVE_READ | |
| 62 base::PLATFORM_FILE_ASYNC; | 62 base::PLATFORM_FILE_ASYNC; |
| 63 | 63 |
| 64 GDataFileSystemInterface* GetGDataFileSystem(Profile* profile) { | 64 GDataFileSystemInterface* GetGDataFileSystem(Profile* profile) { |
| 65 GDataSystemService* system_service = | 65 GDataSystemService* system_service = |
| 66 GDataSystemServiceFactory::GetForProfile(profile); | 66 GDataSystemServiceFactory::GetForProfile(profile); |
| 67 return system_service ? system_service->file_system() : NULL; | 67 return system_service ? system_service->file_system() : NULL; |
| 68 } | 68 } |
| 69 | 69 |
| 70 GDataCache* GetGDataCache(Profile* profile) { | 70 DriveCache* GetDriveCache(Profile* profile) { |
| 71 GDataSystemService* system_service = | 71 GDataSystemService* system_service = |
| 72 GDataSystemServiceFactory::GetForProfile(profile); | 72 GDataSystemServiceFactory::GetForProfile(profile); |
| 73 return system_service ? system_service->cache() : NULL; | 73 return system_service ? system_service->cache() : NULL; |
| 74 } | 74 } |
| 75 | 75 |
| 76 FileWriteHelper* GetFileWriteHelper(Profile* profile) { | 76 FileWriteHelper* GetFileWriteHelper(Profile* profile) { |
| 77 GDataSystemService* system_service = | 77 GDataSystemService* system_service = |
| 78 GDataSystemServiceFactory::GetForProfile(profile); | 78 GDataSystemServiceFactory::GetForProfile(profile); |
| 79 return system_service ? system_service->file_write_helper() : NULL; | 79 return system_service ? system_service->file_write_helper() : NULL; |
| 80 } | 80 } |
| 81 | 81 |
| 82 void GetHostedDocumentURLBlockingThread(const FilePath& gdata_cache_path, | 82 void GetHostedDocumentURLBlockingThread(const FilePath& drive_cache_path, |
| 83 GURL* url) { | 83 GURL* url) { |
| 84 std::string json; | 84 std::string json; |
| 85 if (!file_util::ReadFileToString(gdata_cache_path, &json)) { | 85 if (!file_util::ReadFileToString(drive_cache_path, &json)) { |
| 86 NOTREACHED() << "Unable to read file " << gdata_cache_path.value(); | 86 NOTREACHED() << "Unable to read file " << drive_cache_path.value(); |
| 87 return; | 87 return; |
| 88 } | 88 } |
| 89 DVLOG(1) << "Hosted doc content " << json; | 89 DVLOG(1) << "Hosted doc content " << json; |
| 90 scoped_ptr<base::Value> val(base::JSONReader::Read(json)); | 90 scoped_ptr<base::Value> val(base::JSONReader::Read(json)); |
| 91 base::DictionaryValue* dict_val; | 91 base::DictionaryValue* dict_val; |
| 92 if (!val.get() || !val->GetAsDictionary(&dict_val)) { | 92 if (!val.get() || !val->GetAsDictionary(&dict_val)) { |
| 93 NOTREACHED() << "Parse failure for " << json; | 93 NOTREACHED() << "Parse failure for " << json; |
| 94 return; | 94 return; |
| 95 } | 95 } |
| 96 std::string edit_url; | 96 std::string edit_url; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 123 return; | 123 return; |
| 124 | 124 |
| 125 DCHECK(entry_proto.get()); | 125 DCHECK(entry_proto.get()); |
| 126 const std::string& base_name = entry_proto->base_name(); | 126 const std::string& base_name = entry_proto->base_name(); |
| 127 const GURL edit_url = GetFileResourceUrl(resource_id, base_name); | 127 const GURL edit_url = GetFileResourceUrl(resource_id, base_name); |
| 128 OpenEditURLUIThread(profile, &edit_url); | 128 OpenEditURLUIThread(profile, &edit_url); |
| 129 DVLOG(1) << "OnFindEntryByResourceId " << edit_url; | 129 DVLOG(1) << "OnFindEntryByResourceId " << edit_url; |
| 130 } | 130 } |
| 131 | 131 |
| 132 // Invoked upon completion of GetEntryInfoByPath initiated by | 132 // Invoked upon completion of GetEntryInfoByPath initiated by |
| 133 // InsertGDataCachePathPermissions. | 133 // InsertDriveCachePathPermissions. |
| 134 void OnGetEntryInfoForInsertGDataCachePathsPermissions( | 134 void OnGetEntryInfoForInsertDriveCachePathsPermissions( |
| 135 Profile* profile, | 135 Profile* profile, |
| 136 std::vector<std::pair<FilePath, int> >* cache_paths, | 136 std::vector<std::pair<FilePath, int> >* cache_paths, |
| 137 const base::Closure& callback, | 137 const base::Closure& callback, |
| 138 GDataFileError error, | 138 GDataFileError error, |
| 139 scoped_ptr<DriveEntryProto> entry_proto) { | 139 scoped_ptr<DriveEntryProto> entry_proto) { |
| 140 DCHECK(profile); | 140 DCHECK(profile); |
| 141 DCHECK(cache_paths); | 141 DCHECK(cache_paths); |
| 142 DCHECK(!callback.is_null()); | 142 DCHECK(!callback.is_null()); |
| 143 | 143 |
| 144 if (entry_proto.get() && !entry_proto->has_file_specific_info()) | 144 if (entry_proto.get() && !entry_proto->has_file_specific_info()) |
| 145 error = GDATA_FILE_ERROR_NOT_FOUND; | 145 error = GDATA_FILE_ERROR_NOT_FOUND; |
| 146 | 146 |
| 147 GDataCache* cache = GetGDataCache(profile); | 147 DriveCache* cache = GetDriveCache(profile); |
| 148 if (!cache || error != GDATA_FILE_OK) { | 148 if (!cache || error != GDATA_FILE_OK) { |
| 149 callback.Run(); | 149 callback.Run(); |
| 150 return; | 150 return; |
| 151 } | 151 } |
| 152 | 152 |
| 153 DCHECK(entry_proto.get()); | 153 DCHECK(entry_proto.get()); |
| 154 const std::string& resource_id = entry_proto->resource_id(); | 154 const std::string& resource_id = entry_proto->resource_id(); |
| 155 const std::string& file_md5 = entry_proto->file_specific_info().file_md5(); | 155 const std::string& file_md5 = entry_proto->file_specific_info().file_md5(); |
| 156 | 156 |
| 157 // We check permissions for raw cache file paths only for read-only | 157 // We check permissions for raw cache file paths only for read-only |
| 158 // operations (when fileEntry.file() is called), so read only permissions | 158 // operations (when fileEntry.file() is called), so read only permissions |
| 159 // should be sufficient for all cache paths. For the rest of supported | 159 // should be sufficient for all cache paths. For the rest of supported |
| 160 // operations the file access check is done for drive/ paths. | 160 // operations the file access check is done for drive/ paths. |
| 161 cache_paths->push_back(std::make_pair( | 161 cache_paths->push_back(std::make_pair( |
| 162 cache->GetCacheFilePath(resource_id, file_md5, | 162 cache->GetCacheFilePath(resource_id, file_md5, |
| 163 GDataCache::CACHE_TYPE_PERSISTENT, | 163 DriveCache::CACHE_TYPE_PERSISTENT, |
| 164 GDataCache::CACHED_FILE_FROM_SERVER), | 164 DriveCache::CACHED_FILE_FROM_SERVER), |
| 165 kReadOnlyFilePermissions)); | 165 kReadOnlyFilePermissions)); |
| 166 // TODO(tbarzic): When we start supporting openFile operation, we may have to | 166 // TODO(tbarzic): When we start supporting openFile operation, we may have to |
| 167 // change permission for localy modified files to match handler's permissions. | 167 // change permission for localy modified files to match handler's permissions. |
| 168 cache_paths->push_back(std::make_pair( | 168 cache_paths->push_back(std::make_pair( |
| 169 cache->GetCacheFilePath(resource_id, file_md5, | 169 cache->GetCacheFilePath(resource_id, file_md5, |
| 170 GDataCache::CACHE_TYPE_PERSISTENT, | 170 DriveCache::CACHE_TYPE_PERSISTENT, |
| 171 GDataCache::CACHED_FILE_LOCALLY_MODIFIED), | 171 DriveCache::CACHED_FILE_LOCALLY_MODIFIED), |
| 172 kReadOnlyFilePermissions)); | 172 kReadOnlyFilePermissions)); |
| 173 cache_paths->push_back(std::make_pair( | 173 cache_paths->push_back(std::make_pair( |
| 174 cache->GetCacheFilePath(resource_id, file_md5, | 174 cache->GetCacheFilePath(resource_id, file_md5, |
| 175 GDataCache::CACHE_TYPE_PERSISTENT, | 175 DriveCache::CACHE_TYPE_PERSISTENT, |
| 176 GDataCache::CACHED_FILE_MOUNTED), | 176 DriveCache::CACHED_FILE_MOUNTED), |
| 177 kReadOnlyFilePermissions)); | 177 kReadOnlyFilePermissions)); |
| 178 cache_paths->push_back(std::make_pair( | 178 cache_paths->push_back(std::make_pair( |
| 179 cache->GetCacheFilePath(resource_id, file_md5, | 179 cache->GetCacheFilePath(resource_id, file_md5, |
| 180 GDataCache::CACHE_TYPE_TMP, | 180 DriveCache::CACHE_TYPE_TMP, |
| 181 GDataCache::CACHED_FILE_FROM_SERVER), | 181 DriveCache::CACHED_FILE_FROM_SERVER), |
| 182 kReadOnlyFilePermissions)); | 182 kReadOnlyFilePermissions)); |
| 183 | 183 |
| 184 callback.Run(); | 184 callback.Run(); |
| 185 } | 185 } |
| 186 | 186 |
| 187 bool ParseTimezone(const base::StringPiece& timezone, | 187 bool ParseTimezone(const base::StringPiece& timezone, |
| 188 bool ahead, | 188 bool ahead, |
| 189 int* out_offset_to_utc_in_minutes) { | 189 int* out_offset_to_utc_in_minutes) { |
| 190 DCHECK(out_offset_to_utc_in_minutes); | 190 DCHECK(out_offset_to_utc_in_minutes); |
| 191 | 191 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 GURL GetFileResourceUrl(const std::string& resource_id, | 227 GURL GetFileResourceUrl(const std::string& resource_id, |
| 228 const std::string& file_name) { | 228 const std::string& file_name) { |
| 229 std::string url(base::StringPrintf( | 229 std::string url(base::StringPrintf( |
| 230 "%s:%s", | 230 "%s:%s", |
| 231 chrome::kDriveScheme, | 231 chrome::kDriveScheme, |
| 232 net::EscapePath(resource_id).c_str())); | 232 net::EscapePath(resource_id).c_str())); |
| 233 return GURL(url); | 233 return GURL(url); |
| 234 } | 234 } |
| 235 | 235 |
| 236 void ModifyGDataFileResourceUrl(Profile* profile, | 236 void ModifyGDataFileResourceUrl(Profile* profile, |
| 237 const FilePath& gdata_cache_path, | 237 const FilePath& drive_cache_path, |
| 238 GURL* url) { | 238 GURL* url) { |
| 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 240 | 240 |
| 241 GDataFileSystemInterface* file_system = GetGDataFileSystem(profile); | 241 GDataFileSystemInterface* file_system = GetGDataFileSystem(profile); |
| 242 if (!file_system) | 242 if (!file_system) |
| 243 return; | 243 return; |
| 244 GDataCache* cache = GetGDataCache(profile); | 244 DriveCache* cache = GetDriveCache(profile); |
| 245 if (!cache) | 245 if (!cache) |
| 246 return; | 246 return; |
| 247 | 247 |
| 248 if (cache->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_TMP_DOCUMENTS). | 248 if (cache->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP_DOCUMENTS). |
| 249 IsParent(gdata_cache_path)) { | 249 IsParent(drive_cache_path)) { |
| 250 // Handle hosted documents. The edit url is in the temporary file, so we | 250 // Handle hosted documents. The edit url is in the temporary file, so we |
| 251 // read it on a blocking thread. | 251 // read it on a blocking thread. |
| 252 GURL* edit_url = new GURL(); | 252 GURL* edit_url = new GURL(); |
| 253 content::BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, | 253 content::BrowserThread::GetBlockingPool()->PostTaskAndReply(FROM_HERE, |
| 254 base::Bind(&GetHostedDocumentURLBlockingThread, | 254 base::Bind(&GetHostedDocumentURLBlockingThread, |
| 255 gdata_cache_path, edit_url), | 255 drive_cache_path, edit_url), |
| 256 base::Bind(&OpenEditURLUIThread, profile, base::Owned(edit_url))); | 256 base::Bind(&OpenEditURLUIThread, profile, base::Owned(edit_url))); |
| 257 *url = GURL(); | 257 *url = GURL(); |
| 258 } else if (cache->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_TMP). | 258 } else if (cache->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_TMP). |
| 259 IsParent(gdata_cache_path) || | 259 IsParent(drive_cache_path) || |
| 260 cache->GetCacheDirectoryPath(GDataCache::CACHE_TYPE_PERSISTENT). | 260 cache->GetCacheDirectoryPath(DriveCache::CACHE_TYPE_PERSISTENT). |
| 261 IsParent(gdata_cache_path)) { | 261 IsParent(drive_cache_path)) { |
| 262 // Handle all other gdata files. | 262 // Handle all other gdata files. |
| 263 const std::string resource_id = | 263 const std::string resource_id = |
| 264 gdata_cache_path.BaseName().RemoveExtension().AsUTF8Unsafe(); | 264 drive_cache_path.BaseName().RemoveExtension().AsUTF8Unsafe(); |
| 265 file_system->GetEntryInfoByResourceId( | 265 file_system->GetEntryInfoByResourceId( |
| 266 resource_id, | 266 resource_id, |
| 267 base::Bind(&OnGetEntryInfoByResourceId, | 267 base::Bind(&OnGetEntryInfoByResourceId, |
| 268 profile, | 268 profile, |
| 269 resource_id)); | 269 resource_id)); |
| 270 *url = GURL(); | 270 *url = GURL(); |
| 271 } | 271 } |
| 272 } | 272 } |
| 273 | 273 |
| 274 bool IsUnderGDataMountPoint(const FilePath& path) { | 274 bool IsUnderGDataMountPoint(const FilePath& path) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 285 | 285 |
| 286 // -1 to include 'drive'. | 286 // -1 to include 'drive'. |
| 287 FilePath extracted; | 287 FilePath extracted; |
| 288 for (size_t i = arraysize(kGDataMountPointPathComponents) - 1; | 288 for (size_t i = arraysize(kGDataMountPointPathComponents) - 1; |
| 289 i < components.size(); ++i) { | 289 i < components.size(); ++i) { |
| 290 extracted = extracted.Append(components[i]); | 290 extracted = extracted.Append(components[i]); |
| 291 } | 291 } |
| 292 return extracted; | 292 return extracted; |
| 293 } | 293 } |
| 294 | 294 |
| 295 void InsertGDataCachePathsPermissions( | 295 void InsertDriveCachePathsPermissions( |
| 296 Profile* profile, | 296 Profile* profile, |
| 297 scoped_ptr<std::vector<FilePath> > gdata_paths, | 297 scoped_ptr<std::vector<FilePath> > gdata_paths, |
| 298 std::vector<std::pair<FilePath, int> >* cache_paths, | 298 std::vector<std::pair<FilePath, int> >* cache_paths, |
| 299 const base::Closure& callback) { | 299 const base::Closure& callback) { |
| 300 DCHECK(profile); | 300 DCHECK(profile); |
| 301 DCHECK(gdata_paths.get()); | 301 DCHECK(gdata_paths.get()); |
| 302 DCHECK(cache_paths); | 302 DCHECK(cache_paths); |
| 303 DCHECK(!callback.is_null()); | 303 DCHECK(!callback.is_null()); |
| 304 | 304 |
| 305 GDataFileSystemInterface* file_system = GetGDataFileSystem(profile); | 305 GDataFileSystemInterface* file_system = GetGDataFileSystem(profile); |
| 306 if (!file_system || gdata_paths->empty()) { | 306 if (!file_system || gdata_paths->empty()) { |
| 307 callback.Run(); | 307 callback.Run(); |
| 308 return; | 308 return; |
| 309 } | 309 } |
| 310 | 310 |
| 311 // Remove one file path entry from the back of the input vector |gdata_paths|. | 311 // Remove one file path entry from the back of the input vector |gdata_paths|. |
| 312 FilePath gdata_path = gdata_paths->back(); | 312 FilePath gdata_path = gdata_paths->back(); |
| 313 gdata_paths->pop_back(); | 313 gdata_paths->pop_back(); |
| 314 | 314 |
| 315 // Call GetEntryInfoByPath() to get file info for |gdata_path| then insert | 315 // Call GetEntryInfoByPath() to get file info for |gdata_path| then insert |
| 316 // all possible cache paths to the output vector |cache_paths|. | 316 // all possible cache paths to the output vector |cache_paths|. |
| 317 // Note that we can only process one file path at a time. Upon completion | 317 // Note that we can only process one file path at a time. Upon completion |
| 318 // of OnGetEntryInfoForInsertGDataCachePathsPermissions(), we recursively call | 318 // of OnGetEntryInfoForInsertDriveCachePathsPermissions(), we recursively call |
| 319 // InsertGDataCachePathsPermissions() to process the next file path from the | 319 // InsertDriveCachePathsPermissions() to process the next file path from the |
| 320 // back of the input vector |gdata_paths| until it is empty. | 320 // back of the input vector |gdata_paths| until it is empty. |
| 321 file_system->GetEntryInfoByPath( | 321 file_system->GetEntryInfoByPath( |
| 322 gdata_path, | 322 gdata_path, |
| 323 base::Bind(&OnGetEntryInfoForInsertGDataCachePathsPermissions, | 323 base::Bind(&OnGetEntryInfoForInsertDriveCachePathsPermissions, |
| 324 profile, | 324 profile, |
| 325 cache_paths, | 325 cache_paths, |
| 326 base::Bind(&InsertGDataCachePathsPermissions, | 326 base::Bind(&InsertDriveCachePathsPermissions, |
| 327 profile, | 327 profile, |
| 328 base::Passed(&gdata_paths), | 328 base::Passed(&gdata_paths), |
| 329 cache_paths, | 329 cache_paths, |
| 330 callback))); | 330 callback))); |
| 331 } | 331 } |
| 332 | 332 |
| 333 bool IsGDataAvailable(Profile* profile) { | 333 bool IsGDataAvailable(Profile* profile) { |
| 334 if (!chromeos::UserManager::Get()->IsUserLoggedIn() || | 334 if (!chromeos::UserManager::Get()->IsUserLoggedIn() || |
| 335 chromeos::UserManager::Get()->IsLoggedInAsGuest() || | 335 chromeos::UserManager::Get()->IsLoggedInAsGuest() || |
| 336 chromeos::UserManager::Get()->IsLoggedInAsDemoUser()) | 336 chromeos::UserManager::Get()->IsLoggedInAsDemoUser()) |
| (...skipping 315 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 652 const base::Closure& reply_task) { | 652 const base::Closure& reply_task) { |
| 653 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 653 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 654 | 654 |
| 655 const bool posted = blocking_task_runner->PostTaskAndReply( | 655 const bool posted = blocking_task_runner->PostTaskAndReply( |
| 656 from_here, request_task, reply_task); | 656 from_here, request_task, reply_task); |
| 657 DCHECK(posted); | 657 DCHECK(posted); |
| 658 } | 658 } |
| 659 | 659 |
| 660 } // namespace util | 660 } // namespace util |
| 661 } // namespace gdata | 661 } // namespace gdata |
| OLD | NEW |