OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 #include "chrome/browser/extensions/api/storage/settings_test_util.h" | |
6 | |
7 #include "base/files/file_path.h" | |
8 #include "chrome/browser/extensions/api/storage/settings_frontend.h" | |
9 #include "chrome/browser/extensions/extension_system_factory.h" | |
10 #include "extensions/browser/extension_registry.h" | |
11 #include "extensions/common/extension.h" | |
12 #include "extensions/common/permissions/permissions_data.h" | |
13 | |
14 namespace extensions { | |
15 | |
16 namespace settings_test_util { | |
17 | |
18 // Intended as a StorageCallback from GetStorage. | |
19 static void AssignStorage(ValueStore** dst, ValueStore* src) { | |
20 *dst = src; | |
21 } | |
22 | |
23 ValueStore* GetStorage(scoped_refptr<const Extension> extension, | |
24 settings_namespace::Namespace settings_namespace, | |
25 SettingsFrontend* frontend) { | |
26 ValueStore* storage = NULL; | |
27 frontend->RunWithStorage( | |
28 extension, settings_namespace, base::Bind(&AssignStorage, &storage)); | |
29 base::MessageLoop::current()->RunUntilIdle(); | |
30 return storage; | |
31 } | |
32 | |
33 ValueStore* GetStorage(scoped_refptr<const Extension> extension, | |
34 SettingsFrontend* frontend) { | |
35 return GetStorage(extension, settings_namespace::SYNC, frontend); | |
36 } | |
37 | |
38 scoped_refptr<const Extension> AddExtensionWithId(Profile* profile, | |
39 const std::string& id, | |
40 Manifest::Type type) { | |
41 return AddExtensionWithIdAndPermissions( | |
42 profile, id, type, std::set<std::string>()); | |
43 } | |
44 | |
45 scoped_refptr<const Extension> AddExtensionWithIdAndPermissions( | |
46 Profile* profile, | |
47 const std::string& id, | |
48 Manifest::Type type, | |
49 const std::set<std::string>& permissions_set) { | |
50 base::DictionaryValue manifest; | |
51 manifest.SetString("name", std::string("Test extension ") + id); | |
52 manifest.SetString("version", "1.0"); | |
53 | |
54 scoped_ptr<base::ListValue> permissions(new base::ListValue()); | |
55 for (std::set<std::string>::const_iterator it = permissions_set.begin(); | |
56 it != permissions_set.end(); ++it) { | |
57 permissions->Append(new base::StringValue(*it)); | |
58 } | |
59 manifest.Set("permissions", permissions.release()); | |
60 | |
61 switch (type) { | |
62 case Manifest::TYPE_EXTENSION: | |
63 break; | |
64 | |
65 case Manifest::TYPE_LEGACY_PACKAGED_APP: { | |
66 base::DictionaryValue* app = new base::DictionaryValue(); | |
67 base::DictionaryValue* app_launch = new base::DictionaryValue(); | |
68 app_launch->SetString("local_path", "fake.html"); | |
69 app->Set("launch", app_launch); | |
70 manifest.Set("app", app); | |
71 break; | |
72 } | |
73 | |
74 default: | |
75 NOTREACHED(); | |
76 } | |
77 | |
78 std::string error; | |
79 scoped_refptr<const Extension> extension( | |
80 Extension::Create(base::FilePath(), | |
81 Manifest::INTERNAL, | |
82 manifest, | |
83 Extension::NO_FLAGS, | |
84 id, | |
85 &error)); | |
86 DCHECK(extension.get()); | |
87 DCHECK(error.empty()); | |
88 | |
89 // Ensure lookups via ExtensionRegistry (and ExtensionService) work even if | |
90 // the test discards the referenced to the returned extension. | |
91 ExtensionRegistry::Get(profile)->AddEnabled(extension); | |
92 | |
93 for (std::set<std::string>::const_iterator it = permissions_set.begin(); | |
94 it != permissions_set.end(); ++it) { | |
95 DCHECK(extension->HasAPIPermission(*it)); | |
96 } | |
97 | |
98 return extension; | |
99 } | |
100 | |
101 // MockExtensionSystem | |
102 | |
103 MockExtensionSystem::MockExtensionSystem(Profile* profile) | |
104 : TestExtensionSystem(profile) {} | |
105 MockExtensionSystem::~MockExtensionSystem() {} | |
106 | |
107 EventRouter* MockExtensionSystem::event_router() { | |
108 if (!event_router_.get()) | |
109 event_router_.reset(new EventRouter(profile_, NULL)); | |
110 return event_router_.get(); | |
111 } | |
112 | |
113 ExtensionService* MockExtensionSystem::extension_service() { | |
114 ExtensionServiceInterface* as_interface = | |
115 static_cast<ExtensionServiceInterface*>(&extension_service_); | |
116 return static_cast<ExtensionService*>(as_interface); | |
117 } | |
118 | |
119 BrowserContextKeyedService* BuildMockExtensionSystem( | |
120 content::BrowserContext* profile) { | |
121 return new MockExtensionSystem(static_cast<Profile*>(profile)); | |
122 } | |
123 | |
124 // MockProfile | |
125 | |
126 MockProfile::MockProfile(const base::FilePath& file_path) | |
127 : TestingProfile(file_path) { | |
128 ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(this, | |
129 &BuildMockExtensionSystem); | |
130 } | |
131 | |
132 MockProfile::~MockProfile() {} | |
133 | |
134 // ScopedSettingsFactory | |
135 | |
136 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {} | |
137 | |
138 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory( | |
139 const scoped_refptr<SettingsStorageFactory>& delegate) | |
140 : delegate_(delegate) {} | |
141 | |
142 ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {} | |
143 | |
144 void ScopedSettingsStorageFactory::Reset( | |
145 const scoped_refptr<SettingsStorageFactory>& delegate) { | |
146 delegate_ = delegate; | |
147 } | |
148 | |
149 ValueStore* ScopedSettingsStorageFactory::Create( | |
150 const base::FilePath& base_path, | |
151 const std::string& extension_id) { | |
152 DCHECK(delegate_.get()); | |
153 return delegate_->Create(base_path, extension_id); | |
154 } | |
155 | |
156 void ScopedSettingsStorageFactory::DeleteDatabaseIfExists( | |
157 const base::FilePath& base_path, | |
158 const std::string& extension_id) { | |
159 delegate_->DeleteDatabaseIfExists(base_path, extension_id); | |
160 } | |
161 | |
162 } // namespace settings_test_util | |
163 | |
164 } // namespace extensions | |
OLD | NEW |