var binding = require('binding').Binding.create('pageCapture');
+var handleUncaughtException = require('uncaught_exception_handler').handle;
var pageCaptureNatives = requireNative('page_capture');
var CreateBlob = pageCaptureNatives.CreateBlob;
var SendResponseAck = pageCaptureNatives.SendResponseAck;
apiFunctions.setCustomCallback('saveAsMHTML',
function(name, request, response) {
- var path = response.mhtmlFilePath;
- var size = response.mhtmlFileLength;
-
- if (request.callback)
- request.callback(CreateBlob(path, size));
+ var callback = request.callback;
request.callback = null;
-
- // Notify the browser. Now that the blob is referenced from JavaScript,
- // the browser can drop its reference to it.
- SendResponseAck(request.id);
+ if (response)
+ response = CreateBlob(response.mhtmlFilePath, response.mhtmlFileLength);
+
+ try {
+ callback(response);
+ } catch (e) {
+ var message = "Error in chrome.pageCapture.saveAsMHTML callback: " + e;
+ if (request.stack)
+ message += "\n" + request.stack;
+ handleUncaughtException(message, e);
+ } finally {
+ // Notify the browser. Now that the blob is referenced from JavaScript,
+ // the browser can drop its reference to it.
+ SendResponseAck(request.id);
+ }
});
});