Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 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 #include "apps/saved_devices_service.h" | |
| 6 | |
| 7 #include <set> | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "apps/saved_devices_service_factory.h" | |
| 11 #include "base/basictypes.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/chrome_notification_types.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/notification_service.h" | |
| 17 #include "device/usb/usb_device.h" | |
| 18 #include "device/usb/usb_device_handle.h" | |
| 19 #include "extensions/browser/extension_host.h" | |
| 20 #include "extensions/browser/extension_prefs.h" | |
| 21 #include "extensions/browser/extension_system.h" | |
| 22 #include "extensions/browser/extension_util.h" | |
| 23 #include "extensions/browser/notification_types.h" | |
| 24 #include "extensions/common/permissions/api_permission.h" | |
| 25 #include "extensions/common/permissions/permission_set.h" | |
| 26 #include "extensions/common/permissions/permissions_data.h" | |
| 27 | |
| 28 namespace apps { | |
| 29 | |
| 30 using device::UsbDevice; | |
| 31 using device::UsbDeviceHandle; | |
| 32 using extensions::APIPermission; | |
| 33 using extensions::Extension; | |
| 34 using extensions::ExtensionHost; | |
| 35 using extensions::ExtensionPrefs; | |
| 36 | |
| 37 namespace { | |
| 38 | |
| 39 // Preference keys | |
| 40 | |
| 41 // The device that the app has permission to access. | |
| 42 const char kDevices[] = "devices"; | |
| 43 | |
| 44 // The type of device saved. | |
| 45 const char kDeviceType[] = "type"; | |
| 46 | |
| 47 // Type identifier for USB devices. | |
| 48 const char kDeviceTypeUsb[] = "usb"; | |
| 49 | |
| 50 // The vendor ID of the device that the app had permission to access. | |
| 51 const char kDeviceVendorId[] = "vendor_id"; | |
| 52 | |
| 53 // The product ID of the device that the app had permission to access. | |
| 54 const char kDeviceProductId[] = "product_id"; | |
| 55 | |
| 56 // The serial number of the device that the app has permission to access. | |
| 57 const char kDeviceSerialNumber[] = "serial_number"; | |
| 58 | |
| 59 // Persists a SavedDeviceEntry in ExtensionPrefs. | |
| 60 void AddSavedDeviceEntry(Profile* profile, | |
| 61 const std::string& extension_id, | |
| 62 const SavedDeviceEntry& device) { | |
| 63 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile); | |
| 64 ExtensionPrefs::ScopedListUpdate update(prefs, extension_id, kDevices); | |
| 65 base::ListValue* devices = update.Get(); | |
| 66 if (!devices) { | |
| 67 devices = update.Create(); | |
| 68 } | |
| 69 | |
| 70 base::Value* device_entry = device.ToValue(); | |
| 71 DCHECK(devices->Find(*device_entry) == devices->end()); | |
| 72 devices->Append(device_entry); | |
| 73 } | |
| 74 | |
| 75 // Clears all SavedDeviceEntry for the app from ExtensionPrefs. | |
| 76 void ClearSavedDeviceEntries(ExtensionPrefs* prefs, | |
| 77 const std::string& extension_id) { | |
| 78 prefs->UpdateExtensionPref(extension_id, kDevices, NULL); | |
| 79 } | |
| 80 | |
| 81 // Returns all SavedDeviceEntries for the app. | |
| 82 std::vector<SavedDeviceEntry> GetSavedDeviceEntries( | |
| 83 ExtensionPrefs* prefs, | |
| 84 const std::string& extension_id) { | |
| 85 std::vector<SavedDeviceEntry> result; | |
| 86 const base::ListValue* devices = NULL; | |
| 87 if (!prefs->ReadPrefAsList(extension_id, kDevices, &devices)) { | |
| 88 return result; | |
| 89 } | |
| 90 | |
| 91 for (base::ListValue::const_iterator it = devices->begin(); | |
| 92 it != devices->end(); | |
| 93 ++it) { | |
| 94 const base::DictionaryValue* device_entry = NULL; | |
| 95 if (!(*it)->GetAsDictionary(&device_entry)) { | |
| 96 continue; | |
| 97 } | |
| 98 int vendor_id; | |
| 99 if (!device_entry->GetIntegerWithoutPathExpansion(kDeviceVendorId, | |
| 100 &vendor_id) || | |
| 101 vendor_id < 0 || vendor_id > UINT16_MAX) { | |
| 102 continue; | |
| 103 } | |
| 104 int product_id; | |
| 105 if (!device_entry->GetIntegerWithoutPathExpansion(kDeviceProductId, | |
| 106 &product_id) || | |
| 107 product_id < 0 || product_id > UINT16_MAX) { | |
| 108 continue; | |
| 109 } | |
| 110 base::string16 serial_number; | |
| 111 if (!device_entry->GetStringWithoutPathExpansion(kDeviceSerialNumber, | |
| 112 &serial_number)) { | |
| 113 continue; | |
| 114 } | |
| 115 | |
| 116 result.push_back(SavedDeviceEntry(vendor_id, product_id, serial_number)); | |
| 117 } | |
| 118 return result; | |
| 119 } | |
| 120 | |
| 121 } // namespace | |
| 122 | |
| 123 SavedDeviceEntry::SavedDeviceEntry(uint16_t vendor_id, | |
| 124 uint16_t product_id, | |
| 125 const base::string16& serial_number) | |
| 126 : vendor_id(vendor_id), | |
| 127 product_id(product_id), | |
| 128 serial_number(serial_number) { | |
| 129 } | |
| 130 | |
| 131 base::Value* SavedDeviceEntry::ToValue() const { | |
| 132 base::DictionaryValue* device_entry_dict = new base::DictionaryValue(); | |
| 133 device_entry_dict->SetStringWithoutPathExpansion(kDeviceType, kDeviceTypeUsb); | |
| 134 device_entry_dict->SetIntegerWithoutPathExpansion(kDeviceVendorId, vendor_id); | |
| 135 device_entry_dict->SetIntegerWithoutPathExpansion(kDeviceProductId, | |
| 136 product_id); | |
| 137 device_entry_dict->SetStringWithoutPathExpansion(kDeviceSerialNumber, | |
| 138 serial_number); | |
| 139 return device_entry_dict; | |
| 140 } | |
| 141 | |
| 142 // static | |
| 143 SavedDevicesService* SavedDevicesService::Get(Profile* profile) { | |
| 144 return SavedDevicesServiceFactory::GetForProfile(profile); | |
| 145 } | |
| 146 | |
| 147 SavedDevicesService::SavedDevicesService(Profile* profile) | |
| 148 : extension_id_to_saved_devices_deleter_(&extension_id_to_saved_devices_), | |
| 149 profile_(profile) { | |
| 150 registrar_.Add(this, | |
| 151 extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED, | |
| 152 content::NotificationService::AllSources()); | |
| 153 registrar_.Add(this, | |
| 154 chrome::NOTIFICATION_APP_TERMINATING, | |
| 155 content::NotificationService::AllSources()); | |
| 156 } | |
| 157 | |
| 158 SavedDevicesService::~SavedDevicesService() { | |
| 159 } | |
| 160 | |
| 161 void SavedDevicesService::Observe(int type, | |
| 162 const content::NotificationSource& source, | |
| 163 const content::NotificationDetails& details) { | |
| 164 switch (type) { | |
| 165 case extensions::NOTIFICATION_EXTENSION_HOST_DESTROYED: { | |
| 166 ExtensionHost* host = content::Details<ExtensionHost>(details).ptr(); | |
| 167 const Extension* extension = host->extension(); | |
| 168 if (extension) { | |
| 169 Clear(extension->id()); | |
|
scheib
2014/09/18 20:31:48
Maybe just Clear(host->extension_id())
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
Done.
| |
| 170 } | |
| 171 break; | |
| 172 } | |
| 173 | |
| 174 case chrome::NOTIFICATION_APP_TERMINATING: { | |
| 175 // Stop listening to NOTIFICATION_EXTENSION_HOST_DESTROYED in particular | |
| 176 // as all extension hosts will be destroyed as a result of shutdown. | |
| 177 registrar_.RemoveAll(); | |
| 178 break; | |
| 179 } | |
| 180 } | |
| 181 } | |
| 182 | |
| 183 SavedDevicesService::SavedDevices* SavedDevicesService::Get( | |
|
scheib
2014/09/18 20:31:48
Please keep the definitions in the same order as t
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
Done.
| |
| 184 const std::string& extension_id) const { | |
| 185 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 186 std::map<std::string, SavedDevices*>::const_iterator it = | |
| 187 extension_id_to_saved_devices_.find(extension_id); | |
| 188 if (it != extension_id_to_saved_devices_.end()) { | |
| 189 return it->second; | |
| 190 } | |
| 191 | |
| 192 return NULL; | |
| 193 } | |
| 194 | |
| 195 SavedDevicesService::SavedDevices* SavedDevicesService::GetOrInsert( | |
| 196 const std::string& extension_id) { | |
| 197 SavedDevices* saved_devices = Get(extension_id); | |
| 198 if (saved_devices) { | |
| 199 return saved_devices; | |
| 200 } | |
| 201 | |
| 202 saved_devices = new SavedDevices(profile_, extension_id); | |
| 203 extension_id_to_saved_devices_[extension_id] = saved_devices; | |
| 204 return saved_devices; | |
| 205 } | |
| 206 | |
| 207 std::vector<SavedDeviceEntry> SavedDevicesService::GetAllDevices( | |
| 208 const std::string& extension_id) const { | |
| 209 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 210 return GetSavedDeviceEntries(prefs, extension_id); | |
| 211 } | |
| 212 | |
| 213 void SavedDevicesService::Clear(const std::string& extension_id) { | |
| 214 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 215 ClearSavedDeviceEntries(ExtensionPrefs::Get(profile_), extension_id); | |
| 216 std::map<std::string, SavedDevices*>::iterator it = | |
| 217 extension_id_to_saved_devices_.find(extension_id); | |
| 218 if (it != extension_id_to_saved_devices_.end()) { | |
| 219 delete it->second; | |
| 220 extension_id_to_saved_devices_.erase(it); | |
| 221 } | |
| 222 } | |
| 223 | |
| 224 SavedDevicesService::SavedDevices::SavedDevices(Profile* profile, | |
| 225 const std::string& extension_id) | |
| 226 : profile_(profile), extension_id_(extension_id) { | |
| 227 ExtensionPrefs* prefs = ExtensionPrefs::Get(profile_); | |
| 228 persistent_devices_ = GetSavedDeviceEntries(prefs, extension_id_); | |
| 229 } | |
| 230 | |
| 231 SavedDevicesService::SavedDevices::~SavedDevices() { | |
| 232 for (std::set<scoped_refptr<UsbDevice> >::iterator it = | |
| 233 ephemeral_devices_.begin(); | |
|
scheib
2014/09/18 20:31:48
Add comments here explaining why only ephemeral de
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
Done.
| |
| 234 it != ephemeral_devices_.end(); | |
| 235 ++it) { | |
| 236 (*it)->RemoveObserver(this); | |
| 237 } | |
| 238 } | |
| 239 | |
| 240 void SavedDevicesService::SavedDevices::RegisterDevice( | |
| 241 scoped_refptr<device::UsbDevice> device, | |
| 242 base::string16* serial_number) { | |
| 243 if (serial_number) { | |
| 244 for (std::vector<SavedDeviceEntry>::const_iterator it = | |
| 245 persistent_devices_.begin(); | |
| 246 it != persistent_devices_.end(); | |
| 247 ++it) { | |
| 248 if (it->vendor_id != device->vendor_id()) { | |
| 249 continue; | |
| 250 } | |
| 251 if (it->product_id != device->product_id()) { | |
| 252 continue; | |
| 253 } | |
| 254 if (it->serial_number == *serial_number) { | |
| 255 return; | |
| 256 } | |
| 257 } | |
| 258 SavedDeviceEntry device_entry = SavedDeviceEntry( | |
| 259 device->vendor_id(), device->product_id(), *serial_number); | |
| 260 persistent_devices_.push_back(device_entry); | |
| 261 | |
| 262 content::BrowserThread::PostTask( | |
| 263 content::BrowserThread::UI, | |
| 264 FROM_HERE, | |
| 265 base::Bind(AddSavedDeviceEntry, profile_, extension_id_, device_entry)); | |
| 266 } else { | |
| 267 ephemeral_devices_.insert(device); | |
| 268 device->AddObserver(this); | |
|
scheib
2014/09/18 20:31:48
Probably comment here too why only ephemeral devic
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
Done.
| |
| 269 } | |
| 270 } | |
| 271 | |
| 272 bool SavedDevicesService::SavedDevices::IsRegistered( | |
| 273 scoped_refptr<UsbDevice> device) const { | |
| 274 if (ephemeral_devices_.find(device) != ephemeral_devices_.end()) { | |
| 275 return true; | |
| 276 } | |
| 277 | |
| 278 bool have_serial_number = false; | |
| 279 base::string16 serial_number; | |
| 280 for (std::vector<SavedDeviceEntry>::const_iterator it = | |
| 281 persistent_devices_.begin(); | |
| 282 it != persistent_devices_.end(); | |
| 283 ++it) { | |
| 284 if (it->vendor_id != device->vendor_id()) { | |
| 285 continue; | |
| 286 } | |
| 287 if (it->product_id != device->product_id()) { | |
| 288 continue; | |
| 289 } | |
| 290 if (!have_serial_number) { | |
| 291 scoped_refptr<UsbDeviceHandle> device_handle = device->Open(); | |
| 292 if (!device_handle.get()) { | |
| 293 break; | |
| 294 } | |
| 295 if (!device_handle->GetSerial(&serial_number)) { | |
| 296 break; | |
| 297 } | |
| 298 have_serial_number = true; | |
| 299 } | |
| 300 if (it->serial_number != serial_number) { | |
| 301 continue; | |
| 302 } | |
| 303 return true; | |
| 304 } | |
| 305 return false; | |
| 306 } | |
| 307 | |
| 308 void SavedDevicesService::SavedDevices::OnDisconnect( | |
| 309 scoped_refptr<UsbDevice> device) { | |
| 310 ephemeral_devices_.erase(device); | |
|
scheib
2014/09/18 20:31:48
... explaining comment
Reilly Grant (use Gerrit)
2014/09/18 21:27:59
Done.
| |
| 311 device->RemoveObserver(this); | |
| 312 } | |
| 313 | |
| 314 } // namespace apps | |
| OLD | NEW |