Skip to content

Commit 0728da6

Browse files
seungsoo47bwikbs
authored andcommitted
[webview_flutter] Add cookie and scroll APIs (flutter-tizen#40)
* Implement a method channel for handling cookies * Add GetScrollX and GetScrollY APIs * Add code to set UserAgent when initializing the webview
1 parent 25f0ba4 commit 0728da6

File tree

6 files changed

+65
-2
lines changed

6 files changed

+65
-2
lines changed

packages/webview_flutter/pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: Tizen implementation of the webview plugin
33
version: 0.0.1
44

55
environment:
6-
sdk: ">=2.7.0 <3.0.0"
6+
sdk: ">=2.2.2 <3.0.0"
77
flutter: ">=1.20.0 <2.0.0"
88

99
dependencies:

packages/webview_flutter/tizen/inc/lwe/LWEWebView.h

+18
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ class LWE_EXPORT LWE {
5353

5454
enum class WebSecurityMode;
5555

56+
class LWE_EXPORT CookieManager {
57+
public:
58+
std::string GetCookie(std::string url);
59+
bool HasCookies();
60+
void ClearCookies();
61+
62+
static CookieManager* GetInstance();
63+
static void Destroy();
64+
65+
private:
66+
CookieManager();
67+
~CookieManager();
68+
};
69+
5670
class LWE_EXPORT Settings {
5771
public:
5872
Settings(const std::string& defaultUA, const std::string& ua);
@@ -290,6 +304,8 @@ class LWE_EXPORT WebContainer {
290304
std::string GetTitle();
291305
void ScrollTo(int x, int y);
292306
void ScrollBy(int x, int y);
307+
int GetScrollX();
308+
int GetScrollY();
293309

294310
size_t Width();
295311
size_t Height();
@@ -370,6 +386,8 @@ class LWE_EXPORT WebView {
370386
std::string GetTitle();
371387
void ScrollTo(int x, int y);
372388
void ScrollBy(int x, int y);
389+
int GetScrollX();
390+
int GetScrollY();
373391

374392
virtual void* Unwrap()
375393
{
Binary file not shown.

packages/webview_flutter/tizen/src/webview.cc

+43-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
172172
webview->HandleMethodCall(call, std::move(result));
173173
});
174174

175+
auto cookieChannel =
176+
std::make_unique<flutter::MethodChannel<flutter::EncodableValue>>(
177+
GetPluginRegistrar()->messenger(),
178+
"plugins.flutter.io/cookie_manager",
179+
&flutter::StandardMethodCodec::GetInstance());
180+
cookieChannel->SetMethodCallHandler(
181+
[webview = this](const auto& call, auto result) {
182+
webview->HandleCookieMethodCall(call, std::move(result));
183+
});
184+
175185
std::string url;
176186
auto initialUrl = params[flutter::EncodableValue("initialUrl")];
177187
if (std::holds_alternative<std::string>(initialUrl)) {
@@ -198,6 +208,16 @@ WebView::WebView(flutter::PluginRegistrar* registrar, int viewId,
198208
}
199209
}
200210

211+
// TODO: Not implemented
212+
// auto media = params[flutter::EncodableValue("autoMediaPlaybackPolicy")];
213+
214+
auto userAgent = params[flutter::EncodableValue("userAgent")];
215+
if (std::holds_alternative<std::string>(userAgent)) {
216+
auto settings = webViewInstance_->GetSettings();
217+
settings.SetUserAgentString(std::get<std::string>(userAgent));
218+
webViewInstance_->SetSettings(settings);
219+
}
220+
201221
webViewInstance_->RegisterOnPageStartedHandler(
202222
[this](LWE::WebContainer* container, const std::string& url) {
203223
LOG_DEBUG("RegisterOnPageStartedHandler(url: %s)\n", url.c_str());
@@ -834,9 +854,31 @@ void WebView::HandleMethodCall(
834854
webViewInstance_->ScrollBy(x, y);
835855
result->Success();
836856
} else if (methodName.compare("getScrollX") == 0) {
837-
result->NotImplemented();
857+
result->Success(flutter::EncodableValue(webViewInstance_->GetScrollX()));
838858
} else if (methodName.compare("getScrollY") == 0) {
859+
result->Success(flutter::EncodableValue(webViewInstance_->GetScrollY()));
860+
} else {
839861
result->NotImplemented();
862+
}
863+
}
864+
865+
void WebView::HandleCookieMethodCall(
866+
const flutter::MethodCall<flutter::EncodableValue>& method_call,
867+
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
868+
if (webViewInstance_ == nullptr) {
869+
result->Error("Not Webview created");
870+
return;
871+
}
872+
873+
const auto methodName = method_call.method_name();
874+
const auto& arguments = *method_call.arguments();
875+
876+
LOG_DEBUG("WebView::HandleMethodCall : %s \n ", methodName.c_str());
877+
878+
if (methodName.compare("clearCookies") == 0) {
879+
LWE::CookieManager* cookie = LWE::CookieManager::GetInstance();
880+
cookie->ClearCookies();
881+
result->Success(flutter::EncodableValue(true));
840882
} else {
841883
result->NotImplemented();
842884
}

packages/webview_flutter/tizen/src/webview.h

+3
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class WebView : public PlatformView {
4343
void HandleMethodCall(
4444
const flutter::MethodCall<flutter::EncodableValue>& method_call,
4545
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
46+
void HandleCookieMethodCall(
47+
const flutter::MethodCall<flutter::EncodableValue>& method_call,
48+
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result);
4649
std::string GetChannelName();
4750
void InitWebView();
4851

0 commit comments

Comments
 (0)