File: extension_creator.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 4,443 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_EXTENSION_CREATOR_H_
#define EXTENSIONS_BROWSER_EXTENSION_CREATOR_H_

#include <stdint.h>

#include <memory>
#include <optional>
#include <string>
#include <vector>

namespace base {
class FilePath;
}

namespace crypto {
class RSAPrivateKey;
}

namespace extensions {

// This class create an installable extension (.crx file) given an input
// directory that contains a valid manifest.json and the extension's resources
// contained within that directory. The output .crx file is always signed with a
// private key that is either provided in `private_key_path` or is internal
// generated randomly (and optionally written to `output_private_key_path`.
class ExtensionCreator {
 public:
  ExtensionCreator();

  ExtensionCreator(const ExtensionCreator&) = delete;
  ExtensionCreator& operator=(const ExtensionCreator&) = delete;

  // Settings to specify treatment of special or ignorable error conditions.
  // TODO(tjudkins): We should get rid of these flags and instead use explicit
  // boolean parameters in their place if they are still needed.
  enum RunFlags {
    kNoRunFlags = 0,
    kOverwriteCRX = 1 << 0,
    kRequireModernManifestVersion = 1 << 1,
  };

  // Categories of error that may need special handling on the UI end.
  enum ErrorType { kOtherError, kCRXExists };

  bool Run(const base::FilePath& extension_dir,
           const base::FilePath& crx_path,
           const base::FilePath& private_key_path,
           const base::FilePath& private_key_output_path,
           int run_flags);

  // Returns the error message that will be present if Run(...) returned false.
  std::string error_message() { return error_message_; }

  ErrorType error_type() { return error_type_; }

 private:
  friend class ExtensionCreatorTest;
  friend class ContentVerifierTest;

  // Verifies input directory's existence. `extension_dir` is the source
  // directory that should contain all the extension resources. `crx_path` is
  // the path to which final crx will be written.
  // `private_key_path` is the optional path to an existing private key to sign
  // the extension. If not provided, a random key will be created (in which case
  // it is written to `private_key_output_path` -- if provided).
  // `flags` is a bitset of RunFlags values.
  bool InitializeInput(const base::FilePath& extension_dir,
                       const base::FilePath& crx_path,
                       const base::FilePath& private_key_path,
                       const base::FilePath& private_key_output_path,
                       int run_flags);

  // Validates the extension by trying to load it and checking language files.
  bool ValidateExtension(const base::FilePath& extension_dir, int run_flags);

  // Reads private key from `private_key_path`.
  std::unique_ptr<crypto::RSAPrivateKey> ReadInputKey(
      const base::FilePath& private_key_path);

  // Generates a key pair and writes the private key to `private_key_path`
  // if provided.
  std::unique_ptr<crypto::RSAPrivateKey> GenerateKey(
      const base::FilePath& private_key_path);

  // Creates temporary zip file for the extension.
  bool CreateZip(const base::FilePath& extension_dir,
                 const base::FilePath& temp_path,
                 base::FilePath* zip_path);

  // Creates a CRX file at `crx_path`, signed with `private_key` and with the
  // contents of the archive at `zip_path`. Injects
  // `compressed_verified_contents` in the header if it not equal to
  // std::nullopt.
  bool CreateCrx(
      const base::FilePath& zip_path,
      crypto::RSAPrivateKey* private_key,
      const base::FilePath& crx_path,
      const std::optional<std::string>& compressed_verified_contents);

  // Creates a temporary directory to store zipped extension and then creates
  // CRX using the zipped extension.
  bool CreateCrxAndPerformCleanup(
      const base::FilePath& extension_dir,
      const base::FilePath& crx_path,
      crypto::RSAPrivateKey* private_key,
      const std::optional<std::string>& compressed_verified_contents);

  // Holds a message for any error that is raised during Run(...).
  std::string error_message_;

  // Type of error that was raised, if any.
  ErrorType error_type_;
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_EXTENSION_CREATOR_H_