summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/settings/settings_frontend.cc
blob: b3fd10aa8252a13920d0fbbf971bc36d24f9a6f0 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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 "base/bind.h"
#include "base/file_path.h"
#include "base/string_number_conversions.h"
#include "chrome/browser/extensions/extension_event_names.h"
#include "chrome/browser/extensions/extension_event_router.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/settings/leveldb_settings_storage_factory.h"
#include "chrome/browser/extensions/settings/settings_backend.h"
#include "chrome/browser/extensions/settings/settings_namespace.h"
#include "chrome/browser/extensions/settings/weak_unlimited_settings_storage.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/value_store/leveldb_value_store.h"
#include "chrome/common/extensions/api/storage.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_service.h"

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 {
    profile_->GetExtensionEventRouter()->DispatchEventToExtension(
        extension_id,
        extension_event_names::kOnSettingsChanged,
        // This is the list of function arguments to pass to the onChanged
        // handler of extensions, an array of [changes, settings_namespace].
        std::string("[") + change_json + ",\"" +
            settings_namespace::ToString(settings_namespace) + "\"]",
        NULL,
        GURL());
  }

 private:
  Profile* const profile_;
};

void CallbackWithSyncableService(
    const SettingsFrontend::SyncableServiceCallback& callback,
    SettingsBackend* backend) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  callback.Run(backend);
}

void CallbackWithStorage(
    const std::string& extension_id,
    const SettingsFrontend::StorageCallback& callback,
    SettingsBackend* backend) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  callback.Run(backend->GetStorage(extension_id));
}

void CallbackWithNullStorage(
    const SettingsFrontend::StorageCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  callback.Run(NULL);
}

void DeleteStorageOnFileThread(
    const std::string& extension_id, SettingsBackend* backend) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  backend->DeleteStorage(extension_id);
}

void CallbackWithUnlimitedStorage(
    const std::string& extension_id,
    const SettingsFrontend::StorageCallback& callback,
    SettingsBackend* backend) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  WeakUnlimitedSettingsStorage unlimited_storage(
      backend->GetStorage(extension_id));
  callback.Run(&unlimited_storage);
}

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

// Ref-counted container for a SettingsBackend object.
class SettingsFrontend::BackendWrapper
    : public base::RefCountedThreadSafe<BackendWrapper> {
 public:
  // Creates a new BackendWrapper and initializes it on the FILE thread.
  static scoped_refptr<BackendWrapper> CreateAndInit(
      const scoped_refptr<SettingsStorageFactory>& factory,
      const SettingsStorageQuotaEnforcer::Limits& quota,
      const scoped_refptr<SettingsObserverList>& observers,
      const FilePath& path) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    scoped_refptr<BackendWrapper> backend_wrapper =
        new BackendWrapper(factory, quota, observers);
    BrowserThread::PostTask(
        BrowserThread::FILE,
        FROM_HERE,
        base::Bind(
            &SettingsFrontend::BackendWrapper::InitOnFileThread,
            backend_wrapper,
            path));
    return backend_wrapper;
  }

  typedef base::Callback<void(SettingsBackend*)> BackendCallback;

  // Runs |callback| with the wrapped Backend on the FILE thread.
  void RunWithBackend(const BackendCallback& callback) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    BrowserThread::PostTask(
        BrowserThread::FILE,
        FROM_HERE,
        base::Bind(
            &SettingsFrontend::BackendWrapper::RunWithBackendOnFileThread,
            this,
            callback));
  }

  SettingsBackend* GetBackend() const {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
    return backend_;
  }

 private:
  friend class base::RefCountedThreadSafe<BackendWrapper>;

  BackendWrapper(
      const scoped_refptr<SettingsStorageFactory>& storage_factory,
      const SettingsStorageQuotaEnforcer::Limits& quota,
      const scoped_refptr<SettingsObserverList>& observers)
      : storage_factory_(storage_factory),
        quota_(quota),
        observers_(observers),
        backend_(NULL) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  }

  virtual ~BackendWrapper() {
    if (BrowserThread::CurrentlyOn(BrowserThread::FILE)) {
      delete backend_;
    } else if (BrowserThread::CurrentlyOn(BrowserThread::UI)) {
      BrowserThread::DeleteSoon(BrowserThread::FILE, FROM_HERE, backend_);
    } else {
      NOTREACHED();
    }
  }

  void InitOnFileThread(const FilePath& path) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
    DCHECK(!backend_);
    backend_ = new SettingsBackend(storage_factory_, path, quota_, observers_);
    storage_factory_ = NULL;
    observers_ = NULL;
  }

  void RunWithBackendOnFileThread(const BackendCallback& callback) {
    DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
    DCHECK(backend_);
    callback.Run(backend_);
  }

  // Only need these until |backend_| exists.
  scoped_refptr<SettingsStorageFactory> storage_factory_;
  const SettingsStorageQuotaEnforcer::Limits quota_;
  scoped_refptr<SettingsObserverList> observers_;

  // Wrapped Backend.  Used exclusively on the FILE thread, and is created on
  // the FILE thread in InitOnFileThread.
  SettingsBackend* backend_;

  DISALLOW_COPY_AND_ASSIGN(BackendWrapper);
};

// SettingsFrontend

// 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();
  backends_[settings_namespace::LOCAL].app =
      BackendWrapper::CreateAndInit(
          factory,
          local_quota_limit_,
          observers_,
          profile_path.AppendASCII(
              ExtensionService::kLocalAppSettingsDirectoryName));
  backends_[settings_namespace::LOCAL].extension =
      BackendWrapper::CreateAndInit(
          factory,
          local_quota_limit_,
          observers_,
          profile_path.AppendASCII(
              ExtensionService::kLocalExtensionSettingsDirectoryName));
  backends_[settings_namespace::SYNC].app =
      BackendWrapper::CreateAndInit(
          factory,
          sync_quota_limit_,
          observers_,
          profile_path.AppendASCII(
              ExtensionService::kSyncAppSettingsDirectoryName));
  backends_[settings_namespace::SYNC].extension =
      BackendWrapper::CreateAndInit(
          factory,
          sync_quota_limit_,
          observers_,
          profile_path.AppendASCII(
              ExtensionService::kSyncExtensionSettingsDirectoryName));
}

SettingsFrontend::~SettingsFrontend() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  observers_->RemoveObserver(profile_observer_.get());
}

syncer::SyncableService* SettingsFrontend::GetBackendForSync(
    syncable::ModelType type) const {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
  std::map<settings_namespace::Namespace, BackendWrappers>::const_iterator
      sync_backends = backends_.find(settings_namespace::SYNC);
  DCHECK(sync_backends != backends_.end());
  switch (type) {
    case syncable::APP_SETTINGS:
      return sync_backends->second.app->GetBackend();
    case syncable::EXTENSION_SETTINGS:
      return sync_backends->second.extension->GetBackend();
    default:
      NOTREACHED();
      return NULL;
  }
}

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

  const Extension* extension =
      profile_->GetExtensionService()->GetExtensionById(extension_id, true);
  if (!extension) {
    BrowserThread::PostTask(
        BrowserThread::FILE,
        FROM_HERE,
        base::Bind(&CallbackWithNullStorage, callback));
    return;
  }

  // A neat way to implement unlimited storage; if the extension has the
  // unlimited storage permission, force through all calls to Set() (in the
  // same way that writes from sync ignore quota).
  // But only if it's local storage (bad stuff would happen if sync'ed
  // storage is allowed to be unlimited).
  bool is_unlimited =
      settings_namespace == settings_namespace::LOCAL &&
      extension->HasAPIPermission(APIPermission::kUnlimitedStorage);

  scoped_refptr<BackendWrapper> backend;
  if (extension->is_app()) {
    backend = backends_[settings_namespace].app;
  } else {
    backend = backends_[settings_namespace].extension;
  }

  backend->RunWithBackend(
      base::Bind(
          is_unlimited ?
              &CallbackWithUnlimitedStorage : &CallbackWithStorage,
          extension_id,
          callback));
}

void SettingsFrontend::DeleteStorageSoon(
    const std::string& extension_id) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SettingsFrontend::BackendWrapper::BackendCallback callback =
      base::Bind(&DeleteStorageOnFileThread, extension_id);
  for (std::map<settings_namespace::Namespace, BackendWrappers>::iterator it =
      backends_.begin(); it != backends_.end(); ++it) {
    it->second.app->RunWithBackend(callback);
    it->second.extension->RunWithBackend(callback);
  }
}

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

// BackendWrappers

SettingsFrontend::BackendWrappers::BackendWrappers() {}
SettingsFrontend::BackendWrappers::~BackendWrappers() {}

}  // namespace extensions