forked from flutter/plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[webview_flutter] Add supporting interfaces for setting cookies to pl…
…atform interface. (flutter#4555)
- Loading branch information
Showing
12 changed files
with
193 additions
and
2 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
packages/webview_flutter/webview_flutter_platform_interface/CHANGELOG.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
...webview_flutter_platform_interface/lib/src/platform_interface/webview_cookie_manager.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:plugin_platform_interface/plugin_platform_interface.dart'; | ||
import 'package:webview_flutter_platform_interface/src/types/webview_cookie.dart'; | ||
|
||
/// Interface for a platform implementation of a cookie manager. | ||
/// | ||
/// Platform implementations should extend this class rather than implement it as `webview_flutter` | ||
/// does not consider newly added methods to be breaking changes. Extending this class | ||
/// (using `extends`) ensures that the subclass will get the default implementation, while | ||
/// platform implementations that `implements` this interface will be broken by newly added | ||
/// [WebViewCookieManagerPlatform] methods. | ||
abstract class WebViewCookieManagerPlatform extends PlatformInterface { | ||
/// Constructs a WebViewCookieManagerPlatform. | ||
WebViewCookieManagerPlatform() : super(token: _token); | ||
|
||
static final Object _token = Object(); | ||
|
||
static WebViewCookieManagerPlatform? _instance; | ||
|
||
/// The instance of [WebViewCookieManagerPlatform] to use. | ||
static WebViewCookieManagerPlatform? get instance => _instance; | ||
|
||
/// Platform-specific plugins should set this with their own platform-specific | ||
/// class that extends [WebViewCookieManagerPlatform] when they register themselves. | ||
static set instance(WebViewCookieManagerPlatform? instance) { | ||
if (instance == null) { | ||
throw AssertionError( | ||
'Platform interfaces can only be set to a non-null instance'); | ||
} | ||
PlatformInterface.verifyToken(instance, _token); | ||
_instance = instance; | ||
} | ||
|
||
/// Clears all cookies for all [WebView] instances. | ||
/// | ||
/// Returns true if cookies were present before clearing, else false. | ||
Future<bool> clearCookies() { | ||
throw UnimplementedError( | ||
'clearCookies is not implemented on the current platform'); | ||
} | ||
|
||
/// Sets a cookie for all [WebView] instances. | ||
Future<void> setCookie(WebViewCookie cookie) { | ||
throw UnimplementedError( | ||
'setCookie is not implemented on the current platform'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...ages/webview_flutter/webview_flutter_platform_interface/lib/src/types/webview_cookie.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
/// A cookie that can be set globally for all web views | ||
/// using [WebViewCookieManagerPlatform]. | ||
class WebViewCookie { | ||
/// Constructs a new [WebViewCookie]. | ||
const WebViewCookie( | ||
{required this.name, | ||
required this.value, | ||
required this.domain, | ||
this.path = '/'}); | ||
|
||
/// The cookie-name of the cookie. | ||
/// | ||
/// Its value should match "cookie-name" in RFC6265bis: | ||
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1 | ||
final String name; | ||
|
||
/// The cookie-value of the cookie. | ||
/// | ||
/// Its value should match "cookie-value" in RFC6265bis: | ||
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1 | ||
final String value; | ||
|
||
/// The domain-value of the cookie. | ||
/// | ||
/// Its value should match "domain-value" in RFC6265bis: | ||
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1 | ||
final String domain; | ||
|
||
/// The path-value of the cookie. | ||
/// Is set to `/` in the constructor by default. | ||
/// | ||
/// Its value should match "path-value" in RFC6265bis: | ||
/// https://datatracker.ietf.org/doc/html/draft-ietf-httpbis-rfc6265bis-02#section-4.1.1 | ||
final String path; | ||
|
||
/// Serializes the [WebViewCookie] to a Map<String, String>. | ||
Map<String, String> toJson() { | ||
return <String, String>{ | ||
'name': name, | ||
'value': value, | ||
'domain': domain, | ||
'path': path | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...w_flutter_platform_interface/test/src/platform_interface/webview_cookie_manager_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:webview_flutter_platform_interface/src/platform_interface/platform_interface.dart'; | ||
import 'package:webview_flutter_platform_interface/src/types/webview_cookie.dart'; | ||
|
||
void main() { | ||
WebViewCookieManagerPlatform? cookieManager; | ||
|
||
setUp(() { | ||
cookieManager = TestWebViewCookieManagerPlatform(); | ||
}); | ||
|
||
test('clearCookies should throw UnimplementedError', () { | ||
expect(() => cookieManager!.clearCookies(), throwsUnimplementedError); | ||
}); | ||
|
||
test('setCookie should throw UnimplementedError', () { | ||
const WebViewCookie cookie = | ||
WebViewCookie(domain: 'flutter.dev', name: 'foo', value: 'bar'); | ||
expect(() => cookieManager!.setCookie(cookie), throwsUnimplementedError); | ||
}); | ||
} | ||
|
||
class TestWebViewCookieManagerPlatform extends WebViewCookieManagerPlatform {} |
21 changes: 21 additions & 0 deletions
21
...ebview_flutter/webview_flutter_platform_interface/test/src/types/webview_cookie_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:webview_flutter_platform_interface/src/types/types.dart'; | ||
|
||
void main() { | ||
test('WebViewCookie should serialize correctly', () { | ||
WebViewCookie cookie; | ||
Map<String, String> serializedCookie; | ||
// Test serialization | ||
cookie = const WebViewCookie( | ||
name: 'foo', value: 'bar', domain: 'example.com', path: '/test'); | ||
serializedCookie = cookie.toJson(); | ||
expect(serializedCookie['name'], 'foo'); | ||
expect(serializedCookie['value'], 'bar'); | ||
expect(serializedCookie['domain'], 'example.com'); | ||
expect(serializedCookie['path'], '/test'); | ||
}); | ||
} |