diff options
author | Paul Rouget <me@paulrouget.com> | 2019-08-20 07:28:00 +0200 |
---|---|---|
committer | Paul Rouget <me@paulrouget.com> | 2019-08-21 11:03:19 +0200 |
commit | 5c1d130217db8ece7313d69a7e76130085d67e94 (patch) | |
tree | 292ed26562dca3f9198abdd78df4082dedf5fc33 /support/hololens/ServoApp | |
parent | 5f89dc87bd26ddb62931251edf7c76c782b96a94 (diff) | |
download | servo-5c1d130217db8ece7313d69a7e76130085d67e94.tar.gz servo-5c1d130217db8ece7313d69a7e76130085d67e94.zip |
Handle servo:// url
Diffstat (limited to 'support/hololens/ServoApp')
-rw-r--r-- | support/hololens/ServoApp/App.cpp | 25 | ||||
-rw-r--r-- | support/hololens/ServoApp/App.h | 2 | ||||
-rw-r--r-- | support/hololens/ServoApp/BrowserPage.cpp | 17 | ||||
-rw-r--r-- | support/hololens/ServoApp/BrowserPage.h | 6 | ||||
-rw-r--r-- | support/hololens/ServoApp/Package.appxmanifest | 22 | ||||
-rw-r--r-- | support/hololens/ServoApp/ServoControl/Servo.cpp | 14 | ||||
-rw-r--r-- | support/hololens/ServoApp/ServoControl/Servo.h | 22 | ||||
-rw-r--r-- | support/hololens/ServoApp/ServoControl/ServoControl.cpp | 14 | ||||
-rw-r--r-- | support/hololens/ServoApp/ServoControl/ServoControl.h | 3 | ||||
-rw-r--r-- | support/hololens/ServoApp/pch.h | 6 |
10 files changed, 91 insertions, 40 deletions
diff --git a/support/hololens/ServoApp/App.cpp b/support/hololens/ServoApp/App.cpp index 9f6495c49e1..9beeb0b9540 100644 --- a/support/hololens/ServoApp/App.cpp +++ b/support/hololens/ServoApp/App.cpp @@ -62,8 +62,29 @@ void App::OnLaunched(LaunchActivatedEventArgs const &e) { } } -void App::OnSuspending([[maybe_unused]] IInspectable const &sender, - [[maybe_unused]] SuspendingEventArgs const &e) { +void App::OnActivated(IActivatedEventArgs const &args) { + if (args.Kind() == + Windows::ApplicationModel::Activation::ActivationKind::Protocol) { + auto protocolActivatedEventArgs{args.as< + Windows::ApplicationModel::Activation::ProtocolActivatedEventArgs>()}; + + Frame rootFrame{nullptr}; + + auto content = Window::Current().Content(); + if (content == nullptr) { + rootFrame = Frame(); + rootFrame.Navigate(xaml_typename<ServoApp::BrowserPage>()); + Window::Current().Content(rootFrame); + Window::Current().Activate(); + } else { + rootFrame = content.try_as<Frame>(); + } + auto page = rootFrame.Content().try_as<BrowserPage>(); + page->LoadServoURI(protocolActivatedEventArgs.Uri()); + } +} + +void App::OnSuspending(IInspectable const &, SuspendingEventArgs const &) { auto content = Window::Current().Content(); Frame rootFrame = content.try_as<Frame>(); auto page = rootFrame.Content().try_as<BrowserPage>(); diff --git a/support/hololens/ServoApp/App.h b/support/hololens/ServoApp/App.h index b372c5b7759..2c46c102d77 100644 --- a/support/hololens/ServoApp/App.h +++ b/support/hololens/ServoApp/App.h @@ -11,6 +11,8 @@ struct App : AppT<App> { void OnLaunched( Windows::ApplicationModel::Activation::LaunchActivatedEventArgs const &); + void App::OnActivated( + Windows::ApplicationModel::Activation::IActivatedEventArgs const &args); void OnSuspending(IInspectable const &, Windows::ApplicationModel::SuspendingEventArgs const &); void OnNavigationFailed( diff --git a/support/hololens/ServoApp/BrowserPage.cpp b/support/hololens/ServoApp/BrowserPage.cpp index 6507306178d..0db7a055cec 100644 --- a/support/hololens/ServoApp/BrowserPage.cpp +++ b/support/hololens/ServoApp/BrowserPage.cpp @@ -7,6 +7,7 @@ #include "BrowserPage.h" #include "BrowserPage.g.cpp" +using namespace winrt::Windows::Foundation; using namespace winrt::Windows::UI::Xaml; using namespace winrt::Windows::UI::Core; using namespace winrt::Windows::UI::ViewManagement; @@ -36,10 +37,20 @@ void BrowserPage::BindServoEvents() { }); } -void BrowserPage::Shutdown() { - servoControl().Shutdown(); +void BrowserPage::LoadServoURI(Uri uri) { + auto scheme = uri.SchemeName(); + + if (scheme != SERVO_SCHEME) { + log("Unexpected URL: ", uri.RawUri().c_str()); + return; + } + std::wstring raw{uri.RawUri()}; + auto raw2 = raw.substr(SERVO_SCHEME_SLASH_SLASH.size()); + servoControl().LoadURIOrSearch(raw2); } +void BrowserPage::Shutdown() { servoControl().Shutdown(); } + /**** USER INTERACTIONS WITH UI ****/ void BrowserPage::OnBackButtonClicked(IInspectable const &, @@ -62,7 +73,7 @@ void BrowserPage::OnStopButtonClicked(IInspectable const &, servoControl().Stop(); } -void BrowserPage::OnURLEdited(IInspectable const &sender, +void BrowserPage::OnURLEdited(IInspectable const &, Input::KeyRoutedEventArgs const &e) { if (e.Key() == Windows::System::VirtualKey::Enter) { servoControl().Focus(FocusState::Programmatic); diff --git a/support/hololens/ServoApp/BrowserPage.h b/support/hololens/ServoApp/BrowserPage.h index c3f1c07943a..1d323eb9e15 100644 --- a/support/hololens/ServoApp/BrowserPage.h +++ b/support/hololens/ServoApp/BrowserPage.h @@ -9,6 +9,9 @@ namespace winrt::ServoApp::implementation { +static const hstring SERVO_SCHEME = L"servo"; +static const hstring SERVO_SCHEME_SLASH_SLASH = L"servo://"; + struct BrowserPage : BrowserPageT<BrowserPage> { public: BrowserPage(); @@ -18,12 +21,13 @@ public: void OnBackButtonClicked(Windows::Foundation::IInspectable const &, Windows::UI::Xaml::RoutedEventArgs const &); void OnReloadButtonClicked(Windows::Foundation::IInspectable const &, - Windows::UI::Xaml::RoutedEventArgs const &); + Windows::UI::Xaml::RoutedEventArgs const &); void OnStopButtonClicked(Windows::Foundation::IInspectable const &, Windows::UI::Xaml::RoutedEventArgs const &); void OnURLEdited(Windows::Foundation::IInspectable const &, Windows::UI::Xaml::Input::KeyRoutedEventArgs const &); void Shutdown(); + void LoadServoURI(Windows::Foundation::Uri uri); private: void BindServoEvents(); diff --git a/support/hololens/ServoApp/Package.appxmanifest b/support/hololens/ServoApp/Package.appxmanifest index c437822284c..52e5909a712 100644 --- a/support/hololens/ServoApp/Package.appxmanifest +++ b/support/hololens/ServoApp/Package.appxmanifest @@ -1,13 +1,10 @@ <?xml version="1.0" encoding="utf-8"?> <Package xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest" xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10" IgnorableNamespaces="uap mp"> - <Identity - Name="1d265729-8836-4bd3-9992-4cb111d1068b" - Publisher="CN=paul" - Version="1.0.0.0" /> - <mp:PhoneIdentity PhoneProductId="1d265729-8836-4bd3-9992-4cb111d1068b" PhonePublisherId="00000000-0000-0000-0000-000000000000"/> + <Identity Name="1d265729-8836-4bd3-9992-4cb111d1068b" Publisher="CN=paul" Version="1.0.0.0" /> + <mp:PhoneIdentity PhoneProductId="1d265729-8836-4bd3-9992-4cb111d1068b" PhonePublisherId="00000000-0000-0000-0000-000000000000" /> <Properties> <DisplayName>ServoApp</DisplayName> - <PublisherDisplayName>paul</PublisherDisplayName> + <PublisherDisplayName>Mozilla</PublisherDisplayName> <Logo>Assets\StoreLogo.png</Logo> </Properties> <Dependencies> @@ -18,15 +15,22 @@ </Resources> <Applications> <Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="ServoApp.App"> - <uap:VisualElements DisplayName="ServoApp" Description="Project for a single page C++/WinRT Universal Windows Platform (UWP) app with no predefined layout" - Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" BackgroundColor="transparent"> + <uap:VisualElements DisplayName="ServoApp" Description="A Servo-based browser." Square150x150Logo="Assets\Square150x150Logo.png" Square44x44Logo="Assets\Square44x44Logo.png" BackgroundColor="transparent"> <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"> </uap:DefaultTile> <uap:SplashScreen Image="Assets\SplashScreen.png" /> </uap:VisualElements> + <Extensions> + <uap:Extension Category="windows.protocol"> + <uap:Protocol Name="servo"> + <uap:Logo>Assets\StoreLogo.png</uap:Logo> + <uap:DisplayName>Servo URL</uap:DisplayName> + </uap:Protocol> + </uap:Extension> + </Extensions> </Application> </Applications> <Capabilities> <Capability Name="internetClient" /> </Capabilities> -</Package> +</Package>
\ No newline at end of file diff --git a/support/hololens/ServoApp/ServoControl/Servo.cpp b/support/hololens/ServoApp/ServoControl/Servo.cpp index 84077811c96..efd59e44ab6 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.cpp +++ b/support/hololens/ServoApp/ServoControl/Servo.cpp @@ -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 diff --git a/support/hololens/ServoApp/ServoControl/Servo.h b/support/hololens/ServoApp/ServoControl/Servo.h index c0ae8298226..b5a730ad523 100644 --- a/support/hololens/ServoApp/ServoControl/Servo.h +++ b/support/hololens/ServoApp/ServoControl/Servo.h @@ -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 diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.cpp b/support/hololens/ServoApp/ServoControl/ServoControl.cpp index bc2823d37af..9de129c1e4e 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.cpp +++ b/support/hololens/ServoApp/ServoControl/ServoControl.cpp @@ -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. diff --git a/support/hololens/ServoApp/ServoControl/ServoControl.h b/support/hololens/ServoApp/ServoControl/ServoControl.h index e5448a7f9ae..bd1d8a74d63 100644 --- a/support/hololens/ServoApp/ServoControl/ServoControl.h +++ b/support/hololens/ServoApp/ServoControl/ServoControl.h @@ -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 {}; } } diff --git a/support/hololens/ServoApp/pch.h b/support/hololens/ServoApp/pch.h index 0b8e6047454..85f3fabce79 100644 --- a/support/hololens/ServoApp/pch.h +++ b/support/hololens/ServoApp/pch.h @@ -41,13 +41,13 @@ #include <winrt/Windows.Storage.Streams.h> #include <winrt/Windows.UI.Core.h> #include <winrt/Windows.UI.Input.Spatial.h> -#include "winrt/Windows.UI.Popups.h" -#include "winrt/Windows.UI.ViewManagement.h" +#include <winrt/Windows.UI.Popups.h> +#include <winrt/Windows.UI.ViewManagement.h> #include <winrt/Windows.UI.Xaml.Controls.h> #include <winrt/Windows.UI.Xaml.Controls.Primitives.h> #include <winrt/Windows.UI.Xaml.Data.h> #include <winrt/Windows.UI.Xaml.h> -#include "winrt/Windows.UI.Xaml.Input.h" +#include <winrt/Windows.UI.Xaml.Input.h> #include <winrt/Windows.UI.Xaml.Interop.h> #include <winrt/Windows.UI.Xaml.Markup.h> #include <winrt/Windows.UI.Xaml.Navigation.h> |