Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / management_policy.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_BROWSER_MANAGEMENT_POLICY_H_
6 #define EXTENSIONS_BROWSER_MANAGEMENT_POLICY_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/basictypes.h"
13 #include "extensions/common/extension.h"
14
15 namespace extensions {
16
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.)
23 //
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.
34   //
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.
39   //
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() {}
47
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;
52
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     virtual bool UserMayLoad(const Extension* extension,
56                              base::string16* error) const;
57
58     // Providers should return false if a user may not enable, disable, or
59     // uninstall the |extension|, or change its usage options (incognito
60     // permission, file access, etc.).
61     virtual bool UserMayModifySettings(const Extension* extension,
62                                        base::string16* error) const;
63
64     // Providers should return true if the |extension| must always remain
65     // enabled. This is distinct from UserMayModifySettings() in that the latter
66     // also prohibits enabling the extension if it is currently disabled.
67     // Providers implementing this method should also implement the others
68     // above, if they wish to completely lock in an extension.
69     virtual bool MustRemainEnabled(const Extension* extension,
70                                    base::string16* error) const;
71
72     // Similar to MustRemainEnabled, but for whether an extension must remain
73     // disabled, and returns an error and/or reason if the caller needs it.
74     virtual bool MustRemainDisabled(const Extension* extension,
75                                     Extension::DisableReason* reason,
76                                     base::string16* error) const;
77
78     // Similar to MustRemainEnabled, but for whether an extension must remain
79     // installed, and returns an error and/or reason if the caller needs it.
80     virtual bool MustRemainInstalled(const Extension* extension,
81                                      base::string16* error) const;
82
83    private:
84     DISALLOW_COPY_AND_ASSIGN(Provider);
85   };
86
87   ManagementPolicy();
88   ~ManagementPolicy();
89
90   // Registers or unregisters a provider, causing it to be added to or removed
91   // from the list of providers queried. Ownership of the provider remains with
92   // the caller. Providers do not need to be unregistered on shutdown.
93   void RegisterProvider(Provider* provider);
94   void UnregisterProvider(Provider* provider);
95
96   // Like RegisterProvider(), but registers multiple providers instead.
97   void RegisterProviders(std::vector<Provider*> providers);
98
99   // Returns true if the user is permitted to install, load, and run the given
100   // extension. If not, |error| may be set to an appropriate message.
101   bool UserMayLoad(const Extension* extension, base::string16* error) const;
102
103   // Returns true if the user is permitted to enable, disable, or uninstall the
104   // given extension, or change the extension's usage options (incognito mode,
105   // file access, etc.). If not, |error| may be set to an appropriate message.
106   bool UserMayModifySettings(const Extension* extension,
107                              base::string16* error) const;
108
109   // Returns true if the extension must remain enabled at all times (e.g. a
110   // compoment extension). In that case, |error| may be set to an appropriate
111   // message.
112   bool MustRemainEnabled(const Extension* extension,
113                          base::string16* error) const;
114
115   // Returns true immediately if any registered provider's MustRemainDisabled
116   // function returns true.
117   bool MustRemainDisabled(const Extension* extension,
118                           Extension::DisableReason* reason,
119                           base::string16* error) const;
120
121   // Returns true immediately if any registered provider's MustRemainInstalled
122   // function returns true.
123   bool MustRemainInstalled(const Extension* extension,
124                            base::string16* error) const;
125
126   // For use in testing.
127   void UnregisterAllProviders();
128   int GetNumProviders() const;
129
130  private:
131   // This is a pointer to a function in the Provider interface, used in
132   // ApplyToProviderList.
133   typedef bool (Provider::*ProviderFunction)(const Extension*,
134                                              base::string16*) const;
135
136   typedef std::set<Provider*> ProviderList;
137
138   // This is a helper to apply a method in the Provider interface to each of
139   // the Provider objects in |providers_|. The return value of this function
140   // will be |normal_result|, unless any of the Provider calls to |function|
141   // return !normal_result, in which case this function will then early-return
142   // !normal_result.
143   bool ApplyToProviderList(ProviderFunction function,
144                            const char* debug_operation_name,
145                            bool normal_result,
146                            const Extension* extension,
147                            base::string16* error) const;
148
149   ProviderList providers_;
150
151   DISALLOW_COPY_AND_ASSIGN(ManagementPolicy);
152 };
153
154 }  // namespace extensions
155
156 #endif  // EXTENSIONS_BROWSER_MANAGEMENT_POLICY_H_