Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / extensions / common / manifest.h
1 // Copyright 2013 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 EXTENSIONS_COMMON_MANIFEST_H_
6 #define EXTENSIONS_COMMON_MANIFEST_H_
7
8 #include <map>
9 #include <set>
10 #include <string>
11
12 #include "base/memory/scoped_ptr.h"
13 #include "base/strings/string16.h"
14 #include "base/values.h"
15
16 namespace extensions {
17 struct InstallWarning;
18
19 // Wraps the DictionaryValue form of extension's manifest. Enforces access to
20 // properties of the manifest using ManifestFeatureProvider.
21 class Manifest {
22  public:
23   // What an extension was loaded from.
24   // NOTE: These values are stored as integers in the preferences and used
25   // in histograms so don't remove or reorder existing items.  Just append
26   // to the end.
27   enum Location {
28     INVALID_LOCATION,
29     INTERNAL,           // A crx file from the internal Extensions directory.
30     EXTERNAL_PREF,      // A crx file from an external directory (via prefs).
31     EXTERNAL_REGISTRY,  // A crx file from an external directory (via eg the
32                         // registry on Windows).
33     UNPACKED,           // From loading an unpacked extension from the
34                         // extensions settings page.
35     COMPONENT,          // An integral component of Chrome itself, which
36                         // happens to be implemented as an extension. We don't
37                         // show these in the management UI.
38     EXTERNAL_PREF_DOWNLOAD,    // A crx file from an external directory (via
39                                // prefs), installed from an update URL.
40     EXTERNAL_POLICY_DOWNLOAD,  // A crx file from an external directory (via
41                                // admin policies), installed from an update URL.
42     COMMAND_LINE,       // --load-extension.
43     EXTERNAL_POLICY,    // A crx file from an external directory (via admin
44                         // policies), cached locally and installed from the
45                         // cache.
46     EXTERNAL_COMPONENT, // Similar to COMPONENT in that it's considered an
47                         // internal implementation detail of chrome, but
48                         // installed from an update URL like the *DOWNLOAD ones.
49
50     NUM_LOCATIONS
51   };
52
53   // Do not change the order of entries or remove entries in this list
54   // as this is used in UMA_HISTOGRAM_ENUMERATIONs about extensions.
55   enum Type {
56     TYPE_UNKNOWN = 0,
57     TYPE_EXTENSION,
58     TYPE_THEME,
59     TYPE_USER_SCRIPT,
60     TYPE_HOSTED_APP,
61     // This is marked legacy because platform apps are preferred. For
62     // backwards compatibility, we can't remove support for packaged apps
63     TYPE_LEGACY_PACKAGED_APP,
64     TYPE_PLATFORM_APP,
65     TYPE_SHARED_MODULE
66   };
67
68   // Given two install sources, return the one which should take priority
69   // over the other. If an extension is installed from two sources A and B,
70   // its install source should be set to GetHigherPriorityLocation(A, B).
71   static Location GetHigherPriorityLocation(Location loc1, Location loc2);
72
73   // Whether the |location| is external or not.
74   static inline bool IsExternalLocation(Location location) {
75     return location == EXTERNAL_PREF ||
76            location == EXTERNAL_REGISTRY ||
77            location == EXTERNAL_PREF_DOWNLOAD ||
78            location == EXTERNAL_POLICY ||
79            location == EXTERNAL_POLICY_DOWNLOAD ||
80            location == EXTERNAL_COMPONENT;
81   }
82
83   // Whether the |location| is unpacked (no CRX) or not.
84   static inline bool IsUnpackedLocation(Location location) {
85     return location == UNPACKED || location == COMMAND_LINE;
86   }
87
88   // Whether extensions with |location| are auto-updatable or not.
89   static inline bool IsAutoUpdateableLocation(Location location) {
90     // Only internal and external extensions can be autoupdated.
91     return location == INTERNAL ||
92            IsExternalLocation(location);
93   }
94
95   // Whether the |location| is a source of extensions force-installed through
96   // policy.
97   static inline bool IsPolicyLocation(Location location) {
98     return location == EXTERNAL_POLICY ||
99            location == EXTERNAL_POLICY_DOWNLOAD;
100   }
101
102   // Whether the |location| is an extension intended to be an internal part of
103   // Chrome.
104   static inline bool IsComponentLocation(Location location) {
105     return location == COMPONENT || location == EXTERNAL_COMPONENT;
106   }
107
108   // Unpacked extensions start off with file access since they are a developer
109   // feature.
110   static inline bool ShouldAlwaysAllowFileAccess(Location location) {
111     return IsUnpackedLocation(location);
112   }
113
114   Manifest(Location location, scoped_ptr<base::DictionaryValue> value);
115   virtual ~Manifest();
116
117   const std::string& extension_id() const { return extension_id_; }
118   void set_extension_id(const std::string& id) { extension_id_ = id; }
119
120   Location location() const { return location_; }
121
122   // Returns false and |error| will be non-empty if the manifest is malformed.
123   // |warnings| will be populated if there are keys in the manifest that cannot
124   // be specified by the extension type.
125   bool ValidateManifest(std::string* error,
126                         std::vector<InstallWarning>* warnings) const;
127
128   // The version of this extension's manifest. We increase the manifest
129   // version when making breaking changes to the extension system. If the
130   // manifest contains no explicit manifest version, this returns the current
131   // system default.
132   int GetManifestVersion() const;
133
134   // Returns the manifest type.
135   Type type() const { return type_; }
136
137   bool is_theme() const { return type_ == TYPE_THEME; }
138   bool is_app() const {
139     return is_legacy_packaged_app() || is_hosted_app() || is_platform_app();
140   }
141   bool is_platform_app() const { return type_ == TYPE_PLATFORM_APP; }
142   bool is_hosted_app() const { return type_ == TYPE_HOSTED_APP; }
143   bool is_legacy_packaged_app() const {
144     return type_ == TYPE_LEGACY_PACKAGED_APP;
145   }
146   bool is_extension() const { return type_ == TYPE_EXTENSION; }
147   bool is_shared_module() const { return type_ == TYPE_SHARED_MODULE; }
148
149   // These access the wrapped manifest value, returning false when the property
150   // does not exist or if the manifest type can't access it.
151   bool HasKey(const std::string& key) const;
152   bool HasPath(const std::string& path) const;
153   bool Get(const std::string& path, const base::Value** out_value) const;
154   bool GetBoolean(const std::string& path, bool* out_value) const;
155   bool GetInteger(const std::string& path, int* out_value) const;
156   bool GetString(const std::string& path, std::string* out_value) const;
157   bool GetString(const std::string& path, base::string16* out_value) const;
158   bool GetDictionary(const std::string& path,
159                      const base::DictionaryValue** out_value) const;
160   bool GetList(const std::string& path,
161                const base::ListValue** out_value) const;
162
163   // Returns a new Manifest equal to this one, passing ownership to
164   // the caller.
165   Manifest* DeepCopy() const;
166
167   // Returns true if this equals the |other| manifest.
168   bool Equals(const Manifest* other) const;
169
170   // Gets the underlying DictionaryValue representing the manifest.
171   // Note: only use this when you KNOW you don't need the validation.
172   const base::DictionaryValue* value() const { return value_.get(); }
173
174  private:
175   // Returns true if the extension can specify the given |path|.
176   bool CanAccessPath(const std::string& path) const;
177   bool CanAccessKey(const std::string& key) const;
178
179   // A persistent, globally unique ID. An extension's ID is used in things
180   // like directory structures and URLs, and is expected to not change across
181   // versions. It is generated as a SHA-256 hash of the extension's public
182   // key, or as a hash of the path in the case of unpacked extensions.
183   std::string extension_id_;
184
185   // The location the extension was loaded from.
186   Location location_;
187
188   // The underlying dictionary representation of the manifest.
189   scoped_ptr<base::DictionaryValue> value_;
190
191   Type type_;
192
193   DISALLOW_COPY_AND_ASSIGN(Manifest);
194 };
195
196 }  // namespace extensions
197
198 #endif  // EXTENSIONS_COMMON_MANIFEST_H_