Files.app: Dispatch 'drive-connection-changed' event on initialization of VolumeManag...
[chromium-blink-merge.git] / extensions / browser / management_policy.h
blob3ae845edbffd034712059733fa2a1306ca95cbfb
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.
5 #ifndef EXTENSIONS_BROWSER_MANAGEMENT_POLICY_H_
6 #define EXTENSIONS_BROWSER_MANAGEMENT_POLICY_H_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "extensions/common/extension.h"
15 namespace extensions {
17 // This class registers providers that want to prohibit certain actions from
18 // being applied to extensions. It must be called, via the ExtensionService,
19 // before allowing a user or a user-level mechanism to perform the respective
20 // action. (That is, installing or otherwise modifying an extension in order
21 // to conform to enterprise administrator policy must be exempted from these
22 // checks.)
24 // This "policy" and its providers should not be confused with administrator
25 // policy, although admin policy is one of the sources ("Providers") of
26 // restrictions registered with and exposed by the ManagementPolicy.
27 class ManagementPolicy {
28 public:
29 // Each mechanism that wishes to limit users' ability to control extensions,
30 // whether one individual extension or the whole system, should implement
31 // the methods of this Provider interface that it needs. In each case, if the
32 // provider does not need to control a certain action, that method does not
33 // need to be implemented.
35 // It is not guaranteed that a particular Provider's methods will be called
36 // each time a user tries to perform one of the controlled actions (the list
37 // of providers is short-circuited as soon as a decision is possible), so
38 // implementations of these methods must have no side effects.
40 // For all of the Provider methods below, if |error| is not NULL and the
41 // method imposes a restriction on the desired action, |error| may be set
42 // to an applicable error message, but this is not required.
43 class Provider {
44 public:
45 Provider() {}
46 virtual ~Provider() {}
48 // A human-readable name for this provider, for use in debug messages.
49 // Implementers should return an empty string in non-debug builds, to save
50 // executable size.
51 virtual std::string GetDebugPolicyProviderName() const = 0;
53 // Providers should return false if a user may not install the |extension|,
54 // or load or run it if it has already been installed.
55 // TODO(treib,pam): The method name is misleading, since this applies to all
56 // extension installations, not just user-initiated ones. Fix either the
57 // name or the semantics. crbug.com/461747
58 virtual bool UserMayLoad(const Extension* extension,
59 base::string16* error) const;
61 // Providers should return false if a user may not enable, disable, or
62 // uninstall the |extension|, or change its usage options (incognito
63 // permission, file access, etc.).
64 // TODO(treib,pam): The method name is misleading, since this applies to all
65 // setting modifications, not just user-initiated ones. Fix either the
66 // name or the semantics. crbug.com/461747
67 virtual bool UserMayModifySettings(const Extension* extension,
68 base::string16* error) const;
70 // Providers should return true if the |extension| must always remain
71 // enabled. This is distinct from UserMayModifySettings() in that the latter
72 // also prohibits enabling the extension if it is currently disabled.
73 // Providers implementing this method should also implement the others
74 // above, if they wish to completely lock in an extension.
75 virtual bool MustRemainEnabled(const Extension* extension,
76 base::string16* error) const;
78 // Similar to MustRemainEnabled, but for whether an extension must remain
79 // disabled, and returns an error and/or reason if the caller needs it.
80 virtual bool MustRemainDisabled(const Extension* extension,
81 Extension::DisableReason* reason,
82 base::string16* error) const;
84 // Similar to MustRemainEnabled, but for whether an extension must remain
85 // installed, and returns an error and/or reason if the caller needs it.
86 virtual bool MustRemainInstalled(const Extension* extension,
87 base::string16* error) const;
89 private:
90 DISALLOW_COPY_AND_ASSIGN(Provider);
93 ManagementPolicy();
94 ~ManagementPolicy();
96 // Registers or unregisters a provider, causing it to be added to or removed
97 // from the list of providers queried. Ownership of the provider remains with
98 // the caller. Providers do not need to be unregistered on shutdown.
99 void RegisterProvider(Provider* provider);
100 void UnregisterProvider(Provider* provider);
102 // Like RegisterProvider(), but registers multiple providers instead.
103 void RegisterProviders(std::vector<Provider*> providers);
105 // Returns true if the user is permitted to install, load, and run the given
106 // extension. If not, |error| may be set to an appropriate message.
107 // TODO(treib,pam): Misleading name; see comment in Provider. crbug.com/461747
108 bool UserMayLoad(const Extension* extension, base::string16* error) const;
110 // Returns true if the user is permitted to enable, disable, or uninstall the
111 // given extension, or change the extension's usage options (incognito mode,
112 // file access, etc.). If not, |error| may be set to an appropriate message.
113 // TODO(treib,pam): Misleading name; see comment in Provider. crbug.com/461747
114 bool UserMayModifySettings(const Extension* extension,
115 base::string16* error) const;
117 // Returns true if the extension must remain enabled at all times (e.g. a
118 // component extension). In that case, |error| may be set to an appropriate
119 // message.
120 bool MustRemainEnabled(const Extension* extension,
121 base::string16* error) const;
123 // Returns true immediately if any registered provider's MustRemainDisabled
124 // function returns true.
125 bool MustRemainDisabled(const Extension* extension,
126 Extension::DisableReason* reason,
127 base::string16* error) const;
129 // Returns true immediately if any registered provider's MustRemainInstalled
130 // function returns true.
131 bool MustRemainInstalled(const Extension* extension,
132 base::string16* error) const;
134 // For use in testing.
135 void UnregisterAllProviders();
136 int GetNumProviders() const;
138 private:
139 // This is a pointer to a function in the Provider interface, used in
140 // ApplyToProviderList.
141 typedef bool (Provider::*ProviderFunction)(const Extension*,
142 base::string16*) const;
144 typedef std::set<Provider*> ProviderList;
146 // This is a helper to apply a method in the Provider interface to each of
147 // the Provider objects in |providers_|. The return value of this function
148 // will be |normal_result|, unless any of the Provider calls to |function|
149 // return !normal_result, in which case this function will then early-return
150 // !normal_result.
151 bool ApplyToProviderList(ProviderFunction function,
152 const char* debug_operation_name,
153 bool normal_result,
154 const Extension* extension,
155 base::string16* error) const;
157 ProviderList providers_;
159 DISALLOW_COPY_AND_ASSIGN(ManagementPolicy);
162 } // namespace extensions
164 #endif // EXTENSIONS_BROWSER_MANAGEMENT_POLICY_H_