Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / extension_management_test_util.h
blob1a347833abfc29e960f8387a861ba9fd11de7f81
1 // Copyright 2014 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 CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
8 #include <string>
10 #include "base/macros.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/values.h"
13 #include "chrome/browser/extensions/extension_management_constants.h"
14 #include "extensions/browser/pref_names.h"
15 #include "extensions/common/extension.h"
17 namespace policy {
18 class MockConfigurationPolicyProvider;
19 class PolicyBundle;
20 } // namespace policy
22 namespace extensions {
24 // Base class for essential routines on preference manipulation.
25 class ExtensionManagementPrefUpdaterBase {
26 public:
27 ExtensionManagementPrefUpdaterBase();
28 virtual ~ExtensionManagementPrefUpdaterBase();
30 // Helper functions for per extension settings.
31 void UnsetPerExtensionSettings(const ExtensionId& id);
32 void ClearPerExtensionSettings(const ExtensionId& id);
34 // Helper functions for 'installation_mode' manipulation.
35 void SetBlacklistedByDefault(bool value);
36 void ClearInstallationModesForIndividualExtensions();
37 void SetIndividualExtensionInstallationAllowed(const ExtensionId& id,
38 bool allowed);
39 void SetIndividualExtensionAutoInstalled(const ExtensionId& id,
40 const std::string& update_url,
41 bool forced);
43 // Helper functions for 'install_sources' manipulation.
44 void UnsetInstallSources();
45 void ClearInstallSources();
46 void AddInstallSource(const std::string& install_source);
47 void RemoveInstallSource(const std::string& install_source);
49 // Helper functions for 'allowed_types' manipulation.
50 void UnsetAllowedTypes();
51 void ClearAllowedTypes();
52 void AddAllowedType(const std::string& allowed_type);
53 void RemoveAllowedType(const std::string& allowed_type);
55 // Helper functions for 'blocked_permissions' manipulation. |prefix| can be
56 // kWildCard or a valid extension ID.
57 void UnsetBlockedPermissions(const std::string& prefix);
58 void ClearBlockedPermissions(const std::string& prefix);
59 void AddBlockedPermission(const std::string& prefix,
60 const std::string& permission);
61 void RemoveBlockedPermission(const std::string& prefix,
62 const std::string& permission);
64 // Helper functions for 'allowed_permissions' manipulation. |id| must be a
65 // valid extension ID.
66 void UnsetAllowedPermissions(const std::string& id);
67 void ClearAllowedPermissions(const std::string& id);
68 void AddAllowedPermission(const std::string& id,
69 const std::string& permission);
70 void RemoveAllowedPermission(const std::string& id,
71 const std::string& permission);
73 // Helper functions for 'minimum_version_required' manipulation. |id| must be
74 // a valid extension ID.
75 void SetMinimumVersionRequired(const std::string& id,
76 const std::string& version);
77 void UnsetMinimumVersionRequired(const std::string& id);
79 // Expose a read-only preference to user.
80 const base::DictionaryValue* GetPref();
82 protected:
83 // Set the preference with |pref|, pass the ownership of it as well.
84 // This function must be called before accessing publicly exposed functions,
85 // for example in constructor of subclass.
86 void SetPref(base::DictionaryValue* pref);
88 // Take the preference. Caller takes ownership of it as well.
89 // This function must be called after accessing publicly exposed functions,
90 // for example in destructor of subclass.
91 scoped_ptr<base::DictionaryValue> TakePref();
93 private:
94 // Helper functions for manipulating sub properties like list of strings.
95 void ClearList(const std::string& path);
96 void AddStringToList(const std::string& path, const std::string& str);
97 void RemoveStringFromList(const std::string& path, const std::string& str);
99 scoped_ptr<base::DictionaryValue> pref_;
101 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase);
104 // A helper class to manipulate the extension management preference in unit
105 // tests.
106 template <class TestingPrefService>
107 class ExtensionManagementPrefUpdater
108 : public ExtensionManagementPrefUpdaterBase {
109 public:
110 explicit ExtensionManagementPrefUpdater(TestingPrefService* service)
111 : service_(service) {
112 const base::Value* pref_value =
113 service_->GetManagedPref(pref_names::kExtensionManagement);
114 const base::DictionaryValue* dict_value = nullptr;
115 if (pref_value && pref_value->GetAsDictionary(&dict_value))
116 SetPref(dict_value->DeepCopy());
117 else
118 SetPref(new base::DictionaryValue);
121 virtual ~ExtensionManagementPrefUpdater() {
122 service_->SetManagedPref(pref_names::kExtensionManagement,
123 TakePref().release());
126 private:
127 TestingPrefService* service_;
129 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater);
132 // A helper class to manipulate the extension management policy in browser
133 // tests.
134 class ExtensionManagementPolicyUpdater
135 : public ExtensionManagementPrefUpdaterBase {
136 public:
137 explicit ExtensionManagementPolicyUpdater(
138 policy::MockConfigurationPolicyProvider* provider);
139 ~ExtensionManagementPolicyUpdater() override;
141 private:
142 policy::MockConfigurationPolicyProvider* provider_;
143 scoped_ptr<policy::PolicyBundle> policies_;
145 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPolicyUpdater);
148 } // namespace extensions
150 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_