Handle servo:// url

This commit is contained in:
Paul Rouget 2019-08-20 07:28:00 +02:00
parent 5f89dc87bd
commit 5c1d130217
10 changed files with 91 additions and 40 deletions

View file

@ -32,12 +32,13 @@ void on_panic(const char *backtrace) {
throw hresult_error(E_FAIL, char2hstring(backtrace));
}
Servo::Servo(GLsizei width, GLsizei height, ServoDelegate &aDelegate)
Servo::Servo(hstring url, GLsizei width, GLsizei height,
ServoDelegate &aDelegate)
: mWindowHeight(height), mWindowWidth(width), mDelegate(aDelegate) {
capi::CInitOptions o;
o.args = "--pref dom.webxr.enabled";
o.url = "https://servo.org";
o.url = *hstring2char(url);
o.width = mWindowWidth;
o.height = mWindowHeight;
o.density = 1.0;
@ -78,4 +79,13 @@ winrt::hstring char2hstring(const char *c_str) {
return str3;
}
std::unique_ptr<char *> hstring2char(hstring hstr) {
const wchar_t *wc = hstr.c_str();
size_t size = hstr.size() + 1;
char *str = new char[size];
size_t converted = 0;
wcstombs_s(&converted, str, size, wc, hstr.size());
return std::make_unique<char*>(str);
}
} // namespace winrt::servo

View file

@ -16,6 +16,9 @@ extern "C" {
}
} // namespace capi
hstring char2hstring(const char *);
std::unique_ptr<char *> hstring2char(hstring);
class ServoDelegate {
public:
// Called from any thread
@ -39,7 +42,7 @@ protected:
class Servo {
public:
Servo(GLsizei, GLsizei, ServoDelegate &);
Servo(hstring, GLsizei, GLsizei, ServoDelegate &);
~Servo();
ServoDelegate &Delegate() { return mDelegate; }
@ -49,19 +52,12 @@ public:
void SetBatchMode(bool mode) { capi::set_batch_mode(mode); }
void GoForward() { capi::go_forward(); }
void GoBack() { capi::go_back(); }
void Click(float x, float y) { capi::click(x, y); }
void Click(float x, float y) { capi::click((int32_t)x, (int32_t)y); }
void Reload() { capi::reload(); }
void Stop() { capi::stop(); }
void LoadUri(hstring uri) {
const wchar_t* wc = uri.c_str();
size_t size = uri.size() + 1;
char* str = new char[size];
size_t converted = 0;
wcstombs_s(&converted, str, size, wc, uri.size());
capi::load_uri(str);
}
void LoadUri(hstring uri) { capi::load_uri(*hstring2char(uri)); }
void Scroll(float dx, float dy, float x, float y) {
capi::scroll(dx, dy, x, y);
capi::scroll((int32_t)dx, (int32_t)dy, (int32_t)x, (int32_t)y);
}
void SetSize(GLsizei width, GLsizei height) {
if (width != mWindowWidth || height != mWindowHeight) {
@ -82,6 +78,4 @@ private:
// the Servo instance. See https://github.com/servo/servo/issues/22967
static Servo *sServo = nullptr;
hstring char2hstring(const char *c_str);
} // namespace servo
} // namespace winrt::servo

View file

@ -44,8 +44,7 @@ void ServoControl::OnLoaded(IInspectable const &, RoutedEventArgs const &) {
Controls::SwapChainPanel ServoControl::Panel() {
// FIXME: is there a better way of doing this?
return GetTemplateChild(L"swapChainPanel")
.as<Controls::SwapChainPanel>();
return GetTemplateChild(L"swapChainPanel").as<Controls::SwapChainPanel>();
}
void ServoControl::CreateRenderSurface() {
@ -105,12 +104,17 @@ Uri ServoControl::LoadURIOrSearch(hstring input) {
hstring input2 = L"https://" + input;
uri = TryParseURI(input2);
if (uri == std::nullopt || !has_dot) {
hstring input3 = L"https://duckduckgo.com/html/?q=" + Uri::EscapeComponent(input);
hstring input3 =
L"https://duckduckgo.com/html/?q=" + Uri::EscapeComponent(input);
uri = TryParseURI(input3);
}
}
auto finalUri = uri.value();
RunOnGLThread([=] { mServo->LoadUri(finalUri.ToString()); });
if (!mLooping) {
mInitialURL = finalUri.ToString();
} else {
RunOnGLThread([=] { mServo->LoadUri(finalUri.ToString()); });
}
return finalUri;
}
@ -136,7 +140,7 @@ void ServoControl::Loop() {
if (mServo == nullptr) {
log("Entering loop");
ServoDelegate *sd = static_cast<ServoDelegate *>(this);
mServo = std::make_unique<Servo>(panelWidth, panelHeight, *sd);
mServo = std::make_unique<Servo>(mInitialURL, panelWidth, panelHeight, *sd);
} else {
// FIXME: this will fail since create_task didn't pick the thread
// where Servo was running initially.

View file

@ -63,6 +63,7 @@ private:
winrt::event<HistoryChangedDelegate> mOnHistoryChangedEvent;
winrt::event<LoadStatusChangedDelegate> mOnLoadStartedEvent;
winrt::event<LoadStatusChangedDelegate> mOnLoadEndedEvent;
hstring mInitialURL = L"https://servo.org";
Windows::UI::Xaml::Controls::SwapChainPanel ServoControl::Panel();
void CreateRenderSurface();
@ -76,7 +77,7 @@ private:
std::optional<Windows::Foundation::Uri> TryParseURI(hstring input) {
try {
return Windows::Foundation::Uri(input);
} catch (hresult_invalid_argument const &e) {
} catch (hresult_invalid_argument const &) {
return {};
}
}