summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/app_notification_manager.h
blob: 74104f33cdbe34cd55dccf638a76f8fa85a32d08 (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// 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.

#ifndef CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_
#define CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_
#pragma once

#include <map>

#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/sequenced_task_runner_helpers.h"
#include "base/synchronization/waitable_event.h"
#include "chrome/browser/extensions/app_notification.h"
#include "chrome/browser/extensions/app_notification_storage.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "sync/api/sync_change.h"
#include "sync/api/syncable_service.h"

class PerfTimer;
class Profile;

namespace syncer {
class SyncErrorFactory;
}

// This class keeps track of notifications for installed apps.
class AppNotificationManager
    : public base::RefCountedThreadSafe<
          AppNotificationManager,
          content::BrowserThread::DeleteOnUIThread>,
      public content::NotificationObserver,
      public syncer::SyncableService {
 public:
  static const unsigned int kMaxNotificationPerApp;
  explicit AppNotificationManager(Profile* profile);

  // Starts up the process of reading from persistent storage.
  void Init();

  // Returns whether add was succcessful.
  // Takes ownership of |item| in all cases.
  bool Add(AppNotification* item);

  const AppNotificationList* GetAll(const std::string& extension_id) const;

  // Returns the most recently added notification for |extension_id| if there
  // are any, or NULL otherwise.
  const AppNotification* GetLast(const std::string& extension_id);

  // Clears all notifications for |extension_id|.
  void ClearAll(const std::string& extension_id);

  // Implementing content::NotificationObserver interface.
  virtual void Observe(int type,
                       const content::NotificationSource& source,
                       const content::NotificationDetails& details) OVERRIDE;

  bool loaded() const { return notifications_.get() != NULL; }

  // syncer::SyncableService implementation.

  // Returns all syncable notifications from this model as syncer::SyncData.
  virtual syncer::SyncDataList GetAllSyncData(
      syncable::ModelType type) const OVERRIDE;
  // Process notifications related changes from Sync, merging them into
  // our model.
  virtual syncer::SyncError ProcessSyncChanges(
      const tracked_objects::Location& from_here,
      const syncer::SyncChangeList& change_list) OVERRIDE;
  // Associate and merge sync data model with our data model.
  virtual syncer::SyncError MergeDataAndStartSyncing(
      syncable::ModelType type,
      const syncer::SyncDataList& initial_sync_data,
      scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
      scoped_ptr<syncer::SyncErrorFactory> sync_error_factory) OVERRIDE;
  virtual void StopSyncing(syncable::ModelType type) OVERRIDE;

 private:
  friend class AppNotificationManagerSyncTest;
  friend class base::DeleteHelper<AppNotificationManager>;
  friend struct content::BrowserThread::DeleteOnThread<
      content::BrowserThread::UI>;
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           NotificationToSyncDataToNotification);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           GetAllSyncDataNoLocal);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           GetAllSyncDataSomeLocal);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ModelAssocModelEmpty);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ModelAssocBothNonEmptyNoOverlap);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ModelAssocBothNonEmptySomeOverlap);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ModelAssocBothNonEmptyTitleMismatch);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ModelAssocBothNonEmptyMatchesLocal);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ProcessSyncChangesErrorModelAssocNotDone);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ProcessSyncChangesEmptyModel);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           StopSyncing);
  FRIEND_TEST_ALL_PREFIXES(AppNotificationManagerSyncTest,
                           ClearAllGetsSynced);

  // Maps extension id to a list of notifications for that extension.
  typedef std::map<std::string, AppNotificationList> NotificationMap;

  virtual ~AppNotificationManager();

  // Starts loading storage_ using |storage_path|.
  void LoadOnFileThread(const FilePath& storage_path);

  // Called on the UI thread to handle the loaded results from storage_.
  void HandleLoadResults(NotificationMap* map);

  // Saves the contents of |list| to storage.
  // Ownership of |list| is transferred here.
  void SaveOnFileThread(const std::string& extension_id,
                        AppNotificationList* list);

  void DeleteOnFileThread(const std::string& extension_id);

  // Gets notifications for a given extension id.
  AppNotificationList& GetAllInternal(const std::string& extension_id);

  // Removes the notification with given extension id and given guid.
  void Remove(const std::string& extension_id, const std::string& guid);

  // Returns the notification for the given |extension_id| and |guid|,
  // NULL if no such notification exists.
  const AppNotification* GetNotification(const std::string& extension_id,
                                         const std::string& guid);

  // Sends a change to syncer to add the given notification.
  void SyncAddChange(const AppNotification& notif);

  // Sends a change to syncer to remove the given notification.
  void SyncRemoveChange(const AppNotification& notif);

  // Sends changes to syncer to remove all notifications in the given list.
  void SyncClearAllChange(const AppNotificationList& list);

  // Converters from AppNotification to syncer::SyncData and vice versa.
  static syncer::SyncData CreateSyncDataFromNotification(
      const AppNotification& notification);
  static AppNotification* CreateNotificationFromSyncData(
      const syncer::SyncData& sync_data);

  Profile* profile_;
  content::NotificationRegistrar registrar_;
  scoped_ptr<NotificationMap> notifications_;

  // This should only be used on the FILE thread.
  scoped_ptr<AppNotificationStorage> storage_;

  // Sync change processor we use to push all our changes.
  scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;

  // Sync error handler that we use to create errors from.
  scoped_ptr<syncer::SyncErrorFactory> sync_error_factory_;

  // Whether the sync model is associated with the local model.
  // In other words, whether we are ready to apply sync changes.
  bool models_associated_;
  // Whether syncer changes are being processed right now.
  bool processing_syncer_changes_;

  // Used for a histogram of load time.
  scoped_ptr<PerfTimer> load_timer_;

  DISALLOW_COPY_AND_ASSIGN(AppNotificationManager);
};

#endif  // CHROME_BROWSER_EXTENSIONS_APP_NOTIFICATION_MANAGER_H_