summaryrefslogtreecommitdiffstats
path: root/chrome/browser/sync/glue/app_notification_data_type_controller.cc
blob: 1fef9087e75b78fa0453976a119ffa5409ff01f8 (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
// 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/sync/glue/app_notification_data_type_controller.h"

#include "base/metrics/histogram.h"
#include "chrome/browser/extensions/app_notification_manager.h"
#include "chrome/browser/extensions/extension_service.h"
#include "chrome/browser/extensions/extension_system.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/sync/glue/generic_change_processor.h"
#include "chrome/browser/sync/profile_sync_components_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/common/chrome_notification_types.h"
#include "content/public/browser/notification_source.h"
#include "sync/api/syncable_service.h"

using content::BrowserThread;

namespace browser_sync {

AppNotificationDataTypeController::AppNotificationDataTypeController(
    ProfileSyncComponentsFactory* profile_sync_factory,
    Profile* profile,
    ProfileSyncService* sync_service)
    : UIDataTypeController(syncer::APP_NOTIFICATIONS,
                           profile_sync_factory,
                           profile,
                           sync_service) {
}

void AppNotificationDataTypeController::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  DCHECK_EQ(chrome::NOTIFICATION_APP_NOTIFICATION_MANAGER_LOADED, type);
  registrar_.RemoveAll();
  OnModelLoaded();
}

AppNotificationDataTypeController::~AppNotificationDataTypeController() {
}

extensions::AppNotificationManager*
AppNotificationDataTypeController::GetAppNotificationManager() {
  return extensions::ExtensionSystem::Get(profile_)->extension_service()->
      app_notification_manager();
}

// We want to start the extensions::AppNotificationManager before we begin
// associating.
bool AppNotificationDataTypeController::StartModels() {
  extensions::AppNotificationManager* manager = GetAppNotificationManager();
  DCHECK(manager);
  if (manager->loaded())
    return true;  // Continue to Associate().

  // Add an observer and continue when the extensions::AppNotificationManager
  // is loaded.
  registrar_.Add(this, chrome::NOTIFICATION_APP_NOTIFICATION_MANAGER_LOADED,
                 content::Source<extensions::AppNotificationManager>(manager));
  return false;  // Don't continue Associate().
}

// Cleanup for our extra registrar usage.
void AppNotificationDataTypeController::StopModels() {
  registrar_.RemoveAll();
}

}  // namespace browser_sync