Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add throttling to download progress events #292

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions src/android/FileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -794,13 +794,23 @@ public void run() {
byte[] buffer = new byte[MAX_BUFFER_SIZE];
int bytesRead = 0;
outputStream = new FileOutputStream(file);
int throttle = 0;
while ((bytesRead = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, bytesRead);

if(throttle <= 100)
{
throttle++;
continue;
}

// Send a progress event.
progress.setLoaded(inputStream.getTotalRawBytesRead());
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, progress.toJSONObject());
progressResult.setKeepCallback(true);
context.sendPluginResult(progressResult);

throttle = 0;
}
} finally {
synchronized (context) {
Expand Down
1 change: 1 addition & 0 deletions src/ios/CDVFileTransfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ extern NSString* const kOptionsKeyCookie;
@property (assign) int responseCode; // atomic
@property (nonatomic, assign) long long bytesTransfered;
@property (nonatomic, assign) long long bytesExpected;
@property (nonatomic, assign) NSUInteger throttle;
@property (nonatomic, assign) BOOL trustAllHosts;
@property (strong) NSFileHandle* targetFileHandle;
@property (nonatomic, strong) CDVFileTransferEntityLengthRequest* entityLengthRequest;
Expand Down
7 changes: 5 additions & 2 deletions src/ios/CDVFileTransfer.m
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection*)connection

@implementation CDVFileTransferDelegate

@synthesize callbackId, connection = _connection, source, target, responseData, responseHeaders, command, bytesTransfered, bytesExpected, direction, responseCode, objectId, targetFileHandle, filePlugin;
@synthesize callbackId, connection = _connection, source, target, responseData, responseHeaders, command, bytesTransfered, bytesExpected, direction, responseCode, objectId, targetFileHandle, filePlugin, throttle;

- (void)connectionDidFinishLoading:(NSURLConnection*)connection
{
Expand Down Expand Up @@ -624,6 +624,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection*)connection
}
}
if (self.direction == CDV_TRANSFER_DOWNLOAD) {
self.throttle = 0;
if (self.targetFileHandle) {
[self.targetFileHandle closeFile];
self.targetFileHandle = nil;
Expand Down Expand Up @@ -796,7 +797,7 @@ - (void)updateBytesExpected:(long long)newBytesExpected

- (void)updateProgress
{
if (self.direction == CDV_TRANSFER_DOWNLOAD) {
if ((self.direction == CDV_TRANSFER_DOWNLOAD) && (self.throttle == 100)) {
BOOL lengthComputable = (self.bytesExpected != NSURLResponseUnknownLength);
// If the response is GZipped, and we have an outstanding HEAD request to get
// the length, then hold off on sending progress events.
Expand All @@ -810,7 +811,9 @@ - (void)updateProgress
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:downloadProgress];
[result setKeepCallbackAsBool:true];
[self.command.commandDelegate sendPluginResult:result callbackId:callbackId];
self.throttle = 0;
}
self.throttle ++;
}

- (void)connection:(NSURLConnection*)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite
Expand Down