Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: chrome/browser/chromeos/gdata/gdata_util.cc

Issue 10543037: gdata: Convert public synchronous functions in GDataFileSystem to asynchronous. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address Satoru's and Toni's concerns. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
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
108 GDataFileSystem* file_system = GetGDataFileSystem(profile);
109 if (!file_system) {
110 if (!callback.is_null())
111 callback.Run();
112 return;
113 }
114
115 std::string resource_id;
116 std::string file_md5;
117
118 // TODO(hshi): GetFileInfoByPathAsync may fail when the gdata file is not
119 // present in cache. For now just grant permission to the entire cache subdir.
120 if (error == base::PLATFORM_FILE_OK && file_info.get()) {
121 resource_id = file_info->gdata_entry().resource_id();
122 file_md5 = file_info->file_md5();
123 }
124
125 // We check permissions for raw cache file paths only for read-only
126 // operations (when fileEntry.file() is called), so read only permissions
127 // should be sufficient for all cache paths. For the rest of supported
128 // operations the file access check is done for drive/ paths.
129 cache_paths->push_back(std::make_pair(
130 file_system->GetCacheFilePath(resource_id, file_md5,
131 GDataCache::CACHE_TYPE_PERSISTENT,
132 GDataFileSystem::CACHED_FILE_FROM_SERVER),
133 kReadOnlyFilePermissions));
134 // TODO(tbarzic): When we start supporting openFile operation, we may have to
135 // change permission for localy modified files to match handler's permissions.
136 cache_paths->push_back(std::make_pair(
137 file_system->GetCacheFilePath(resource_id, file_md5,
138 GDataCache::CACHE_TYPE_PERSISTENT,
139 GDataFileSystem::CACHED_FILE_LOCALLY_MODIFIED),
140 kReadOnlyFilePermissions));
141 cache_paths->push_back(std::make_pair(
142 file_system->GetCacheFilePath(resource_id, file_md5,
143 GDataCache::CACHE_TYPE_PERSISTENT,
144 GDataFileSystem::CACHED_FILE_MOUNTED),
145 kReadOnlyFilePermissions));
146 cache_paths->push_back(std::make_pair(
147 file_system->GetCacheFilePath(resource_id, file_md5,
148 GDataCache::CACHE_TYPE_TMP,
149 GDataFileSystem::CACHED_FILE_FROM_SERVER),
150 kReadOnlyFilePermissions));
151
152 if (!callback.is_null())
153 callback.Run();
154 }
155
96 } // namespace 156 } // namespace
97 157
98 const FilePath& GetGDataMountPointPath() { 158 const FilePath& GetGDataMountPointPath() {
99 CR_DEFINE_STATIC_LOCAL(FilePath, gdata_mount_path, 159 CR_DEFINE_STATIC_LOCAL(FilePath, gdata_mount_path,
100 (FilePath::FromUTF8Unsafe(kGDataMountPointPath))); 160 (FilePath::FromUTF8Unsafe(kGDataMountPointPath)));
101 return gdata_mount_path; 161 return gdata_mount_path;
102 } 162 }
103 163
104 const std::string& GetGDataMountPointPathAsString() { 164 const std::string& GetGDataMountPointPathAsString() {
105 CR_DEFINE_STATIC_LOCAL(std::string, gdata_mount_path_string, 165 CR_DEFINE_STATIC_LOCAL(std::string, gdata_mount_path_string,
(...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after
224 FilePath extracted; 284 FilePath extracted;
225 for (size_t i = arraysize(kGDataMountPointPathComponents) - 1; 285 for (size_t i = arraysize(kGDataMountPointPathComponents) - 1;
226 i < components.size(); ++i) { 286 i < components.size(); ++i) {
227 extracted = extracted.Append(components[i]); 287 extracted = extracted.Append(components[i]);
228 } 288 }
229 return extracted; 289 return extracted;
230 } 290 }
231 291
232 void InsertGDataCachePathsPermissions( 292 void InsertGDataCachePathsPermissions(
233 Profile* profile, 293 Profile* profile,
234 const FilePath& gdata_path, 294 scoped_ptr<std::vector<FilePath> > gdata_paths,
235 std::vector<std::pair<FilePath, int> >* cache_paths ) { 295 std::vector<std::pair<FilePath, int> >* cache_paths,
296 const base::Closure& callback) {
297 DCHECK(profile);
298 DCHECK(gdata_paths.get());
236 DCHECK(cache_paths); 299 DCHECK(cache_paths);
237 300
238 GDataFileSystem* file_system = GetGDataFileSystem(profile); 301 GDataFileSystem* file_system = GetGDataFileSystem(profile);
239 if (!file_system) 302 if (!file_system || gdata_paths->empty()) {
303 if (!callback.is_null())
tbarzic 2012/06/08 17:59:13 I don't think this method makes much sense without
hshi1 2012/06/08 18:21:41 Done.
304 callback.Run();
240 return; 305 return;
306 }
241 307
242 GDataFileProperties file_properties; 308 // Remove one file path entry from the back of the input vector |gdata_paths|.
243 file_system->GetFileInfoByPath(gdata_path, &file_properties); 309 FilePath gdata_path = gdata_paths->back();
310 gdata_paths->pop_back();
244 311
245 std::string resource_id = file_properties.resource_id; 312 // Call GetFileInfoByPathAsync() to get file info for |gdata_path| then insert
246 std::string file_md5 = file_properties.file_md5; 313 // all possible cache paths to the output vector |cache_paths|.
247 314 // Note that we can only process one file path at a time. Upon completion
248 // We check permissions for raw cache file paths only for read-only 315 // of OnGetFileInfoForInsertGDataCachePathsPermissions(), we recursively call
249 // operations (when fileEntry.file() is called), so read only permissions 316 // InsertGDataCachePathsPermissions() to process the next file path from the
250 // should be sufficient for all cache paths. For the rest of supported 317 // back of the input vector |gdata_paths| until it is empty.
251 // operations the file access check is done for drive/ paths. 318 file_system->GetFileInfoByPathAsync(
252 cache_paths->push_back(std::make_pair( 319 gdata_path,
253 file_system->GetCacheFilePath(resource_id, file_md5, 320 base::Bind(&OnGetFileInfoForInsertGDataCachePathsPermissions,
254 GDataCache::CACHE_TYPE_PERSISTENT, 321 profile,
255 GDataFileSystem::CACHED_FILE_FROM_SERVER), 322 cache_paths,
256 kReadOnlyFilePermissions)); 323 base::Bind(&InsertGDataCachePathsPermissions,
257 // TODO(tbarzic): When we start supporting openFile operation, we may have to 324 profile,
258 // change permission for localy modified files to match handler's permissions. 325 base::Passed(&gdata_paths),
259 cache_paths->push_back(std::make_pair( 326 cache_paths,
260 file_system->GetCacheFilePath(resource_id, file_md5, 327 callback)));
261 GDataCache::CACHE_TYPE_PERSISTENT,
262 GDataFileSystem::CACHED_FILE_LOCALLY_MODIFIED),
263 kReadOnlyFilePermissions));
264 cache_paths->push_back(std::make_pair(
265 file_system->GetCacheFilePath(resource_id, file_md5,
266 GDataCache::CACHE_TYPE_PERSISTENT,
267 GDataFileSystem::CACHED_FILE_MOUNTED),
268 kReadOnlyFilePermissions));
269 cache_paths->push_back(std::make_pair(
270 file_system->GetCacheFilePath(resource_id, file_md5,
271 GDataCache::CACHE_TYPE_TMP,
272 GDataFileSystem::CACHED_FILE_FROM_SERVER),
273 kReadOnlyFilePermissions));
274 } 328 }
275 329
276 bool IsGDataAvailable(Profile* profile) { 330 bool IsGDataAvailable(Profile* profile) {
277 if (!chromeos::UserManager::Get()->IsUserLoggedIn() || 331 if (!chromeos::UserManager::Get()->IsUserLoggedIn() ||
278 chromeos::UserManager::Get()->IsLoggedInAsGuest() || 332 chromeos::UserManager::Get()->IsLoggedInAsGuest() ||
279 chromeos::UserManager::Get()->IsLoggedInAsDemoUser()) 333 chromeos::UserManager::Get()->IsLoggedInAsDemoUser())
280 return false; 334 return false;
281 335
282 // Do not allow GData for incognito windows / guest mode. 336 // Do not allow GData for incognito windows / guest mode.
283 if (profile->IsOffTheRecord()) 337 if (profile->IsOffTheRecord())
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
350 // Assign the extracted extensions to md5 and extra_extension. 404 // Assign the extracted extensions to md5 and extra_extension.
351 int extension_count = extensions.size(); 405 int extension_count = extensions.size();
352 *md5 = (extension_count > 0) ? extensions[extension_count - 1] : 406 *md5 = (extension_count > 0) ? extensions[extension_count - 1] :
353 std::string(); 407 std::string();
354 *extra_extension = (extension_count > 1) ? extensions[extension_count - 2] : 408 *extra_extension = (extension_count > 1) ? extensions[extension_count - 2] :
355 std::string(); 409 std::string();
356 } 410 }
357 411
358 } // namespace util 412 } // namespace util
359 } // namespace gdata 413 } // namespace gdata
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698