Home | History | Annotate | Download | only in extensions
      1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_PACK_EXTENSION_JOB_H_
      6 #define CHROME_BROWSER_EXTENSIONS_PACK_EXTENSION_JOB_H_
      7 
      8 #include <string>
      9 
     10 #include "base/files/file_path.h"
     11 #include "base/memory/ref_counted.h"
     12 #include "base/strings/string16.h"
     13 #include "chrome/browser/extensions/extension_creator.h"
     14 #include "content/public/browser/browser_thread.h"
     15 
     16 namespace extensions {
     17 
     18 // Manages packing an extension on the file thread and reporting the result
     19 // back to the UI.
     20 class PackExtensionJob : public base::RefCountedThreadSafe<PackExtensionJob> {
     21  public:
     22   // Interface for people who want to use PackExtensionJob to implement.
     23   class Client {
     24    public:
     25     virtual void OnPackSuccess(const base::FilePath& crx_file,
     26                                const base::FilePath& key_file) = 0;
     27     virtual void OnPackFailure(const std::string& message,
     28                                ExtensionCreator::ErrorType error_type) = 0;
     29 
     30    protected:
     31     virtual ~Client() {}
     32   };
     33 
     34   PackExtensionJob(Client* client,
     35                    const base::FilePath& root_directory,
     36                    const base::FilePath& key_file,
     37                    int run_flags);
     38 
     39   // Starts the packing job.
     40   void Start();
     41 
     42   // The client should call this when it is destroyed to prevent
     43   // PackExtensionJob from attempting to access it.
     44   void ClearClient();
     45 
     46   // The standard packing success message.
     47   static base::string16 StandardSuccessMessage(const base::FilePath& crx_file,
     48                                          const base::FilePath& key_file);
     49 
     50   void set_asynchronous(bool async) { asynchronous_ = async; }
     51 
     52  private:
     53   friend class base::RefCountedThreadSafe<PackExtensionJob>;
     54 
     55   virtual ~PackExtensionJob();
     56 
     57   // If |asynchronous_| is false, this is run on whichever thread calls it.
     58   void Run();
     59   void ReportSuccessOnClientThread();
     60   void ReportFailureOnClientThread(const std::string& error,
     61                                    ExtensionCreator::ErrorType error_type);
     62 
     63   content::BrowserThread::ID client_thread_id_;
     64   Client* client_;
     65   base::FilePath root_directory_;
     66   base::FilePath key_file_;
     67   base::FilePath crx_file_out_;
     68   base::FilePath key_file_out_;
     69   bool asynchronous_;
     70   int run_flags_;  // Bitset of ExtensionCreator::RunFlags values - we always
     71                    // assume kRequireModernManifestVersion, though.
     72 
     73   DISALLOW_COPY_AND_ASSIGN(PackExtensionJob);
     74 };
     75 
     76 }  // namespace extensions
     77 
     78 #endif  // CHROME_BROWSER_EXTENSIONS_PACK_EXTENSION_JOB_H_
     79