fune/servo/components/script/dom/permissionstatus.rs
Gregory Katz efe0749332 servo: Merge #15590 - Adds an as_str() method to WebIDL enums to hide slice of strings (from gregkatz:webidl-enum-as-str); r=Ms2ger
<!-- Please describe your changes on the following line: -->
This PR adds an `as_str()` method to WebIDL enums to hide slice of strings from callers. It uses the new method in two places. It also eliminates a to_string() call that seemed unnecessary to me.

---
<!-- 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] These changes fix #15586 (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [x] These changes do not require tests because github issue says no tests needed.

<!-- 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: 05623b36a15b594bbc690fcd8e3b642995618de1

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : b6a2dd1ce49c8f9e5d1ba95685e81600b33f0207
2017-02-16 09:15:15 -08:00

62 lines
2.1 KiB
Rust

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use dom::bindings::codegen::Bindings::EventHandlerBinding::EventHandlerNonNull;
use dom::bindings::codegen::Bindings::PermissionStatusBinding::{self, PermissionDescriptor, PermissionName};
use dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionState;
use dom::bindings::codegen::Bindings::PermissionStatusBinding::PermissionStatusMethods;
use dom::bindings::js::Root;
use dom::bindings::reflector::reflect_dom_object;
use dom::eventtarget::EventTarget;
use dom::globalscope::GlobalScope;
use std::cell::Cell;
use std::fmt::{self, Display, Formatter};
// https://w3c.github.io/permissions/#permissionstatus
#[dom_struct]
pub struct PermissionStatus {
eventtarget: EventTarget,
state: Cell<PermissionState>,
query: Cell<PermissionName>,
}
impl PermissionStatus {
pub fn new_inherited(query: PermissionName) -> PermissionStatus {
PermissionStatus {
eventtarget: EventTarget::new_inherited(),
state: Cell::new(PermissionState::Denied),
query: Cell::new(query),
}
}
pub fn new(global: &GlobalScope, query: &PermissionDescriptor) -> Root<PermissionStatus> {
reflect_dom_object(box PermissionStatus::new_inherited(query.name),
global,
PermissionStatusBinding::Wrap)
}
pub fn set_state(&self, state: PermissionState) {
self.state.set(state);
}
pub fn get_query(&self) -> PermissionName {
self.query.get()
}
}
impl PermissionStatusMethods for PermissionStatus {
// https://w3c.github.io/permissions/#dom-permissionstatus-state
fn State(&self) -> PermissionState {
self.state.get()
}
// https://w3c.github.io/permissions/#dom-permissionstatus-onchange
event_handler!(onchange, GetOnchange, SetOnchange);
}
impl Display for PermissionName {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{}", self.as_str())
}
}