[email protected] | 6d77749 | 2012-07-11 17:33:43 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/extensions/pack_extension_job.h" |
| 6 | |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
[email protected] | 8e6ac4b | 2011-10-17 19:04:31 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 3268d7b7 | 2013-03-28 17:41:43 | [diff] [blame] | 10 | #include "base/strings/sys_string_conversions.h" |
[email protected] | 112158af | 2013-06-07 23:46:18 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 12 | #include "base/threading/sequenced_task_runner_handle.h" |
[email protected] | af39f00 | 2014-08-22 10:18:18 | [diff] [blame] | 13 | #include "chrome/grit/generated_resources.h" |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
Devlin Cronin | 420995d | 2018-01-22 20:54:06 | [diff] [blame] | 15 | #include "extensions/browser/extension_creator.h" |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 16 | #include "extensions/browser/extension_file_task_runner.h" |
[email protected] | b22c8af6 | 2013-07-23 23:17:02 | [diff] [blame] | 17 | #include "extensions/common/constants.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 18 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 19 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 20 | using content::BrowserThread; |
| 21 | |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 22 | namespace extensions { |
| 23 | |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 24 | PackExtensionJob::PackExtensionJob(Client* client, |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 25 | const base::FilePath& root_directory, |
| 26 | const base::FilePath& key_file, |
[email protected] | 93d973a | 2012-01-08 07:38:26 | [diff] [blame] | 27 | int run_flags) |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 28 | : client_(client), |
| 29 | key_file_(key_file), |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 30 | run_flags_(run_flags | ExtensionCreator::kRequireModernManifestVersion) { |
[email protected] | 0349ab5d | 2010-08-11 21:41:57 | [diff] [blame] | 31 | root_directory_ = root_directory.StripTrailingSeparators(); |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 32 | } |
| 33 | |
[email protected] | 8e38341 | 2010-10-19 16:57:03 | [diff] [blame] | 34 | PackExtensionJob::~PackExtensionJob() {} |
| 35 | |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 36 | void PackExtensionJob::Start() { |
| 37 | if (run_mode_ == RunMode::ASYNCHRONOUS) { |
| 38 | scoped_refptr<base::SequencedTaskRunner> task_runner = |
| 39 | base::SequencedTaskRunnerHandle::Get(); |
| 40 | GetExtensionFileTaskRunner()->PostTask( |
| 41 | FROM_HERE, |
| 42 | base::BindOnce(&PackExtensionJob::Run, |
| 43 | // See class level comments for why Unretained is safe. |
| 44 | base::Unretained(this), std::move(task_runner))); |
| 45 | } else { |
| 46 | DCHECK_EQ(RunMode::SYNCHRONOUS, run_mode_); |
| 47 | Run(nullptr); |
| 48 | } |
| 49 | } |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 50 | |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 51 | void PackExtensionJob::Run( |
| 52 | scoped_refptr<base::SequencedTaskRunner> async_reply_task_runner) { |
| 53 | DCHECK_EQ(!!async_reply_task_runner, run_mode_ == RunMode::ASYNCHRONOUS) |
| 54 | << "Provide task runner iff we are running in asynchronous mode."; |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 55 | auto crx_file_out = std::make_unique<base::FilePath>( |
Istiaque Ahmed | ebc79301 | 2017-08-14 18:48:34 | [diff] [blame] | 56 | root_directory_.AddExtension(kExtensionFileExtension)); |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 57 | |
Jinho Bang | b5216cec | 2018-01-17 19:43:11 | [diff] [blame] | 58 | auto key_file_out = std::make_unique<base::FilePath>(); |
Istiaque Ahmed | ebc79301 | 2017-08-14 18:48:34 | [diff] [blame] | 59 | if (key_file_.empty()) |
| 60 | *key_file_out = root_directory_.AddExtension(kExtensionKeyFileExtension); |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 61 | |
| 62 | // TODO(aa): Need to internationalize the errors that ExtensionCreator |
| 63 | // returns. See bug 20734. |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 64 | ExtensionCreator creator; |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 65 | if (creator.Run(root_directory_, *crx_file_out, key_file_, *key_file_out, |
[email protected] | 93d973a | 2012-01-08 07:38:26 | [diff] [blame] | 66 | run_flags_)) { |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 67 | if (run_mode_ == RunMode::ASYNCHRONOUS) { |
| 68 | async_reply_task_runner->PostTask( |
| 69 | FROM_HERE, |
| 70 | base::BindOnce(&PackExtensionJob::ReportSuccessOnClientSequence, |
| 71 | // See class level comments for why Unretained is safe. |
| 72 | base::Unretained(this), std::move(crx_file_out), |
| 73 | std::move(key_file_out))); |
| 74 | |
[email protected] | ccd875e7 | 2010-12-13 23:49:02 | [diff] [blame] | 75 | } else { |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 76 | ReportSuccessOnClientSequence(std::move(crx_file_out), |
| 77 | std::move(key_file_out)); |
[email protected] | ccd875e7 | 2010-12-13 23:49:02 | [diff] [blame] | 78 | } |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 79 | } else { |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 80 | if (run_mode_ == RunMode::ASYNCHRONOUS) { |
| 81 | async_reply_task_runner->PostTask( |
| 82 | FROM_HERE, |
| 83 | base::BindOnce(&PackExtensionJob::ReportFailureOnClientSequence, |
| 84 | // See class level comments for why Unretained is safe. |
| 85 | base::Unretained(this), creator.error_message(), |
| 86 | creator.error_type())); |
[email protected] | ccd875e7 | 2010-12-13 23:49:02 | [diff] [blame] | 87 | } else { |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 88 | DCHECK_EQ(RunMode::SYNCHRONOUS, run_mode_); |
| 89 | ReportFailureOnClientSequence(creator.error_message(), |
| 90 | creator.error_type()); |
[email protected] | ccd875e7 | 2010-12-13 23:49:02 | [diff] [blame] | 91 | } |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 95 | void PackExtensionJob::ReportSuccessOnClientSequence( |
| 96 | std::unique_ptr<base::FilePath> crx_file_out, |
| 97 | std::unique_ptr<base::FilePath> key_file_out) { |
| 98 | DCHECK(client_); |
| 99 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 100 | client_->OnPackSuccess(*crx_file_out, *key_file_out); |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 101 | } |
| 102 | |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 103 | void PackExtensionJob::ReportFailureOnClientSequence( |
[email protected] | 93d973a | 2012-01-08 07:38:26 | [diff] [blame] | 104 | const std::string& error, |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 105 | ExtensionCreator::ErrorType error_type) { |
Istiaque Ahmed | 6a06cc9 | 2017-08-12 00:31:33 | [diff] [blame] | 106 | DCHECK(client_); |
| 107 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 108 | client_->OnPackFailure(error, error_type); |
[email protected] | 13a8961 | 2009-09-01 03:17:42 | [diff] [blame] | 109 | } |
[email protected] | 0349ab5d | 2010-08-11 21:41:57 | [diff] [blame] | 110 | |
| 111 | // static |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 112 | std::u16string PackExtensionJob::StandardSuccessMessage( |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 113 | const base::FilePath& crx_file, |
| 114 | const base::FilePath& key_file) { |
Jan Wilken Dörrie | f27844b | 2021-03-11 23:18:48 | [diff] [blame] | 115 | std::u16string crx_file_string = crx_file.LossyDisplayName(); |
| 116 | std::u16string key_file_string = key_file.LossyDisplayName(); |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame] | 117 | if (key_file_string.empty()) { |
| 118 | return l10n_util::GetStringFUTF16( |
[email protected] | 0349ab5d | 2010-08-11 21:41:57 | [diff] [blame] | 119 | IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_UPDATE, |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame] | 120 | crx_file_string); |
[email protected] | 0349ab5d | 2010-08-11 21:41:57 | [diff] [blame] | 121 | } else { |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame] | 122 | return l10n_util::GetStringFUTF16( |
[email protected] | 0349ab5d | 2010-08-11 21:41:57 | [diff] [blame] | 123 | IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_NEW, |
[email protected] | d131857 | 2010-12-29 22:37:45 | [diff] [blame] | 124 | crx_file_string, |
| 125 | key_file_string); |
[email protected] | 0349ab5d | 2010-08-11 21:41:57 | [diff] [blame] | 126 | } |
| 127 | } |
[email protected] | d9ede58 | 2012-08-14 19:21:38 | [diff] [blame] | 128 | |
| 129 | } // namespace extensions |