summaryrefslogtreecommitdiffstats
path: root/extensions/utility/unpacker.h
blob: b05cf3cc08a0e3226e91284b407e6a0e91d7833a (plain)
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
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_UTILITY_UNPACKER_H_
#define EXTENSIONS_UTILITY_UNPACKER_H_

#include <string>
#include <vector>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "extensions/common/manifest.h"

class SkBitmap;

namespace base {
class DictionaryValue;
}

namespace extensions {

// This class unpacks an extension.  It is designed to be used in a sandboxed
// child process.  We parse various bits of the extension, then report back to
// the browser process, who then transcodes the pre-parsed bits and writes them
// back out to disk for later use.
class Unpacker {
 public:
  Unpacker(const base::FilePath& working_dir,
           const base::FilePath& extension_dir,
           const std::string& extension_id,
           Manifest::Location location,
           int creation_flags);
  ~Unpacker();

  // Runs the processing steps for the extension. On success, this returns true
  // and the decoded images will be in a file at
  // |working_dir|/kDecodedImagesFilename and the decoded messages will be in a
  // file at |working_dir|/kDecodedMessageCatalogsFilename.
  bool Run();

  const base::string16& error_message() { return error_message_; }
  base::DictionaryValue* parsed_manifest() { return parsed_manifest_.get(); }
  base::DictionaryValue* parsed_catalogs() { return parsed_catalogs_.get(); }

 private:
  // Write the decoded images to kDecodedImagesFilename.  We do this instead
  // of sending them over IPC, since they are so large.  Returns true on
  // success.
  bool DumpImagesToFile();

  // Write the decoded messages to kDecodedMessageCatalogsFilename.  We do this
  // instead of sending them over IPC, since they are so large.  Returns true on
  // success.
  bool DumpMessageCatalogsToFile();

  // Parse the manifest.json file inside the extension (not in the header).
  scoped_ptr<base::DictionaryValue> ReadManifest();

  // Parse all _locales/*/messages.json files inside the extension.
  bool ReadAllMessageCatalogs(const std::string& default_locale);

  // Decodes the image at the given path and puts it in our list of decoded
  // images.
  bool AddDecodedImage(const base::FilePath& path);

  // Parses the catalog at the given path and puts it in our list of parsed
  // catalogs.
  bool ReadMessageCatalog(const base::FilePath& message_path);

  // Set the error message.
  void SetError(const std::string& error);
  void SetUTF16Error(const base::string16& error);

  // The directory to do work in.
  base::FilePath working_dir_;

  // The directory where the extension source lives.
  base::FilePath extension_dir_;

  // The extension ID if known.
  std::string extension_id_;

  // The location to use for the created extension.
  Manifest::Location location_;

  // The creation flags to use with the created extension.
  int creation_flags_;

  // The parsed version of the manifest JSON contained in the extension.
  scoped_ptr<base::DictionaryValue> parsed_manifest_;

  // A list of decoded images and the paths where those images came from.  Paths
  // are relative to the manifest file.
  struct InternalData;
  scoped_ptr<InternalData> internal_data_;

  // Dictionary of relative paths and catalogs per path. Paths are in the form
  // of _locales/locale, without messages.json base part.
  scoped_ptr<base::DictionaryValue> parsed_catalogs_;

  // The last error message that was set.  Empty if there were no errors.
  base::string16 error_message_;

  DISALLOW_COPY_AND_ASSIGN(Unpacker);
};

}  // namespace extensions

#endif  // EXTENSIONS_UTILITY_UNPACKER_H_