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