Skip to content

Commit

Permalink
[webview_flutter] Add cookie and scroll APIs (flutter-tizen#40)
Browse files Browse the repository at this point in the history
* Implement a method channel for handling cookies
* Add GetScrollX and GetScrollY APIs
* Add code to set UserAgent when initializing the webview
  • Loading branch information
seungsoo47 authored Feb 17, 2021
1 parent c5a8930 commit 7059ce2
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 2 deletions.
Binary file not shown.
2 changes: 1 addition & 1 deletion packages/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ description: Tizen implementation of the webview plugin
version: 0.0.1

environment:
sdk: ">=2.7.0 <3.0.0"
sdk: ">=2.2.2 <3.0.0"
flutter: ">=1.20.0 <2.0.0"

dependencies:
Expand Down
18 changes: 18 additions & 0 deletions packages/webview_flutter/tizen/inc/lwe/LWEWebView.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ class LWE_EXPORT LWE {

enum class WebSecurityMode;

class LWE_EXPORT CookieManager {
public:
std::string GetCookie(std::string url);
bool HasCookies();
void ClearCookies();

static CookieManager* GetInstance();
static void Destroy();

private:
CookieManager();
~CookieManager();
};

class LWE_EXPORT Settings {
public:
Settings(const std::string& defaultUA, const std::string& ua);
Expand Down Expand Up @@ -290,6 +304,8 @@ class LWE_EXPORT WebContainer {
std::string GetTitle();
void ScrollTo(int x, int y);
void ScrollBy(int x, int y);
int GetScrollX();
int GetScrollY();

size_t Width();
size_t Height();
Expand Down Expand Up @@ -370,6 +386,8 @@ class LWE_EXPORT WebView {
std::string GetTitle();
void ScrollTo(int x, int y);
void ScrollBy(int x, int y);
int GetScrollX();
int GetScrollY();

virtual void* Unwrap()
{
Expand Down
Binary file not shown.
44 changes: 43 additions & 1 deletion packages/webview_flutter/tizen/src/webview.cc
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
webview->HandleMethodCall(call, std::move(result));
});

auto cookieChannel =
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
GetPluginRegistrar()->messenger(),
"plugins.flutter.io/cookie_manager",
&flutter::StandardMethodCodec::GetInstance());
cookieChannel->SetMethodCallHandler(
[webview = this](const auto& call, auto result) {
webview->HandleCookieMethodCall(call, std::move(result));
});

std::string url;
auto initialUrl = params[flutter::EncodableValue("initialUrl")];
if (std::holds_alternative<std::string>(initialUrl)) {
Expand All @@ -198,6 +208,16 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
}
}

// TODO: Not implemented
// auto media = params[flutter::EncodableValue("autoMediaPlaybackPolicy")];

auto userAgent = params[flutter::EncodableValue("userAgent")];
if (std::holds_alternative<std::string>(userAgent)) {
auto settings = webViewInstance_->GetSettings();
settings.SetUserAgentString(std::get<std::string>(userAgent));
webViewInstance_->SetSettings(settings);
}

webViewInstance_->RegisterOnPageStartedHandler(
[this](LWE::WebContainer* container, const std::string& url) {
LOG_DEBUG("RegisterOnPageStartedHandler(url: %s)\n", url.c_str());
Expand Down Expand Up @@ -834,9 +854,31 @@ void WebView::HandleMethodCall(
webViewInstance_->ScrollBy(x, y);
result->Success();
} else if (methodName.compare("getScrollX") == 0) {
result->NotImplemented();
result->Success(flutter::EncodableValue(webViewInstance_->GetScrollX()));
} else if (methodName.compare("getScrollY") == 0) {
result->Success(flutter::EncodableValue(webViewInstance_->GetScrollY()));
} else {
result->NotImplemented();
}
}

void WebView::HandleCookieMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (webViewInstance_ == nullptr) {
result->Error("Not Webview created");
return;
}

const auto methodName = method_call.method_name();
const auto& arguments = *method_call.arguments();

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

if (methodName.compare("clearCookies") == 0) {
LWE::CookieManager* cookie = LWE::CookieManager::GetInstance();
cookie->ClearCookies();
result->Success(flutter::EncodableValue(true));
} else {
result->NotImplemented();
}
Expand Down
3 changes: 3 additions & 0 deletions packages/webview_flutter/tizen/src/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class WebView : public PlatformView {
void HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
void HandleCookieMethodCall(
const flutter::MethodCall<flutter::EncodableValue>& method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
std::string GetChannelName();
void InitWebView();

Expand Down

0 comments on commit 7059ce2

Please sign in to comment.