summaryrefslogtreecommitdiffstats
path: root/chrome/common/extensions/feature.h
blob: 0a956890be5918ddad00c873c5540ea9f8409dc2 (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
// 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 CHROME_COMMON_EXTENSIONS_FEATURE_H_
#define CHROME_COMMON_EXTENSIONS_FEATURE_H_
#pragma once

#include <set>
#include <string>

#include "base/memory/scoped_ptr.h"
#include "base/values.h"
#include "chrome/common/extensions/extension.h"

namespace extensions {

// Represents a single feature accessible to an extension developer, such as a
// top-level manifest key, a permission, or a programmatic API. A feature can
// express requirements for where it can be accessed, and supports testing
// support for those requirements.
class Feature {
 public:
  // The JavaScript contexts the feature is supported in.
  enum Context {
    UNSPECIFIED_CONTEXT,
    PRIVILEGED_CONTEXT,  // A context in a privileged extension process.
    UNPRIVILEGED_CONTEXT,  // A context in a normal, unprivileged renderer.
    CONTENT_SCRIPT_CONTEXT  // A context from a content script.
  };

  // The location required of extensions the feature is supported in.
  enum Location {
    UNSPECIFIED_LOCATION,
    COMPONENT_LOCATION
  };

  // The platforms the feature is supported in.
  enum Platform {
    UNSPECIFIED_PLATFORM,
    CHROMEOS_PLATFORM
  };

  Feature();
  ~Feature();

  // Parses a feature from its JSON representation.
  static scoped_ptr<Feature> Parse(const DictionaryValue* value);

  // Gets the platform the code is currently running on.
  static Platform GetCurrentPlatform();

  // Gets the Feature::Location value for the specified Extension::Location.
  static Location ConvertLocation(Extension::Location extension_location);

  std::set<std::string>* whitelist() { return &whitelist_; }
  std::set<Extension::Type>* extension_types() { return &extension_types_; }
  std::set<Context>* contexts() { return &contexts_; }

  Location location() const { return location_; }
  void set_location(Location location) { location_ = location; }

  Platform platform() const { return platform_; }
  void set_platform(Platform platform) { platform_ = platform; }

  int min_manifest_version() const { return min_manifest_version_; }
  void set_min_manifest_version(int min_manifest_version) {
    min_manifest_version_ = min_manifest_version;
  }

  int max_manifest_version() const { return max_manifest_version_; }
  void set_max_manifest_version(int max_manifest_version) {
    max_manifest_version_ = max_manifest_version;
  }

  // Returns true if the feature is available to the specified extension. Use
  // this overload for features that are not associated with a specific context.
  bool IsAvailable(const Extension* extension) {
    return IsAvailable(extension, UNSPECIFIED_CONTEXT);
  }

  // Returns true if the feature is available to the specified extension, in the
  // specified context type.
  bool IsAvailable(const Extension* extension, Context context) {
    return IsAvailable(extension->id(), extension->GetType(),
                       ConvertLocation(extension->location()), context,
                       GetCurrentPlatform(),
                       extension->manifest_version());
  }

  // Returns true if the feature is available to extensions with the specified
  // properties. Use this overload for features that are not associated with a
  // specific context, and when a full Extension object is not available.
  bool IsAvailable(const std::string& extension_id, Extension::Type type,
                   Location location, int manifest_version) {
    return IsAvailable(extension_id, type, location, UNSPECIFIED_CONTEXT,
                       GetCurrentPlatform(), manifest_version);
  }

  // Returns true if the feature is available to extensions with the specified
  // properties, in the specified context type, and on the specified platform.
  // This overload is mainly used for testing.
  bool IsAvailable(const std::string& extension_id, Extension::Type type,
                   Location location, Context context,
                   Platform platform, int manifest_version);

 private:
  // For clarify and consistency, we handle the default value of each of these
  // members the same way: it matches everything. It is up to the higher level
  // code that reads Features out of static data to validate that data and set
  // sensible defaults.
  std::set<std::string> whitelist_;
  std::set<Extension::Type> extension_types_;
  std::set<Context> contexts_;
  Location location_;  // we only care about component/not-component now
  Platform platform_;  // we only care about chromeos/not-chromeos now
  int min_manifest_version_;
  int max_manifest_version_;

  DISALLOW_COPY_AND_ASSIGN(Feature);
};

}  // namespace extensions

#endif  // CHROME_COMMON_EXTENSIONS_FEATURE_H_