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

Side by Side Diff: chrome/browser/chromeos/drive/file_system_core_util.cc

Issue 1192493003: Move browser-agnostic code from file_system_util to file_system_core_util. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@drive-prefservice
Patch Set: Fixed copyright headers in the 3 new files. Created 5 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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/drive/file_system_util.h" 5 #include "chrome/browser/chromeos/drive/file_system_core_util.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/bind_helpers.h" 12 #include "base/bind_helpers.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h" 14 #include "base/files/file_util.h"
15 #include "base/i18n/icu_string_conversions.h" 15 #include "base/i18n/icu_string_conversions.h"
16 #include "base/json/json_file_value_serializer.h" 16 #include "base/json/json_file_value_serializer.h"
17 #include "base/logging.h" 17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h" 18 #include "base/memory/scoped_ptr.h"
19 #include "base/prefs/pref_service.h" 19 #include "base/prefs/pref_service.h"
20 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h" 22 #include "base/strings/stringprintf.h"
23 #include "base/thread_task_runner_handle.h" 23 #include "base/thread_task_runner_handle.h"
24 #include "base/threading/sequenced_worker_pool.h" 24 #include "base/threading/sequenced_worker_pool.h"
25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chromeos/drive/drive.pb.h" 25 #include "chrome/browser/chromeos/drive/drive.pb.h"
27 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
28 #include "chrome/browser/chromeos/drive/drive_pref_names.h" 26 #include "chrome/browser/chromeos/drive/drive_pref_names.h"
29 #include "chrome/browser/chromeos/drive/file_system_interface.h" 27 #include "chrome/browser/chromeos/drive/file_system_interface.h"
30 #include "chrome/browser/chromeos/drive/job_list.h" 28 #include "chrome/browser/chromeos/drive/job_list.h"
31 #include "chrome/browser/chromeos/drive/write_on_cache_file.h" 29 #include "chrome/browser/chromeos/drive/write_on_cache_file.h"
32 #include "chrome/browser/chromeos/profiles/profile_helper.h"
33 #include "chrome/browser/chromeos/profiles/profile_util.h"
34 #include "chrome/browser/profiles/profile.h"
35 #include "chrome/browser/profiles/profile_manager.h"
36 #include "chrome/common/chrome_constants.h"
37 #include "chrome/common/chrome_paths_internal.h"
38 #include "chrome/common/pref_names.h"
39 #include "chromeos/chromeos_constants.h"
40 #include "components/user_manager/user_manager.h"
41 #include "content/public/browser/browser_thread.h"
42 #include "net/base/escape.h" 30 #include "net/base/escape.h"
43 #include "storage/browser/fileapi/file_system_url.h"
44
45 using content::BrowserThread;
46 31
47 namespace drive { 32 namespace drive {
48 namespace util { 33 namespace util {
49 34
50 namespace { 35 namespace {
51 36
52 std::string ReadStringFromGDocFile(const base::FilePath& file_path, 37 std::string ReadStringFromGDocFile(const base::FilePath& file_path,
53 const std::string& key) { 38 const std::string& key) {
54 const int64 kMaxGDocSize = 4096; 39 const int64 kMaxGDocSize = 4096;
55 int64 file_size = 0; 40 int64 file_size = 0;
56 if (!base::GetFileSize(file_path, &file_size) || 41 if (!base::GetFileSize(file_path, &file_size) || file_size > kMaxGDocSize) {
57 file_size > kMaxGDocSize) {
58 LOG(WARNING) << "File too large to be a GDoc file " << file_path.value(); 42 LOG(WARNING) << "File too large to be a GDoc file " << file_path.value();
59 return std::string(); 43 return std::string();
60 } 44 }
61 45
62 JSONFileValueDeserializer reader(file_path); 46 JSONFileValueDeserializer reader(file_path);
63 std::string error_message; 47 std::string error_message;
64 scoped_ptr<base::Value> root_value(reader.Deserialize(NULL, &error_message)); 48 scoped_ptr<base::Value> root_value(reader.Deserialize(NULL, &error_message));
65 if (!root_value) { 49 if (!root_value) {
66 LOG(WARNING) << "Failed to parse " << file_path.value() << " as JSON." 50 LOG(WARNING) << "Failed to parse " << file_path.value() << " as JSON."
67 << " error = " << error_message; 51 << " error = " << error_message;
68 return std::string(); 52 return std::string();
69 } 53 }
70 54
71 base::DictionaryValue* dictionary_value = NULL; 55 base::DictionaryValue* dictionary_value = NULL;
72 std::string result; 56 std::string result;
73 if (!root_value->GetAsDictionary(&dictionary_value) || 57 if (!root_value->GetAsDictionary(&dictionary_value) ||
74 !dictionary_value->GetString(key, &result)) { 58 !dictionary_value->GetString(key, &result)) {
75 LOG(WARNING) << "No value for the given key is stored in " 59 LOG(WARNING) << "No value for the given key is stored in "
76 << file_path.value() << ". key = " << key; 60 << file_path.value() << ". key = " << key;
77 return std::string(); 61 return std::string();
78 } 62 }
79 63
80 return result; 64 return result;
81 } 65 }
82 66
83 // Returns DriveIntegrationService instance, if Drive is enabled.
84 // Otherwise, NULL.
85 DriveIntegrationService* GetIntegrationServiceByProfile(Profile* profile) {
86 DriveIntegrationService* service =
87 DriveIntegrationServiceFactory::FindForProfile(profile);
88 if (!service || !service->IsMounted())
89 return NULL;
90 return service;
91 }
92
93 } // namespace 67 } // namespace
94 68
95 const base::FilePath& GetDriveGrandRootPath() { 69 const base::FilePath& GetDriveGrandRootPath() {
96 CR_DEFINE_STATIC_LOCAL( 70 CR_DEFINE_STATIC_LOCAL(
97 base::FilePath, grand_root_path, 71 base::FilePath, grand_root_path,
98 (base::FilePath::FromUTF8Unsafe(kDriveGrandRootDirName))); 72 (base::FilePath::FromUTF8Unsafe(kDriveGrandRootDirName)));
99 return grand_root_path; 73 return grand_root_path;
100 } 74 }
101 75
102 const base::FilePath& GetDriveMyDriveRootPath() { 76 const base::FilePath& GetDriveMyDriveRootPath() {
103 CR_DEFINE_STATIC_LOCAL( 77 CR_DEFINE_STATIC_LOCAL(
104 base::FilePath, drive_root_path, 78 base::FilePath, drive_root_path,
105 (GetDriveGrandRootPath().AppendASCII(kDriveMyDriveRootDirName))); 79 (GetDriveGrandRootPath().AppendASCII(kDriveMyDriveRootDirName)));
106 return drive_root_path; 80 return drive_root_path;
107 } 81 }
108 82
109 base::FilePath GetDriveMountPointPathForUserIdHash( 83 base::FilePath GetDriveMountPointPathForUserIdHash(
110 const std::string user_id_hash) { 84 const std::string user_id_hash) {
111 static const base::FilePath::CharType kSpecialMountPointRoot[] = 85 static const base::FilePath::CharType kSpecialMountPointRoot[] =
112 FILE_PATH_LITERAL("/special"); 86 FILE_PATH_LITERAL("/special");
113 static const char kDriveMountPointNameBase[] = "drive"; 87 static const char kDriveMountPointNameBase[] = "drive";
114 return base::FilePath(kSpecialMountPointRoot).AppendASCII( 88 return base::FilePath(kSpecialMountPointRoot)
115 net::EscapeQueryParamValue( 89 .AppendASCII(net::EscapeQueryParamValue(
116 kDriveMountPointNameBase + 90 kDriveMountPointNameBase +
117 (user_id_hash.empty() ? "" : "-" + user_id_hash), false)); 91 (user_id_hash.empty() ? "" : "-" + user_id_hash),
118 } 92 false));
119
120 base::FilePath GetDriveMountPointPath(Profile* profile) {
121 std::string id = chromeos::ProfileHelper::GetUserIdHashFromProfile(profile);
122 if (id.empty() || id == chrome::kLegacyProfileDir) {
123 // ProfileHelper::GetUserIdHashFromProfile works only when multi-profile is
124 // enabled. In that case, we fall back to use UserManager (it basically just
125 // returns currently active users's hash in such a case.) I still try
126 // ProfileHelper first because it works better in tests.
127 const user_manager::User* const user =
128 user_manager::UserManager::IsInitialized()
129 ? chromeos::ProfileHelper::Get()->GetUserByProfile(
130 profile->GetOriginalProfile())
131 : NULL;
132 if (user)
133 id = user->username_hash();
134 }
135 return GetDriveMountPointPathForUserIdHash(id);
136 }
137
138 FileSystemInterface* GetFileSystemByProfile(Profile* profile) {
139 DCHECK_CURRENTLY_ON(BrowserThread::UI);
140
141 DriveIntegrationService* integration_service =
142 GetIntegrationServiceByProfile(profile);
143 return integration_service ? integration_service->file_system() : NULL;
144 }
145
146 FileSystemInterface* GetFileSystemByProfileId(void* profile_id) {
147 DCHECK_CURRENTLY_ON(BrowserThread::UI);
148
149 // |profile_id| needs to be checked with ProfileManager::IsValidProfile
150 // before using it.
151 Profile* profile = reinterpret_cast<Profile*>(profile_id);
152 if (!g_browser_process->profile_manager()->IsValidProfile(profile))
153 return NULL;
154 return GetFileSystemByProfile(profile);
155 }
156
157 DriveAppRegistry* GetDriveAppRegistryByProfile(Profile* profile) {
158 DCHECK_CURRENTLY_ON(BrowserThread::UI);
159
160 DriveIntegrationService* integration_service =
161 GetIntegrationServiceByProfile(profile);
162 return integration_service ?
163 integration_service->drive_app_registry() :
164 NULL;
165 }
166
167 DriveServiceInterface* GetDriveServiceByProfile(Profile* profile) {
168 DCHECK_CURRENTLY_ON(BrowserThread::UI);
169
170 DriveIntegrationService* integration_service =
171 GetIntegrationServiceByProfile(profile);
172 return integration_service ? integration_service->drive_service() : NULL;
173 } 93 }
174 94
175 bool IsUnderDriveMountPoint(const base::FilePath& path) { 95 bool IsUnderDriveMountPoint(const base::FilePath& path) {
176 return !ExtractDrivePath(path).empty(); 96 return !ExtractDrivePath(path).empty();
177 } 97 }
178 98
179 base::FilePath ExtractDrivePath(const base::FilePath& path) { 99 base::FilePath ExtractDrivePath(const base::FilePath& path) {
180 std::vector<base::FilePath::StringType> components; 100 std::vector<base::FilePath::StringType> components;
181 path.GetComponents(&components); 101 path.GetComponents(&components);
182 if (components.size() < 3) 102 if (components.size() < 3)
183 return base::FilePath(); 103 return base::FilePath();
184 if (components[0] != FILE_PATH_LITERAL("/")) 104 if (components[0] != FILE_PATH_LITERAL("/"))
185 return base::FilePath(); 105 return base::FilePath();
186 if (components[1] != FILE_PATH_LITERAL("special")) 106 if (components[1] != FILE_PATH_LITERAL("special"))
187 return base::FilePath(); 107 return base::FilePath();
188 static const base::FilePath::CharType kPrefix[] = FILE_PATH_LITERAL("drive"); 108 static const base::FilePath::CharType kPrefix[] = FILE_PATH_LITERAL("drive");
189 if (components[2].compare(0, arraysize(kPrefix) - 1, kPrefix) != 0) 109 if (components[2].compare(0, arraysize(kPrefix) - 1, kPrefix) != 0)
190 return base::FilePath(); 110 return base::FilePath();
191 111
192 base::FilePath drive_path = GetDriveGrandRootPath(); 112 base::FilePath drive_path = GetDriveGrandRootPath();
193 for (size_t i = 3; i < components.size(); ++i) 113 for (size_t i = 3; i < components.size(); ++i)
194 drive_path = drive_path.Append(components[i]); 114 drive_path = drive_path.Append(components[i]);
195 return drive_path; 115 return drive_path;
196 } 116 }
197 117
198 Profile* ExtractProfileFromPath(const base::FilePath& path) {
199 DCHECK_CURRENTLY_ON(BrowserThread::UI);
200
201 const std::vector<Profile*>& profiles =
202 g_browser_process->profile_manager()->GetLoadedProfiles();
203 for (size_t i = 0; i < profiles.size(); ++i) {
204 Profile* original_profile = profiles[i]->GetOriginalProfile();
205 if (original_profile == profiles[i] &&
206 !chromeos::ProfileHelper::IsSigninProfile(original_profile)) {
207 const base::FilePath base = GetDriveMountPointPath(original_profile);
208 if (base == path || base.IsParent(path))
209 return original_profile;
210 }
211 }
212 return NULL;
213 }
214
215 base::FilePath ExtractDrivePathFromFileSystemUrl(
216 const storage::FileSystemURL& url) {
217 if (!url.is_valid() || url.type() != storage::kFileSystemTypeDrive)
218 return base::FilePath();
219 return ExtractDrivePath(url.path());
220 }
221
222 base::FilePath GetCacheRootPath(Profile* profile) {
223 base::FilePath cache_base_path;
224 chrome::GetUserCacheDirectory(profile->GetPath(), &cache_base_path);
225 base::FilePath cache_root_path =
226 cache_base_path.Append(chromeos::kDriveCacheDirname);
227 static const base::FilePath::CharType kFileCacheVersionDir[] =
228 FILE_PATH_LITERAL("v1");
229 return cache_root_path.Append(kFileCacheVersionDir);
230 }
231
232 std::string EscapeCacheFileName(const std::string& filename) { 118 std::string EscapeCacheFileName(const std::string& filename) {
233 // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape 119 // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape
234 std::string escaped; 120 std::string escaped;
235 for (size_t i = 0; i < filename.size(); ++i) { 121 for (size_t i = 0; i < filename.size(); ++i) {
236 char c = filename[i]; 122 char c = filename[i];
237 if (c == '%' || c == '.' || c == '/') { 123 if (c == '%' || c == '.' || c == '/') {
238 base::StringAppendF(&escaped, "%%%02X", c); 124 base::StringAppendF(&escaped, "%%%02X", c);
239 } else { 125 } else {
240 escaped.push_back(c); 126 escaped.push_back(c);
241 } 127 }
242 } 128 }
243 return escaped; 129 return escaped;
244 } 130 }
245 131
246 std::string UnescapeCacheFileName(const std::string& filename) { 132 std::string UnescapeCacheFileName(const std::string& filename) {
247 std::string unescaped; 133 std::string unescaped;
248 for (size_t i = 0; i < filename.size(); ++i) { 134 for (size_t i = 0; i < filename.size(); ++i) {
249 char c = filename[i]; 135 char c = filename[i];
250 if (c == '%' && i + 2 < filename.length()) { 136 if (c == '%' && i + 2 < filename.length()) {
251 c = (HexDigitToInt(filename[i + 1]) << 4) + 137 c = (HexDigitToInt(filename[i + 1]) << 4) +
252 HexDigitToInt(filename[i + 2]); 138 HexDigitToInt(filename[i + 2]);
253 i += 2; 139 i += 2;
254 } 140 }
255 unescaped.push_back(c); 141 unescaped.push_back(c);
256 } 142 }
257 return unescaped; 143 return unescaped;
258 } 144 }
259 145
260 std::string NormalizeFileName(const std::string& input) { 146 std::string NormalizeFileName(const std::string& input) {
261 DCHECK(base::IsStringUTF8(input)); 147 DCHECK(base::IsStringUTF8(input));
262 148
263 std::string output; 149 std::string output;
264 if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output)) 150 if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output))
265 output = input; 151 output = input;
266 base::ReplaceChars(output, "/", "_", &output); 152 base::ReplaceChars(output, "/", "_", &output);
267 if (!output.empty() && output.find_first_not_of('.', 0) == std::string::npos) 153 if (!output.empty() && output.find_first_not_of('.', 0) == std::string::npos)
268 output = "_"; 154 output = "_";
269 return output; 155 return output;
270 } 156 }
271 157
272 void PrepareWritableFileAndRun(Profile* profile,
273 const base::FilePath& path,
274 const PrepareWritableFileCallback& callback) {
275 DCHECK_CURRENTLY_ON(BrowserThread::UI);
276 DCHECK(!callback.is_null());
277
278 FileSystemInterface* file_system = GetFileSystemByProfile(profile);
279 if (!file_system || !IsUnderDriveMountPoint(path)) {
280 content::BrowserThread::GetBlockingPool()->PostTask(
281 FROM_HERE, base::Bind(callback, FILE_ERROR_FAILED, base::FilePath()));
282 return;
283 }
284
285 WriteOnCacheFile(file_system,
286 ExtractDrivePath(path),
287 std::string(), // mime_type
288 callback);
289 }
290
291 void EnsureDirectoryExists(Profile* profile,
292 const base::FilePath& directory,
293 const FileOperationCallback& callback) {
294 DCHECK_CURRENTLY_ON(BrowserThread::UI);
295 DCHECK(!callback.is_null());
296 if (IsUnderDriveMountPoint(directory)) {
297 FileSystemInterface* file_system = GetFileSystemByProfile(profile);
298 DCHECK(file_system);
299 file_system->CreateDirectory(
300 ExtractDrivePath(directory),
301 true /* is_exclusive */,
302 true /* is_recursive */,
303 callback);
304 } else {
305 base::ThreadTaskRunnerHandle::Get()->PostTask(
306 FROM_HERE, base::Bind(callback, FILE_ERROR_OK));
307 }
308 }
309
310 void EmptyFileOperationCallback(FileError error) { 158 void EmptyFileOperationCallback(FileError error) {
311 } 159 }
312 160
313 bool CreateGDocFile(const base::FilePath& file_path, 161 bool CreateGDocFile(const base::FilePath& file_path,
314 const GURL& url, 162 const GURL& url,
315 const std::string& resource_id) { 163 const std::string& resource_id) {
316 std::string content = base::StringPrintf( 164 std::string content =
317 "{\"url\": \"%s\", \"resource_id\": \"%s\"}", 165 base::StringPrintf("{\"url\": \"%s\", \"resource_id\": \"%s\"}",
318 url.spec().c_str(), resource_id.c_str()); 166 url.spec().c_str(), resource_id.c_str());
319 return base::WriteFile(file_path, content.data(), content.size()) == 167 return base::WriteFile(file_path, content.data(), content.size()) ==
320 static_cast<int>(content.size()); 168 static_cast<int>(content.size());
321 } 169 }
322 170
323 GURL ReadUrlFromGDocFile(const base::FilePath& file_path) { 171 GURL ReadUrlFromGDocFile(const base::FilePath& file_path) {
324 return GURL(ReadStringFromGDocFile(file_path, "url")); 172 return GURL(ReadStringFromGDocFile(file_path, "url"));
325 } 173 }
326 174
327 std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path) { 175 std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path) {
328 return ReadStringFromGDocFile(file_path, "resource_id"); 176 return ReadStringFromGDocFile(file_path, "resource_id");
329 } 177 }
330 178
331 bool IsDriveEnabledForProfile(Profile* profile) {
332 DCHECK_CURRENTLY_ON(BrowserThread::UI);
333
334 if (!chromeos::IsProfileAssociatedWithGaiaAccount(profile))
335 return false;
336
337 // Disable Drive if preference is set. This can happen with commandline flag
338 // --disable-drive or enterprise policy, or with user settings.
339 if (profile->GetPrefs()->GetBoolean(prefs::kDisableDrive))
340 return false;
341
342 return true;
343 }
344
345 ConnectionStatusType GetDriveConnectionStatus(Profile* profile) {
346 drive::DriveServiceInterface* const drive_service =
347 drive::util::GetDriveServiceByProfile(profile);
348
349 if (!drive_service)
350 return DRIVE_DISCONNECTED_NOSERVICE;
351 if (net::NetworkChangeNotifier::IsOffline())
352 return DRIVE_DISCONNECTED_NONETWORK;
353 if (!drive_service->CanSendRequest())
354 return DRIVE_DISCONNECTED_NOTREADY;
355
356 const bool is_connection_cellular =
357 net::NetworkChangeNotifier::IsConnectionCellular(
358 net::NetworkChangeNotifier::GetConnectionType());
359 const bool disable_sync_over_celluar =
360 profile->GetPrefs()->GetBoolean(prefs::kDisableDriveOverCellular);
361
362 if (is_connection_cellular && disable_sync_over_celluar)
363 return DRIVE_CONNECTED_METERED;
364 return DRIVE_CONNECTED;
365 }
366
367 } // namespace util 179 } // namespace util
368 } // namespace drive 180 } // namespace drive
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698