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

Side by Side Diff: chrome/browser/media_gallery/media_device_notifications_chromeos.cc

Issue 10919051: Move device notification impl out of chrome/browser/media_gallery (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: modify for chromeos and win Created 8 years, 3 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
(Empty)
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.
4
5 // chromeos::MediaDeviceNotifications implementation.
6
7 #include "chrome/browser/media_gallery/media_device_notifications_chromeos.h"
8
9 #include "base/file_path.h"
10 #include "base/logging.h"
11 #include "base/metrics/histogram.h"
12 #include "base/stl_util.h"
13 #include "base/string_number_conversions.h"
14 #include "base/utf_string_conversions.h"
15 #include "chrome/browser/media_gallery/media_device_notifications_utils.h"
16 #include "chrome/browser/media_gallery/media_storage_util.h"
17 #include "content/public/browser/browser_thread.h"
18
19 namespace chromeos {
20
21 namespace {
22
23 bool GetDeviceInfo(const std::string& source_path, std::string* unique_id,
24 string16* device_label) {
25 // Get the media device uuid and label if exists.
26 const disks::DiskMountManager::Disk* disk =
27 disks::DiskMountManager::GetInstance()->FindDiskBySourcePath(source_path);
28 if (!disk)
29 return false;
30
31 *unique_id = disk->fs_uuid();
32
33 // TODO(kmadhusu): If device label is empty, extract vendor and model details
34 // and use them as device_label.
35 *device_label = UTF8ToUTF16(disk->device_label().empty() ?
36 FilePath(source_path).BaseName().value() :
37 disk->device_label());
38 return true;
39 }
40
41 } // namespace
42
43 using content::BrowserThread;
44
45 MediaDeviceNotifications::MediaDeviceNotifications() {
46 DCHECK(disks::DiskMountManager::GetInstance());
47 disks::DiskMountManager::GetInstance()->AddObserver(this);
48 CheckExistingMountPointsOnUIThread();
49 }
50
51 MediaDeviceNotifications::~MediaDeviceNotifications() {
52 disks::DiskMountManager* manager = disks::DiskMountManager::GetInstance();
53 if (manager) {
54 manager->RemoveObserver(this);
55 }
56 }
57
58 void MediaDeviceNotifications::CheckExistingMountPointsOnUIThread() {
59 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
60 const disks::DiskMountManager::MountPointMap& mount_point_map =
61 disks::DiskMountManager::GetInstance()->mount_points();
62 for (disks::DiskMountManager::MountPointMap::const_iterator it =
63 mount_point_map.begin(); it != mount_point_map.end(); ++it) {
64 BrowserThread::PostTask(
65 BrowserThread::FILE, FROM_HERE,
66 base::Bind(&MediaDeviceNotifications::CheckMountedPathOnFileThread,
67 this, it->second));
68 }
69 }
70
71 void MediaDeviceNotifications::DiskChanged(
72 disks::DiskMountManagerEventType event,
73 const disks::DiskMountManager::Disk* disk) {
74 }
75
76 void MediaDeviceNotifications::DeviceChanged(
77 disks::DiskMountManagerEventType event,
78 const std::string& device_path) {
79 }
80
81 void MediaDeviceNotifications::MountCompleted(
82 disks::DiskMountManager::MountEvent event_type,
83 MountError error_code,
84 const disks::DiskMountManager::MountPointInfo& mount_info) {
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
86
87 // Ignore mount points that are not devices.
88 if (mount_info.mount_type != MOUNT_TYPE_DEVICE)
89 return;
90 // Ignore errors.
91 if (error_code != MOUNT_ERROR_NONE)
92 return;
93 if (mount_info.mount_condition != disks::MOUNT_CONDITION_NONE)
94 return;
95
96 switch (event_type) {
97 case disks::DiskMountManager::MOUNTING: {
98 if (ContainsKey(mount_map_, mount_info.mount_path)) {
99 NOTREACHED();
100 return;
101 }
102
103 BrowserThread::PostTask(
104 BrowserThread::FILE, FROM_HERE,
105 base::Bind(&MediaDeviceNotifications::CheckMountedPathOnFileThread,
106 this, mount_info));
107 break;
108 }
109 case disks::DiskMountManager::UNMOUNTING: {
110 MountMap::iterator it = mount_map_.find(mount_info.mount_path);
111 if (it == mount_map_.end())
112 return;
113 base::SystemMonitor::Get()->ProcessRemovableStorageDetached(it->second);
114 mount_map_.erase(it);
115 break;
116 }
117 }
118 }
119
120 void MediaDeviceNotifications::CheckMountedPathOnFileThread(
121 const disks::DiskMountManager::MountPointInfo& mount_info) {
122 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
123
124 if (!chrome::IsMediaDevice(mount_info.mount_path))
125 return;
126
127 BrowserThread::PostTask(
128 BrowserThread::UI, FROM_HERE,
129 base::Bind(&MediaDeviceNotifications::AddMountedPathOnUIThread,
130 this, mount_info));
131 }
132
133 void MediaDeviceNotifications::AddMountedPathOnUIThread(
134 const disks::DiskMountManager::MountPointInfo& mount_info) {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136
137 if (ContainsKey(mount_map_, mount_info.mount_path)) {
138 NOTREACHED();
139 return;
140 }
141
142 // Get the media device uuid and label if exists.
143 std::string unique_id;
144 string16 device_label;
145 if (!GetDeviceInfo(mount_info.source_path, &unique_id, &device_label))
146 return;
147
148 // Keep track of device uuid, to see how often we receive empty uuid values.
149 UMA_HISTOGRAM_BOOLEAN("MediaDeviceNotification.device_uuid_available",
150 !unique_id.empty());
151 if (unique_id.empty())
152 return;
153
154 std::string device_id = chrome::MediaStorageUtil::MakeDeviceId(
155 chrome::MediaStorageUtil::REMOVABLE_MASS_STORAGE_WITH_DCIM, unique_id);
156 mount_map_.insert(std::make_pair(mount_info.mount_path, device_id));
157 base::SystemMonitor::Get()->ProcessRemovableStorageAttached(
158 device_id,
159 device_label,
160 mount_info.mount_path);
161 }
162
163 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698