Skip to content

Commit c609ff6

Browse files
knight9999janpio
authored andcommitted
(browser) support safari private browsing mode by using base64 text instead of blob (#301)
### Platforms affected cordova-browser ### Motivation and Context Bascially cordova-browser uses indexeddb in order to store File(Blob) object as file-saving. However the private browsing mode on safari browser in mac does not permit to store File(Blob) object into the indexeddb. (See Webkit source code https://github.com/WebKit/webkit/blob/5dada1bf8d706e1c544f1a23bfb67f3eaeb4f412/Source/WebCore/Modules/indexeddb/IDBObjectStore.cpp#L328-L331 ) This PR is to fix this issue. ### Description The private borwsing mode on safari throws `DataCloneError` when storing Blob object to indexeddb. Therefore this PR detects `DataCloneError` and store base64 encoded text instead of Blob object itself. According to this, decoding/encoding helper functions are introduced. Note that the safari browser (emulator ?) in the saucelab used in travis ci seems to work in private browsing mode. For example previous master branch fails tests for safari browser in travis ci like https://travis-ci.org/apache/cordova-plugin-file/builds/502885593?utm_source=github_status&utm_medium=notification . This PR fix the issue. ### Testing The test with both public and private browsing modes on safari on My local mac are done. I confirmed this PR passes all test suites (specs). ### Checklist - [ ] I've run the tests to see all new and existing tests pass - [ ] I added automated test coverage as appropriate for this change - [ ] Commit is prefixed with `(platform)` if this change only applies to one platform (e.g. `(android)`) - [ ] If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct [keyword to close issues using keywords](https://help.github.com/articles/closing-issues-using-keywords/)) - [ ] I've updated the documentation if necessary
1 parent b762743 commit c609ff6

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

src/browser/FileProxy.js

+78-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
/* global require, exports, module */
2323
/* global FILESYSTEM_PREFIX */
2424
/* global IDBKeyRange */
25+
/* global FileReader */
26+
/* global atob, btoa, Blob */
2527

2628
/* Heavily based on https://github.com/ebidel/idb.filesystem.js */
2729

@@ -685,6 +687,41 @@
685687

686688
MyFile.prototype.constructor = MyFile;
687689

690+
var MyFileHelper = {
691+
toJson: function (myFile, success) {
692+
/*
693+
Safari private browse mode cannot store Blob object to indexeddb.
694+
Then use pure json object instead of Blob object.
695+
*/
696+
var fr = new FileReader();
697+
fr.onload = function (ev) {
698+
var base64 = btoa(String.fromCharCode.apply(null, new Uint8Array(fr.result)));
699+
success({
700+
opt: {
701+
size: myFile.size,
702+
name: myFile.name,
703+
type: myFile.type,
704+
lastModifiedDate: myFile.lastModifiedDate,
705+
storagePath: myFile.storagePath
706+
},
707+
base64: base64
708+
});
709+
};
710+
fr.readAsArrayBuffer(myFile.blob_);
711+
},
712+
setBase64: function (myFile, base64) {
713+
if (base64) {
714+
var arrayBuffer = (new Uint8Array(
715+
[].map.call(atob(base64), function (c) { return c.charCodeAt(0); })
716+
)).buffer;
717+
718+
myFile.blob_ = new Blob([arrayBuffer], { type: myFile.type });
719+
} else {
720+
myFile.blob_ = new Blob();
721+
}
722+
}
723+
};
724+
688725
// When saving an entry, the fullPath should always lead with a slash and never
689726
// end with one (e.g. a directory). Also, resolve '.' and '..' to an absolute
690727
// one. This method ensures path is legit!
@@ -847,7 +884,17 @@
847884

848885
tx.onabort = errorCallback || onError;
849886
tx.oncomplete = function () {
850-
successCallback(request.result);
887+
var entry = request.result;
888+
if (entry && entry.file_json) {
889+
/*
890+
Safari private browse mode cannot store Blob object to indexeddb.
891+
Then use pure json object instead of Blob object.
892+
*/
893+
entry.file_ = new MyFile(entry.file_json.opt);
894+
MyFileHelper.setBase64(entry.file_, entry.file_json.base64);
895+
delete entry.file_json;
896+
}
897+
successCallback(entry);
851898
};
852899
};
853900

@@ -946,7 +993,7 @@
946993
tx.objectStore(FILE_STORE_)['delete'](fullPath);
947994
};
948995

949-
idb_.put = function (entry, storagePath, successCallback, errorCallback) {
996+
idb_.put = function (entry, storagePath, successCallback, errorCallback, retry) {
950997
if (!this.db) {
951998
if (errorCallback) {
952999
errorCallback(FileError.INVALID_MODIFICATION_ERR);
@@ -961,7 +1008,35 @@
9611008
successCallback(entry);
9621009
};
9631010

964-
tx.objectStore(FILE_STORE_).put(entry, storagePath);
1011+
try {
1012+
tx.objectStore(FILE_STORE_).put(entry, storagePath);
1013+
} catch (e) {
1014+
if (e.name === 'DataCloneError') {
1015+
tx.oncomplete = null;
1016+
/*
1017+
Safari private browse mode cannot store Blob object to indexeddb.
1018+
Then use pure json object instead of Blob object.
1019+
*/
1020+
1021+
var successCallback2 = function (entry) {
1022+
entry.file_ = new MyFile(entry.file_json.opt);
1023+
delete entry.file_json;
1024+
successCallback(entry);
1025+
};
1026+
1027+
if (!retry) {
1028+
if (entry.file_ && entry.file_ instanceof MyFile && entry.file_.blob_) {
1029+
MyFileHelper.toJson(entry.file_, function (json) {
1030+
entry.file_json = json;
1031+
delete entry.file_;
1032+
idb_.put(entry, storagePath, successCallback2, errorCallback, true);
1033+
});
1034+
return;
1035+
}
1036+
}
1037+
}
1038+
throw e;
1039+
}
9651040
};
9661041

9671042
// Global error handler. Errors bubble from request, to transaction, to db.

0 commit comments

Comments
 (0)