Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

throw a "lowMemory" event when Cordova notifies us of low memory #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
48 changes: 48 additions & 0 deletions src/ios/ChromeSystemMemory.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#import <Foundation/Foundation.h>
#import <mach/mach.h>
#import <mach/mach_host.h>
@import WebKit;
@import UIKit;

#if CHROME_SYSTEM_MEMORY_VERBOSE_LOGGING
#define VERBOSE_LOG NSLog
Expand All @@ -21,6 +23,52 @@ - (void)getInfo:(CDVInvokedUrlCommand*)command;

@implementation ChromeSystemMemory

- (void)onMemoryWarning
{
NSString* javascriptString =
@"(function() {"
" var ev = document.createEvent('Event');"
" ev.initEvent('lowMemory', true, true);"
" document.dispatchEvent(ev);"
"}());";

// first, try Cordova 4.0+ webViewEngine evaluateJavaScript:completionHandler:
if ([self respondsToSelector:@selector(setWebViewEngine:)]) {
VERBOSE_LOG(@"Trying webViewEngine...");
id engine = self.webViewEngine;
if (engine) {
[engine evaluateJavaScript:javascriptString completionHandler:^(id result, NSError *error) {
if (error != nil) {
VERBOSE_LOG(@"ERROR: evaluateJavaScript failed: %@", error.localizedDescription);
}
}];
return;
}
}

// if not, fall back to checking WKWebView's evaluateJavaScript:completionHandler:
if ([self.webView respondsToSelector:@selector(evaluateJavaScript:completionHandler:)]) {
VERBOSE_LOG(@"Trying WKWebView...");
[(WKWebView*)self.webView evaluateJavaScript:javascriptString completionHandler:^(id result, NSError *error) {
if (error != nil) {
VERBOSE_LOG(@"ERROR: sending JavaScript low memory event failed: %@", error.localizedDescription);
}
}];
return;
}

// finally, fall back to UIWebView's stringByEvaluatingJavaScriptFromString:
if ([self.webView respondsToSelector:@selector(stringByEvaluatingJavaScriptFromString:)]) {
VERBOSE_LOG(@"Trying UIWebView...");
NSString* result = [(UIWebView*)self.webView stringByEvaluatingJavaScriptFromString:javascriptString];
if (result != nil && result != (id)[NSNull null] && result.length != 0) {
VERBOSE_LOG(@"sending JavaScript low memory event returned: $@", result);
}
return;
}

VERBOSE_LOG(@"Giving up. :(");
}

- (NSError *)kernelCallError:(NSString *)errMsg
{
Expand Down