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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/drive/file_system_util.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/i18n/icu_string_conversions.h"
16 #include "base/json/json_file_value_serializer.h"
17 #include "base/logging.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/prefs/pref_service.h"
20 #include "base/strings/string_number_conversions.h"
21 #include "base/strings/string_util.h"
22 #include "base/strings/stringprintf.h"
23 #include "base/thread_task_runner_handle.h"
24 #include "base/threading/sequenced_worker_pool.h"
25 #include "chrome/browser/browser_process.h"
26 #include "chrome/browser/chromeos/drive/drive_integration_service.h"
27 #include "chrome/browser/chromeos/drive/file_system_interface.h"
28 #include "chrome/browser/chromeos/drive/write_on_cache_file.h"
29 #include "chrome/browser/chromeos/profiles/profile_helper.h"
30 #include "chrome/browser/chromeos/profiles/profile_util.h"
31 #include "chrome/browser/profiles/profile.h"
32 #include "chrome/browser/profiles/profile_manager.h"
33 #include "chrome/common/chrome_constants.h"
34 #include "chrome/common/chrome_paths_internal.h"
35 #include "chromeos/chromeos_constants.h"
36 #include "components/drive/drive.pb.h"
37 #include "components/drive/drive_pref_names.h"
38 #include "components/drive/file_system_core_util.h"
39 #include "components/drive/job_list.h"
40 #include "components/user_manager/user_manager.h"
41 #include "content/public/browser/browser_thread.h"
42 #include "net/base/escape.h"
43 #include "storage/browser/fileapi/file_system_url.h"
45 using content::BrowserThread
;
52 // Returns DriveIntegrationService instance, if Drive is enabled.
54 DriveIntegrationService
* GetIntegrationServiceByProfile(Profile
* profile
) {
55 DriveIntegrationService
* service
=
56 DriveIntegrationServiceFactory::FindForProfile(profile
);
57 if (!service
|| !service
->IsMounted())
64 base::FilePath
GetDriveMountPointPath(Profile
* profile
) {
65 std::string id
= chromeos::ProfileHelper::GetUserIdHashFromProfile(profile
);
66 if (id
.empty() || id
== chrome::kLegacyProfileDir
) {
67 // ProfileHelper::GetUserIdHashFromProfile works only when multi-profile is
68 // enabled. In that case, we fall back to use UserManager (it basically just
69 // returns currently active users's hash in such a case.) I still try
70 // ProfileHelper first because it works better in tests.
71 const user_manager::User
* const user
=
72 user_manager::UserManager::IsInitialized()
73 ? chromeos::ProfileHelper::Get()->GetUserByProfile(
74 profile
->GetOriginalProfile())
77 id
= user
->username_hash();
79 return GetDriveMountPointPathForUserIdHash(id
);
82 base::FilePath
GetDriveMountPointPathForUserIdHash(
83 const std::string user_id_hash
) {
84 static const base::FilePath::CharType kSpecialMountPointRoot
[] =
85 FILE_PATH_LITERAL("/special");
86 static const char kDriveMountPointNameBase
[] = "drive";
87 return base::FilePath(kSpecialMountPointRoot
)
88 .AppendASCII(net::EscapeQueryParamValue(
89 kDriveMountPointNameBase
+
90 (user_id_hash
.empty() ? "" : "-" + user_id_hash
),
94 bool IsUnderDriveMountPoint(const base::FilePath
& path
) {
95 return !ExtractDrivePath(path
).empty();
98 base::FilePath
ExtractDrivePath(const base::FilePath
& path
) {
99 std::vector
<base::FilePath::StringType
> components
;
100 path
.GetComponents(&components
);
101 if (components
.size() < 3)
102 return base::FilePath();
103 if (components
[0] != FILE_PATH_LITERAL("/"))
104 return base::FilePath();
105 if (components
[1] != FILE_PATH_LITERAL("special"))
106 return base::FilePath();
107 static const base::FilePath::CharType kPrefix
[] = FILE_PATH_LITERAL("drive");
108 if (components
[2].compare(0, arraysize(kPrefix
) - 1, kPrefix
) != 0)
109 return base::FilePath();
111 base::FilePath drive_path
= GetDriveGrandRootPath();
112 for (size_t i
= 3; i
< components
.size(); ++i
)
113 drive_path
= drive_path
.Append(components
[i
]);
117 FileSystemInterface
* GetFileSystemByProfile(Profile
* profile
) {
118 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
120 DriveIntegrationService
* integration_service
=
121 GetIntegrationServiceByProfile(profile
);
122 return integration_service
? integration_service
->file_system() : NULL
;
125 FileSystemInterface
* GetFileSystemByProfileId(void* profile_id
) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
128 // |profile_id| needs to be checked with ProfileManager::IsValidProfile
130 Profile
* profile
= reinterpret_cast<Profile
*>(profile_id
);
131 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
))
133 return GetFileSystemByProfile(profile
);
136 DriveAppRegistry
* GetDriveAppRegistryByProfile(Profile
* profile
) {
137 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
139 DriveIntegrationService
* integration_service
=
140 GetIntegrationServiceByProfile(profile
);
141 return integration_service
? integration_service
->drive_app_registry() : NULL
;
144 DriveServiceInterface
* GetDriveServiceByProfile(Profile
* profile
) {
145 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
147 DriveIntegrationService
* integration_service
=
148 GetIntegrationServiceByProfile(profile
);
149 return integration_service
? integration_service
->drive_service() : NULL
;
152 Profile
* ExtractProfileFromPath(const base::FilePath
& path
) {
153 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
155 const std::vector
<Profile
*>& profiles
=
156 g_browser_process
->profile_manager()->GetLoadedProfiles();
157 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
158 Profile
* original_profile
= profiles
[i
]->GetOriginalProfile();
159 if (original_profile
== profiles
[i
] &&
160 !chromeos::ProfileHelper::IsSigninProfile(original_profile
)) {
161 const base::FilePath base
= GetDriveMountPointPath(original_profile
);
162 if (base
== path
|| base
.IsParent(path
))
163 return original_profile
;
169 base::FilePath
ExtractDrivePathFromFileSystemUrl(
170 const storage::FileSystemURL
& url
) {
171 if (!url
.is_valid() || url
.type() != storage::kFileSystemTypeDrive
)
172 return base::FilePath();
173 return ExtractDrivePath(url
.path());
176 base::FilePath
GetCacheRootPath(Profile
* profile
) {
177 base::FilePath cache_base_path
;
178 chrome::GetUserCacheDirectory(profile
->GetPath(), &cache_base_path
);
179 base::FilePath cache_root_path
=
180 cache_base_path
.Append(chromeos::kDriveCacheDirname
);
181 static const base::FilePath::CharType kFileCacheVersionDir
[] =
182 FILE_PATH_LITERAL("v1");
183 return cache_root_path
.Append(kFileCacheVersionDir
);
186 void PrepareWritableFileAndRun(Profile
* profile
,
187 const base::FilePath
& path
,
188 const PrepareWritableFileCallback
& callback
) {
189 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
190 DCHECK(!callback
.is_null());
192 FileSystemInterface
* file_system
= GetFileSystemByProfile(profile
);
193 if (!file_system
|| !IsUnderDriveMountPoint(path
)) {
194 content::BrowserThread::GetBlockingPool()->PostTask(
195 FROM_HERE
, base::Bind(callback
, FILE_ERROR_FAILED
, base::FilePath()));
199 WriteOnCacheFile(file_system
, ExtractDrivePath(path
),
200 std::string(), // mime_type
204 void EnsureDirectoryExists(Profile
* profile
,
205 const base::FilePath
& directory
,
206 const FileOperationCallback
& callback
) {
207 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
208 DCHECK(!callback
.is_null());
209 if (IsUnderDriveMountPoint(directory
)) {
210 FileSystemInterface
* file_system
= GetFileSystemByProfile(profile
);
212 file_system
->CreateDirectory(ExtractDrivePath(directory
),
213 true /* is_exclusive */,
214 true /* is_recursive */, callback
);
216 base::ThreadTaskRunnerHandle::Get()->PostTask(
217 FROM_HERE
, base::Bind(callback
, FILE_ERROR_OK
));
221 bool IsDriveEnabledForProfile(Profile
* profile
) {
222 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
224 if (!chromeos::IsProfileAssociatedWithGaiaAccount(profile
))
227 // Disable Drive if preference is set. This can happen with commandline flag
228 // --disable-drive or enterprise policy, or with user settings.
229 if (profile
->GetPrefs()->GetBoolean(prefs::kDisableDrive
))
235 ConnectionStatusType
GetDriveConnectionStatus(Profile
* profile
) {
236 drive::DriveServiceInterface
* const drive_service
=
237 drive::util::GetDriveServiceByProfile(profile
);
240 return DRIVE_DISCONNECTED_NOSERVICE
;
241 if (net::NetworkChangeNotifier::IsOffline())
242 return DRIVE_DISCONNECTED_NONETWORK
;
243 if (!drive_service
->CanSendRequest())
244 return DRIVE_DISCONNECTED_NOTREADY
;
246 const bool is_connection_cellular
=
247 net::NetworkChangeNotifier::IsConnectionCellular(
248 net::NetworkChangeNotifier::GetConnectionType());
249 const bool disable_sync_over_celluar
=
250 profile
->GetPrefs()->GetBoolean(prefs::kDisableDriveOverCellular
);
252 if (is_connection_cellular
&& disable_sync_over_celluar
)
253 return DRIVE_CONNECTED_METERED
;
254 return DRIVE_CONNECTED
;