1// Copyright (c) 2011 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/sync/glue/extension_model_associator.h"
6
7#include "base/logging.h"
8#include "chrome/browser/sync/engine/syncapi.h"
9#include "chrome/browser/sync/glue/extension_data.h"
10#include "chrome/browser/sync/glue/extension_sync_traits.h"
11#include "chrome/browser/sync/glue/extension_sync.h"
12#include "chrome/browser/sync/profile_sync_service.h"
13#include "chrome/browser/sync/protocol/extension_specifics.pb.h"
14#include "chrome/browser/sync/syncable/nigori_util.h"
15#include "content/browser/browser_thread.h"
16
17namespace browser_sync {
18
19ExtensionModelAssociator::ExtensionModelAssociator(
20    const ExtensionSyncTraits& traits,
21    ExtensionServiceInterface* extension_service,
22    sync_api::UserShare* user_share)
23    : traits_(traits), extension_service_(extension_service),
24      user_share_(user_share) {
25  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
26  DCHECK(extension_service_);
27  DCHECK(user_share_);
28}
29
30ExtensionModelAssociator::~ExtensionModelAssociator() {
31  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
32}
33
34bool ExtensionModelAssociator::AssociateModels() {
35  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
36  ExtensionDataMap extension_data_map;
37  if (!SlurpExtensionData(
38          traits_, *extension_service_, user_share_, &extension_data_map)) {
39    return false;
40  }
41  if (!FlushExtensionData(
42          traits_, extension_data_map, extension_service_, user_share_)) {
43    return false;
44  }
45
46  return true;
47}
48
49bool ExtensionModelAssociator::DisassociateModels() {
50  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
51  // Nothing to do.
52  return true;
53}
54
55bool ExtensionModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) {
56  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
57  return RootNodeHasChildren(traits_.root_node_tag, user_share_, has_nodes);
58}
59
60bool ExtensionModelAssociator::CryptoReadyIfNecessary() {
61  // We only access the cryptographer while holding a transaction.
62  sync_api::ReadTransaction trans(user_share_);
63  const syncable::ModelTypeSet& encrypted_types =
64      GetEncryptedDataTypes(trans.GetWrappedTrans());
65  return encrypted_types.count(traits_.model_type) == 0 ||
66      trans.GetCryptographer()->is_ready();
67}
68
69}  // namespace browser_sync
70