Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 30 additions & 63 deletions src/objc/ModalWebViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ - (void)viewDidLoad {
// Create a WKWebsiteDataStore
self.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
configuration.websiteDataStore = self.websiteDataStore;
// [self.websiteDataStore.httpCookieStore addObserver:self];

// Initialize and configure the WKWebView
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds
Expand Down Expand Up @@ -65,6 +64,7 @@ + (BOOL)accessInstanceVariablesDirectly {
}

- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
// Check if the controller or its navigation controller is being dismissed
if (self.isBeingDismissed || self.navigationController.isBeingDismissed) {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
Expand All @@ -84,45 +84,37 @@ - (void)viewDidDisappear:(BOOL)animated {
}
}

- (NSDictionary *)getBrowserCookiesForCurrentUrl {
__block NSMutableDictionary *cookieDict = [NSMutableDictionary dictionary];
- (void)getBrowserCookiesForCurrentUrlWithCompletion:(void (^)(NSDictionary *))completion {
NSMutableDictionary *cookieDict = [NSMutableDictionary dictionary];
NSURL *url = self.webView.URL;
if (url == nil) {
return cookieDict;
completion(cookieDict);
return;
}

WKHTTPCookieStore *cookieStore =
self.webView.configuration.websiteDataStore.httpCookieStore;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> *cookies) {
for (NSHTTPCookie *cookie in cookies) {
// Update self.cookies with the latest cookies
if ([url.host hasSuffix:cookie.domain]) {
[cookieDict setObject:cookie.value forKey:cookie.name];
}
}
dispatch_semaphore_signal(semaphore);
completion(cookieDict);
}];
dispatch_semaphore_wait(semaphore,
dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC));
return cookieDict;
}

- (void)updateCapturedCookies {
- (void)updateCapturedCookiesWithCompletion:(void (^)(void))completion {
WKHTTPCookieStore *cookieStore =
self.webView.configuration.websiteDataStore.httpCookieStore;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);

[cookieStore getAllCookies:^(NSArray<NSHTTPCookie *> *cookies) {
for (NSHTTPCookie *cookie in cookies) {
// Update self.cookies with the latest cookies
[self.cookies setObject:cookie.value forKey:cookie.name];
}
dispatch_semaphore_signal(semaphore);
completion();
}];
dispatch_semaphore_wait(semaphore,
dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC));
}

- (void)openRequest:(NSMutableURLRequest *)request {
Expand All @@ -139,20 +131,6 @@ - (instancetype)initWithRequest:(NSMutableURLRequest *)request {
}

- (void)close {
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:@"close" forKey:@"event"];
[dict setObject:[NSString stringWithFormat:@"%f", [[NSDate date]
timeIntervalSince1970]]
forKey:@"id"];
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:&error];
NSString *payload = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];

opacity_core::emit_webview_event([payload UTF8String]);

[self dismissViewControllerAnimated:YES completion:nil];
}

Expand All @@ -169,32 +147,21 @@ - (void)resetVisitedUrls {

#pragma mark - WKNavigationDelegate Methods

// Called when the web view starts to load a page
- (void)webView:(WKWebView *)webView
didStartProvisionalNavigation:(WKNavigation *)navigation {
if (webView.URL) {
[self addToVisitedUrls:webView.URL.absoluteString];
}
}

- (NSString *)getHtmlBody {
__block NSString *body = nil;
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
- (void)getHtmlBodyWithCompletion:(void (^)(NSString *))completion {
[self.webView
evaluateJavaScript:@"document.documentElement.outerHTML.toString()"
completionHandler:^(NSString *html, NSError *error) {
if (!error) {
body = html;
}
dispatch_semaphore_signal(semaphore);
completion(html);
}];
dispatch_semaphore_wait(semaphore,
dispatch_time(DISPATCH_TIME_NOW, 5 * NSEC_PER_SEC));

return body;
}

/// Called when the page finishes loading
- (void)webView:(WKWebView *)webView
didFinishNavigation:(WKNavigation *)navigation {
NSURL *url = webView.URL;
Expand All @@ -208,24 +175,26 @@ - (void)webView:(WKWebView *)webView
[dict setObject:@"navigation" forKey:@"event"];
[dict setObject:event_id forKey:@"id"];

NSString *body = [self getHtmlBody];
if (body != nil) {
[dict setObject:body forKey:@"html_body"];
}

[self updateCapturedCookies];
[dict setObject:self.cookies forKey:@"cookies"];
[dict setObject:self.visitedUrls forKey:@"visited_urls"];

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:&error];
NSString *payload = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];
[self getHtmlBodyWithCompletion:^(NSString *body) {
if (body != nil) {
[dict setObject:body forKey:@"html_body"];
}

opacity_core::emit_webview_event([payload UTF8String]);
[self resetVisitedUrls];
[self updateCapturedCookiesWithCompletion:^{
[dict setObject:self.cookies forKey:@"cookies"];
[dict setObject:self.visitedUrls forKey:@"visited_urls"];

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict
options:0
error:&error];
NSString *payload = [[NSString alloc] initWithData:jsonData
encoding:NSUTF8StringEncoding];

opacity_core::emit_webview_event([payload UTF8String]);
[self resetVisitedUrls];
}];
}];
}
}

Expand All @@ -239,7 +208,6 @@ - (void)webView:(WKWebView *)webView
}
}

// Called if an error occurs during navigation
- (void)webView:(WKWebView *)webView
didFailProvisionalNavigation:(WKNavigation *)navigation
withError:(NSError *)error {
Expand Down Expand Up @@ -299,7 +267,6 @@ - (void)URLSession:(NSURLSession *)session
completionHandler(request);
}

// This method is called before the web view starts loading a page
- (void)webView:(WKWebView *)webView
decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
decisionHandler:
Expand Down Expand Up @@ -329,4 +296,4 @@ - (void)webView:(WKWebView *)webView
decisionHandler(WKNavigationActionPolicyAllow);
}

@end
@end
1 change: 0 additions & 1 deletion src/objc/OpacityIOSHelper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ void ios_close_webview() {
}

const char *ios_get_browser_cookies_for_current_url() {
// Check if browser is not opened
if (modalWebVC == nil) {
return "";
}
Expand Down