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/drive_pref_names.h"
28 #include "chrome/browser/chromeos/drive/file_system_core_util.h"
29 #include "chrome/browser/chromeos/drive/file_system_interface.h"
30 #include "chrome/browser/chromeos/drive/job_list.h"
31 #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 "chromeos/chromeos_constants.h"
39 #include "components/drive/drive.pb.h"
40 #include "components/user_manager/user_manager.h"
41 #include "content/public/browser/browser_thread.h"
42 #include "storage/browser/fileapi/file_system_url.h"
44 using content::BrowserThread
;
51 // Returns DriveIntegrationService instance, if Drive is enabled.
53 DriveIntegrationService
* GetIntegrationServiceByProfile(Profile
* profile
) {
54 DriveIntegrationService
* service
=
55 DriveIntegrationServiceFactory::FindForProfile(profile
);
56 if (!service
|| !service
->IsMounted())
63 base::FilePath
GetDriveMountPointPath(Profile
* profile
) {
64 std::string id
= chromeos::ProfileHelper::GetUserIdHashFromProfile(profile
);
65 if (id
.empty() || id
== chrome::kLegacyProfileDir
) {
66 // ProfileHelper::GetUserIdHashFromProfile works only when multi-profile is
67 // enabled. In that case, we fall back to use UserManager (it basically just
68 // returns currently active users's hash in such a case.) I still try
69 // ProfileHelper first because it works better in tests.
70 const user_manager::User
* const user
=
71 user_manager::UserManager::IsInitialized()
72 ? chromeos::ProfileHelper::Get()->GetUserByProfile(
73 profile
->GetOriginalProfile())
76 id
= user
->username_hash();
78 return GetDriveMountPointPathForUserIdHash(id
);
81 FileSystemInterface
* GetFileSystemByProfile(Profile
* profile
) {
82 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
84 DriveIntegrationService
* integration_service
=
85 GetIntegrationServiceByProfile(profile
);
86 return integration_service
? integration_service
->file_system() : NULL
;
89 FileSystemInterface
* GetFileSystemByProfileId(void* profile_id
) {
90 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
92 // |profile_id| needs to be checked with ProfileManager::IsValidProfile
94 Profile
* profile
= reinterpret_cast<Profile
*>(profile_id
);
95 if (!g_browser_process
->profile_manager()->IsValidProfile(profile
))
97 return GetFileSystemByProfile(profile
);
100 DriveAppRegistry
* GetDriveAppRegistryByProfile(Profile
* profile
) {
101 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
103 DriveIntegrationService
* integration_service
=
104 GetIntegrationServiceByProfile(profile
);
105 return integration_service
? integration_service
->drive_app_registry() : NULL
;
108 DriveServiceInterface
* GetDriveServiceByProfile(Profile
* profile
) {
109 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
111 DriveIntegrationService
* integration_service
=
112 GetIntegrationServiceByProfile(profile
);
113 return integration_service
? integration_service
->drive_service() : NULL
;
116 Profile
* ExtractProfileFromPath(const base::FilePath
& path
) {
117 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
119 const std::vector
<Profile
*>& profiles
=
120 g_browser_process
->profile_manager()->GetLoadedProfiles();
121 for (size_t i
= 0; i
< profiles
.size(); ++i
) {
122 Profile
* original_profile
= profiles
[i
]->GetOriginalProfile();
123 if (original_profile
== profiles
[i
] &&
124 !chromeos::ProfileHelper::IsSigninProfile(original_profile
)) {
125 const base::FilePath base
= GetDriveMountPointPath(original_profile
);
126 if (base
== path
|| base
.IsParent(path
))
127 return original_profile
;
133 base::FilePath
ExtractDrivePathFromFileSystemUrl(
134 const storage::FileSystemURL
& url
) {
135 if (!url
.is_valid() || url
.type() != storage::kFileSystemTypeDrive
)
136 return base::FilePath();
137 return ExtractDrivePath(url
.path());
140 base::FilePath
GetCacheRootPath(Profile
* profile
) {
141 base::FilePath cache_base_path
;
142 chrome::GetUserCacheDirectory(profile
->GetPath(), &cache_base_path
);
143 base::FilePath cache_root_path
=
144 cache_base_path
.Append(chromeos::kDriveCacheDirname
);
145 static const base::FilePath::CharType kFileCacheVersionDir
[] =
146 FILE_PATH_LITERAL("v1");
147 return cache_root_path
.Append(kFileCacheVersionDir
);
150 void PrepareWritableFileAndRun(Profile
* profile
,
151 const base::FilePath
& path
,
152 const PrepareWritableFileCallback
& callback
) {
153 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
154 DCHECK(!callback
.is_null());
156 FileSystemInterface
* file_system
= GetFileSystemByProfile(profile
);
157 if (!file_system
|| !IsUnderDriveMountPoint(path
)) {
158 content::BrowserThread::GetBlockingPool()->PostTask(
159 FROM_HERE
, base::Bind(callback
, FILE_ERROR_FAILED
, base::FilePath()));
163 WriteOnCacheFile(file_system
, ExtractDrivePath(path
),
164 std::string(), // mime_type
168 void EnsureDirectoryExists(Profile
* profile
,
169 const base::FilePath
& directory
,
170 const FileOperationCallback
& callback
) {
171 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
172 DCHECK(!callback
.is_null());
173 if (IsUnderDriveMountPoint(directory
)) {
174 FileSystemInterface
* file_system
= GetFileSystemByProfile(profile
);
176 file_system
->CreateDirectory(ExtractDrivePath(directory
),
177 true /* is_exclusive */,
178 true /* is_recursive */, callback
);
180 base::ThreadTaskRunnerHandle::Get()->PostTask(
181 FROM_HERE
, base::Bind(callback
, FILE_ERROR_OK
));
185 bool IsDriveEnabledForProfile(Profile
* profile
) {
186 DCHECK_CURRENTLY_ON(BrowserThread::UI
);
188 if (!chromeos::IsProfileAssociatedWithGaiaAccount(profile
))
191 // Disable Drive if preference is set. This can happen with commandline flag
192 // --disable-drive or enterprise policy, or with user settings.
193 if (profile
->GetPrefs()->GetBoolean(prefs::kDisableDrive
))
199 ConnectionStatusType
GetDriveConnectionStatus(Profile
* profile
) {
200 drive::DriveServiceInterface
* const drive_service
=
201 drive::util::GetDriveServiceByProfile(profile
);
204 return DRIVE_DISCONNECTED_NOSERVICE
;
205 if (net::NetworkChangeNotifier::IsOffline())
206 return DRIVE_DISCONNECTED_NONETWORK
;
207 if (!drive_service
->CanSendRequest())
208 return DRIVE_DISCONNECTED_NOTREADY
;
210 const bool is_connection_cellular
=
211 net::NetworkChangeNotifier::IsConnectionCellular(
212 net::NetworkChangeNotifier::GetConnectionType());
213 const bool disable_sync_over_celluar
=
214 profile
->GetPrefs()->GetBoolean(prefs::kDisableDriveOverCellular
);
216 if (is_connection_cellular
&& disable_sync_over_celluar
)
217 return DRIVE_CONNECTED_METERED
;
218 return DRIVE_CONNECTED
;