Home | History | Annotate | Download | only in glue
      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/bookmark_data_type_controller.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "chrome/browser/bookmarks/bookmark_model.h"
      9 #include "chrome/browser/profiles/profile.h"
     10 #include "chrome/browser/sync/profile_sync_factory.h"
     11 #include "chrome/browser/sync/profile_sync_service.h"
     12 #include "content/browser/browser_thread.h"
     13 #include "content/common/notification_details.h"
     14 #include "content/common/notification_source.h"
     15 #include "content/common/notification_type.h"
     16 
     17 namespace browser_sync {
     18 
     19 BookmarkDataTypeController::BookmarkDataTypeController(
     20     ProfileSyncFactory* profile_sync_factory,
     21     Profile* profile,
     22     ProfileSyncService* sync_service)
     23     : FrontendDataTypeController(profile_sync_factory,
     24                                  profile,
     25                                  sync_service) {
     26 }
     27 
     28 BookmarkDataTypeController::~BookmarkDataTypeController() {
     29 }
     30 
     31 // We want to start the bookmark model before we begin associating.
     32 bool BookmarkDataTypeController::StartModels() {
     33   // If the bookmarks model is loaded, continue with association.
     34   BookmarkModel* bookmark_model = profile_->GetBookmarkModel();
     35   if (bookmark_model && bookmark_model->IsLoaded()) {
     36     return true;  // Continue to Associate().
     37   }
     38 
     39   // Add an observer and continue when the bookmarks model is loaded.
     40   registrar_.Add(this, NotificationType::BOOKMARK_MODEL_LOADED,
     41                  Source<Profile>(sync_service_->profile()));
     42   return false;  // Don't continue Start.
     43 }
     44 
     45 // Cleanup for our extra registrar usage.
     46 void BookmarkDataTypeController::CleanupState() {
     47   registrar_.RemoveAll();
     48 }
     49 
     50 void BookmarkDataTypeController::Observe(NotificationType type,
     51                                          const NotificationSource& source,
     52                                          const NotificationDetails& details) {
     53   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     54   DCHECK_EQ(NotificationType::BOOKMARK_MODEL_LOADED, type.value);
     55   registrar_.RemoveAll();
     56   DCHECK_EQ(state_, MODEL_STARTING);
     57   state_ = ASSOCIATING;
     58   Associate();
     59 }
     60 
     61 syncable::ModelType BookmarkDataTypeController::type() const {
     62   return syncable::BOOKMARKS;
     63 }
     64 
     65 void BookmarkDataTypeController::CreateSyncComponents() {
     66   ProfileSyncFactory::SyncComponents sync_components = profile_sync_factory_->
     67       CreateBookmarkSyncComponents(sync_service_, this);
     68   model_associator_.reset(sync_components.model_associator);
     69   change_processor_.reset(sync_components.change_processor);
     70 }
     71 
     72 void BookmarkDataTypeController::RecordUnrecoverableError(
     73     const tracked_objects::Location& from_here,
     74     const std::string& message) {
     75   UMA_HISTOGRAM_COUNTS("Sync.BookmarkRunFailures", 1);
     76 }
     77 
     78 void BookmarkDataTypeController::RecordAssociationTime(base::TimeDelta time) {
     79   UMA_HISTOGRAM_TIMES("Sync.BookmarkAssociationTime", time);
     80 }
     81 
     82 void BookmarkDataTypeController::RecordStartFailure(StartResult result) {
     83   UMA_HISTOGRAM_ENUMERATION("Sync.BookmarkStartFailures",
     84                             result,
     85                             MAX_START_RESULT);
     86 }
     87 
     88 }  // namespace browser_sync
     89