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 #include "extensions/browser/api/storage/settings_test_util.h"
7 #include "base/files/file_path.h"
8 #include "extensions/browser/api/storage/storage_frontend.h"
9 #include "extensions/browser/extension_registry.h"
10 #include "extensions/browser/extension_system_provider.h"
11 #include "extensions/browser/extensions_browser_client.h"
12 #include "extensions/common/extension.h"
13 #include "extensions/common/permissions/permissions_data.h"
15 namespace extensions
{
17 namespace settings_test_util
{
19 // Creates a kilobyte of data.
20 scoped_ptr
<base::Value
> CreateKilobyte() {
21 std::string kilobyte_string
;
22 for (int i
= 0; i
< 1024; ++i
) {
23 kilobyte_string
+= "a";
25 return scoped_ptr
<base::Value
>(new base::StringValue(kilobyte_string
));
28 // Creates a megabyte of data.
29 scoped_ptr
<base::Value
> CreateMegabyte() {
30 base::ListValue
* megabyte
= new base::ListValue();
31 for (int i
= 0; i
< 1000; ++i
) {
32 megabyte
->Append(CreateKilobyte().release());
34 return scoped_ptr
<base::Value
>(megabyte
);
37 // Intended as a StorageCallback from GetStorage.
38 static void AssignStorage(ValueStore
** dst
, ValueStore
* src
) {
42 ValueStore
* GetStorage(scoped_refptr
<const Extension
> extension
,
43 settings_namespace::Namespace settings_namespace
,
44 StorageFrontend
* frontend
) {
45 ValueStore
* storage
= NULL
;
46 frontend
->RunWithStorage(
47 extension
, settings_namespace
, base::Bind(&AssignStorage
, &storage
));
48 base::MessageLoop::current()->RunUntilIdle();
52 ValueStore
* GetStorage(scoped_refptr
<const Extension
> extension
,
53 StorageFrontend
* frontend
) {
54 return GetStorage(extension
, settings_namespace::SYNC
, frontend
);
57 scoped_refptr
<const Extension
> AddExtensionWithId(
58 content::BrowserContext
* context
,
59 const std::string
& id
,
60 Manifest::Type type
) {
61 return AddExtensionWithIdAndPermissions(
62 context
, id
, type
, std::set
<std::string
>());
65 scoped_refptr
<const Extension
> AddExtensionWithIdAndPermissions(
66 content::BrowserContext
* context
,
67 const std::string
& id
,
69 const std::set
<std::string
>& permissions_set
) {
70 base::DictionaryValue manifest
;
71 manifest
.SetString("name", std::string("Test extension ") + id
);
72 manifest
.SetString("version", "1.0");
74 scoped_ptr
<base::ListValue
> permissions(new base::ListValue());
75 for (std::set
<std::string
>::const_iterator it
= permissions_set
.begin();
76 it
!= permissions_set
.end(); ++it
) {
77 permissions
->Append(new base::StringValue(*it
));
79 manifest
.Set("permissions", permissions
.release());
82 case Manifest::TYPE_EXTENSION
:
85 case Manifest::TYPE_LEGACY_PACKAGED_APP
: {
86 base::DictionaryValue
* app
= new base::DictionaryValue();
87 base::DictionaryValue
* app_launch
= new base::DictionaryValue();
88 app_launch
->SetString("local_path", "fake.html");
89 app
->Set("launch", app_launch
);
90 manifest
.Set("app", app
);
99 scoped_refptr
<const Extension
> extension(
100 Extension::Create(base::FilePath(),
106 DCHECK(extension
.get());
107 DCHECK(error
.empty());
109 // Ensure lookups via ExtensionRegistry (and ExtensionService) work even if
110 // the test discards the referenced to the returned extension.
111 ExtensionRegistry::Get(context
)->AddEnabled(extension
);
113 for (std::set
<std::string
>::const_iterator it
= permissions_set
.begin();
114 it
!= permissions_set
.end(); ++it
) {
115 DCHECK(extension
->permissions_data()->HasAPIPermission(*it
));
121 // ScopedSettingsFactory
123 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {}
125 ScopedSettingsStorageFactory::ScopedSettingsStorageFactory(
126 const scoped_refptr
<SettingsStorageFactory
>& delegate
)
127 : delegate_(delegate
) {}
129 ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {}
131 void ScopedSettingsStorageFactory::Reset(
132 const scoped_refptr
<SettingsStorageFactory
>& delegate
) {
133 delegate_
= delegate
;
136 ValueStore
* ScopedSettingsStorageFactory::Create(
137 const base::FilePath
& base_path
,
138 const std::string
& extension_id
) {
139 DCHECK(delegate_
.get());
140 return delegate_
->Create(base_path
, extension_id
);
143 void ScopedSettingsStorageFactory::DeleteDatabaseIfExists(
144 const base::FilePath
& base_path
,
145 const std::string
& extension_id
) {
146 delegate_
->DeleteDatabaseIfExists(base_path
, extension_id
);
149 } // namespace settings_test_util
151 } // namespace extensions