OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/sync/glue/theme_model_associator.h" | 5 #include "chrome/browser/sync/glue/theme_model_associator.h" |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/tracked.h" | 9 #include "base/tracked.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/sync/api/sync_error.h" |
11 #include "chrome/browser/sync/engine/syncapi.h" | 12 #include "chrome/browser/sync/engine/syncapi.h" |
12 #include "chrome/browser/sync/glue/sync_backend_host.h" | 13 #include "chrome/browser/sync/glue/sync_backend_host.h" |
13 #include "chrome/browser/sync/glue/theme_util.h" | 14 #include "chrome/browser/sync/glue/theme_util.h" |
14 #include "chrome/browser/sync/profile_sync_service.h" | 15 #include "chrome/browser/sync/profile_sync_service.h" |
15 #include "chrome/browser/sync/protocol/theme_specifics.pb.h" | 16 #include "chrome/browser/sync/protocol/theme_specifics.pb.h" |
16 | 17 |
17 namespace browser_sync { | 18 namespace browser_sync { |
18 | 19 |
19 namespace { | 20 namespace { |
20 | 21 |
21 static const char kThemesTag[] = "google_chrome_themes"; | 22 static const char kThemesTag[] = "google_chrome_themes"; |
22 static const char kCurrentThemeNodeTitle[] = "Current Theme"; | 23 static const char kCurrentThemeNodeTitle[] = "Current Theme"; |
23 | 24 |
24 static const char kNoThemesFolderError[] = | 25 static const char kNoThemesFolderError[] = |
25 "Server did not create the top-level themes node. We " | 26 "Server did not create the top-level themes node. We " |
26 "might be running against an out-of-date server."; | 27 "might be running against an out-of-date server."; |
27 | 28 |
28 } // namespace | 29 } // namespace |
29 | 30 |
30 ThemeModelAssociator::ThemeModelAssociator( | 31 ThemeModelAssociator::ThemeModelAssociator( |
31 ProfileSyncService* sync_service) | 32 ProfileSyncService* sync_service) |
32 : sync_service_(sync_service) { | 33 : sync_service_(sync_service) { |
33 DCHECK(sync_service_); | 34 DCHECK(sync_service_); |
34 } | 35 } |
35 | 36 |
36 ThemeModelAssociator::~ThemeModelAssociator() {} | 37 ThemeModelAssociator::~ThemeModelAssociator() {} |
37 | 38 |
38 bool ThemeModelAssociator::AssociateModels() { | 39 bool ThemeModelAssociator::AssociateModels(SyncError* error) { |
39 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 40 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
40 sync_api::ReadNode root(&trans); | 41 sync_api::ReadNode root(&trans); |
41 if (!root.InitByTagLookup(kThemesTag)) { | 42 if (!root.InitByTagLookup(kThemesTag)) { |
42 LOG(ERROR) << kNoThemesFolderError; | 43 error->Reset(FROM_HERE, kNoThemesFolderError, model_type()); |
43 return false; | 44 return false; |
44 } | 45 } |
45 | 46 |
46 Profile* profile = sync_service_->profile(); | 47 Profile* profile = sync_service_->profile(); |
47 sync_api::WriteNode node(&trans); | 48 sync_api::WriteNode node(&trans); |
48 // TODO(akalin): When we have timestamps, we may want to do | 49 // TODO(akalin): When we have timestamps, we may want to do |
49 // something more intelligent than preferring the sync data over our | 50 // something more intelligent than preferring the sync data over our |
50 // local data. | 51 // local data. |
51 if (node.InitByClientTagLookup(syncable::THEMES, kCurrentThemeClientTag)) { | 52 if (node.InitByClientTagLookup(syncable::THEMES, kCurrentThemeClientTag)) { |
52 // Update the current theme from the sync data. | 53 // Update the current theme from the sync data. |
53 // TODO(akalin): If the sync data does not have | 54 // TODO(akalin): If the sync data does not have |
54 // use_system_theme_by_default and we do, update that flag on the | 55 // use_system_theme_by_default and we do, update that flag on the |
55 // sync data. | 56 // sync data. |
56 sync_pb::ThemeSpecifics theme_specifics = node.GetThemeSpecifics(); | 57 sync_pb::ThemeSpecifics theme_specifics = node.GetThemeSpecifics(); |
57 if (UpdateThemeSpecificsOrSetCurrentThemeIfNecessary(profile, | 58 if (UpdateThemeSpecificsOrSetCurrentThemeIfNecessary(profile, |
58 &theme_specifics)) | 59 &theme_specifics)) |
59 node.SetThemeSpecifics(theme_specifics); | 60 node.SetThemeSpecifics(theme_specifics); |
60 } else { | 61 } else { |
61 // Set the sync data from the current theme. | 62 // Set the sync data from the current theme. |
62 sync_api::WriteNode node(&trans); | 63 sync_api::WriteNode node(&trans); |
63 if (!node.InitUniqueByCreation(syncable::THEMES, root, | 64 if (!node.InitUniqueByCreation(syncable::THEMES, root, |
64 kCurrentThemeClientTag)) { | 65 kCurrentThemeClientTag)) { |
65 LOG(ERROR) << "Could not create current theme node."; | 66 error->Reset(FROM_HERE, |
| 67 "Could not create current theme node.", |
| 68 model_type()); |
66 return false; | 69 return false; |
67 } | 70 } |
68 node.SetIsFolder(false); | 71 node.SetIsFolder(false); |
69 node.SetTitle(UTF8ToWide(kCurrentThemeNodeTitle)); | 72 node.SetTitle(UTF8ToWide(kCurrentThemeNodeTitle)); |
70 sync_pb::ThemeSpecifics theme_specifics; | 73 sync_pb::ThemeSpecifics theme_specifics; |
71 GetThemeSpecificsFromCurrentTheme(profile, &theme_specifics); | 74 GetThemeSpecificsFromCurrentTheme(profile, &theme_specifics); |
72 node.SetThemeSpecifics(theme_specifics); | 75 node.SetThemeSpecifics(theme_specifics); |
73 } | 76 } |
74 return true; | 77 return true; |
75 } | 78 } |
76 | 79 |
77 bool ThemeModelAssociator::DisassociateModels() { | 80 bool ThemeModelAssociator::DisassociateModels(SyncError* error) { |
78 // We don't maintain any association state, so nothing to do. | 81 // We don't maintain any association state, so nothing to do. |
79 return true; | 82 return true; |
80 } | 83 } |
81 | 84 |
82 bool ThemeModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 85 bool ThemeModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
83 DCHECK(has_nodes); | 86 DCHECK(has_nodes); |
84 *has_nodes = false; | 87 *has_nodes = false; |
85 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 88 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
86 sync_api::ReadNode root(&trans); | 89 sync_api::ReadNode root(&trans); |
87 if (!root.InitByTagLookup(kThemesTag)) { | 90 if (!root.InitByTagLookup(kThemesTag)) { |
88 LOG(ERROR) << kNoThemesFolderError; | 91 LOG(ERROR) << kNoThemesFolderError; |
89 return false; | 92 return false; |
90 } | 93 } |
91 // The sync model has user created nodes iff the themes folder has | 94 // The sync model has user created nodes iff the themes folder has |
92 // any children. | 95 // any children. |
93 *has_nodes = root.GetFirstChildId() != sync_api::kInvalidId; | 96 *has_nodes = root.GetFirstChildId() != sync_api::kInvalidId; |
94 return true; | 97 return true; |
95 } | 98 } |
96 | 99 |
97 bool ThemeModelAssociator::CryptoReadyIfNecessary() { | 100 bool ThemeModelAssociator::CryptoReadyIfNecessary() { |
98 // We only access the cryptographer while holding a transaction. | 101 // We only access the cryptographer while holding a transaction. |
99 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 102 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
100 syncable::ModelTypeSet encrypted_types; | 103 syncable::ModelTypeSet encrypted_types; |
101 encrypted_types = sync_api::GetEncryptedTypes(&trans); | 104 encrypted_types = sync_api::GetEncryptedTypes(&trans); |
102 return encrypted_types.count(syncable::THEMES) == 0 || | 105 return encrypted_types.count(syncable::THEMES) == 0 || |
103 sync_service_->IsCryptographerReady(&trans); | 106 sync_service_->IsCryptographerReady(&trans); |
104 } | 107 } |
105 | 108 |
106 } // namespace browser_sync | 109 } // namespace browser_sync |
OLD | NEW |