-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[webview_flutter] Send history events #2379
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
## 1.0.3 | ||
|
||
* Add support for Navigation History observation. | ||
|
||
## 1.0.2 | ||
|
||
* Android Code Inspection and Clean up. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2019 The Chromium 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 <Flutter/Flutter.h> | ||
#import <Foundation/Foundation.h> | ||
#import <WebKit/WebKit.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface FLTWKHistoryDelegate : NSObject | ||
- (instancetype)initWithWebView:(WKWebView *)webView channel:(FlutterMethodChannel *)channel; | ||
- (void)stopObserving:(WKWebView *)webView; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2019 The Chromium 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 "FLTWKHistoryDelegate.h" | ||
|
||
NSString *const keyPath = @"URL"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: this should be |
||
|
||
@implementation FLTWKHistoryDelegate { | ||
FlutterMethodChannel *_methodChannel; | ||
} | ||
|
||
- (instancetype)initWithWebView:(id)webView channel:(FlutterMethodChannel *)channel { | ||
self = [super init]; | ||
if (self) { | ||
_methodChannel = channel; | ||
[webView addObserver:self | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this feature based on KVO rather than |
||
forKeyPath:keyPath | ||
options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld | ||
context:nil]; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (void)stopObserving:(WKWebView *)webView { | ||
[webView removeObserver:self forKeyPath:keyPath]; | ||
} | ||
|
||
- (void)observeValueForKeyPath:(NSString *)keyPath | ||
ofObject:(id)object | ||
change:(NSDictionary *)change | ||
context:(void *)context { | ||
if ([change[NSKeyValueChangeNewKey] isKindOfClass:[NSURL class]]) { | ||
NSURL *newUrl = change[NSKeyValueChangeNewKey]; | ||
[_methodChannel invokeMethod:@"onUpdateVisitedHistory" | ||
arguments:@{@"url" : [newUrl absoluteString]}]; | ||
} | ||
} | ||
|
||
@end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,9 @@ abstract class WebViewPlatformCallbacksHandler { | |
|
||
/// Report web resource loading error to the host application. | ||
void onWebResourceError(WebResourceError error); | ||
|
||
/// Invoked by [WebViewPlatformController] when the URL has changed. | ||
void onUpdateVisitedHistory(String url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The naming here seems very Android-centric; is there a reason not to call it something more generic like |
||
} | ||
|
||
/// Possible error type categorizations used by [WebResourceError]. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -889,6 +889,22 @@ void main() { | |
|
||
expect(platformWebView.userAgent, 'UA'); | ||
}); | ||
testWidgets('onUpdateVisitedHistory', (WidgetTester tester) async { | ||
String visitedUrl; | ||
|
||
await tester.pumpWidget(WebView( | ||
initialUrl: 'https://youtube.com', | ||
onUpdateVisitedHistory: (String url) { | ||
visitedUrl = url; | ||
})); | ||
|
||
final FakePlatformWebView platformWebView = | ||
fakePlatformViewsController.lastCreatedView; | ||
|
||
platformWebView.fakeOnUpdateVisitedHistoryCallback('https://google.com'); | ||
|
||
expect(visitedUrl, 'https://google.com'); | ||
}); | ||
} | ||
|
||
class FakePlatformWebView { | ||
|
@@ -1052,6 +1068,24 @@ class FakePlatformWebView { | |
); | ||
} | ||
|
||
void fakeOnUpdateVisitedHistoryCallback(String nextUrl) { | ||
final StandardMethodCodec codec = const StandardMethodCodec(); | ||
|
||
final ByteData data = codec.encodeMethodCall(MethodCall( | ||
'onUpdateVisitedHistory', | ||
<dynamic, dynamic>{'url': nextUrl}, | ||
)); | ||
|
||
// TODO(hterkelsen): Remove this when defaultBinaryMessages is in stable. | ||
// https://github.com/flutter/flutter/issues/33446 | ||
// ignore: deprecated_member_use | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is obsolete. |
||
BinaryMessages.handlePlatformMessage( | ||
channel.name, | ||
data, | ||
(ByteData data) {}, | ||
); | ||
} | ||
|
||
void _loadUrl(String url) { | ||
history = history.sublist(0, currentPosition + 1); | ||
history.add(url); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This class and its methods need declaration comments, per Google style.