1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/chromeos/file_manager/volume_manager.h"
#include "base/basictypes.h"
#include "base/bind.h"
#include "base/callback.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/singleton.h"
#include "base/path_service.h"
#include "base/prefs/pref_service.h"
#include "chrome/browser/chromeos/drive/drive_integration_service.h"
#include "chrome/browser/chromeos/drive/file_errors.h"
#include "chrome/browser/chromeos/drive/file_system_interface.h"
#include "chrome/browser/chromeos/drive/file_system_util.h"
#include "chrome/browser/chromeos/file_manager/mounted_disk_monitor.h"
#include "chrome/browser/chromeos/file_manager/volume_manager_factory.h"
#include "chrome/browser/chromeos/file_manager/volume_manager_observer.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/pref_names.h"
#include "chromeos/dbus/cros_disks_client.h"
#include "chromeos/disks/disk_mount_manager.h"
#include "content/public/browser/browser_thread.h"
namespace file_manager {
namespace {
// Called on completion of MarkCacheFileAsUnmounted.
void OnMarkCacheFileAsUnmounted(drive::FileError error) {
// Do nothing.
}
VolumeType MountTypeToVolumeType(
chromeos::MountType type) {
switch (type) {
case chromeos::MOUNT_TYPE_DEVICE:
return VOLUME_TYPE_REMOVABLE_DISK_PARTITION;
case chromeos::MOUNT_TYPE_ARCHIVE:
return VOLUME_TYPE_MOUNTED_ARCHIVE_FILE;
default:
NOTREACHED();
}
return VOLUME_TYPE_DOWNLOADS_DIRECTORY;
}
// Returns the VolumeInfo for Drive file system.
VolumeInfo CreateDriveVolumeInfo() {
const base::FilePath& drive_path = drive::util::GetDriveMountPointPath();
VolumeInfo volume_info;
volume_info.type = VOLUME_TYPE_GOOGLE_DRIVE;
volume_info.device_type = chromeos::DEVICE_TYPE_UNKNOWN;
volume_info.source_path = drive_path;
volume_info.mount_path = drive_path;
volume_info.mount_condition = chromeos::disks::MOUNT_CONDITION_NONE;
volume_info.is_parent = false;
volume_info.is_read_only = false;
return volume_info;
}
VolumeInfo CreateDownloadsVolumeInfo(
const base::FilePath& downloads_path) {
VolumeInfo volume_info;
volume_info.type = VOLUME_TYPE_DOWNLOADS_DIRECTORY;
volume_info.device_type = chromeos::DEVICE_TYPE_UNKNOWN;
// Keep source_path empty.
volume_info.mount_path = downloads_path;
volume_info.mount_condition = chromeos::disks::MOUNT_CONDITION_NONE;
volume_info.is_parent = false;
volume_info.is_read_only = false;
return volume_info;
}
VolumeInfo CreateVolumeInfoFromMountPointInfo(
const chromeos::disks::DiskMountManager::MountPointInfo& mount_point,
const chromeos::disks::DiskMountManager::Disk* disk) {
VolumeInfo volume_info;
volume_info.type = MountTypeToVolumeType(mount_point.mount_type);
volume_info.source_path = base::FilePath(mount_point.source_path);
volume_info.mount_path = base::FilePath(mount_point.mount_path);
volume_info.mount_condition = mount_point.mount_condition;
if (disk) {
volume_info.device_type = disk->device_type();
volume_info.system_path_prefix =
base::FilePath(disk->system_path_prefix());
volume_info.drive_label = disk->drive_label();
volume_info.is_parent = disk->is_parent();
volume_info.is_read_only = disk->is_read_only();
} else {
volume_info.device_type = chromeos::DEVICE_TYPE_UNKNOWN;
volume_info.is_parent = false;
volume_info.is_read_only = false;
}
return volume_info;
}
} // namespace
VolumeInfo::VolumeInfo() {
}
VolumeInfo::~VolumeInfo() {
}
VolumeManager::VolumeManager(
Profile* profile,
drive::DriveIntegrationService* drive_integration_service,
chromeos::PowerManagerClient* power_manager_client,
chromeos::disks::DiskMountManager* disk_mount_manager)
: profile_(profile),
drive_integration_service_(drive_integration_service),
disk_mount_manager_(disk_mount_manager),
mounted_disk_monitor_(
new MountedDiskMonitor(power_manager_client, disk_mount_manager)) {
DCHECK(disk_mount_manager);
}
VolumeManager::~VolumeManager() {
}
VolumeManager* VolumeManager::Get(content::BrowserContext* context) {
return VolumeManagerFactory::Get(context);
}
void VolumeManager::Initialize() {
if (drive_integration_service_)
drive_integration_service_->AddObserver(this);
// Subscribe to DiskMountManager.
disk_mount_manager_->AddObserver(this);
disk_mount_manager_->RequestMountInfoRefresh();
// Subscribe to Profile Preference change.
pref_change_registrar_.Init(profile_->GetPrefs());
pref_change_registrar_.Add(
prefs::kExternalStorageDisabled,
base::Bind(&VolumeManager::OnExternalStorageDisabledChanged,
base::Unretained(this)));
}
void VolumeManager::Shutdown() {
pref_change_registrar_.RemoveAll();
disk_mount_manager_->RemoveObserver(this);
if (drive_integration_service_)
drive_integration_service_->RemoveObserver(this);
}
void VolumeManager::AddObserver(VolumeManagerObserver* observer) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(observer);
observers_.AddObserver(observer);
}
void VolumeManager::RemoveObserver(VolumeManagerObserver* observer) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(observer);
observers_.RemoveObserver(observer);
}
std::vector<VolumeInfo> VolumeManager::GetVolumeInfoList() const {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
std::vector<VolumeInfo> result;
// Adds "Drive" volume.
if (drive_integration_service_ && drive_integration_service_->IsMounted())
result.push_back(CreateDriveVolumeInfo());
// Adds "Downloads".
base::FilePath home_path;
if (PathService::Get(base::DIR_HOME, &home_path)) {
result.push_back(
CreateDownloadsVolumeInfo(home_path.AppendASCII("Downloads")));
}
// Adds disks (both removable disks and zip archives).
const chromeos::disks::DiskMountManager::MountPointMap& mount_points =
disk_mount_manager_->mount_points();
for (chromeos::disks::DiskMountManager::MountPointMap::const_iterator it =
mount_points.begin();
it != mount_points.end(); ++it) {
if (it->second.mount_type == chromeos::MOUNT_TYPE_DEVICE ||
it->second.mount_type == chromeos::MOUNT_TYPE_ARCHIVE)
result.push_back(CreateVolumeInfoFromMountPointInfo(
it->second,
disk_mount_manager_->FindDiskBySourcePath(it->second.source_path)));
}
return result;
}
void VolumeManager::OnFileSystemMounted() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Raise mount event.
// We can pass chromeos::MOUNT_ERROR_NONE even when authentication is failed
// or network is unreachable. These two errors will be handled later.
VolumeInfo volume_info = CreateDriveVolumeInfo();
FOR_EACH_OBSERVER(VolumeManagerObserver, observers_,
OnVolumeMounted(chromeos::MOUNT_ERROR_NONE,
volume_info,
false)); // Not remounting.
}
void VolumeManager::OnFileSystemBeingUnmounted() {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
VolumeInfo volume_info = CreateDriveVolumeInfo();
FOR_EACH_OBSERVER(
VolumeManagerObserver, observers_,
OnVolumeUnmounted(chromeos::MOUNT_ERROR_NONE, volume_info));
}
void VolumeManager::OnDiskEvent(
chromeos::disks::DiskMountManager::DiskEvent event,
const chromeos::disks::DiskMountManager::Disk* disk) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
// Disregard hidden devices.
if (disk->is_hidden())
return;
switch (event) {
case chromeos::disks::DiskMountManager::DISK_ADDED: {
if (disk->device_path().empty()) {
DVLOG(1) << "Empty system path for " << disk->device_path();
return;
}
bool mounting = false;
if (disk->mount_path().empty() && disk->has_media() &&
!profile_->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled)) {
// If disk is not mounted yet and it has media and there is no policy
// forbidding external storage, give it a try.
// Initiate disk mount operation. MountPath auto-detects the filesystem
// format if the second argument is empty. The third argument (mount
// label) is not used in a disk mount operation.
disk_mount_manager_->MountPath(
disk->device_path(), std::string(), std::string(),
chromeos::MOUNT_TYPE_DEVICE);
mounting = true;
}
// Notify to observers.
FOR_EACH_OBSERVER(VolumeManagerObserver, observers_,
OnDiskAdded(*disk, mounting));
return;
}
case chromeos::disks::DiskMountManager::DISK_REMOVED:
// If the disk is already mounted, unmount it.
if (!disk->mount_path().empty()) {
disk_mount_manager_->UnmountPath(
disk->mount_path(),
chromeos::UNMOUNT_OPTIONS_LAZY,
chromeos::disks::DiskMountManager::UnmountPathCallback());
}
// Notify to observers.
FOR_EACH_OBSERVER(VolumeManagerObserver, observers_,
OnDiskRemoved(*disk));
return;
case chromeos::disks::DiskMountManager::DISK_CHANGED:
DVLOG(1) << "Ignore CHANGED event.";
return;
}
NOTREACHED();
}
void VolumeManager::OnDeviceEvent(
chromeos::disks::DiskMountManager::DeviceEvent event,
const std::string& device_path) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DVLOG(1) << "OnDeviceEvent: " << event << ", " << device_path;
switch (event) {
case chromeos::disks::DiskMountManager::DEVICE_ADDED:
FOR_EACH_OBSERVER(VolumeManagerObserver, observers_,
OnDeviceAdded(device_path));
return;
case chromeos::disks::DiskMountManager::DEVICE_REMOVED:
FOR_EACH_OBSERVER(VolumeManagerObserver, observers_,
OnDeviceRemoved(device_path));
return;
case chromeos::disks::DiskMountManager::DEVICE_SCANNED:
DVLOG(1) << "Ignore SCANNED event: " << device_path;
return;
}
NOTREACHED();
}
void VolumeManager::OnMountEvent(
chromeos::disks::DiskMountManager::MountEvent event,
chromeos::MountError error_code,
const chromeos::disks::DiskMountManager::MountPointInfo& mount_info) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DCHECK(mount_info.mount_type != chromeos::MOUNT_TYPE_INVALID);
if (mount_info.mount_type == chromeos::MOUNT_TYPE_ARCHIVE) {
// If the file is not mounted now, tell it to drive file system so that
// it can handle file caching correctly.
// Note that drive file system knows if the file is managed by drive file
// system or not, so here we report all paths.
if ((event == chromeos::disks::DiskMountManager::MOUNTING &&
error_code != chromeos::MOUNT_ERROR_NONE) ||
(event == chromeos::disks::DiskMountManager::UNMOUNTING &&
error_code == chromeos::MOUNT_ERROR_NONE)) {
drive::FileSystemInterface* file_system =
drive::util::GetFileSystemByProfile(profile_);
if (file_system) {
file_system->MarkCacheFileAsUnmounted(
base::FilePath(mount_info.source_path),
base::Bind(&OnMarkCacheFileAsUnmounted));
}
}
}
// Notify a mounting/unmounting event to observers.
const chromeos::disks::DiskMountManager::Disk* disk =
disk_mount_manager_->FindDiskBySourcePath(mount_info.source_path);
VolumeInfo volume_info =
CreateVolumeInfoFromMountPointInfo(mount_info, disk);
switch (event) {
case chromeos::disks::DiskMountManager::MOUNTING: {
bool is_remounting =
disk && mounted_disk_monitor_->DiskIsRemounting(*disk);
FOR_EACH_OBSERVER(
VolumeManagerObserver, observers_,
OnVolumeMounted(error_code, volume_info, is_remounting));
return;
}
case chromeos::disks::DiskMountManager::UNMOUNTING:
FOR_EACH_OBSERVER(VolumeManagerObserver, observers_,
OnVolumeUnmounted(error_code, volume_info));
return;
}
NOTREACHED();
}
void VolumeManager::OnFormatEvent(
chromeos::disks::DiskMountManager::FormatEvent event,
chromeos::FormatError error_code,
const std::string& device_path) {
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
DVLOG(1) << "OnDeviceEvent: " << event << ", " << error_code
<< ", " << device_path;
switch (event) {
case chromeos::disks::DiskMountManager::FORMAT_STARTED:
FOR_EACH_OBSERVER(
VolumeManagerObserver, observers_,
OnFormatStarted(device_path,
error_code == chromeos::FORMAT_ERROR_NONE));
return;
case chromeos::disks::DiskMountManager::FORMAT_COMPLETED:
if (error_code == chromeos::FORMAT_ERROR_NONE) {
// If format is completed successfully, try to mount the device.
// MountPath auto-detects filesystem format if second argument is
// empty. The third argument (mount label) is not used in a disk mount
// operation.
disk_mount_manager_->MountPath(
device_path, std::string(), std::string(),
chromeos::MOUNT_TYPE_DEVICE);
}
FOR_EACH_OBSERVER(
VolumeManagerObserver, observers_,
OnFormatCompleted(device_path,
error_code == chromeos::FORMAT_ERROR_NONE));
return;
}
NOTREACHED();
}
void VolumeManager::OnExternalStorageDisabledChanged() {
// If the policy just got disabled we have to unmount every device currently
// mounted. The opposite is fine - we can let the user re-plug her device to
// make it available.
if (profile_->GetPrefs()->GetBoolean(prefs::kExternalStorageDisabled)) {
// We do not iterate on mount_points directly, because mount_points can
// be changed by UnmountPath().
// TODO(hidehiko): Is it necessary to unmount mounted archives, too, here?
while (!disk_mount_manager_->mount_points().empty()) {
std::string mount_path =
disk_mount_manager_->mount_points().begin()->second.mount_path;
LOG(INFO) << "Unmounting " << mount_path << " because of preference.";
disk_mount_manager_->UnmountPath(
mount_path,
chromeos::UNMOUNT_OPTIONS_NONE,
chromeos::disks::DiskMountManager::UnmountPathCallback());
}
}
}
} // namespace file_manager
|