summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/settings/settings_frontend.cc
blob: 1d26b260d7b55ff7f46afc38edb4c8e8554a15c2 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// 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/settings/settings_frontend.h"

#include <limits>

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/file_path.h"
#include "base/json/json_reader.h"
#include "chrome/browser/extensions/event_names.h"
#include "chrome/browser/extensions/event_router.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/extensions/settings/leveldb_settings_storage_factory.h"
#include "chrome/browser/extensions/settings/settings_backend.h"
#include "chrome/browser/extensions/settings/sync_or_local_value_store_cache.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/storage.h"
#include "content/public/browser/browser_thread.h"

#if defined(ENABLE_CONFIGURATION_POLICY)
#include "chrome/browser/extensions/settings/managed_value_store_cache.h"
#endif

using content::BrowserThread;

namespace extensions {

namespace {

// Settings change Observer which forwards changes on to the extension
// processes for |profile| and its incognito partner if it exists.
class DefaultObserver : public SettingsObserver {
 public:
  explicit DefaultObserver(Profile* profile) : profile_(profile) {}

  // SettingsObserver implementation.
  virtual void OnSettingsChanged(
      const std::string& extension_id,
      settings_namespace::Namespace settings_namespace,
      const std::string& change_json) OVERRIDE {
    // TODO(gdk): This is a temporary hack while the refactoring for
    // string-based event payloads is removed. http://crbug.com/136045
    scoped_ptr<ListValue> args(new ListValue());
    args->Append(base::JSONReader::Read(change_json));
    args->Append(Value::CreateStringValue(settings_namespace::ToString(
        settings_namespace)));
    extensions::ExtensionSystem::Get(profile_)->event_router()->
        DispatchEventToExtension(extension_id, event_names::kOnSettingsChanged,
                                 args.Pass(), NULL, GURL());
  }

 private:
  Profile* const profile_;
};

SettingsStorageQuotaEnforcer::Limits GetLocalLimits() {
  SettingsStorageQuotaEnforcer::Limits limits = {
    static_cast<size_t>(api::storage::local::QUOTA_BYTES),
    std::numeric_limits<size_t>::max(),
    std::numeric_limits<size_t>::max()
  };
  return limits;
}

SettingsStorageQuotaEnforcer::Limits GetSyncLimits() {
  SettingsStorageQuotaEnforcer::Limits limits = {
    static_cast<size_t>(api::storage::sync::QUOTA_BYTES),
    static_cast<size_t>(api::storage::sync::QUOTA_BYTES_PER_ITEM),
    static_cast<size_t>(api::storage::sync::MAX_ITEMS)
  };
  return limits;
}

}  // namespace

// static
SettingsFrontend* SettingsFrontend::Create(Profile* profile) {
  return new SettingsFrontend(new LeveldbSettingsStorageFactory(), profile);
}

// static
SettingsFrontend* SettingsFrontend::Create(
    const scoped_refptr<SettingsStorageFactory>& storage_factory,
    Profile* profile) {
  return new SettingsFrontend(storage_factory, profile);
}

SettingsFrontend::SettingsFrontend(
    const scoped_refptr<SettingsStorageFactory>& factory, Profile* profile)
    : local_quota_limit_(GetLocalLimits()),
      sync_quota_limit_(GetSyncLimits()),
      profile_(profile),
      observers_(new SettingsObserverList()),
      profile_observer_(new DefaultObserver(profile)) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK(!profile->IsOffTheRecord());

  observers_->AddObserver(profile_observer_.get());

  const FilePath& profile_path = profile->GetPath();
  caches_[settings_namespace::LOCAL] =
      new SyncOrLocalValueStoreCache(
          settings_namespace::LOCAL,
          factory,
          local_quota_limit_,
          observers_,
          profile_path);
  caches_[settings_namespace::SYNC] =
      new SyncOrLocalValueStoreCache(
          settings_namespace::SYNC,
          factory,
          sync_quota_limit_,
          observers_,
          profile_path);

#if defined(ENABLE_CONFIGURATION_POLICY)
  caches_[settings_namespace::MANAGED] =
      new ManagedValueStoreCache(profile->GetPolicyService(), observers_);
#endif
}

SettingsFrontend::~SettingsFrontend() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  observers_->RemoveObserver(profile_observer_.get());
  // Destroy each cache in its preferred thread. The delete task will execute
  // after any other task that might've been posted before.
  for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) {
    ValueStoreCache* cache = it->second;
    cache->ShutdownOnUI();
    cache->GetMessageLoop()->DeleteSoon(FROM_HERE, cache);
  }
}

syncer::SyncableService* SettingsFrontend::GetBackendForSync(
    syncer::ModelType type) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  CacheMap::const_iterator it = caches_.find(settings_namespace::SYNC);
  DCHECK(it != caches_.end());
  const SyncOrLocalValueStoreCache* sync_cache =
      static_cast<const SyncOrLocalValueStoreCache*>(it->second);
  switch (type) {
    case syncer::APP_SETTINGS:
      return sync_cache->GetAppBackend();
    case syncer::EXTENSION_SETTINGS:
      return sync_cache->GetExtensionBackend();
    default:
      NOTREACHED();
      return NULL;
  }
}

bool SettingsFrontend::IsStorageEnabled(
    settings_namespace::Namespace settings_namespace) const {
  return caches_.find(settings_namespace) != caches_.end();
}

void SettingsFrontend::RunWithStorage(
    const std::string& extension_id,
    settings_namespace::Namespace settings_namespace,
    const ValueStoreCache::StorageCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));

  ValueStoreCache* cache = caches_[settings_namespace];
  CHECK(cache);

  // The |extension| has already been referenced earlier in the stack, so it
  // can't be gone here.
  // TODO(kalman): change RunWithStorage() to take a
  // scoped_refptr<const Extension> instead.
  scoped_refptr<const Extension> extension =
      extensions::ExtensionSystem::Get(profile_)->extension_service()->
          GetExtensionById(extension_id, true);
  CHECK(extension);

  cache->GetMessageLoop()->PostTask(
      FROM_HERE,
      base::Bind(&ValueStoreCache::RunWithValueStoreForExtension,
                 base::Unretained(cache), callback, extension));
}

void SettingsFrontend::DeleteStorageSoon(
    const std::string& extension_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  for (CacheMap::iterator it = caches_.begin(); it != caches_.end(); ++it) {
    ValueStoreCache* cache = it->second;
    cache->GetMessageLoop()->PostTask(
        FROM_HERE,
        base::Bind(&ValueStoreCache::DeleteStorageSoon,
                   base::Unretained(cache),
                   extension_id));
  }
}

scoped_refptr<SettingsObserverList> SettingsFrontend::GetObservers() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  return observers_;
}

void SettingsFrontend::DisableStorageForTesting(
    settings_namespace::Namespace settings_namespace) {
  CacheMap::iterator it = caches_.find(settings_namespace);
  if (it != caches_.end()) {
    ValueStoreCache* cache = it->second;
    cache->ShutdownOnUI();
    cache->GetMessageLoop()->DeleteSoon(FROM_HERE, cache);
    caches_.erase(it);
  }
}

}  // namespace extensions