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_
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 extensions
{
19 // Base class for essential routines on preference manipulation.
20 class ExtensionManagementPrefUpdaterBase
{
22 ExtensionManagementPrefUpdaterBase();
23 virtual ~ExtensionManagementPrefUpdaterBase();
25 // Helper functions for per extension settings.
26 void UnsetPerExtensionSettings(const ExtensionId
& id
);
27 void ClearPerExtensionSettings(const ExtensionId
& id
);
29 // Helper functions for 'installation_mode' manipulation.
30 void SetBlacklistedByDefault(bool value
);
31 void ClearInstallationModesForIndividualExtensions();
32 void SetIndividualExtensionInstallationAllowed(const ExtensionId
& id
,
34 void SetIndividualExtensionAutoInstalled(const ExtensionId
& id
,
35 const std::string
& update_url
,
38 // Helper functions for 'install_sources' manipulation.
39 void UnsetInstallSources();
40 void ClearInstallSources();
41 void AddInstallSource(const std::string
& install_source
);
42 void RemoveInstallSource(const std::string
& install_source
);
44 // Helper functions for 'allowed_types' manipulation.
45 void UnsetAllowedTypes();
46 void ClearAllowedTypes();
47 void AddAllowedType(const std::string
& allowed_type
);
48 void RemoveAllowedType(const std::string
& allowd_type
);
50 // Expose a read-only preference to user.
51 const base::DictionaryValue
* GetPref();
54 // Set the preference with |pref|, pass the ownership of it as well.
55 // This function must be called before accessing publicly exposed functions,
56 // for example in constructor of subclass.
57 void SetPref(base::DictionaryValue
* pref
);
59 // Take the preference. Caller takes ownership of it as well.
60 // This function must be called after accessing publicly exposed functions,
61 // for example in destructor of subclass.
62 scoped_ptr
<base::DictionaryValue
> TakePref();
65 // Helper functions for manipulating sub properties like list of strings.
66 void ClearList(const std::string
& path
);
67 void AddStringToList(const std::string
& path
, const std::string
& str
);
68 void RemoveStringFromList(const std::string
& path
, const std::string
& str
);
70 scoped_ptr
<base::DictionaryValue
> pref_
;
72 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase
);
75 // A helper class to manipulate the extension management preference in unit
77 template <class TestingPrefService
>
78 class ExtensionManagementPrefUpdater
79 : public ExtensionManagementPrefUpdaterBase
{
81 explicit ExtensionManagementPrefUpdater(TestingPrefService
* service
)
83 const base::Value
* pref_value
=
84 service_
->GetManagedPref(pref_names::kExtensionManagement
);
86 const base::DictionaryValue
* dict_value
= NULL
;
87 pref_value
->GetAsDictionary(&dict_value
);
88 SetPref(dict_value
->DeepCopy());
90 SetPref(new base::DictionaryValue
);
94 virtual ~ExtensionManagementPrefUpdater() {
95 service_
->SetManagedPref(pref_names::kExtensionManagement
,
96 TakePref().release());
100 TestingPrefService
* service_
;
102 DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater
);
105 } // namespace extensions
107 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_