blob: 1a347833abfc29e960f8387a861ba9fd11de7f81 [file] [log] [blame]
binjinb2454382014-09-22 15:17:431// 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.
4
5#ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
6#define CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
7
8#include <string>
9
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"
16
binjine6b58b52014-10-31 01:55:5717namespace policy {
18class MockConfigurationPolicyProvider;
19class PolicyBundle;
20} // namespace policy
21
binjinb2454382014-09-22 15:17:4322namespace extensions {
23
24// Base class for essential routines on preference manipulation.
25class ExtensionManagementPrefUpdaterBase {
26 public:
27 ExtensionManagementPrefUpdaterBase();
28 virtual ~ExtensionManagementPrefUpdaterBase();
29
binjin6a6eb152014-09-24 18:36:3430 // Helper functions for per extension settings.
31 void UnsetPerExtensionSettings(const ExtensionId& id);
32 void ClearPerExtensionSettings(const ExtensionId& id);
33
binjinb2454382014-09-22 15:17:4334 // 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);
42
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);
48
49 // Helper functions for 'allowed_types' manipulation.
50 void UnsetAllowedTypes();
51 void ClearAllowedTypes();
52 void AddAllowedType(const std::string& allowed_type);
binjine6b58b52014-10-31 01:55:5753 void RemoveAllowedType(const std::string& allowed_type);
54
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);
63
64 // Helper functions for 'allowed_permissions' manipulation. |id| must be a
binjin8e3d0182014-12-04 16:44:2865 // valid extension ID.
binjine6b58b52014-10-31 01:55:5766 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);
binjinb2454382014-09-22 15:17:4372
binjin8e3d0182014-12-04 16:44:2873 // 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);
78
binjinb2454382014-09-22 15:17:4379 // Expose a read-only preference to user.
80 const base::DictionaryValue* GetPref();
81
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);
87
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();
92
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);
98
99 scoped_ptr<base::DictionaryValue> pref_;
100
101 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase);
102};
103
104// A helper class to manipulate the extension management preference in unit
105// tests.
106template <class TestingPrefService>
107class 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);
binjine6b58b52014-10-31 01:55:57114 const base::DictionaryValue* dict_value = nullptr;
115 if (pref_value && pref_value->GetAsDictionary(&dict_value))
binjinb2454382014-09-22 15:17:43116 SetPref(dict_value->DeepCopy());
binjine6b58b52014-10-31 01:55:57117 else
binjinb2454382014-09-22 15:17:43118 SetPref(new base::DictionaryValue);
binjinb2454382014-09-22 15:17:43119 }
120
121 virtual ~ExtensionManagementPrefUpdater() {
122 service_->SetManagedPref(pref_names::kExtensionManagement,
123 TakePref().release());
124 }
125
126 private:
127 TestingPrefService* service_;
128
129 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater);
130};
131
binjine6b58b52014-10-31 01:55:57132// A helper class to manipulate the extension management policy in browser
133// tests.
134class ExtensionManagementPolicyUpdater
135 : public ExtensionManagementPrefUpdaterBase {
136 public:
137 explicit ExtensionManagementPolicyUpdater(
138 policy::MockConfigurationPolicyProvider* provider);
139 ~ExtensionManagementPolicyUpdater() override;
140
141 private:
142 policy::MockConfigurationPolicyProvider* provider_;
143 scoped_ptr<policy::PolicyBundle> policies_;
144
145 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPolicyUpdater);
146};
147
binjinb2454382014-09-22 15:17:43148} // namespace extensions
149
150#endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_