Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / extensions / extension_creator.h
blobde7aa7657e612dac00b59e3e1ba917f645bc38b3
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_CREATOR_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_CREATOR_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
13 namespace base {
14 class FilePath;
17 namespace crypto {
18 class RSAPrivateKey;
21 namespace extensions {
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();
32 // Settings to specify treatment of special or ignorable error conditions.
33 enum RunFlags {
34 kNoRunFlags = 0x0,
35 kOverwriteCRX = 0x1,
36 kRequireModernManifestVersion = 0x2,
39 // Categories of error that may need special handling on the UI end.
40 enum ErrorType { kOtherError, kCRXExists };
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);
48 // Returns the error message that will be present if Run(...) returned false.
49 std::string error_message() { return error_message_; }
51 ErrorType error_type() { return error_type_; }
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);
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);
72 // Reads private key from |private_key_path|.
73 crypto::RSAPrivateKey* ReadInputKey(const base::FilePath& private_key_path);
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);
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);
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);
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);
94 // Holds a message for any error that is raised during Run(...).
95 std::string error_message_;
97 // Type of error that was raised, if any.
98 ErrorType error_type_;
100 DISALLOW_COPY_AND_ASSIGN(ExtensionCreator);
103 } // namespace extensions
105 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_CREATOR_H_