15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
8eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/basictypes.h"
112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/files/file_path.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ComponentInstaller;
14eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdochclass ComponentPatcher;
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// In charge of unpacking the component CRX package and verifying that it is
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// well formed and the cryptographic signature is correct. If there is no
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// error the component specific installer will be invoked to proceed with
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the component installation or update.
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class should be used only by the component updater. It is inspired
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and overlaps with code in the extension's SandboxedUnpacker.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The main differences are:
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - The public key hash is full SHA256.
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - Does not use a sandboxed unpacker. A valid component is fully trusted.
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - The manifest can have different attributes and resources are not
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   transcoded.
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ComponentUnpacker {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Possible error conditions.
31eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Add only to the bottom of this enum; the order must be kept stable.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Error {
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kNone,
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kInvalidParams,
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kInvalidFile,
36eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kUnzipPathError,
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kUnzipFailed,
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kNoManifest,
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kBadManifest,
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kBadExtension,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kInvalidId,
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kInstallerError,
43eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kIoError,
44eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kDeltaVerificationFailure,
45eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kDeltaBadCommands,
46eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kDeltaUnsupportedCommand,
47eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kDeltaOperationFailure,
48eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kDeltaPatchProcessFailure,
49eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kDeltaMissingExistingFile,
50eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    kFingerprintWriteFailed,
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Unpacks, verifies and calls the installer. |pk_hash| is the expected
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // public key SHA256 hash. |path| is the current location of the CRX.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ComponentUnpacker(const std::vector<uint8>& pk_hash,
552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                    const base::FilePath& path,
56eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    const std::string& fingerprint,
57eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch                    ComponentPatcher* patcher,
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                    ComponentInstaller* installer);
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If something went wrong during unpacking or installer invocation, the
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // destructor will delete the unpacked CRX files.
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~ComponentUnpacker();
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Error error() const { return error_; }
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
66eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int extended_error() const { return extended_error_; }
67eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::FilePath unpack_path_;
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Error error_;
71eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  int extended_error_;  // Provides additional error information.
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_COMPONENT_UPDATER_COMPONENT_UNPACKER_H_
75