Skip to content

Commit

Permalink
[windows] Implement getInitialUrl metnod
Browse files Browse the repository at this point in the history
  • Loading branch information
lijy91 committed Mar 20, 2022
1 parent ed7916a commit 4b6c471
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions windows/protocol_handler_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
// This must be included before many other Windows headers.
#include <windows.h>

// For getPlatformVersion; remove unless needed for your plugin implementation.
#include <VersionHelpers.h>

#include <flutter/method_channel.h>
#include <flutter/plugin_registrar_windows.h>
#include <flutter/standard_method_codec.h>
Expand All @@ -29,6 +26,8 @@ class ProtocolHandlerPlugin : public flutter::Plugin {
return channel_.get();
}

std::string ProtocolHandlerPlugin::GetInitialUrl();

virtual ~ProtocolHandlerPlugin();

private:
Expand Down Expand Up @@ -102,20 +101,25 @@ std::optional<LRESULT> ProtocolHandlerPlugin::HandleWindowProc(HWND hwnd,
return std::nullopt;
}

std::string ProtocolHandlerPlugin::GetInitialUrl() {
int argc;
wchar_t** argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
if (argv == nullptr || argc < 2) {
return "";
}

std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
std::string url = converter.to_bytes(argv[1]);
::LocalFree(argv);
return url;
}

void ProtocolHandlerPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("getPlatformVersion") == 0) {
std::ostringstream version_stream;
version_stream << "Windows ";
if (IsWindows10OrGreater()) {
version_stream << "10+";
} else if (IsWindows8OrGreater()) {
version_stream << "8";
} else if (IsWindows7OrGreater()) {
version_stream << "7";
}
result->Success(flutter::EncodableValue(version_stream.str()));
if (method_call.method_name().compare("getInitialUrl") == 0) {
std::string value = GetInitialUrl();
result->Success(flutter::EncodableValue(value.c_str()));
} else {
result->NotImplemented();
}
Expand Down

0 comments on commit 4b6c471

Please sign in to comment.