servo: Merge #14468 - Move allowedService and blocklist checks into caching functions (from szeged:blocklist-allowed-services); r=jdm
authorZakor Gyula <zakorgy@inf.u-szeged.hu>
Tue, 06 Dec 2016 19:26:50 -0800
changeset 340355 808bf2778f20f6d86934b70d8dffdb4d763f35d4
parent 340354 ba738af8082702f5cefbc7a49ac0a72e81187d50
child 340356 1ba608da113c7639625000bbb9470c39f6228839
push id86548
push userkwierso@gmail.com
push dateSat, 04 Feb 2017 01:35:21 +0000
treeherdermozilla-inbound@e7b96d015d03 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
servo: Merge #14468 - Move allowedService and blocklist checks into caching functions (from szeged:blocklist-allowed-services); r=jdm <!-- Please describe your changes on the following line: --> Move allowedService and blocklist checks into caching functions in `bluetooth/lib.rs`, to avoid caching not allowed services and blocklisted services, characteristics and descriptors. --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: --> - [x] `./mach build -d` does not report any errors - [x] `./mach test-tidy` does not report any errors - [x] There are tests for these changes <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. --> Source-Repo: https://github.com/servo/servo Source-Revision: 55248aeb2b890a13f7d021ad4e37662d57135945
servo/components/bluetooth/lib.rs
--- a/servo/components/bluetooth/lib.rs
+++ b/servo/components/bluetooth/lib.rs
@@ -419,20 +419,25 @@ impl BluetoothManager {
     }
 
     // Service
 
     fn get_and_cache_gatt_services(&mut self,
                                    adapter: &mut BluetoothAdapter,
                                    device_id: &str)
                                    -> Vec<BluetoothGATTService> {
-        let services = match self.get_device(adapter, device_id) {
+        let mut services = match self.get_device(adapter, device_id) {
             Some(d) => d.get_gatt_services().unwrap_or(vec!()),
             None => vec!(),
         };
+
+        services.retain(|s| !uuid_is_blocklisted(&s.get_uuid().unwrap_or(String::new()), Blocklist::All) &&
+                            self.allowed_services
+                                .get(device_id)
+                                .map_or(false, |uuids| uuids.contains(&s.get_uuid().unwrap_or(String::new()))));
         for service in &services {
             self.cached_services.insert(service.get_id(), service.clone());
             self.service_to_device.insert(service.get_id(), device_id.to_owned());
         }
         services
     }
 
     fn get_gatt_service(&mut self, adapter: &mut BluetoothAdapter, service_id: &str) -> Option<&BluetoothGATTService> {
@@ -456,21 +461,22 @@ impl BluetoothManager {
     }
 
     // Characteristic
 
     fn get_and_cache_gatt_characteristics(&mut self,
                                           adapter: &mut BluetoothAdapter,
                                           service_id: &str)
                                           -> Vec<BluetoothGATTCharacteristic> {
-        let characteristics = match self.get_gatt_service(adapter, service_id) {
+        let mut characteristics = match self.get_gatt_service(adapter, service_id) {
             Some(s) => s.get_gatt_characteristics().unwrap_or(vec!()),
             None => vec!(),
         };
 
+        characteristics.retain(|c| !uuid_is_blocklisted(&c.get_uuid().unwrap_or(String::new()), Blocklist::All));
         for characteristic in &characteristics {
             self.cached_characteristics.insert(characteristic.get_id(), characteristic.clone());
             self.characteristic_to_service.insert(characteristic.get_id(), service_id.to_owned());
         }
         characteristics
     }
 
     fn get_gatt_characteristic(&mut self,
@@ -519,21 +525,22 @@ impl BluetoothManager {
     }
 
     // Descriptor
 
     fn get_and_cache_gatt_descriptors(&mut self,
                                       adapter: &mut BluetoothAdapter,
                                       characteristic_id: &str)
                                       -> Vec<BluetoothGATTDescriptor> {
-        let descriptors = match self.get_gatt_characteristic(adapter, characteristic_id) {
+        let mut descriptors = match self.get_gatt_characteristic(adapter, characteristic_id) {
             Some(c) => c.get_gatt_descriptors().unwrap_or(vec!()),
             None => vec!(),
         };
 
+        descriptors.retain(|d| !uuid_is_blocklisted(&d.get_uuid().unwrap_or(String::new()), Blocklist::All));
         for descriptor in &descriptors {
             self.cached_descriptors.insert(descriptor.get_id(), descriptor.clone());
             self.descriptor_to_characteristic.insert(descriptor.get_id(), characteristic_id.to_owned());
         }
         descriptors
     }
 
     fn get_gatt_descriptor(&mut self,
@@ -738,20 +745,16 @@ impl BluetoothManager {
                             uuid: uuid,
                             is_primary: true,
                             instance_id: service.get_id(),
                         }
                     );
                 }
             }
         }
-        services_vec.retain(|s| !uuid_is_blocklisted(&s.uuid, Blocklist::All) &&
-                                self.allowed_services
-                                    .get(&device_id)
-                                    .map_or(false, |uuids| uuids.contains(&s.uuid)));
 
         // Step 7.
         if services_vec.is_empty() {
             return drop(sender.send(Err(BluetoothError::NotFound)));
         }
 
         return drop(sender.send(Ok(BluetoothResponse::GetPrimaryServices(services_vec))));
     }
@@ -914,17 +917,16 @@ impl BluetoothManager {
                         indicate: properties.contains(INDICATE),
                         authenticated_signed_writes: properties.contains(AUTHENTICATED_SIGNED_WRITES),
                         reliable_write: properties.contains(RELIABLE_WRITE),
                         writable_auxiliaries: properties.contains(WRITABLE_AUXILIARIES),
                     }
                 );
             }
         }
-        characteristics_vec.retain(|c| !uuid_is_blocklisted(&c.uuid, Blocklist::All));
 
         // Step 7.
         if characteristics_vec.is_empty() {
             return drop(sender.send(Err(BluetoothError::NotFound)));
         }
 
         return drop(sender.send(Ok(BluetoothResponse::GetCharacteristics(characteristics_vec))));
     }
@@ -982,17 +984,16 @@ impl BluetoothManager {
                 descriptors_vec.push(
                     BluetoothDescriptorMsg {
                         uuid: uuid,
                         instance_id: descriptor.get_id(),
                     }
                 );
             }
         }
-        descriptors_vec.retain(|d| !uuid_is_blocklisted(&d.uuid, Blocklist::All));
 
         // Step 7.
         if descriptors_vec.is_empty() {
             return drop(sender.send(Err(BluetoothError::NotFound)));
         }
         return drop(sender.send(Ok(BluetoothResponse::GetDescriptors(descriptors_vec))));
     }