• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
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 
17 namespace extensions {
18 
19 // Base class for essential routines on preference manipulation.
20 class ExtensionManagementPrefUpdaterBase {
21  public:
22   ExtensionManagementPrefUpdaterBase();
23   virtual ~ExtensionManagementPrefUpdaterBase();
24 
25   // Helper functions for per extension settings.
26   void UnsetPerExtensionSettings(const ExtensionId& id);
27   void ClearPerExtensionSettings(const ExtensionId& id);
28 
29   // Helper functions for 'installation_mode' manipulation.
30   void SetBlacklistedByDefault(bool value);
31   void ClearInstallationModesForIndividualExtensions();
32   void SetIndividualExtensionInstallationAllowed(const ExtensionId& id,
33                                                  bool allowed);
34   void SetIndividualExtensionAutoInstalled(const ExtensionId& id,
35                                            const std::string& update_url,
36                                            bool forced);
37 
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);
43 
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);
49 
50   // Expose a read-only preference to user.
51   const base::DictionaryValue* GetPref();
52 
53  protected:
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);
58 
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();
63 
64  private:
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);
69 
70   scoped_ptr<base::DictionaryValue> pref_;
71 
72   DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdaterBase);
73 };
74 
75 // A helper class to manipulate the extension management preference in unit
76 // tests.
77 template <class TestingPrefService>
78 class ExtensionManagementPrefUpdater
79     : public ExtensionManagementPrefUpdaterBase {
80  public:
ExtensionManagementPrefUpdater(TestingPrefService * service)81   explicit ExtensionManagementPrefUpdater(TestingPrefService* service)
82       : service_(service) {
83     const base::Value* pref_value =
84         service_->GetManagedPref(pref_names::kExtensionManagement);
85     if (pref_value) {
86       const base::DictionaryValue* dict_value = NULL;
87       pref_value->GetAsDictionary(&dict_value);
88       SetPref(dict_value->DeepCopy());
89     } else {
90       SetPref(new base::DictionaryValue);
91     }
92   }
93 
~ExtensionManagementPrefUpdater()94   virtual ~ExtensionManagementPrefUpdater() {
95     service_->SetManagedPref(pref_names::kExtensionManagement,
96                              TakePref().release());
97   }
98 
99  private:
100   TestingPrefService* service_;
101 
102   DISALLOW_COPY_AND_ASSIGN(ExtensionManagementPrefUpdater);
103 };
104 
105 }  // namespace extensions
106 
107 #endif  // CHROME_BROWSER_EXTENSIONS_EXTENSION_MANAGEMENT_TEST_UTIL_H_
108