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