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#include "extensions/browser/api/storage/settings_test_util.h"
6
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"
14
15namespace extensions {
16
17namespace settings_test_util {
18
19// Creates a kilobyte of data.
20scoped_ptr<base::Value> CreateKilobyte() {
21  std::string kilobyte_string;
22  for (int i = 0; i < 1024; ++i) {
23    kilobyte_string += "a";
24  }
25  return scoped_ptr<base::Value>(new base::StringValue(kilobyte_string));
26}
27
28// Creates a megabyte of data.
29scoped_ptr<base::Value> CreateMegabyte() {
30  base::ListValue* megabyte = new base::ListValue();
31  for (int i = 0; i < 1000; ++i) {
32    megabyte->Append(CreateKilobyte().release());
33  }
34  return scoped_ptr<base::Value>(megabyte);
35}
36
37// Intended as a StorageCallback from GetStorage.
38static void AssignStorage(ValueStore** dst, ValueStore* src) {
39  *dst = src;
40}
41
42ValueStore* 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();
49  return storage;
50}
51
52ValueStore* GetStorage(scoped_refptr<const Extension> extension,
53                       StorageFrontend* frontend) {
54  return GetStorage(extension, settings_namespace::SYNC, frontend);
55}
56
57scoped_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>());
63}
64
65scoped_refptr<const Extension> AddExtensionWithIdAndPermissions(
66    content::BrowserContext* context,
67    const std::string& id,
68    Manifest::Type type,
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");
73
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));
78  }
79  manifest.Set("permissions", permissions.release());
80
81  switch (type) {
82    case Manifest::TYPE_EXTENSION:
83      break;
84
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);
91      break;
92    }
93
94    default:
95      NOTREACHED();
96  }
97
98  std::string error;
99  scoped_refptr<const Extension> extension(
100      Extension::Create(base::FilePath(),
101                        Manifest::INTERNAL,
102                        manifest,
103                        Extension::NO_FLAGS,
104                        id,
105                        &error));
106  DCHECK(extension.get());
107  DCHECK(error.empty());
108
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);
112
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));
116  }
117
118  return extension;
119}
120
121// MockExtensionSystemWithEventRouter
122
123MockExtensionSystemWithEventRouter::MockExtensionSystemWithEventRouter(
124    content::BrowserContext* context)
125    : MockExtensionSystem(context) {
126}
127
128MockExtensionSystemWithEventRouter::~MockExtensionSystemWithEventRouter() {
129}
130
131KeyedService* MockExtensionSystemWithEventRouter::Build(
132    content::BrowserContext* context) {
133  return new MockExtensionSystemWithEventRouter(context);
134}
135
136EventRouter* MockExtensionSystemWithEventRouter::event_router() {
137  if (!event_router_.get())
138    event_router_.reset(new EventRouter(browser_context(), NULL));
139  return event_router_.get();
140}
141
142// ScopedSettingsFactory
143
144ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {}
145
146ScopedSettingsStorageFactory::ScopedSettingsStorageFactory(
147    const scoped_refptr<SettingsStorageFactory>& delegate)
148    : delegate_(delegate) {}
149
150ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {}
151
152void ScopedSettingsStorageFactory::Reset(
153    const scoped_refptr<SettingsStorageFactory>& delegate) {
154  delegate_ = delegate;
155}
156
157ValueStore* ScopedSettingsStorageFactory::Create(
158    const base::FilePath& base_path,
159    const std::string& extension_id) {
160  DCHECK(delegate_.get());
161  return delegate_->Create(base_path, extension_id);
162}
163
164void ScopedSettingsStorageFactory::DeleteDatabaseIfExists(
165    const base::FilePath& base_path,
166    const std::string& extension_id) {
167  delegate_->DeleteDatabaseIfExists(base_path, extension_id);
168}
169
170}  // namespace settings_test_util
171
172}  // namespace extensions
173