Auto merge of #10840 - ConnorGBrewster:window_alert, r=jdm

Implement alert dialogs

fix #10812

Implements alert dialogs using tinyfiledialogs

r? @jdm

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="35" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/10840)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-05-04 09:10:40 -07:00
commit b4f573db1a
10 changed files with 122 additions and 19 deletions

View file

@ -805,6 +805,10 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
debug!("constellation got SetDocumentState message");
self.document_states.insert(pipeline_id, state);
}
Request::Script(FromScriptMsg::Alert(pipeline_id, message, sender)) => {
debug!("constellation got Alert message");
self.handle_alert(pipeline_id, message, sender);
}
// Messages from layout thread
@ -1080,6 +1084,44 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
}
}
fn handle_alert(&mut self, pipeline_id: PipelineId, message: String, sender: IpcSender<bool>) {
let display_alert_dialog = if prefs::get_pref("dom.mozbrowser.enabled").as_boolean().unwrap_or(false) {
let parent_pipeline_info = self.pipelines.get(&pipeline_id).and_then(|source| source.parent_info);
if let Some(_) = parent_pipeline_info {
let root_pipeline_id = self.root_frame_id
.and_then(|root_frame_id| self.frames.get(&root_frame_id))
.map(|root_frame| root_frame.current);
let ancestor_info = self.get_root_pipeline_and_containing_parent(&pipeline_id);
if let Some(ancestor_info) = ancestor_info {
if root_pipeline_id == Some(ancestor_info.0) {
match root_pipeline_id.and_then(|pipeline_id| self.pipelines.get(&pipeline_id)) {
Some(root_pipeline) => {
// https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowsershowmodalprompt
let event = MozBrowserEvent::ShowModalPrompt("alert".to_owned(), "Alert".to_owned(),
String::from(message), "".to_owned());
root_pipeline.trigger_mozbrowser_event(ancestor_info.1, event);
}
None => return warn!("Alert sent to Pipeline {:?} after closure.", root_pipeline_id),
}
} else {
warn!("A non-current frame is trying to show an alert.")
}
}
false
} else {
true
}
} else {
true
};
let result = sender.send(display_alert_dialog);
if let Err(e) = result {
self.handle_send_error(pipeline_id, e);
}
}
fn handle_load_url_msg(&mut self, source_id: PipelineId, load_data: LoadData) {
self.load_url(source_id, load_data);
}
@ -1956,6 +1998,29 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
}
}
/// For a given pipeline, determine the iframe in the root pipeline that transitively contains
/// it. There could be arbitrary levels of nested iframes in between them.
fn get_root_pipeline_and_containing_parent(&self, pipeline_id: &PipelineId) -> Option<(PipelineId, SubpageId)> {
if let Some(pipeline) = self.pipelines.get(pipeline_id) {
if let Some(mut ancestor_info) = pipeline.parent_info {
if let Some(mut ancestor) = self.pipelines.get(&ancestor_info.0) {
while let Some(next_info) = ancestor.parent_info {
ancestor_info = next_info;
ancestor = match self.pipelines.get(&ancestor_info.0) {
Some(ancestor) => ancestor,
None => {
warn!("Get parent pipeline before root via closed pipeline {:?}.", ancestor_info.0);
return None;
},
};
}
return Some(ancestor_info);
}
}
}
None
}
// https://developer.mozilla.org/en-US/docs/Web/Events/mozbrowserlocationchange
// Note that this is a no-op if the pipeline is not an immediate child iframe of the root
fn trigger_mozbrowserlocationchange(&self, pipeline_id: PipelineId) {
@ -1987,19 +2052,15 @@ impl<LTF: LayoutThreadFactory, STF: ScriptThreadFactory> Constellation<LTF, STF>
fn trigger_mozbrowsererror(&self, pipeline_id: PipelineId, reason: String, backtrace: String) {
if !prefs::get_pref("dom.mozbrowser.enabled").as_boolean().unwrap_or(false) { return; }
if let Some(pipeline) = self.pipelines.get(&pipeline_id) {
if let Some(mut ancestor_info) = pipeline.parent_info {
if let Some(mut ancestor) = self.pipelines.get(&ancestor_info.0) {
while let Some(next_info) = ancestor.parent_info {
ancestor_info = next_info;
ancestor = match self.pipelines.get(&ancestor_info.0) {
Some(ancestor) => ancestor,
None => return warn!("Mozbrowsererror via closed pipeline {:?}.", ancestor_info.0),
};
}
let ancestor_info = self.get_root_pipeline_and_containing_parent(&pipeline_id);
if let Some(ancestor_info) = ancestor_info {
match self.pipelines.get(&ancestor_info.0) {
Some(ancestor) => {
let event = MozBrowserEvent::Error(MozBrowserErrorType::Fatal, Some(reason), Some(backtrace));
ancestor.trigger_mozbrowser_event(ancestor_info.1, event);
}
},
None => return warn!("Mozbrowsererror via closed pipeline {:?}.", ancestor_info.0),
}
}
}