Skip to content

[webview_flutter] Add some APIs #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
8 changes: 8 additions & 0 deletions packages/webview_flutter/tizen/inc/lwe/LWEWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ class LWE_EXPORT WebContainer {
void SetUserData(const std::string& key, void* data);
void* GetUserData(const std::string& key);

std::string GetTitle();
void ScrollTo(int x, int y);
void ScrollBy(int x, int y);

size_t Width();
size_t Height();

Expand Down Expand Up @@ -363,6 +367,10 @@ class LWE_EXPORT WebView {
void SetUserData(const std::string& key, void* data);
void* GetUserData(const std::string& key);

std::string GetTitle();
void ScrollTo(int x, int y);
void ScrollBy(int x, int y);

virtual void* Unwrap()
{
// Some platform returns associated native handle ex) Evas_Object*
Expand Down
Binary file not shown.
60 changes: 43 additions & 17 deletions packages/webview_flutter/tizen/src/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ static std::string ErrorCodeToString(int errorCode) {
case RequestErrorType::UnsupportedSchemeError:
return "unsupportedScheme";
}
std::string message = "Could not find a string for errorCode: " + errorCode;

std::string message =
"Could not find a string for errorCode: " + std::to_string(errorCode);
throw std::invalid_argument(message);
}

Expand Down Expand Up @@ -170,11 +172,12 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
webview->HandleMethodCall(call, std::move(result));
});

std::string url;
auto initialUrl = params[flutter::EncodableValue("initialUrl")];
if (std::holds_alternative<std::string>(initialUrl)) {
currentUrl_ = std::get<std::string>(initialUrl);
url = std::get<std::string>(initialUrl);
} else {
currentUrl_ = "about:blank";
url = "about:blank";
}

auto settings = params[flutter::EncodableValue("settings")];
Expand Down Expand Up @@ -207,7 +210,8 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
});
webViewInstance_->RegisterOnPageLoadedHandler(
[this](LWE::WebContainer* container, const std::string& url) {
LOG_DEBUG("RegisterOnPageLoadedHandler(url: %s)\n", url.c_str());
LOG_DEBUG("RegisterOnPageLoadedHandler(url: %s)(title:%s)\n",
url.c_str(), container->GetTitle().c_str());
flutter::EncodableMap map;
map.insert(
std::make_pair<flutter::EncodableValue, flutter::EncodableValue>(
Expand Down Expand Up @@ -261,25 +265,29 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
return true;
});

webViewInstance_->LoadURL(currentUrl_);
webViewInstance_->LoadURL(url);
}

void WebView::ApplySettings(flutter::EncodableMap settings) {
for (auto const& [key, val] : settings) {
if (std::holds_alternative<std::string>(key)) {
std::string k = std::get<std::string>(key);
if ("jsMode" == k) {
// TODO: Not implemented
// NOTE: Not supported by LWE on Tizen.
} else if ("hasNavigationDelegate" == k) {
if (std::holds_alternative<bool>(val)) {
hasNavigationDelegate_ = std::get<bool>(val);
}
} else if ("debuggingEnabled" == k) {
// TODO: Not implemented
// NOTE: Not supported by LWE on Tizen.
} else if ("gestureNavigationEnabled" == k) {
// TODO: Not implemented
// NOTE: Not supported by LWE on Tizen.
} else if ("userAgent" == k) {
// TODO: Not implemented
if (std::holds_alternative<std::string>(val)) {
auto settings = webViewInstance_->GetSettings();
settings.SetUserAgentString(std::get<std::string>(val));
webViewInstance_->SetSettings(settings);
}
} else {
throw std::invalid_argument("Unknown WebView setting: " + k);
}
Expand Down Expand Up @@ -739,14 +747,26 @@ void WebView::HandleMethodCall(
const auto methodName = method_call.method_name();
const auto& arguments = *method_call.arguments();

LOG_DEBUG("HandleMethodCall : %s \n ", methodName.c_str());
LOG_DEBUG("WebView::HandleMethodCall : %s \n ", methodName.c_str());

if (methodName.compare("loadUrl") == 0) {
currentUrl_ = ExtractStringFromMap(arguments, "url");
webViewInstance_->LoadURL(GetCurrentUrl());
std::string url = ExtractStringFromMap(arguments, "url");
webViewInstance_->LoadURL(url);
result->Success();
} else if (methodName.compare("updateSettings") == 0) {
result->NotImplemented();
if (std::holds_alternative<flutter::EncodableMap>(arguments)) {
auto settings = std::get<flutter::EncodableMap>(arguments);
if (settings.size() > 0) {
try {
ApplySettings(settings);
} catch (const std::invalid_argument& ex) {
LOG_ERROR("[Exception] %s\n", ex.what());
result->Error(ex.what());
return;
}
}
}
result->Success();
} else if (methodName.compare("canGoBack") == 0) {
result->Success(flutter::EncodableValue(webViewInstance_->CanGoBack()));
} else if (methodName.compare("canGoForward") == 0) {
Expand All @@ -761,7 +781,7 @@ void WebView::HandleMethodCall(
webViewInstance_->Reload();
result->Success();
} else if (methodName.compare("currentUrl") == 0) {
result->Success(flutter::EncodableValue(GetCurrentUrl().c_str()));
result->Success(flutter::EncodableValue(webViewInstance_->GetURL()));
} else if (methodName.compare("evaluateJavascript") == 0) {
if (std::holds_alternative<std::string>(arguments)) {
std::string jsString = std::get<std::string>(arguments);
Expand Down Expand Up @@ -802,11 +822,17 @@ void WebView::HandleMethodCall(
webViewInstance_->ClearCache();
result->Success();
} else if (methodName.compare("getTitle") == 0) {
result->NotImplemented();
result->Success(flutter::EncodableValue(webViewInstance_->GetTitle()));
} else if (methodName.compare("scrollTo") == 0) {
result->NotImplemented();
int x = ExtractIntFromMap(arguments, "x");
int y = ExtractIntFromMap(arguments, "y");
webViewInstance_->ScrollTo(x, y);
result->Success();
} else if (methodName.compare("scrollBy") == 0) {
result->NotImplemented();
int x = ExtractIntFromMap(arguments, "x");
int y = ExtractIntFromMap(arguments, "y");
webViewInstance_->ScrollBy(x, y);
result->Success();
} else if (methodName.compare("getScrollX") == 0) {
result->NotImplemented();
} else if (methodName.compare("getScrollY") == 0) {
Expand Down
2 changes: 0 additions & 2 deletions packages/webview_flutter/tizen/src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,13 @@ class WebView : public PlatformView {
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
std::string GetChannelName();
const std::string& GetCurrentUrl() { return currentUrl_; }
void InitWebView();

void RegisterJavaScriptChannelName(const std::string& name);
void ApplySettings(flutter::EncodableMap);

FlutterTextureRegistrar* textureRegistrar_;
LWE::WebContainer* webViewInstance_;
std::string currentUrl_;
double width_;
double height_;
tbm_surface_h tbmSurface_;
Expand Down