blob: 8dba52d4c64c56bcac3fa6b7ef063e12683b5ab3 [file] [log] [blame]
[email protected]6d777492012-07-11 17:33:431// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]13a89612009-09-01 03:17:422// 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 Bangb5216cec2018-01-17 19:43:117#include <memory>
8
[email protected]8e6ac4b2011-10-17 19:04:319#include "base/bind.h"
[email protected]3268d7b72013-03-28 17:41:4310#include "base/strings/sys_string_conversions.h"
[email protected]112158af2013-06-07 23:46:1811#include "base/strings/utf_string_conversions.h"
Istiaque Ahmed6a06cc92017-08-12 00:31:3312#include "base/threading/sequenced_task_runner_handle.h"
[email protected]af39f002014-08-22 10:18:1813#include "chrome/grit/generated_resources.h"
Istiaque Ahmed6a06cc92017-08-12 00:31:3314#include "content/public/browser/browser_thread.h"
Devlin Cronin420995d2018-01-22 20:54:0615#include "extensions/browser/extension_creator.h"
Istiaque Ahmed6a06cc92017-08-12 00:31:3316#include "extensions/browser/extension_file_task_runner.h"
[email protected]b22c8af62013-07-23 23:17:0217#include "extensions/common/constants.h"
[email protected]c051a1b2011-01-21 23:30:1718#include "ui/base/l10n/l10n_util.h"
[email protected]13a89612009-09-01 03:17:4219
[email protected]631bb742011-11-02 11:29:3920using content::BrowserThread;
21
[email protected]d9ede582012-08-14 19:21:3822namespace extensions {
23
[email protected]13a89612009-09-01 03:17:4224PackExtensionJob::PackExtensionJob(Client* client,
[email protected]650b2d52013-02-10 03:41:4525 const base::FilePath& root_directory,
26 const base::FilePath& key_file,
[email protected]93d973a2012-01-08 07:38:2627 int run_flags)
Istiaque Ahmed6a06cc92017-08-12 00:31:3328 : client_(client),
29 key_file_(key_file),
[email protected]b7462f32012-09-02 15:18:1230 run_flags_(run_flags | ExtensionCreator::kRequireModernManifestVersion) {
[email protected]0349ab5d2010-08-11 21:41:5731 root_directory_ = root_directory.StripTrailingSeparators();
[email protected]13a89612009-09-01 03:17:4232}
33
[email protected]8e383412010-10-19 16:57:0334PackExtensionJob::~PackExtensionJob() {}
35
Istiaque Ahmed6a06cc92017-08-12 00:31:3336void 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]13a89612009-09-01 03:17:4250
Istiaque Ahmed6a06cc92017-08-12 00:31:3351void 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 Bangb5216cec2018-01-17 19:43:1155 auto crx_file_out = std::make_unique<base::FilePath>(
Istiaque Ahmedebc793012017-08-14 18:48:3456 root_directory_.AddExtension(kExtensionFileExtension));
Istiaque Ahmed6a06cc92017-08-12 00:31:3357
Jinho Bangb5216cec2018-01-17 19:43:1158 auto key_file_out = std::make_unique<base::FilePath>();
Istiaque Ahmedebc793012017-08-14 18:48:3459 if (key_file_.empty())
60 *key_file_out = root_directory_.AddExtension(kExtensionKeyFileExtension);
[email protected]13a89612009-09-01 03:17:4261
62 // TODO(aa): Need to internationalize the errors that ExtensionCreator
63 // returns. See bug 20734.
[email protected]d9ede582012-08-14 19:21:3864 ExtensionCreator creator;
Istiaque Ahmed6a06cc92017-08-12 00:31:3365 if (creator.Run(root_directory_, *crx_file_out, key_file_, *key_file_out,
[email protected]93d973a2012-01-08 07:38:2666 run_flags_)) {
Istiaque Ahmed6a06cc92017-08-12 00:31:3367 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]ccd875e72010-12-13 23:49:0275 } else {
Istiaque Ahmed6a06cc92017-08-12 00:31:3376 ReportSuccessOnClientSequence(std::move(crx_file_out),
77 std::move(key_file_out));
[email protected]ccd875e72010-12-13 23:49:0278 }
[email protected]13a89612009-09-01 03:17:4279 } else {
Istiaque Ahmed6a06cc92017-08-12 00:31:3380 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]ccd875e72010-12-13 23:49:0287 } else {
Istiaque Ahmed6a06cc92017-08-12 00:31:3388 DCHECK_EQ(RunMode::SYNCHRONOUS, run_mode_);
89 ReportFailureOnClientSequence(creator.error_message(),
90 creator.error_type());
[email protected]ccd875e72010-12-13 23:49:0291 }
[email protected]13a89612009-09-01 03:17:4292 }
93}
94
Istiaque Ahmed6a06cc92017-08-12 00:31:3395void 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]13a89612009-09-01 03:17:42101}
102
Istiaque Ahmed6a06cc92017-08-12 00:31:33103void PackExtensionJob::ReportFailureOnClientSequence(
[email protected]93d973a2012-01-08 07:38:26104 const std::string& error,
[email protected]d9ede582012-08-14 19:21:38105 ExtensionCreator::ErrorType error_type) {
Istiaque Ahmed6a06cc92017-08-12 00:31:33106 DCHECK(client_);
107 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
108 client_->OnPackFailure(error, error_type);
[email protected]13a89612009-09-01 03:17:42109}
[email protected]0349ab5d2010-08-11 21:41:57110
111// static
Jan Wilken Dörrief27844b2021-03-11 23:18:48112std::u16string PackExtensionJob::StandardSuccessMessage(
[email protected]650b2d52013-02-10 03:41:45113 const base::FilePath& crx_file,
114 const base::FilePath& key_file) {
Jan Wilken Dörrief27844b2021-03-11 23:18:48115 std::u16string crx_file_string = crx_file.LossyDisplayName();
116 std::u16string key_file_string = key_file.LossyDisplayName();
[email protected]d1318572010-12-29 22:37:45117 if (key_file_string.empty()) {
118 return l10n_util::GetStringFUTF16(
[email protected]0349ab5d2010-08-11 21:41:57119 IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_UPDATE,
[email protected]d1318572010-12-29 22:37:45120 crx_file_string);
[email protected]0349ab5d2010-08-11 21:41:57121 } else {
[email protected]d1318572010-12-29 22:37:45122 return l10n_util::GetStringFUTF16(
[email protected]0349ab5d2010-08-11 21:41:57123 IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_NEW,
[email protected]d1318572010-12-29 22:37:45124 crx_file_string,
125 key_file_string);
[email protected]0349ab5d2010-08-11 21:41:57126 }
127}
[email protected]d9ede582012-08-14 19:21:38128
129} // namespace extensions