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

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: Reverted some changes in chrome_browser_chromeos.gypi. 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 (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/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" 31 #include "storage/browser/fileapi/file_system_url.h"
44 32
45 using content::BrowserThread;
46
47 namespace drive { 33 namespace drive {
48 namespace util { 34 namespace util {
49 35
50 namespace { 36 namespace {
51 37
52 std::string ReadStringFromGDocFile(const base::FilePath& file_path, 38 std::string ReadStringFromGDocFile(const base::FilePath& file_path,
53 const std::string& key) { 39 const std::string& key) {
54 const int64 kMaxGDocSize = 4096; 40 const int64 kMaxGDocSize = 4096;
55 int64 file_size = 0; 41 int64 file_size = 0;
56 if (!base::GetFileSize(file_path, &file_size) || 42 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(); 43 LOG(WARNING) << "File too large to be a GDoc file " << file_path.value();
59 return std::string(); 44 return std::string();
60 } 45 }
61 46
62 JSONFileValueDeserializer reader(file_path); 47 JSONFileValueDeserializer reader(file_path);
63 std::string error_message; 48 std::string error_message;
64 scoped_ptr<base::Value> root_value(reader.Deserialize(NULL, &error_message)); 49 scoped_ptr<base::Value> root_value(reader.Deserialize(NULL, &error_message));
65 if (!root_value) { 50 if (!root_value) {
66 LOG(WARNING) << "Failed to parse " << file_path.value() << " as JSON." 51 LOG(WARNING) << "Failed to parse " << file_path.value() << " as JSON."
67 << " error = " << error_message; 52 << " error = " << error_message;
68 return std::string(); 53 return std::string();
69 } 54 }
70 55
71 base::DictionaryValue* dictionary_value = NULL; 56 base::DictionaryValue* dictionary_value = NULL;
72 std::string result; 57 std::string result;
73 if (!root_value->GetAsDictionary(&dictionary_value) || 58 if (!root_value->GetAsDictionary(&dictionary_value) ||
74 !dictionary_value->GetString(key, &result)) { 59 !dictionary_value->GetString(key, &result)) {
75 LOG(WARNING) << "No value for the given key is stored in " 60 LOG(WARNING) << "No value for the given key is stored in "
76 << file_path.value() << ". key = " << key; 61 << file_path.value() << ". key = " << key;
77 return std::string(); 62 return std::string();
78 } 63 }
79 64
80 return result; 65 return result;
81 } 66 }
82 67
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 68 } // namespace
94 69
95 const base::FilePath& GetDriveGrandRootPath() { 70 const base::FilePath& GetDriveGrandRootPath() {
96 CR_DEFINE_STATIC_LOCAL( 71 CR_DEFINE_STATIC_LOCAL(
97 base::FilePath, grand_root_path, 72 base::FilePath, grand_root_path,
98 (base::FilePath::FromUTF8Unsafe(kDriveGrandRootDirName))); 73 (base::FilePath::FromUTF8Unsafe(kDriveGrandRootDirName)));
99 return grand_root_path; 74 return grand_root_path;
100 } 75 }
101 76
102 const base::FilePath& GetDriveMyDriveRootPath() { 77 const base::FilePath& GetDriveMyDriveRootPath() {
103 CR_DEFINE_STATIC_LOCAL( 78 CR_DEFINE_STATIC_LOCAL(
104 base::FilePath, drive_root_path, 79 base::FilePath, drive_root_path,
105 (GetDriveGrandRootPath().AppendASCII(kDriveMyDriveRootDirName))); 80 (GetDriveGrandRootPath().AppendASCII(kDriveMyDriveRootDirName)));
106 return drive_root_path; 81 return drive_root_path;
107 } 82 }
108 83
109 base::FilePath GetDriveMountPointPathForUserIdHash( 84 base::FilePath GetDriveMountPointPathForUserIdHash(
110 const std::string user_id_hash) { 85 const std::string user_id_hash) {
111 static const base::FilePath::CharType kSpecialMountPointRoot[] = 86 static const base::FilePath::CharType kSpecialMountPointRoot[] =
112 FILE_PATH_LITERAL("/special"); 87 FILE_PATH_LITERAL("/special");
113 static const char kDriveMountPointNameBase[] = "drive"; 88 static const char kDriveMountPointNameBase[] = "drive";
114 return base::FilePath(kSpecialMountPointRoot).AppendASCII( 89 return base::FilePath(kSpecialMountPointRoot)
115 net::EscapeQueryParamValue( 90 .AppendASCII(net::EscapeQueryParamValue(
116 kDriveMountPointNameBase + 91 kDriveMountPointNameBase +
117 (user_id_hash.empty() ? "" : "-" + user_id_hash), false)); 92 (user_id_hash.empty() ? "" : "-" + user_id_hash),
118 } 93 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 } 94 }
174 95
175 bool IsUnderDriveMountPoint(const base::FilePath& path) { 96 bool IsUnderDriveMountPoint(const base::FilePath& path) {
176 return !ExtractDrivePath(path).empty(); 97 return !ExtractDrivePath(path).empty();
177 } 98 }
178 99
179 base::FilePath ExtractDrivePath(const base::FilePath& path) { 100 base::FilePath ExtractDrivePath(const base::FilePath& path) {
180 std::vector<base::FilePath::StringType> components; 101 std::vector<base::FilePath::StringType> components;
181 path.GetComponents(&components); 102 path.GetComponents(&components);
182 if (components.size() < 3) 103 if (components.size() < 3)
183 return base::FilePath(); 104 return base::FilePath();
184 if (components[0] != FILE_PATH_LITERAL("/")) 105 if (components[0] != FILE_PATH_LITERAL("/"))
185 return base::FilePath(); 106 return base::FilePath();
186 if (components[1] != FILE_PATH_LITERAL("special")) 107 if (components[1] != FILE_PATH_LITERAL("special"))
187 return base::FilePath(); 108 return base::FilePath();
188 static const base::FilePath::CharType kPrefix[] = FILE_PATH_LITERAL("drive"); 109 static const base::FilePath::CharType kPrefix[] = FILE_PATH_LITERAL("drive");
189 if (components[2].compare(0, arraysize(kPrefix) - 1, kPrefix) != 0) 110 if (components[2].compare(0, arraysize(kPrefix) - 1, kPrefix) != 0)
190 return base::FilePath(); 111 return base::FilePath();
191 112
192 base::FilePath drive_path = GetDriveGrandRootPath(); 113 base::FilePath drive_path = GetDriveGrandRootPath();
193 for (size_t i = 3; i < components.size(); ++i) 114 for (size_t i = 3; i < components.size(); ++i)
194 drive_path = drive_path.Append(components[i]); 115 drive_path = drive_path.Append(components[i]);
195 return drive_path; 116 return drive_path;
196 } 117 }
197 118
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( 119 base::FilePath ExtractDrivePathFromFileSystemUrl(
hashimoto 2015/06/19 01:03:24 Do you need this function in this file? By omittin
Łukasz Anforowicz 2015/06/19 17:30:40 Done (moved back to file_system_util.cc). Thanks
216 const storage::FileSystemURL& url) { 120 const storage::FileSystemURL& url) {
217 if (!url.is_valid() || url.type() != storage::kFileSystemTypeDrive) 121 if (!url.is_valid() || url.type() != storage::kFileSystemTypeDrive)
218 return base::FilePath(); 122 return base::FilePath();
219 return ExtractDrivePath(url.path()); 123 return ExtractDrivePath(url.path());
220 } 124 }
221 125
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) { 126 std::string EscapeCacheFileName(const std::string& filename) {
233 // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape 127 // This is based on net/base/escape.cc: net::(anonymous namespace)::Escape
234 std::string escaped; 128 std::string escaped;
235 for (size_t i = 0; i < filename.size(); ++i) { 129 for (size_t i = 0; i < filename.size(); ++i) {
236 char c = filename[i]; 130 char c = filename[i];
237 if (c == '%' || c == '.' || c == '/') { 131 if (c == '%' || c == '.' || c == '/') {
238 base::StringAppendF(&escaped, "%%%02X", c); 132 base::StringAppendF(&escaped, "%%%02X", c);
239 } else { 133 } else {
240 escaped.push_back(c); 134 escaped.push_back(c);
241 } 135 }
242 } 136 }
243 return escaped; 137 return escaped;
244 } 138 }
245 139
246 std::string UnescapeCacheFileName(const std::string& filename) { 140 std::string UnescapeCacheFileName(const std::string& filename) {
247 std::string unescaped; 141 std::string unescaped;
248 for (size_t i = 0; i < filename.size(); ++i) { 142 for (size_t i = 0; i < filename.size(); ++i) {
249 char c = filename[i]; 143 char c = filename[i];
250 if (c == '%' && i + 2 < filename.length()) { 144 if (c == '%' && i + 2 < filename.length()) {
251 c = (HexDigitToInt(filename[i + 1]) << 4) + 145 c = (HexDigitToInt(filename[i + 1]) << 4) +
252 HexDigitToInt(filename[i + 2]); 146 HexDigitToInt(filename[i + 2]);
253 i += 2; 147 i += 2;
254 } 148 }
255 unescaped.push_back(c); 149 unescaped.push_back(c);
256 } 150 }
257 return unescaped; 151 return unescaped;
258 } 152 }
259 153
260 std::string NormalizeFileName(const std::string& input) { 154 std::string NormalizeFileName(const std::string& input) {
261 DCHECK(base::IsStringUTF8(input)); 155 DCHECK(base::IsStringUTF8(input));
262 156
263 std::string output; 157 std::string output;
264 if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output)) 158 if (!base::ConvertToUtf8AndNormalize(input, base::kCodepageUTF8, &output))
265 output = input; 159 output = input;
266 base::ReplaceChars(output, "/", "_", &output); 160 base::ReplaceChars(output, "/", "_", &output);
267 if (!output.empty() && output.find_first_not_of('.', 0) == std::string::npos) 161 if (!output.empty() && output.find_first_not_of('.', 0) == std::string::npos)
268 output = "_"; 162 output = "_";
269 return output; 163 return output;
270 } 164 }
271 165
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) { 166 void EmptyFileOperationCallback(FileError error) {
311 } 167 }
312 168
313 bool CreateGDocFile(const base::FilePath& file_path, 169 bool CreateGDocFile(const base::FilePath& file_path,
314 const GURL& url, 170 const GURL& url,
315 const std::string& resource_id) { 171 const std::string& resource_id) {
316 std::string content = base::StringPrintf( 172 std::string content =
317 "{\"url\": \"%s\", \"resource_id\": \"%s\"}", 173 base::StringPrintf("{\"url\": \"%s\", \"resource_id\": \"%s\"}",
318 url.spec().c_str(), resource_id.c_str()); 174 url.spec().c_str(), resource_id.c_str());
319 return base::WriteFile(file_path, content.data(), content.size()) == 175 return base::WriteFile(file_path, content.data(), content.size()) ==
320 static_cast<int>(content.size()); 176 static_cast<int>(content.size());
321 } 177 }
322 178
323 GURL ReadUrlFromGDocFile(const base::FilePath& file_path) { 179 GURL ReadUrlFromGDocFile(const base::FilePath& file_path) {
324 return GURL(ReadStringFromGDocFile(file_path, "url")); 180 return GURL(ReadStringFromGDocFile(file_path, "url"));
325 } 181 }
326 182
327 std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path) { 183 std::string ReadResourceIdFromGDocFile(const base::FilePath& file_path) {
328 return ReadStringFromGDocFile(file_path, "resource_id"); 184 return ReadStringFromGDocFile(file_path, "resource_id");
329 } 185 }
330 186
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 187 } // namespace util
368 } // namespace drive 188 } // namespace drive
OLDNEW
« no previous file with comments | « chrome/browser/chromeos/drive/file_system_core_util.h ('k') | chrome/browser/chromeos/drive/file_system_core_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698