]> khamidou — GitHub mirrors - electron.git/commitdiff
read files from asar archives in custom protocol
authorRobo <hop2deep@gmail.com>
Wed, 4 Mar 2015 05:22:51 +0000 (10:52 +0530)
committerRobo <hop2deep@gmail.com>
Thu, 5 Mar 2015 03:56:34 +0000 (09:26 +0530)
atom/browser/net/adapter_request_job.cc
spec/api-protocol-spec.coffee

index 6b5b905aba2e29037ea763c04acbe00ad8410e26..f57592392ec8a063db56ccd3a3552a554db5dd61 100644 (file)
@@ -6,6 +6,8 @@
 
 #include "base/threading/sequenced_worker_pool.h"
 #include "atom/browser/net/url_request_string_job.h"
+#include "atom/browser/net/asar/url_request_asar_job.h"
+#include "atom/common/asar/asar_util.h"
 #include "content/public/browser/browser_thread.h"
 #include "net/base/net_errors.h"
 #include "net/url_request/url_request_error_job.h"
@@ -88,13 +90,33 @@ void AdapterRequestJob::CreateStringJobAndStart(const std::string& mime_type,
 void AdapterRequestJob::CreateFileJobAndStart(const base::FilePath& path) {
   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
 
-  real_job_ = new net::URLRequestFileJob(
-      request(),
-      network_delegate(),
-      path,
-      content::BrowserThread::GetBlockingPool()->
-          GetTaskRunnerWithShutdownBehavior(
-              base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
+  base::FilePath asar_path, relative_path;
+  if (!asar::GetAsarArchivePath(path, &asar_path, &relative_path)) {
+    real_job_ = new net::URLRequestFileJob(
+        request(),
+        network_delegate(),
+        path,
+        content::BrowserThread::GetBlockingPool()->
+            GetTaskRunnerWithShutdownBehavior(
+                base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
+  } else {
+    auto archive = asar::GetOrCreateAsarArchive(asar_path);
+    if (!archive)
+      real_job_ = new net::URLRequestErrorJob(
+        request(),
+        network_delegate(),
+        net::ERR_FILE_NOT_FOUND);
+
+    real_job_ = new asar::URLRequestAsarJob(
+        request(),
+        network_delegate(),
+        archive,
+        relative_path,
+        content::BrowserThread::GetBlockingPool()->
+            GetTaskRunnerWithShutdownBehavior(
+                base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
+  }
+
   real_job_->Start();
 }
 
index b0b152aa5fd9683030c6318fdf302d3b6149639b..bf7408df2b26953a22229443a00473bdc9be7587 100644 (file)
@@ -74,6 +74,23 @@ describe 'protocol module', ->
           assert false, 'Got error: ' + errorType + ' ' + error
           protocol.unregisterProtocol 'atom-file-job'
 
+    it 'returns RequestFileJob should send file from asar archive', (done) ->
+      p = path.join __dirname, 'fixtures', 'asar', 'a.asar', 'file1'
+      job = new protocol.RequestFileJob(p)
+      handler = remote.createFunctionWithReturnValue job
+      protocol.registerProtocol 'atom-file-job', handler
+
+      $.ajax
+        url: 'atom-file-job://' + p
+        success: (data) ->
+          content = require('fs').readFileSync(p)
+          assert.equal data, String(content)
+          protocol.unregisterProtocol 'atom-file-job'
+          done()
+        error: (xhr, errorType, error) ->
+          assert false, 'Got error: ' + errorType + ' ' + error
+          protocol.unregisterProtocol 'atom-file-job'
+
   describe 'protocol.isHandledProtocol', ->
     it 'returns true if the scheme can be handled', ->
       assert.equal protocol.isHandledProtocol('file'), true