diff options
author | Attila Dusnoki <dati91@users.noreply.github.com> | 2017-02-10 13:04:22 +0100 |
---|---|---|
committer | Attila Dusnoki <dati91@gmail.com> | 2017-02-13 14:58:11 +0100 |
commit | 0b713fd689e118be8e086e47c6a10baf24c14c85 (patch) | |
tree | 24884fa8bba1a11190f3a81fbd1a38316d3f9318 /components/script/dom/permissions.rs | |
parent | 04fe75b9cc4498360c023dbfd3cf924932e806af (diff) | |
download | servo-0b713fd689e118be8e086e47c6a10baf24c14c85.tar.gz servo-0b713fd689e118be8e086e47c6a10baf24c14c85.zip |
Refactor permission operations into one function
Diffstat (limited to 'components/script/dom/permissions.rs')
-rw-r--r-- | components/script/dom/permissions.rs | 167 |
1 files changed, 71 insertions, 96 deletions
diff --git a/components/script/dom/permissions.rs b/components/script/dom/permissions.rs index 5279f844962..676fd31d16d 100644 --- a/components/script/dom/permissions.rs +++ b/components/script/dom/permissions.rs @@ -39,6 +39,12 @@ pub trait PermissionAlgorithm { fn permission_revoke(descriptor: &Self::Descriptor, status: &Self::Status); } +enum Operation { + Query, + Request, + Revoke, +} + // https://w3c.github.io/permissions/#permissions #[dom_struct] pub struct Permissions { @@ -57,17 +63,24 @@ impl Permissions { global, PermissionsBinding::Wrap) } -} -impl PermissionsMethods for Permissions { #[allow(unrooted_must_root)] - #[allow(unsafe_code)] // https://w3c.github.io/permissions/#dom-permissions-query - unsafe fn Query(&self, cx: *mut JSContext, permissionDesc: *mut JSObject) -> Rc<Promise> { - // Step 3. - let p = Promise::new(&self.global()); + // https://w3c.github.io/permissions/#dom-permissions-request + // https://w3c.github.io/permissions/#dom-permissions-revoke + fn manipulate(&self, + op: Operation, + cx: *mut JSContext, + permissionDesc: *mut JSObject, + promise: Option<Rc<Promise>>) + -> Rc<Promise> { + // (Query, Request) Step 3. + let p = match promise { + Some(promise) => promise, + None => Promise::new(&self.global()), + }; - // Step 1. + // (Query, Request, Revoke) Step 1. let root_desc = match Permissions::create_descriptor(cx, permissionDesc) { Ok(descriptor) => descriptor, Err(error) => { @@ -76,10 +89,10 @@ impl PermissionsMethods for Permissions { }, }; - // Step 5. + // (Query, Request) Step 5. let status = PermissionStatus::new(&self.global(), &root_desc); - // Step 2. + // (Query, Request, Revoke) Step 2. match root_desc.name { PermissionName::Bluetooth => { let bluetooth_desc = match Bluetooth::create_descriptor(cx, permissionDesc) { @@ -89,112 +102,74 @@ impl PermissionsMethods for Permissions { return p; }, }; - // Step 5. + + // (Query, Request) Step 5. let result = BluetoothPermissionResult::new(&self.global(), &status); - // Step 6. - Bluetooth::permission_query(cx, &p, &bluetooth_desc, &result); - // Step 7. in permission_query + + match &op { + // (Request) Step 6 - 8. + &Operation::Request => Bluetooth::permission_request(cx, &p, &bluetooth_desc, &result), + + // (Query) Step 6 - 7. + &Operation::Query => Bluetooth::permission_query(cx, &p, &bluetooth_desc, &result), + + // (Revoke) Step 3 - 4. + &Operation::Revoke => Bluetooth::permission_revoke(&bluetooth_desc, &result), + } }, _ => { - // Step 6. - Permissions::permission_query(cx, &p, &root_desc, &status); + match &op { + &Operation::Request => { + // (Request) Step 6. + Permissions::permission_request(cx, &p, &root_desc, &status); + + // (Request) Step 7. The default algorithm always resolve + + // (Request) Step 8. + p.resolve_native(cx, &status); + }, + &Operation::Query => { + // (Query) Step 6. + Permissions::permission_query(cx, &p, &root_desc, &status); - // Step 7. - p.resolve_native(cx, &status); + // (Query) Step 7. + p.resolve_native(cx, &status); + }, + // (Revoke) Step 3 - 4. + &Operation::Revoke => Permissions::permission_revoke(&root_desc, &status), + } }, }; + match op { + // (Revoke) Step 5. + Operation::Revoke => self.manipulate(Operation::Query, cx, permissionDesc, Some(p)), - // Step 4. - return p; + // (Query, Request) Step 4. + _ => p, + } + } +} + +impl PermissionsMethods for Permissions { + #[allow(unrooted_must_root)] + #[allow(unsafe_code)] + // https://w3c.github.io/permissions/#dom-permissions-query + unsafe fn Query(&self, cx: *mut JSContext, permissionDesc: *mut JSObject) -> Rc<Promise> { + self.manipulate(Operation::Query, cx, permissionDesc, None) } #[allow(unrooted_must_root)] #[allow(unsafe_code)] // https://w3c.github.io/permissions/#dom-permissions-request unsafe fn Request(&self, cx: *mut JSContext, permissionDesc: *mut JSObject) -> Rc<Promise> { - // Step 3. - let p = Promise::new(&self.global()); - - // Step 1. - let root_desc = match Permissions::create_descriptor(cx, permissionDesc) { - Ok(descriptor) => descriptor, - Err(error) => { - p.reject_error(cx, error); - return p; - }, - }; - - // Step 5. - let status = PermissionStatus::new(&self.global(), &root_desc); - - // Step 2. - match root_desc.name { - PermissionName::Bluetooth => { - let bluetooth_desc = match Bluetooth::create_descriptor(cx, permissionDesc) { - Ok(descriptor) => descriptor, - Err(error) => { - p.reject_error(cx, error); - return p; - }, - }; - // Step 5. - let result = BluetoothPermissionResult::new(&self.global(), &status); - // Step 6. - Bluetooth::permission_request(cx, &p, &bluetooth_desc, &result); - // Step 7 - 8. in permission_request - }, - _ => { - // Step 6. - Permissions::permission_request(cx, &p, &root_desc, &status); - - // Step 7. The default algorithm always resolve - - // Step 8. - p.resolve_native(cx, &status); - }, - }; - // Step 4. - return p; + self.manipulate(Operation::Request, cx, permissionDesc, None) } #[allow(unrooted_must_root)] #[allow(unsafe_code)] // https://w3c.github.io/permissions/#dom-permissions-revoke unsafe fn Revoke(&self, cx: *mut JSContext, permissionDesc: *mut JSObject) -> Rc<Promise> { - // Step 1. - let root_desc = match Permissions::create_descriptor(cx, permissionDesc) { - Ok(descriptor) => descriptor, - Err(error) => { - let p = Promise::new(&self.global()); - p.reject_error(cx, error); - return p; - }, - }; - - let status = PermissionStatus::new(&self.global(), &root_desc); - - // Step 2. - match root_desc.name { - PermissionName::Bluetooth => { - let bluetooth_desc = match Bluetooth::create_descriptor(cx, permissionDesc) { - Ok(descriptor) => descriptor, - Err(error) => { - let p = Promise::new(&self.global()); - p.reject_error(cx, error); - return p; - }, - }; - let result = BluetoothPermissionResult::new(&self.global(), &status); - // Step 3 - 4. in permission_revoke - Bluetooth::permission_revoke(&bluetooth_desc, &result); - }, - _ => { - Permissions::permission_revoke(&root_desc, &status); - }, - }; - - // Step 5. - return self.Query(cx, permissionDesc); + self.manipulate(Operation::Revoke, cx, permissionDesc, None) } } |