external_pref_loader.h revision 58e6fbe4ee35d65e14b626c557d37565bf8ad179
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_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
7
8#include "chrome/browser/extensions/external_loader.h"
9
10#include <string>
11
12#include "base/compiler_specific.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/values.h"
15
16namespace extensions {
17
18// A specialization of the ExternalLoader that uses a json file to
19// look up which external extensions are registered.
20// Instances of this class are expected to be created and destroyed on the UI
21// thread and they are expecting public method calls from the UI thread.
22class ExternalPrefLoader : public ExternalLoader {
23 public:
24  enum Options {
25    NONE = 0,
26
27    // Ensure that only root can force an external install by checking
28    // that all components of the path to external extensions files are
29    // owned by root and not writable by any non-root user.
30    ENSURE_PATH_CONTROLLED_BY_ADMIN = 1 << 0
31  };
32
33  // |base_path_id| is the directory containing the external_extensions.json
34  // file or the standalone extension manifest files. Relative file paths to
35  // extension files are resolved relative to this path.
36  ExternalPrefLoader(int base_path_id, Options options);
37
38  virtual const base::FilePath GetBaseCrxFilePath() OVERRIDE;
39
40 protected:
41  virtual ~ExternalPrefLoader() {}
42
43  virtual void StartLoading() OVERRIDE;
44  bool IsOptionSet(Options option) {
45    return (options_ & option) != 0;
46  }
47
48 private:
49  friend class base::RefCountedThreadSafe<ExternalLoader>;
50
51  // Actually searches for and loads candidate standalone extension preference
52  // files in the path corresponding to |base_path_id|.
53  // Must be called on the file thread.
54  void LoadOnFileThread();
55
56  // Extracts the information contained in an external_extension.json file
57  // regarding which extensions to install. |prefs| will be modified to
58  // receive the extracted extension information.
59  // Must be called from the File thread.
60  void ReadExternalExtensionPrefFile(DictionaryValue * prefs);
61
62  // Extracts the information contained in standalone external extension
63  // json files (<extension id>.json) regarding what external extensions
64  // to install. |prefs| will be modified to receive the extracted extension
65  // information.
66  // Must be called from the File thread.
67  void ReadStandaloneExtensionPrefFiles(DictionaryValue * prefs);
68
69  // The resource id of the base path with the information about the json
70  // file containing which extensions to load.
71  int base_path_id_;
72
73  Options options_;
74
75  // The path (coresponding to |base_path_id_| containing the json files
76  // describing which extensions to load.
77  base::FilePath base_path_;
78
79  DISALLOW_COPY_AND_ASSIGN(ExternalPrefLoader);
80};
81
82// A simplified version of ExternalPrefLoader that loads the dictionary
83// from json data specified in a string.
84class ExternalTestingLoader : public ExternalLoader {
85 public:
86  ExternalTestingLoader(const std::string& json_data,
87                        const base::FilePath& fake_base_path);
88
89  virtual const base::FilePath GetBaseCrxFilePath() OVERRIDE;
90
91 protected:
92  virtual void StartLoading() OVERRIDE;
93
94 private:
95  friend class base::RefCountedThreadSafe<ExternalLoader>;
96
97  virtual ~ExternalTestingLoader();
98
99  base::FilePath fake_base_path_;
100  scoped_ptr<DictionaryValue> testing_prefs_;
101
102  DISALLOW_COPY_AND_ASSIGN(ExternalTestingLoader);
103};
104
105}  // namespace extensions
106
107#endif  // CHROME_BROWSER_EXTENSIONS_EXTERNAL_PREF_LOADER_H_
108