OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "services/asset_bundle/asset_unpacker_job.h" | 5 #include "services/asset_bundle/asset_unpacker_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
12 #include "base/threading/worker_pool.h" | |
13 #include "services/asset_bundle/asset_bundle_impl.h" | 12 #include "services/asset_bundle/asset_bundle_impl.h" |
14 #include "third_party/zlib/google/zip.h" | 13 #include "third_party/zlib/google/zip.h" |
15 | 14 |
16 namespace mojo { | 15 namespace mojo { |
17 namespace asset_bundle { | 16 namespace asset_bundle { |
18 namespace { | 17 namespace { |
19 | 18 |
20 void UnzipAssets( | 19 void UnzipAssets( |
21 const base::FilePath& zip_path, | 20 const base::FilePath& zip_path, |
22 scoped_ptr<base::ScopedTempDir> asset_dir, | 21 scoped_ptr<base::ScopedTempDir> asset_dir, |
23 scoped_refptr<base::TaskRunner> task_runner, | 22 scoped_refptr<base::TaskRunner> task_runner, |
24 base::Callback<void(scoped_ptr<base::ScopedTempDir>)> callback) { | 23 base::Callback<void(scoped_ptr<base::ScopedTempDir>)> callback) { |
25 if (!zip::Unzip(zip_path, asset_dir->path())) { | 24 if (!zip::Unzip(zip_path, asset_dir->path())) { |
26 task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr)); | 25 task_runner->PostTask(FROM_HERE, base::Bind(callback, nullptr)); |
27 } else { | 26 } else { |
28 task_runner->PostTask(FROM_HERE, | 27 task_runner->PostTask(FROM_HERE, |
29 base::Bind(callback, base::Passed(asset_dir.Pass()))); | 28 base::Bind(callback, base::Passed(asset_dir.Pass()))); |
30 } | 29 } |
31 base::DeleteFile(zip_path, false); | 30 base::DeleteFile(zip_path, false); |
32 } | 31 } |
33 | 32 |
34 } // namespace | 33 } // namespace |
35 | 34 |
36 AssetUnpackerJob::AssetUnpackerJob(InterfaceRequest<AssetBundle> asset_bundle) | 35 AssetUnpackerJob::AssetUnpackerJob( |
37 : asset_bundle_(asset_bundle.Pass()), weak_factory_(this) { | 36 InterfaceRequest<AssetBundle> asset_bundle, |
| 37 scoped_refptr<base::TaskRunner> worker_runner) |
| 38 : asset_bundle_(asset_bundle.Pass()), |
| 39 worker_runner_(worker_runner.Pass()), |
| 40 weak_factory_(this) { |
38 } | 41 } |
39 | 42 |
40 AssetUnpackerJob::~AssetUnpackerJob() { | 43 AssetUnpackerJob::~AssetUnpackerJob() { |
41 } | 44 } |
42 | 45 |
43 void AssetUnpackerJob::Unpack(ScopedDataPipeConsumerHandle zipped_assets) { | 46 void AssetUnpackerJob::Unpack(ScopedDataPipeConsumerHandle zipped_assets) { |
44 base::FilePath zip_path; | 47 base::FilePath zip_path; |
45 if (!CreateTemporaryFile(&zip_path)) { | 48 if (!CreateTemporaryFile(&zip_path)) { |
46 delete this; | 49 delete this; |
47 return; | 50 return; |
48 } | 51 } |
49 scoped_refptr<base::TaskRunner> worker = | 52 common::CopyToFile(zipped_assets.Pass(), zip_path, worker_runner_.get(), |
50 base::WorkerPool::GetTaskRunner(true); | |
51 common::CopyToFile(zipped_assets.Pass(), zip_path, worker.get(), | |
52 base::Bind(&AssetUnpackerJob::OnZippedAssetsAvailable, | 53 base::Bind(&AssetUnpackerJob::OnZippedAssetsAvailable, |
53 weak_factory_.GetWeakPtr(), zip_path)); | 54 weak_factory_.GetWeakPtr(), zip_path)); |
54 } | 55 } |
55 | 56 |
56 void AssetUnpackerJob::OnZippedAssetsAvailable(const base::FilePath& zip_path, | 57 void AssetUnpackerJob::OnZippedAssetsAvailable(const base::FilePath& zip_path, |
57 bool success) { | 58 bool success) { |
58 if (!success) { | 59 if (!success) { |
59 delete this; | 60 delete this; |
60 return; | 61 return; |
61 } | 62 } |
62 scoped_ptr<base::ScopedTempDir> asset_dir(new base::ScopedTempDir()); | 63 scoped_ptr<base::ScopedTempDir> asset_dir(new base::ScopedTempDir()); |
63 if (!asset_dir->CreateUniqueTempDir()) { | 64 if (!asset_dir->CreateUniqueTempDir()) { |
64 delete this; | 65 delete this; |
65 return; | 66 return; |
66 } | 67 } |
67 base::WorkerPool::PostTask( | 68 worker_runner_->PostTask( |
68 FROM_HERE, | 69 FROM_HERE, |
69 base::Bind(&UnzipAssets, zip_path, base::Passed(asset_dir.Pass()), | 70 base::Bind(&UnzipAssets, zip_path, base::Passed(asset_dir.Pass()), |
70 base::MessageLoop::current()->task_runner(), | 71 base::MessageLoop::current()->task_runner(), |
71 base::Bind(&AssetUnpackerJob::OnUnzippedAssetsAvailable, | 72 base::Bind(&AssetUnpackerJob::OnUnzippedAssetsAvailable, |
72 weak_factory_.GetWeakPtr())), | 73 weak_factory_.GetWeakPtr()))); |
73 true); | |
74 } | 74 } |
75 | 75 |
76 void AssetUnpackerJob::OnUnzippedAssetsAvailable( | 76 void AssetUnpackerJob::OnUnzippedAssetsAvailable( |
77 scoped_ptr<base::ScopedTempDir> asset_dir) { | 77 scoped_ptr<base::ScopedTempDir> asset_dir) { |
78 if (asset_dir) | 78 if (asset_dir) |
79 new AssetBundleImpl(asset_bundle_.Pass(), asset_dir.Pass()); | 79 new AssetBundleImpl(asset_bundle_.Pass(), asset_dir.Pass(), worker_runner_); |
80 | 80 |
81 delete this; | 81 delete this; |
82 } | 82 } |
83 | 83 |
84 } // namespace asset_bundle | 84 } // namespace asset_bundle |
85 } // namespace mojo | 85 } // namespace mojo |
OLD | NEW |