Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
      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 
      7 #include "base/message_loop.h"
      8 #include "base/sys_string_conversions.h"
      9 #include "base/utf_string_conversions.h"
     10 #include "base/task.h"
     11 #include "chrome/browser/extensions/extension_creator.h"
     12 #include "chrome/common/chrome_constants.h"
     13 #include "grit/generated_resources.h"
     14 #include "ui/base/l10n/l10n_util.h"
     15 
     16 PackExtensionJob::PackExtensionJob(Client* client,
     17                                    const FilePath& root_directory,
     18                                    const FilePath& key_file)
     19     : client_(client), key_file_(key_file), asynchronous_(true) {
     20   root_directory_ = root_directory.StripTrailingSeparators();
     21   CHECK(BrowserThread::GetCurrentThreadIdentifier(&client_thread_id_));
     22 }
     23 
     24 void PackExtensionJob::Start() {
     25   if (asynchronous_) {
     26     BrowserThread::PostTask(
     27         BrowserThread::FILE, FROM_HERE,
     28         NewRunnableMethod(this, &PackExtensionJob::Run));
     29   } else {
     30     Run();
     31   }
     32 }
     33 
     34 void PackExtensionJob::ClearClient() {
     35   client_ = NULL;
     36 }
     37 
     38 PackExtensionJob::~PackExtensionJob() {}
     39 
     40 void PackExtensionJob::Run() {
     41   crx_file_out_ = FilePath(root_directory_.value() +
     42                            chrome::kExtensionFileExtension);
     43 
     44   if (key_file_.empty())
     45     key_file_out_ = FilePath(root_directory_.value() +
     46                              chrome::kExtensionKeyFileExtension);
     47 
     48   // TODO(aa): Need to internationalize the errors that ExtensionCreator
     49   // returns. See bug 20734.
     50   ExtensionCreator creator;
     51   if (creator.Run(root_directory_, crx_file_out_, key_file_, key_file_out_)) {
     52     if (asynchronous_) {
     53       BrowserThread::PostTask(
     54           client_thread_id_, FROM_HERE,
     55           NewRunnableMethod(this,
     56                             &PackExtensionJob::ReportSuccessOnClientThread));
     57     } else {
     58       ReportSuccessOnClientThread();
     59     }
     60   } else {
     61     if (asynchronous_) {
     62       BrowserThread::PostTask(
     63           client_thread_id_, FROM_HERE,
     64           NewRunnableMethod(
     65               this, &PackExtensionJob::ReportFailureOnClientThread,
     66               creator.error_message()));
     67     } else {
     68       ReportFailureOnClientThread(creator.error_message());
     69     }
     70   }
     71 }
     72 
     73 void PackExtensionJob::ReportSuccessOnClientThread() {
     74   if (client_)
     75     client_->OnPackSuccess(crx_file_out_, key_file_out_);
     76 }
     77 
     78 void PackExtensionJob::ReportFailureOnClientThread(const std::string& error) {
     79   if (client_)
     80     client_->OnPackFailure(error);
     81 }
     82 
     83 // static
     84 string16 PackExtensionJob::StandardSuccessMessage(const FilePath& crx_file,
     85                                                   const FilePath& key_file) {
     86   string16 crx_file_string = crx_file.LossyDisplayName();
     87   string16 key_file_string = key_file.LossyDisplayName();
     88   if (key_file_string.empty()) {
     89     return l10n_util::GetStringFUTF16(
     90         IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_UPDATE,
     91         crx_file_string);
     92   } else {
     93     return l10n_util::GetStringFUTF16(
     94         IDS_EXTENSION_PACK_DIALOG_SUCCESS_BODY_NEW,
     95         crx_file_string,
     96         key_file_string);
     97   }
     98 }
     99