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_EXTENSION_CREATOR_H_
      6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_CREATOR_H_
      7 
      8 #include <string>
      9 #include <vector>
     10 
     11 #include "base/basictypes.h"
     12 
     13 namespace base {
     14 class FilePath;
     15 }
     16 
     17 namespace crypto {
     18 class RSAPrivateKey;
     19 }
     20 
     21 namespace extensions {
     22 
     23 // This class create an installable extension (.crx file) given an input
     24 // directory that contains a valid manifest.json and the extension's resources
     25 // contained within that directory. The output .crx file is always signed with a
     26 // private key that is either provided in |private_key_path| or is internal
     27 // generated randomly (and optionally written to |output_private_key_path|.
     28 class ExtensionCreator {
     29  public:
     30   ExtensionCreator();
     31 
     32   // Settings to specify treatment of special or ignorable error conditions.
     33   enum RunFlags {
     34     kNoRunFlags = 0x0,
     35     kOverwriteCRX = 0x1,
     36     kRequireModernManifestVersion = 0x2,
     37   };
     38 
     39   // Categories of error that may need special handling on the UI end.
     40   enum ErrorType { kOtherError, kCRXExists };
     41 
     42   bool Run(const base::FilePath& extension_dir,
     43            const base::FilePath& crx_path,
     44            const base::FilePath& private_key_path,
     45            const base::FilePath& private_key_output_path,
     46            int run_flags);
     47 
     48   // Returns the error message that will be present if Run(...) returned false.
     49   std::string error_message() { return error_message_; }
     50 
     51   ErrorType error_type() { return error_type_; }
     52 
     53  private:
     54   // Verifies input directory's existence. |extension_dir| is the source
     55   // directory that should contain all the extension resources. |crx_path| is
     56   // the path to which final crx will be written.
     57   // |private_key_path| is the optional path to an existing private key to sign
     58   // the extension. If not provided, a random key will be created (in which case
     59   // it is written to |private_key_output_path| -- if provided).
     60   // |flags| is a bitset of RunFlags values.
     61   bool InitializeInput(const base::FilePath& extension_dir,
     62                        const base::FilePath& crx_path,
     63                        const base::FilePath& private_key_path,
     64                        const base::FilePath& private_key_output_path,
     65                        int run_flags);
     66 
     67   // Validates the manifest by trying to load the extension.
     68   bool ValidateManifest(const base::FilePath& extension_dir,
     69                         crypto::RSAPrivateKey* key_pair,
     70                         int run_flags);
     71 
     72   // Reads private key from |private_key_path|.
     73   crypto::RSAPrivateKey* ReadInputKey(const base::FilePath& private_key_path);
     74 
     75   // Generates a key pair and writes the private key to |private_key_path|
     76   // if provided.
     77   crypto::RSAPrivateKey* GenerateKey(const base::FilePath& private_key_path);
     78 
     79   // Creates temporary zip file for the extension.
     80   bool CreateZip(const base::FilePath& extension_dir, const base::FilePath& temp_path,
     81                  base::FilePath* zip_path);
     82 
     83   // Signs the temporary zip and returns the signature.
     84   bool SignZip(const base::FilePath& zip_path,
     85                crypto::RSAPrivateKey* private_key,
     86                std::vector<uint8>* signature);
     87 
     88   // Export installable .crx to |crx_path|.
     89   bool WriteCRX(const base::FilePath& zip_path,
     90                 crypto::RSAPrivateKey* private_key,
     91                 const std::vector<uint8>& signature,
     92                 const base::FilePath& crx_path);
     93 
     94   // Holds a message for any error that is raised during Run(...).
     95   std::string error_message_;
     96 
     97   // Type of error that was raised, if any.
     98   ErrorType error_type_;
     99 
    100   DISALLOW_COPY_AND_ASSIGN(ExtensionCreator);
    101 };
    102 
    103 }  // namespace extensions
    104 
    105 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_CREATOR_H_
    106