1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_THEMES_THEME_SYNCABLE_SERVICE_H_
6#define CHROME_BROWSER_THEMES_THEME_SYNCABLE_SERVICE_H_
7
8#include "base/gtest_prod_util.h"
9#include "base/threading/thread_checker.h"
10#include "sync/api/sync_change.h"
11#include "sync/api/sync_data.h"
12#include "sync/api/sync_error_factory.h"
13#include "sync/api/sync_error.h"
14#include "sync/api/syncable_service.h"
15
16class Profile;
17class ThemeService;
18class ThemeSyncableServiceTest;
19
20namespace sync_pb {
21class ThemeSpecifics;
22}
23
24class ThemeSyncableService : public syncer::SyncableService {
25 public:
26  ThemeSyncableService(Profile* profile,  // Same profile used by theme_service.
27                       ThemeService* theme_service);
28  virtual ~ThemeSyncableService();
29
30  static syncer::ModelType model_type() { return syncer::THEMES; }
31
32  // Called by ThemeService when user changes theme.
33  void OnThemeChange();
34
35  // syncer::SyncableService implementation.
36  virtual syncer::SyncMergeResult MergeDataAndStartSyncing(
37      syncer::ModelType type,
38      const syncer::SyncDataList& initial_sync_data,
39      scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
40      scoped_ptr<syncer::SyncErrorFactory> error_handler) OVERRIDE;
41  virtual void StopSyncing(syncer::ModelType type) OVERRIDE;
42  virtual syncer::SyncDataList GetAllSyncData(
43      syncer::ModelType type) const OVERRIDE;
44  virtual syncer::SyncError ProcessSyncChanges(
45      const tracked_objects::Location& from_here,
46      const syncer::SyncChangeList& change_list) OVERRIDE;
47
48  // Client tag and tile of theme node in sync.
49  static const char kCurrentThemeClientTag[];
50  static const char kCurrentThemeNodeTitle[];
51
52 private:
53  static bool AreThemeSpecificsEqual(
54      const sync_pb::ThemeSpecifics& a,
55      const sync_pb::ThemeSpecifics& b,
56      bool is_system_theme_distinct_from_default_theme);
57
58  // Set theme from theme specifics in |sync_data| using
59  // SetCurrentThemeFromThemeSpecifics() if it's different from |current_specs|.
60  void MaybeSetTheme(const sync_pb::ThemeSpecifics& current_specs,
61                     const syncer::SyncData& sync_data);
62
63  void SetCurrentThemeFromThemeSpecifics(
64      const sync_pb::ThemeSpecifics& theme_specifics);
65
66  // If the current theme is syncable, fills in the passed |theme_specifics|
67  // structure based on the currently applied theme and returns |true|.
68  // Otherwise returns |false|.
69  bool GetThemeSpecificsFromCurrentTheme(
70      sync_pb::ThemeSpecifics* theme_specifics) const;
71
72  // Updates theme specifics in sync to |theme_specifics|.
73  syncer::SyncError ProcessNewTheme(
74      syncer::SyncChange::SyncChangeType change_type,
75      const sync_pb::ThemeSpecifics& theme_specifics);
76
77  Profile* const profile_;
78  ThemeService* const theme_service_;
79
80  scoped_ptr<syncer::SyncChangeProcessor> sync_processor_;
81  scoped_ptr<syncer::SyncErrorFactory> sync_error_handler_;
82
83  // Persist use_system_theme_by_default for platforms that use it, even if
84  // we're not on one.
85  bool use_system_theme_by_default_;
86
87  base::ThreadChecker thread_checker_;
88
89  FRIEND_TEST_ALL_PREFIXES(ThemeSyncableServiceTest, AreThemeSpecificsEqual);
90
91  DISALLOW_COPY_AND_ASSIGN(ThemeSyncableService);
92};
93
94#endif  // CHROME_BROWSER_THEMES_THEME_SYNCABLE_SERVICE_H_
95