1// Copyright 2013 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 "chrome/browser/extensions/pending_enables.h"
6
7#include "chrome/browser/extensions/extension_service.h"
8#include "chrome/browser/extensions/sync_bundle.h"
9#include "components/sync_driver/sync_prefs.h"
10
11namespace extensions {
12
13PendingEnables::PendingEnables(scoped_ptr<sync_driver::SyncPrefs> sync_prefs,
14                               SyncBundle* sync_bundle,
15                               syncer::ModelType enable_type)
16    : sync_prefs_(sync_prefs.Pass()),
17      sync_bundle_(sync_bundle),
18      enable_type_(enable_type),
19      is_sync_enabled_for_test_(false) {}
20
21PendingEnables::~PendingEnables() {
22}
23
24void PendingEnables::OnExtensionEnabled(const std::string& extension_id) {
25  if (IsWaitingForSync())
26    ids_.insert(extension_id);
27}
28
29void PendingEnables::OnExtensionDisabled(const std::string& extension_id) {
30  if (IsWaitingForSync())
31    ids_.erase(extension_id);
32}
33
34void PendingEnables::OnSyncStarted(ExtensionService* service) {
35  for (std::set<std::string>::const_iterator it = ids_.begin();
36       it != ids_.end(); ++it) {
37    const Extension* extension = service->GetExtensionById(*it, true);
38    if (extension)
39      sync_bundle_->SyncChangeIfNeeded(*extension);
40  }
41  ids_.clear();
42}
43
44bool PendingEnables::Contains(const std::string& extension_id) const {
45  return ids_.find(extension_id) != ids_.end();
46}
47
48bool PendingEnables::IsSyncEnabled() {
49  if (is_sync_enabled_for_test_)
50    return true;
51  return sync_prefs_ &&
52      sync_prefs_->HasSyncSetupCompleted() &&
53      sync_prefs_->GetPreferredDataTypes(syncer::ModelTypeSet(enable_type_))
54          .Has(enable_type_);
55}
56
57bool PendingEnables::IsWaitingForSync() {
58  return IsSyncEnabled() && !sync_bundle_->IsSyncing();
59}
60
61}  // namespace extensions
62
63