summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/storage/settings_test_util.cc
blob: 82c1845fb82b63307cf2020aacd7c2858499cdcf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/storage/settings_test_util.h"

#include "base/files/file_path.h"
#include "chrome/browser/extensions/api/storage/settings_frontend.h"
#include "chrome/browser/extensions/extension_system_factory.h"
#include "chrome/common/extensions/extension.h"
#include "chrome/common/extensions/permissions/permissions_data.h"

namespace extensions {

namespace settings_test_util {

// Intended as a StorageCallback from GetStorage.
static void AssignStorage(ValueStore** dst, ValueStore* src) {
  *dst = src;
}

ValueStore* GetStorage(
    const std::string& extension_id,
    settings_namespace::Namespace settings_namespace,
    SettingsFrontend* frontend) {
  ValueStore* storage = NULL;
  frontend->RunWithStorage(
      extension_id,
      settings_namespace,
      base::Bind(&AssignStorage, &storage));
  base::MessageLoop::current()->RunUntilIdle();
  return storage;
}

ValueStore* GetStorage(
    const std::string& extension_id, SettingsFrontend* frontend) {
  return GetStorage(extension_id, settings_namespace::SYNC, frontend);
}

// MockExtensionService

MockExtensionService::MockExtensionService() {}

MockExtensionService::~MockExtensionService() {}

const Extension* MockExtensionService::GetExtensionById(
    const std::string& id, bool include_disabled) const {
  std::map<std::string, scoped_refptr<Extension> >::const_iterator
      maybe_extension = extensions_.find(id);
  return maybe_extension == extensions_.end() ?
      NULL : maybe_extension->second.get();
}

void MockExtensionService::AddExtensionWithId(
    const std::string& id, Manifest::Type type) {
  std::set<std::string> empty_permissions;
  AddExtensionWithIdAndPermissions(id, type, empty_permissions);
}

void MockExtensionService::AddExtensionWithIdAndPermissions(
    const std::string& id,
    Manifest::Type type,
    const std::set<std::string>& permissions_set) {
  base::DictionaryValue manifest;
  manifest.SetString("name", std::string("Test extension ") + id);
  manifest.SetString("version", "1.0");

  scoped_ptr<base::ListValue> permissions(new base::ListValue());
  for (std::set<std::string>::const_iterator it = permissions_set.begin();
      it != permissions_set.end(); ++it) {
    permissions->Append(new base::StringValue(*it));
  }
  manifest.Set("permissions", permissions.release());

  switch (type) {
    case Manifest::TYPE_EXTENSION:
      break;

    case Manifest::TYPE_LEGACY_PACKAGED_APP: {
      base::DictionaryValue* app = new base::DictionaryValue();
      base::DictionaryValue* app_launch = new base::DictionaryValue();
      app_launch->SetString("local_path", "fake.html");
      app->Set("launch", app_launch);
      manifest.Set("app", app);
      break;
    }

    default:
      NOTREACHED();
  }

  std::string error;
  scoped_refptr<Extension> extension(Extension::Create(
      base::FilePath(),
      Manifest::INTERNAL,
      manifest,
      Extension::NO_FLAGS,
      id,
      &error));
  DCHECK(extension.get());
  DCHECK(error.empty());
  extensions_[id] = extension;

  for (std::set<std::string>::const_iterator it = permissions_set.begin();
      it != permissions_set.end(); ++it) {
    DCHECK(extension->HasAPIPermission(*it));
  }
}

// MockExtensionSystem

MockExtensionSystem::MockExtensionSystem(Profile* profile)
      : TestExtensionSystem(profile) {}
MockExtensionSystem::~MockExtensionSystem() {}

EventRouter* MockExtensionSystem::event_router() {
  if (!event_router_.get())
    event_router_.reset(new EventRouter(profile_, NULL));
  return event_router_.get();
}

ExtensionService* MockExtensionSystem::extension_service() {
  ExtensionServiceInterface* as_interface =
      static_cast<ExtensionServiceInterface*>(&extension_service_);
  return static_cast<ExtensionService*>(as_interface);
}

BrowserContextKeyedService* BuildMockExtensionSystem(
    content::BrowserContext* profile) {
  return new MockExtensionSystem(static_cast<Profile*>(profile));
}

// MockProfile

MockProfile::MockProfile(const base::FilePath& file_path)
    : TestingProfile(file_path) {
  ExtensionSystemFactory::GetInstance()->SetTestingFactoryAndUse(this,
      &BuildMockExtensionSystem);
}

MockProfile::~MockProfile() {}

// ScopedSettingsFactory

ScopedSettingsStorageFactory::ScopedSettingsStorageFactory() {}

ScopedSettingsStorageFactory::ScopedSettingsStorageFactory(
    const scoped_refptr<SettingsStorageFactory>& delegate)
    : delegate_(delegate) {}

ScopedSettingsStorageFactory::~ScopedSettingsStorageFactory() {}

void ScopedSettingsStorageFactory::Reset(
    const scoped_refptr<SettingsStorageFactory>& delegate) {
  delegate_ = delegate;
}

ValueStore* ScopedSettingsStorageFactory::Create(
    const base::FilePath& base_path,
    const std::string& extension_id) {
  DCHECK(delegate_.get());
  return delegate_->Create(base_path, extension_id);
}

}  // namespace settings_test_util

}  // namespace extensions