summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/component_loader.h
blob: 3583d13a8f5f9a3a2c05b4b73d44c45057bf96a4 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright (c) 2011 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 CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
#define CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_
#pragma once

#include <string>

#include "base/gtest_prod_util.h"
#include "base/file_path.h"
#include "base/values.h"
#include "chrome/browser/prefs/pref_change_registrar.h"
#include "content/public/browser/notification_observer.h"

class Extension;
class ExtensionServiceInterface;
class PrefService;

namespace extensions {

// For registering, loading, and unloading component extensions.
class ComponentLoader : public content::NotificationObserver {
 public:
  ComponentLoader(ExtensionServiceInterface* extension_service,
                  PrefService* prefs,
                  PrefService* local_state);
  virtual ~ComponentLoader();

  size_t registered_extensions_count() const {
    return component_extensions_.size();
  }

  // Loads any registered component extensions.
  void LoadAll();

  // Registers and possibly loads a component extension. If ExtensionService
  // has been initialized, the extension is loaded; otherwise, the load is
  // deferred until LoadAll is called.
  const Extension* Add(std::string& manifest_contents,
                       const FilePath& root_directory);

  // Convenience method for registering a component extension by resource id.
  const Extension* Add(int manifest_resource_id,
                       const FilePath& root_directory);

  // Loads a component extension from file system. Replaces previously added
  // extension with the same ID.
  const Extension* AddOrReplace(const FilePath& path);

  // Unloads a component extension and removes it from the list of component
  // extensions to be loaded.
  void Remove(const FilePath& root_directory);
  void Remove(const std::string& id);

  // Adds the default component extensions.
  //
  // Component extension manifests must contain a 'key' property with a unique
  // public key, serialized in base64. You can create a suitable value with the
  // following commands on a unixy system:
  //
  //   ssh-keygen -t rsa -b 1024 -N '' -f /tmp/key.pem
  //   openssl rsa -pubout -outform DER < /tmp/key.pem 2>/dev/null | base64 -w 0
  void AddDefaultComponentExtensions();

  // content::NotificationObserver implementation
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  static void RegisterUserPrefs(PrefService* prefs);

  // Parse the given JSON manifest. Returns NULL if it cannot be parsed, or if
  // if the result is not a DictionaryValue.
  DictionaryValue* ParseManifest(const std::string& manifest_contents) const;

  // Clear the list of registered extensions.
  void ClearAllRegistered();

 private:
  // Information about a registered component extension.
  struct ComponentExtensionInfo {
    ComponentExtensionInfo(const DictionaryValue* manifest,
                           const FilePath& root_directory)
        : manifest(manifest),
          root_directory(root_directory) {
    }

    // The parsed contents of the extensions's manifest file.
    const DictionaryValue* manifest;

    // Directory where the extension is stored.
    FilePath root_directory;
  };

  const Extension* Add(const DictionaryValue* parsed_manifest,
                       const FilePath& root_directory);

  // Loads a registered component extension.
  const Extension* Load(const ComponentExtensionInfo& info);

  void AddFileManagerExtension();

  // Add the enterprise webstore extension, or reload it if already loaded.
  void AddOrReloadEnterpriseWebStore();

  // Returns true if an extension with the specified id has been added.
  bool Exists(const std::string& id) const;

  // Determine the extension id.
  static std::string GenerateId(const base::DictionaryValue* manifest);

  PrefService* prefs_;
  PrefService* local_state_;

  ExtensionServiceInterface* extension_service_;

  // List of registered component extensions (see Extension::Location).
  typedef std::vector<ComponentExtensionInfo> RegisteredComponentExtensions;
  RegisteredComponentExtensions component_extensions_;

  PrefChangeRegistrar pref_change_registrar_;
};

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_COMPONENT_LOADER_H_