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