| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/location.h" | 8 #include "base/location.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 static const char kThemesTag[] = "google_chrome_themes"; | 25 static const char kThemesTag[] = "google_chrome_themes"; |
| 26 static const char kCurrentThemeNodeTitle[] = "Current Theme"; | 26 static const char kCurrentThemeNodeTitle[] = "Current Theme"; |
| 27 | 27 |
| 28 static const char kNoThemesFolderError[] = | 28 static const char kNoThemesFolderError[] = |
| 29 "Server did not create the top-level themes node. We " | 29 "Server did not create the top-level themes node. We " |
| 30 "might be running against an out-of-date server."; | 30 "might be running against an out-of-date server."; |
| 31 | 31 |
| 32 } // namespace | 32 } // namespace |
| 33 | 33 |
| 34 ThemeModelAssociator::ThemeModelAssociator( | 34 ThemeModelAssociator::ThemeModelAssociator( |
| 35 ProfileSyncService* sync_service) | 35 ProfileSyncService* sync_service, |
| 36 : sync_service_(sync_service) { | 36 DataTypeErrorHandler* error_handler) |
| 37 : sync_service_(sync_service), |
| 38 error_handler_(error_handler) { |
| 37 DCHECK(sync_service_); | 39 DCHECK(sync_service_); |
| 38 } | 40 } |
| 39 | 41 |
| 40 ThemeModelAssociator::~ThemeModelAssociator() {} | 42 ThemeModelAssociator::~ThemeModelAssociator() {} |
| 41 | 43 |
| 42 bool ThemeModelAssociator::AssociateModels(SyncError* error) { | 44 SyncError ThemeModelAssociator::AssociateModels() { |
| 43 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 45 sync_api::WriteTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 44 sync_api::ReadNode root(&trans); | 46 sync_api::ReadNode root(&trans); |
| 45 if (!root.InitByTagLookup(kThemesTag)) { | 47 if (!root.InitByTagLookup(kThemesTag)) { |
| 46 error->Reset(FROM_HERE, kNoThemesFolderError, model_type()); | 48 return error_handler_->CreateAndUploadError(FROM_HERE, |
| 47 return false; | 49 kNoThemesFolderError, |
| 50 model_type()); |
| 48 } | 51 } |
| 49 | 52 |
| 50 Profile* profile = sync_service_->profile(); | 53 Profile* profile = sync_service_->profile(); |
| 51 sync_api::WriteNode node(&trans); | 54 sync_api::WriteNode node(&trans); |
| 52 // TODO(akalin): When we have timestamps, we may want to do | 55 // TODO(akalin): When we have timestamps, we may want to do |
| 53 // something more intelligent than preferring the sync data over our | 56 // something more intelligent than preferring the sync data over our |
| 54 // local data. | 57 // local data. |
| 55 if (node.InitByClientTagLookup(syncable::THEMES, kCurrentThemeClientTag)) { | 58 if (node.InitByClientTagLookup(syncable::THEMES, kCurrentThemeClientTag)) { |
| 56 // Update the current theme from the sync data. | 59 // Update the current theme from the sync data. |
| 57 // TODO(akalin): If the sync data does not have | 60 // TODO(akalin): If the sync data does not have |
| 58 // use_system_theme_by_default and we do, update that flag on the | 61 // use_system_theme_by_default and we do, update that flag on the |
| 59 // sync data. | 62 // sync data. |
| 60 sync_pb::ThemeSpecifics theme_specifics = node.GetThemeSpecifics(); | 63 sync_pb::ThemeSpecifics theme_specifics = node.GetThemeSpecifics(); |
| 61 if (UpdateThemeSpecificsOrSetCurrentThemeIfNecessary(profile, | 64 if (UpdateThemeSpecificsOrSetCurrentThemeIfNecessary(profile, |
| 62 &theme_specifics)) | 65 &theme_specifics)) |
| 63 node.SetThemeSpecifics(theme_specifics); | 66 node.SetThemeSpecifics(theme_specifics); |
| 64 } else { | 67 } else { |
| 65 // Set the sync data from the current theme. | 68 // Set the sync data from the current theme. |
| 66 sync_api::WriteNode node(&trans); | 69 sync_api::WriteNode node(&trans); |
| 67 if (!node.InitUniqueByCreation(syncable::THEMES, root, | 70 if (!node.InitUniqueByCreation(syncable::THEMES, root, |
| 68 kCurrentThemeClientTag)) { | 71 kCurrentThemeClientTag)) { |
| 69 error->Reset(FROM_HERE, | 72 return error_handler_->CreateAndUploadError( |
| 70 "Could not create current theme node.", | 73 FROM_HERE, |
| 71 model_type()); | 74 "Could not create current theme node.", |
| 72 return false; | 75 model_type()); |
| 73 } | 76 } |
| 74 node.SetIsFolder(false); | 77 node.SetIsFolder(false); |
| 75 node.SetTitle(UTF8ToWide(kCurrentThemeNodeTitle)); | 78 node.SetTitle(UTF8ToWide(kCurrentThemeNodeTitle)); |
| 76 sync_pb::ThemeSpecifics theme_specifics; | 79 sync_pb::ThemeSpecifics theme_specifics; |
| 77 GetThemeSpecificsFromCurrentTheme(profile, &theme_specifics); | 80 GetThemeSpecificsFromCurrentTheme(profile, &theme_specifics); |
| 78 node.SetThemeSpecifics(theme_specifics); | 81 node.SetThemeSpecifics(theme_specifics); |
| 79 } | 82 } |
| 80 return true; | 83 return SyncError(); |
| 81 } | 84 } |
| 82 | 85 |
| 83 bool ThemeModelAssociator::DisassociateModels(SyncError* error) { | 86 SyncError ThemeModelAssociator::DisassociateModels() { |
| 84 // We don't maintain any association state, so nothing to do. | 87 // We don't maintain any association state, so nothing to do. |
| 85 return true; | 88 return SyncError(); |
| 86 } | 89 } |
| 87 | 90 |
| 88 bool ThemeModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { | 91 bool ThemeModelAssociator::SyncModelHasUserCreatedNodes(bool* has_nodes) { |
| 89 DCHECK(has_nodes); | 92 DCHECK(has_nodes); |
| 90 *has_nodes = false; | 93 *has_nodes = false; |
| 91 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 94 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 92 sync_api::ReadNode root(&trans); | 95 sync_api::ReadNode root(&trans); |
| 93 if (!root.InitByTagLookup(kThemesTag)) { | 96 if (!root.InitByTagLookup(kThemesTag)) { |
| 94 LOG(ERROR) << kNoThemesFolderError; | 97 LOG(ERROR) << kNoThemesFolderError; |
| 95 return false; | 98 return false; |
| 96 } | 99 } |
| 97 // The sync model has user created nodes iff the themes folder has | 100 // The sync model has user created nodes iff the themes folder has |
| 98 // any children. | 101 // any children. |
| 99 *has_nodes = root.HasChildren(); | 102 *has_nodes = root.HasChildren(); |
| 100 return true; | 103 return true; |
| 101 } | 104 } |
| 102 | 105 |
| 103 bool ThemeModelAssociator::CryptoReadyIfNecessary() { | 106 bool ThemeModelAssociator::CryptoReadyIfNecessary() { |
| 104 // We only access the cryptographer while holding a transaction. | 107 // We only access the cryptographer while holding a transaction. |
| 105 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); | 108 sync_api::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); |
| 106 const syncable::ModelTypeSet encrypted_types = | 109 const syncable::ModelTypeSet encrypted_types = |
| 107 sync_api::GetEncryptedTypes(&trans); | 110 sync_api::GetEncryptedTypes(&trans); |
| 108 return !encrypted_types.Has(syncable::THEMES) || | 111 return !encrypted_types.Has(syncable::THEMES) || |
| 109 sync_service_->IsCryptographerReady(&trans); | 112 sync_service_->IsCryptographerReady(&trans); |
| 110 } | 113 } |
| 111 | 114 |
| 112 } // namespace browser_sync | 115 } // namespace browser_sync |
| OLD | NEW |