1// Copyright (c) 2011 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#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/basictypes.h"
13
14namespace crypto {
15class RSAPrivateKey;
16}
17
18class FilePath;
19
20// This class create an installable extension (.crx file) given an input
21// directory that contains a valid manifest.json and the extension's resources
22// contained within that directory. The output .crx file is always signed with a
23// private key that is either provided in |private_key_path| or is internal
24// generated randomly (and optionally written to |output_private_key_path|.
25class ExtensionCreator {
26 public:
27  ExtensionCreator() {}
28
29  bool Run(const FilePath& extension_dir,
30           const FilePath& crx_path,
31           const FilePath& private_key_path,
32           const FilePath& private_key_output_path);
33
34  // Returns the error message that will be present if Run(...) returned false.
35  std::string error_message() { return error_message_; }
36
37 private:
38  // Verifies input directory's existence. |extension_dir| is the source
39  // directory that should contain all the extension resources.
40  // |private_key_path| is the optional path to an existing private key to sign
41  // the extension. If not provided, a random key will be created (in which case
42  // it is written to |private_key_output_path| -- if provided).
43  bool InitializeInput(const FilePath& extension_dir,
44                       const FilePath& private_key_path,
45                       const FilePath& private_key_output_path);
46
47  // Reads private key from |private_key_path|.
48  crypto::RSAPrivateKey* ReadInputKey(const FilePath& private_key_path);
49
50  // Generates a key pair and writes the private key to |private_key_path|
51  // if provided.
52  crypto::RSAPrivateKey* GenerateKey(const FilePath& private_key_path);
53
54  // Creates temporary zip file for the extension.
55  bool CreateZip(const FilePath& extension_dir, const FilePath& temp_path,
56                 FilePath* zip_path);
57
58  // Signs the temporary zip and returns the signature.
59  bool SignZip(const FilePath& zip_path,
60               crypto::RSAPrivateKey* private_key,
61               std::vector<uint8>* signature);
62
63  // Export installable .crx to |crx_path|.
64  bool WriteCRX(const FilePath& zip_path,
65                crypto::RSAPrivateKey* private_key,
66                const std::vector<uint8>& signature,
67                const FilePath& crx_path);
68
69  // Holds a message for any error that is raised during Run(...).
70  std::string error_message_;
71
72  DISALLOW_COPY_AND_ASSIGN(ExtensionCreator);
73};
74
75#endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_CREATOR_H_
76