Skip to content

Commit

Permalink
Merge pull request #15 from Cortys/2.0.1
Browse files Browse the repository at this point in the history
Update to Version 2.0.1
  • Loading branch information
Cortys committed Aug 31, 2014
2 parents 40a012e + 0f4fcb3 commit 4eb3d2e
Show file tree
Hide file tree
Showing 18 changed files with 7,257 additions and 205 deletions.
Binary file modified download.zip
Binary file not shown.
Binary file modified extension.crx
Binary file not shown.
225 changes: 149 additions & 76 deletions source/backgroundManager.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
//(C) 2013 Sperglord Enterprises
//Code is under GNUGPLv3 - read http://www.gnu.org/licenses/gpl.html

zip.workerScriptsPath = "/zip/";

var ports = { // stores all opened connections of tabs to bg page
reader: {}, // reader tab connections
controller: {} // controller tab connections
},
openers = {}, // contains the opener tab id as value for each child tab id as key
closedReader = function(sender) {
closedReader = function(port) {
var sender = port.senderId;
if(!port.zipFile)
URL.revokeObjectURL(port.zipUrl);
else
port.zipFile.remove(function() {
port.zipFile = null;
});
if(typeof openers[sender] !== "undefined") {
var opener = ports.controller[openers[sender]];
if(opener) {
Expand All @@ -15,91 +24,155 @@ var ports = { // stores all opened connections of tabs to bg page
}
delete openers[sender];
}
},
requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem,
fs = null,
getWriter = function(callback) {
var tmpName, run;
if(!settings.tempMemory || !requestFileSystem) {
callback(new zip.BlobWriter());
fs = null;
}
else {
run = function() {
tmpName = "tmp"+(Date.now())+".zip";
function create() {
fs.root.getFile(tmpName, {
create : true
}, function(zipFile) {
callback(new zip.FileWriter(zipFile), zipFile);
});
}
fs.root.getFile(tmpName, null, function() {
run();
}, create);
};

if(!fs)
requestFileSystem(TEMPORARY, 4 * 1024 * 1024 * 1024, function (f) {
fs = f;
run();
});
else
run();
}
};

connector.onConnect.addListener(function(port) {
var sender = port.sender.tab.id;
getSettings(function() {

ports[port.name][sender] = port;
connector.onConnect.addListener(function(port) {

if(port.name == "controller")
port.children = {}; // each connection stores the ids of the tabs it opened as children; key = tab id, value always true
if(port.name == "download") {
port.receive(function(request, callback) {
downloadFile(request.name, request.data, request.overwrite, callback);
});
return true;
}

if(port.name == "reader") // ZIPPING logic - moved to background script to have a dedicated tab/thread for compression so that it doesn't make the reader tab itself slow
port.receive(function(request, callback) {
if(request.what == "new_zip") {
port.zip = new JSZip();
port.zip.file(".meta.asc", "This is a ComiXology backup.\nPlease do not distribute it.\nBackup created by "+(request.user||"[UNKNOWN USER]"));
callback({ what:"new_zip_created" });
}
else if(request.what == "add_page") {
var name = "page"+nullFill(request.i, request.len)+"."+request.extension;
if(request.toZip && port.zip)
port.zip.file(name, request.page.substr(request.page.indexOf(",")+1), { base64:true });
callback({ what:"page_added", name:name });
}
else if(request.what == "start_zipping" && port.zip) {
var result = port.zip.generate({
type: "blob",
compression: request.compress?"DEFLATE":"STORE"
});
port.zip = null;
callback({ what:"completed_zipping", url:URL.createObjectURL(result) });
}
else if(request.what == "download_blob") {
downloadFile(request.name, request.data, request.overwrite, function() {
callback({ what:"download_started" });
});
return true;
}
else if(request.what == "message_to_opener" && openers[sender] !== "undefined" && ports.controller[openers[sender]]) {
ports.controller[openers[sender]].send({ what:"child_message", tab:sender, message:request.message }, function(data) {
callback(data);
});
return true;
}
else if(request.what == "broadcast_to_openers") {
for(var tab in ports.controller) {
ports.controller[tab].send({ what:"child_broadcast", tab:sender, message:request.message }, function(data) {
var sender = port.senderId = port.sender.tab.id;

ports[port.name][sender] = port;

if(port.name == "controller")
port.children = {}; // each connection stores the ids of the tabs it opened as children; key = tab id, value always true

if(port.name == "reader") // ZIPPING logic - moved to background script to have a dedicated tab/thread for compression so that it doesn't make the reader tab itself slow
port.receive(function(request, callback) {
if(request.what == "new_zip") {
getWriter(function (writer, zipFile) {
port.zipFile = zipFile;
zip.createWriter(writer, function(writer) {
port.zip = writer;
writer.add(".meta.asc", new zip.TextReader("This is a ComiXology backup.\nPlease do not distribute it.\nBackup created by "+(request.user||"[UNKNOWN USER]")));
callback({ what:"new_zip_created" });
});
});
return true;
}
else if(request.what == "add_page") {
var name = "page"+nullFill(request.i, request.len)+"."+request.extension,
d = callback.bind(null, { what:"page_added", name:name });
if(request.toZip && port.zip) {
port.zip.add(name, new zip.Data64URIReader(request.page), function() {
d();
});
return true;
}
d();
}
else if(request.what == "start_zipping" && port.zip) {
port.zip.close(function(result) {
port.zip = null;
port.zipUrl = port.zipFile?port.zipFile.toURL():URL.createObjectURL(result);
callback({ what:"completed_zipping" });
});
return true;
}
else if(request.what == "download_blob") {
downloadFile(request.name, port.zipUrl, false, function() {
if(!port.zipFile)
URL.revokeObjectURL(port.zipUrl);
else
port.zipFile.remove(function() {
port.zipFile = null;
});
callback({ what:"download_complete" });
});
return true;
}
else if(request.what == "message_to_opener" && openers[sender] !== "undefined" && ports.controller[openers[sender]]) {
ports.controller[openers[sender]].send({ what:"child_message", tab:sender, message:request.message }, function(data) {
callback(data);
});
return true;
}
return true;
}
else if(request.what == "is_child")
callback(sender in openers);
else if(request.what == "unlink_from_opener") // prevents tab to be closed if opener is closed (reader and opener are still connected though and can send messages)
port.unlinked = true;
});
else if(port.name == "controller") // TAB handling logic for the controller tabs (like "My Books")
port.receive(function(request, callback) {
if(request.what == "open_background_tab") {
chrome.tabs.create({ url:request.url, active:request.active }, function(tab) {
openers[tab.id] = sender;
port.children[tab.id] = true;
callback(tab.id);
});
return true;
}
else if(request.what == "close_background_tab")
chrome.tabs.remove(request.tab);
else if(request.what == "message_to_child" && ports.reader[request.tab]) {
ports.reader[request.tab].send({ what:"opener_message", message:request.message }, callback);
return true;
}
});
else if(request.what == "broadcast_to_openers") {
for(var tab in ports.controller) {
ports.controller[tab].send({ what:"child_broadcast", tab:sender, message:request.message }, function(data) {
callback(data);
});
}
return true;
}
else if(request.what == "is_child")
callback(sender in openers);
else if(request.what == "unlink_from_opener") // prevents tab to be closed if opener is closed (reader and opener are still connected though and can send messages)
port.unlinked = true;
});
else if(port.name == "controller") // TAB handling logic for the controller tabs (like "My Books")
port.receive(function(request, callback) {
if(request.what == "open_background_tab") {
chrome.tabs.create({ url:request.url, active:request.active }, function(tab) {
openers[tab.id] = sender;
port.children[tab.id] = true;
callback(tab.id);
});
return true;
}
else if(request.what == "close_background_tab")
chrome.tabs.remove(request.tab);
else if(request.what == "message_to_child" && ports.reader[request.tab]) {
ports.reader[request.tab].send({ what:"opener_message", message:request.message }, callback);
return true;
}
});

var disconnectAction = port.name == "controller"?function() {
for (var tab in port.children)
if(ports.reader[tab] && !ports.reader[tab].unlinked)
chrome.tabs.remove(tab*1);
}:closedReader;
var disconnectAction = port.name == "controller"?function() {
for (var tab in port.children)
if(ports.reader[tab] && !ports.reader[tab].unlinked)
chrome.tabs.remove(tab*1);
}:closedReader;

port.onDisconnect.addListener(function() {
delete ports[port.name][sender];
disconnectAction(sender);
port = sender = null;
port.onDisconnect.addListener(function() {
delete ports[port.name][sender];
disconnectAction(port);
port = sender = null;
});
});

});

chrome.tabs.onRemoved.addListener(closedReader);
chrome.runtime.onMessage.addListener(function(message) {
if(message == "update_settings")
getSettings();
});
Loading

0 comments on commit 4eb3d2e

Please sign in to comment.