Auto merge of #23649 - jdm:marketplace, r=jdm

Add support for market:// urls

Rebased from #23648.

---
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #21992.
- [x] These changes do not require tests because can't test embedders.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/23649)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2019-06-28 17:31:25 -04:00 committed by GitHub
commit 177b2a6fa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 2 deletions

View file

@ -350,6 +350,9 @@ impl HostTrait for HostCallbacks {
fn on_load_started(&self) {} fn on_load_started(&self) {}
fn on_load_ended(&self) {} fn on_load_ended(&self) {}
fn on_title_changed(&self, _title: String) {} fn on_title_changed(&self, _title: String) {}
fn on_allow_navigation(&self, _url: String) -> bool {
true
}
fn on_url_changed(&self, url: String) { fn on_url_changed(&self, url: String) {
if let Ok(cstr) = CString::new(url.as_str()) { if let Ok(cstr) = CString::new(url.as_str()) {
if let Some(url_update) = self.url_update.0 { if let Some(url_update) = self.url_update.0 {

View file

@ -97,6 +97,8 @@ pub trait HostTrait {
fn on_load_ended(&self); fn on_load_ended(&self);
/// Page title has changed. /// Page title has changed.
fn on_title_changed(&self, title: String); fn on_title_changed(&self, title: String);
/// Allow Navigation.
fn on_allow_navigation(&self, url: String) -> bool;
/// Page URL has changed. /// Page URL has changed.
fn on_url_changed(&self, url: String); fn on_url_changed(&self, url: String);
/// Back/forward state has changed. /// Back/forward state has changed.
@ -474,9 +476,13 @@ impl ServoGlue {
let title = format!("{} - Servo", title); let title = format!("{} - Servo", title);
self.callbacks.host_callbacks.on_title_changed(title); self.callbacks.host_callbacks.on_title_changed(title);
}, },
EmbedderMsg::AllowNavigationRequest(pipeline_id, _url) => { EmbedderMsg::AllowNavigationRequest(pipeline_id, url) => {
if let Some(_browser_id) = browser_id { if let Some(_browser_id) = browser_id {
let window_event = WindowEvent::AllowNavigationResponse(pipeline_id, true); let data: bool = self
.callbacks
.host_callbacks
.on_allow_navigation(url.to_string());
let window_event = WindowEvent::AllowNavigationResponse(pipeline_id, data);
let _ = self.process_event(window_event); let _ = self.process_event(window_event);
} }
}, },

View file

@ -37,6 +37,7 @@ pub struct CHostCallbacks {
pub on_load_started: extern "C" fn(), pub on_load_started: extern "C" fn(),
pub on_load_ended: extern "C" fn(), pub on_load_ended: extern "C" fn(),
pub on_title_changed: extern "C" fn(title: *const c_char), pub on_title_changed: extern "C" fn(title: *const c_char),
pub on_allow_navigation: extern "C" fn(url: *const c_char) -> bool,
pub on_url_changed: extern "C" fn(url: *const c_char), pub on_url_changed: extern "C" fn(url: *const c_char),
pub on_history_changed: extern "C" fn(can_go_back: bool, can_go_forward: bool), pub on_history_changed: extern "C" fn(can_go_back: bool, can_go_forward: bool),
pub on_animating_changed: extern "C" fn(animating: bool), pub on_animating_changed: extern "C" fn(animating: bool),
@ -322,6 +323,14 @@ impl HostTrait for HostCallbacks {
(self.0.on_title_changed)(title_ptr); (self.0.on_title_changed)(title_ptr);
} }
fn on_allow_navigation(&self, url: String) -> bool {
debug!("on_allow_navigation");
let url = CString::new(url).expect("Can't create string");
let url_ptr = url.as_ptr();
mem::forget(url);
(self.0.on_allow_navigation)(url_ptr)
}
fn on_url_changed(&self, url: String) { fn on_url_changed(&self, url: String) {
debug!("on_url_changed"); debug!("on_url_changed");
let url = CString::new(url).expect("Can't create string"); let url = CString::new(url).expect("Can't create string");

View file

@ -411,6 +411,26 @@ impl HostTrait for HostCallbacks {
.unwrap(); .unwrap();
} }
fn on_allow_navigation(&self, url: String) -> bool {
debug!("on_allow_navigation");
let env = self.jvm.get_env().unwrap();
let s = match new_string(&env, &url) {
Ok(s) => s,
Err(_) => return false,
};
let s = JValue::from(JObject::from(s));
let allow = env.call_method(
self.callbacks.as_obj(),
"onAllowNavigation",
"(Ljava/lang/String;)Z",
&[s],
);
match allow {
Ok(allow) => return allow.z().unwrap(),
Err(_) => return true,
}
}
fn on_url_changed(&self, url: String) { fn on_url_changed(&self, url: String) {
debug!("on_url_changed"); debug!("on_url_changed");
let env = self.jvm.get_env().unwrap(); let env = self.jvm.get_env().unwrap();

View file

@ -20,6 +20,7 @@ import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.TextView; import android.widget.TextView;
import android.util.Log;
import org.mozilla.servoview.ServoView; import org.mozilla.servoview.ServoView;
import org.mozilla.servoview.Servo; import org.mozilla.servoview.Servo;
@ -162,6 +163,19 @@ public class MainActivity extends Activity implements Servo.Client {
mCanGoBack = canGoBack; mCanGoBack = canGoBack;
} }
@Override
public boolean onAllowNavigation(String url) {
if (url.startsWith("market://")) {
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return false;
} catch (Exception e) {
Log.e("onAllowNavigation", e.toString());
}
}
return true;
}
public void onRedrawing(boolean redrawing) { public void onRedrawing(boolean redrawing) {
if (redrawing) { if (redrawing) {
mIdleText.setText("LOOP"); mIdleText.setText("LOOP");

View file

@ -169,6 +169,8 @@ public class Servo {
} }
public interface Client { public interface Client {
boolean onAllowNavigation(String url);
void onLoadStarted(); void onLoadStarted();
void onLoadEnded(); void onLoadEnded();
@ -234,6 +236,10 @@ public class Servo {
mRunCallback.inGLThread(() -> mGfxCb.animationStateChanged(animating)); mRunCallback.inGLThread(() -> mGfxCb.animationStateChanged(animating));
} }
public boolean onAllowNavigation(String url) {
return mClient.onAllowNavigation(url);
}
public void onLoadStarted() { public void onLoadStarted() {
mRunCallback.inUIThread(() -> mClient.onLoadStarted()); mRunCallback.inUIThread(() -> mClient.onLoadStarted());
} }