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 CHROME_UTILITY_EXTENSIONS_UNPACKER_H_
6#define CHROME_UTILITY_EXTENSIONS_UNPACKER_H_
7
8#include <string>
9#include <vector>
10
11#include "base/files/file_path.h"
12#include "base/memory/scoped_ptr.h"
13#include "extensions/common/manifest.h"
14
15class SkBitmap;
16
17namespace base {
18class DictionaryValue;
19}
20
21namespace extensions {
22
23// This class unpacks an extension.  It is designed to be used in a sandboxed
24// child process.  We unpack and parse various bits of the extension, then
25// report back to the browser process, who then transcodes the pre-parsed bits
26// and writes them back out to disk for later use.
27class Unpacker {
28 public:
29  Unpacker(const base::FilePath& extension_path,
30           const std::string& extension_id,
31           Manifest::Location location,
32           int creation_flags);
33  ~Unpacker();
34
35  // Install the extension file at |extension_path|.  Returns true on success.
36  // Otherwise, error_message will contain a string explaining what went wrong.
37  bool Run();
38
39  // Write the decoded images to kDecodedImagesFilename.  We do this instead
40  // of sending them over IPC, since they are so large.  Returns true on
41  // success.
42  bool DumpImagesToFile();
43
44  // Write the decoded messages to kDecodedMessageCatalogsFilename.  We do this
45  // instead of sending them over IPC, since they are so large.  Returns true on
46  // success.
47  bool DumpMessageCatalogsToFile();
48
49  const base::string16& error_message() { return error_message_; }
50  base::DictionaryValue* parsed_manifest() {
51    return parsed_manifest_.get();
52  }
53  base::DictionaryValue* parsed_catalogs() { return parsed_catalogs_.get(); }
54
55 private:
56  // Parse the manifest.json file inside the extension (not in the header).
57  // Caller takes ownership of return value.
58  base::DictionaryValue* ReadManifest();
59
60  // Parse all _locales/*/messages.json files inside the extension.
61  bool ReadAllMessageCatalogs(const std::string& default_locale);
62
63  // Decodes the image at the given path and puts it in our list of decoded
64  // images.
65  bool AddDecodedImage(const base::FilePath& path);
66
67  // Parses the catalog at the given path and puts it in our list of parsed
68  // catalogs.
69  bool ReadMessageCatalog(const base::FilePath& message_path);
70
71  // Set the error message.
72  void SetError(const std::string& error);
73  void SetUTF16Error(const base::string16& error);
74
75  // The extension to unpack.
76  base::FilePath extension_path_;
77
78  // The extension ID if known.
79  std::string extension_id_;
80
81  // The location to use for the created extension.
82  Manifest::Location location_;
83
84  // The creation flags to use with the created extension.
85  int creation_flags_;
86
87  // The place we unpacked the extension to.
88  base::FilePath temp_install_dir_;
89
90  // The parsed version of the manifest JSON contained in the extension.
91  scoped_ptr<base::DictionaryValue> parsed_manifest_;
92
93  // A list of decoded images and the paths where those images came from.  Paths
94  // are relative to the manifest file.
95  struct InternalData;
96  scoped_ptr<InternalData> internal_data_;
97
98  // Dictionary of relative paths and catalogs per path. Paths are in the form
99  // of _locales/locale, without messages.json base part.
100  scoped_ptr<base::DictionaryValue> parsed_catalogs_;
101
102  // The last error message that was set.  Empty if there were no errors.
103  base::string16 error_message_;
104
105  DISALLOW_COPY_AND_ASSIGN(Unpacker);
106};
107
108}  // namespace extensions
109
110#endif  // CHROME_UTILITY_EXTENSIONS_UNPACKER_H_
111