Skip to content

Commit

Permalink
[webview_flutter] Add a method for getting the user agent (#4472)
Browse files Browse the repository at this point in the history
Aggregate PR for adding a method for retrieving the user agent. 

Internal: b/272582342
  • Loading branch information
bparrishMines committed Sep 30, 2023
1 parent 41a3ec2 commit f68661d
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 26 deletions.
4 changes: 4 additions & 0 deletions packages/webview_flutter/webview_flutter/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.3.0

* Adds support to retrieve the user agent. See `WebViewController.getUserAgent`.

## 4.2.4

* Adds pub topics to package metadata.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ Future<void> main() async {

await pageFinished.future;

final String customUserAgent = await _getUserAgent(controller);
final String? customUserAgent = await controller.getUserAgent();
expect(customUserAgent, 'Custom_User_Agent1');
});

Expand Down Expand Up @@ -876,22 +876,6 @@ String _webViewString(String value) {
return '"$value"';
}

/// Returns the value used for the HTTP User-Agent: request header in subsequent HTTP requests.
Future<String> _getUserAgent(WebViewController controller) async {
return _runJavascriptReturningResult(controller, 'navigator.userAgent;');
}

Future<String> _runJavascriptReturningResult(
WebViewController controller,
String js,
) async {
if (defaultTargetPlatform == TargetPlatform.iOS) {
return await controller.runJavaScriptReturningResult(js) as String;
}
return jsonDecode(await controller.runJavaScriptReturningResult(js) as String)
as String;
}

class ResizableWebView extends StatefulWidget {
const ResizableWebView({
super.key,
Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/webview_flutter/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ dependencies:
# The example app is bundled with the plugin so we use a path dependency on
# the parent directory to use the current plugin's version.
path: ../
webview_flutter_android: ^3.6.0
webview_flutter_wkwebview: ^3.4.0
webview_flutter_android: ^3.12.0
webview_flutter_wkwebview: ^3.9.0

dev_dependencies:
build_runner: ^2.1.5
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ class WebViewController {
Future<void> setUserAgent(String? userAgent) {
return platform.setUserAgent(userAgent);
}

/// Gets the value used for the HTTP `User-Agent:` request header.
Future<String?> getUserAgent() {
return platform.getUserAgent();
}
}

/// Permissions request when web content requests access to protected resources.
Expand Down
4 changes: 2 additions & 2 deletions packages/webview_flutter/webview_flutter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: webview_flutter
description: A Flutter plugin that provides a WebView widget on Android and iOS.
repository: https://github.com/flutter/packages/tree/main/packages/webview_flutter/webview_flutter
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+webview%22
version: 4.2.4
version: 4.3.0

environment:
sdk: ">=2.19.0 <4.0.0"
Expand All @@ -20,7 +20,7 @@ dependencies:
flutter:
sdk: flutter
webview_flutter_android: ^3.0.0
webview_flutter_platform_interface: ^2.3.0
webview_flutter_platform_interface: ^2.6.0
webview_flutter_wkwebview: ^3.0.0

dev_dependencies:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/legacy/webview_flutter_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i9;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/navigation_delegate_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i8;

Expand Down Expand Up @@ -208,6 +210,16 @@ class MockPlatformNavigationDelegate extends _i1.Mock
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);
@override
_i8.Future<void> setOnHttpError(_i3.HttpResponseErrorCallback? onHttpError) =>
(super.noSuchMethod(
Invocation.method(
#setOnHttpError,
[onHttpError],
),
returnValue: _i8.Future<void>.value(),
returnValueForMissingStub: _i8.Future<void>.value(),
) as _i8.Future<void>);
@override
_i8.Future<void> setOnProgress(_i3.ProgressCallback? onProgress) =>
(super.noSuchMethod(
Invocation.method(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,23 @@ void main() {
requestCallback(const TestPlatformWebViewPermissionRequest());
expect(permissionRequestCallbackCalled, isTrue);
});

test('getUserAgent', () async {
final MockPlatformWebViewController mockPlatformWebViewController =
MockPlatformWebViewController();

const String userAgent = 'str';

when(mockPlatformWebViewController.getUserAgent()).thenAnswer(
(_) => Future<String?>.value(userAgent),
);

final WebViewController webViewController = WebViewController.fromPlatform(
mockPlatformWebViewController,
);

await expectLater(webViewController.getUserAgent(), completion(userAgent));
});
}

class TestPlatformWebViewPermissionRequest
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/webview_controller_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i5;
import 'dart:ui' as _i3;
Expand Down Expand Up @@ -353,6 +355,25 @@ class MockPlatformWebViewController extends _i1.Mock
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<String?> getUserAgent() => (super.noSuchMethod(
Invocation.method(
#getUserAgent,
[],
),
returnValue: _i5.Future<String?>.value(),
) as _i5.Future<String?>);
@override
_i5.Future<void> setOnConsoleMessage(
void Function(_i2.JavaScriptConsoleMessage)? onConsoleMessage) =>
(super.noSuchMethod(
Invocation.method(
#setOnConsoleMessage,
[onConsoleMessage],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
}

/// A class which mocks [PlatformNavigationDelegate].
Expand Down Expand Up @@ -405,6 +426,16 @@ class MockPlatformNavigationDelegate extends _i1.Mock
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setOnHttpError(_i6.HttpResponseErrorCallback? onHttpError) =>
(super.noSuchMethod(
Invocation.method(
#setOnHttpError,
[onHttpError],
),
returnValue: _i5.Future<void>.value(),
returnValueForMissingStub: _i5.Future<void>.value(),
) as _i5.Future<void>);
@override
_i5.Future<void> setOnProgress(_i6.ProgressCallback? onProgress) =>
(super.noSuchMethod(
Invocation.method(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/webview_cookie_manager_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i4;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Mocks generated by Mockito 5.4.0 from annotations
// Mocks generated by Mockito 5.4.1 from annotations
// in webview_flutter/test/webview_widget_test.dart.
// Do not manually edit this file.

// @dart=2.19

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:async' as _i7;
import 'dart:ui' as _i3;
Expand Down Expand Up @@ -371,6 +373,25 @@ class MockPlatformWebViewController extends _i1.Mock
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);
@override
_i7.Future<String?> getUserAgent() => (super.noSuchMethod(
Invocation.method(
#getUserAgent,
[],
),
returnValue: _i7.Future<String?>.value(),
) as _i7.Future<String?>);
@override
_i7.Future<void> setOnConsoleMessage(
void Function(_i2.JavaScriptConsoleMessage)? onConsoleMessage) =>
(super.noSuchMethod(
Invocation.method(
#setOnConsoleMessage,
[onConsoleMessage],
),
returnValue: _i7.Future<void>.value(),
returnValueForMissingStub: _i7.Future<void>.value(),
) as _i7.Future<void>);
}

/// A class which mocks [PlatformWebViewWidget].
Expand Down

0 comments on commit f68661d

Please sign in to comment.