theme_data_type_controller.cc revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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/theme_data_type_controller.h"
6
7#include "base/metrics/histogram.h"
8#include "base/logging.h"
9#include "base/time.h"
10#include "chrome/browser/browser_thread.h"
11#include "chrome/browser/profiles/profile.h"
12#include "chrome/browser/sync/glue/theme_change_processor.h"
13#include "chrome/browser/sync/glue/theme_model_associator.h"
14#include "chrome/browser/sync/profile_sync_service.h"
15#include "chrome/browser/sync/profile_sync_factory.h"
16#include "chrome/browser/sync/unrecoverable_error_handler.h"
17
18namespace browser_sync {
19
20ThemeDataTypeController::ThemeDataTypeController(
21    ProfileSyncFactory* profile_sync_factory,
22    Profile* profile,
23    ProfileSyncService* sync_service)
24    : profile_sync_factory_(profile_sync_factory),
25      profile_(profile),
26      sync_service_(sync_service),
27      state_(NOT_RUNNING) {
28  DCHECK(profile_sync_factory);
29  DCHECK(sync_service);
30}
31
32ThemeDataTypeController::~ThemeDataTypeController() {
33}
34
35void ThemeDataTypeController::Start(StartCallback* start_callback) {
36  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
37  DCHECK(start_callback);
38  if (state_ != NOT_RUNNING) {
39    start_callback->Run(BUSY);
40    delete start_callback;
41    return;
42  }
43
44  start_callback_.reset(start_callback);
45
46  profile_->InitExtensions();
47  ProfileSyncFactory::SyncComponents sync_components =
48      profile_sync_factory_->CreateThemeSyncComponents(sync_service_,
49                                                       this);
50  model_associator_.reset(sync_components.model_associator);
51  change_processor_.reset(sync_components.change_processor);
52
53  bool sync_has_nodes = false;
54  if (!model_associator_->SyncModelHasUserCreatedNodes(&sync_has_nodes)) {
55    StartFailed(UNRECOVERABLE_ERROR);
56    return;
57  }
58
59  base::TimeTicks start_time = base::TimeTicks::Now();
60  bool merge_success = model_associator_->AssociateModels();
61  UMA_HISTOGRAM_TIMES("Sync.ThemeAssociationTime",
62                      base::TimeTicks::Now() - start_time);
63  if (!merge_success) {
64    StartFailed(ASSOCIATION_FAILED);
65    return;
66  }
67
68  sync_service_->ActivateDataType(this, change_processor_.get());
69  state_ = RUNNING;
70  FinishStart(!sync_has_nodes ? OK_FIRST_RUN : OK);
71}
72
73void ThemeDataTypeController::Stop() {
74  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75
76  if (change_processor_ != NULL)
77    sync_service_->DeactivateDataType(this, change_processor_.get());
78
79  if (model_associator_ != NULL)
80    model_associator_->DisassociateModels();
81
82  change_processor_.reset();
83  model_associator_.reset();
84  start_callback_.reset();
85
86  state_ = NOT_RUNNING;
87}
88
89bool ThemeDataTypeController::enabled() {
90    return true;
91  }
92
93syncable::ModelType ThemeDataTypeController::type() {
94  return syncable::THEMES;
95}
96
97browser_sync::ModelSafeGroup ThemeDataTypeController::model_safe_group() {
98  return browser_sync::GROUP_UI;
99}
100
101const char* ThemeDataTypeController::name() const {
102  // For logging only.
103  return "theme";
104}
105
106DataTypeController::State ThemeDataTypeController::state() {
107  return state_;
108}
109
110
111void ThemeDataTypeController::OnUnrecoverableError(
112    const tracked_objects::Location& from_here,
113    const std::string& message) {
114  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
115  UMA_HISTOGRAM_COUNTS("Sync.ThemeRunFailures", 1);
116  sync_service_->OnUnrecoverableError(from_here, message);
117}
118
119void ThemeDataTypeController::FinishStart(StartResult result) {
120  start_callback_->Run(result);
121  start_callback_.reset();
122}
123
124void ThemeDataTypeController::StartFailed(StartResult result) {
125  model_associator_.reset();
126  change_processor_.reset();
127  start_callback_->Run(result);
128  start_callback_.reset();
129  UMA_HISTOGRAM_ENUMERATION("Sync.ThemeStartFailures",
130                            result,
131                            MAX_START_RESULT);
132}
133
134}  // namespace browser_sync
135