Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(697)

Side by Side Diff: chrome/browser/sync/glue/session_model_associator.cc

Issue 10911073: NOT FOR COMMIT: Add DeviceInfo type and ChangeProcessor (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix several issues Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/session_model_associator.h" 5 #include "chrome/browser/sync/glue/session_model_associator.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <set> 8 #include <set>
9 #include <utility> 9 #include <utility>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/command_line.h" 12 #include "base/command_line.h"
13 #include "base/location.h" 13 #include "base/location.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/threading/sequenced_worker_pool.h" 15 #include "base/threading/sequenced_worker_pool.h"
16 #include "chrome/browser/favicon/favicon_service_factory.h" 16 #include "chrome/browser/favicon/favicon_service_factory.h"
17 #include "chrome/browser/history/history.h" 17 #include "chrome/browser/history/history.h"
18 #include "chrome/browser/prefs/pref_service.h" 18 #include "chrome/browser/prefs/pref_service.h"
19 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/browser/sessions/session_id.h" 20 #include "chrome/browser/sessions/session_id.h"
21 #include "chrome/browser/sync/glue/device_info.h"
21 #include "chrome/browser/sync/glue/synced_session.h" 22 #include "chrome/browser/sync/glue/synced_session.h"
22 #include "chrome/browser/sync/glue/synced_tab_delegate.h" 23 #include "chrome/browser/sync/glue/synced_tab_delegate.h"
23 #include "chrome/browser/sync/glue/synced_window_delegate.h" 24 #include "chrome/browser/sync/glue/synced_window_delegate.h"
24 #include "chrome/browser/sync/profile_sync_service.h" 25 #include "chrome/browser/sync/profile_sync_service.h"
25 #include "chrome/common/chrome_notification_types.h" 26 #include "chrome/common/chrome_notification_types.h"
26 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
27 #include "chrome/common/pref_names.h" 28 #include "chrome/common/pref_names.h"
28 #include "chrome/common/url_constants.h" 29 #include "chrome/common/url_constants.h"
29 #include "content/public/browser/navigation_entry.h" 30 #include "content/public/browser/navigation_entry.h"
30 #include "content/public/browser/notification_details.h" 31 #include "content/public/browser/notification_details.h"
31 #include "content/public/browser/notification_service.h" 32 #include "content/public/browser/notification_service.h"
32 #include "sync/api/sync_error.h" 33 #include "sync/api/sync_error.h"
33 #include "sync/internal_api/public/base/model_type.h" 34 #include "sync/internal_api/public/base/model_type.h"
34 #include "sync/internal_api/public/base/model_type_state_map.h" 35 #include "sync/internal_api/public/base/model_type_state_map.h"
35 #include "sync/internal_api/public/read_node.h" 36 #include "sync/internal_api/public/read_node.h"
36 #include "sync/internal_api/public/read_transaction.h" 37 #include "sync/internal_api/public/read_transaction.h"
38 #include "sync/internal_api/public/user_share.h"
37 #include "sync/internal_api/public/write_node.h" 39 #include "sync/internal_api/public/write_node.h"
38 #include "sync/internal_api/public/write_transaction.h" 40 #include "sync/internal_api/public/write_transaction.h"
39 #include "sync/protocol/session_specifics.pb.h" 41 #include "sync/protocol/session_specifics.pb.h"
40 #include "sync/syncable/directory.h"
41 #include "sync/syncable/read_transaction.h"
42 #include "sync/syncable/write_transaction.h"
43 #include "sync/util/get_session_name.h" 42 #include "sync/util/get_session_name.h"
44 #include "sync/util/time.h" 43 #include "sync/util/time.h"
45 #include "ui/gfx/favicon_size.h" 44 #include "ui/gfx/favicon_size.h"
46 #if defined(OS_LINUX) 45 #if defined(OS_LINUX)
47 #include "base/linux_util.h" 46 #include "base/linux_util.h"
48 #elif defined(OS_WIN) 47 #elif defined(OS_WIN)
49 #include <windows.h> 48 #include <windows.h>
50 #elif defined(OS_ANDROID) 49 #elif defined(OS_ANDROID)
51 #include "sync/util/session_utils_android.h" 50 #include "sync/util/session_utils_android.h"
52 #endif 51 #endif
(...skipping 16 matching lines...) Expand all
69 // Default number of days without activity after which a session is considered 68 // Default number of days without activity after which a session is considered
70 // stale and becomes a candidate for garbage collection. 69 // stale and becomes a candidate for garbage collection.
71 static const size_t kDefaultStaleSessionThresholdDays = 14; // 2 weeks. 70 static const size_t kDefaultStaleSessionThresholdDays = 14; // 2 weeks.
72 71
73 #if defined(OS_ANDROID) 72 #if defined(OS_ANDROID)
74 bool IsTabletUI() { 73 bool IsTabletUI() {
75 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI); 74 return CommandLine::ForCurrentProcess()->HasSwitch(switches::kTabletUI);
76 } 75 }
77 #endif 76 #endif
78 77
79 sync_pb::SessionHeader::DeviceType GetLocalDeviceType() {
80 // TODO(yfriedman): Refactor/combine with "DeviceInformation" code in
81 // sync_manager.cc[1060]
82 #if defined(OS_CHROMEOS)
83 return sync_pb::SessionHeader_DeviceType_TYPE_CROS;
84 #elif defined(OS_LINUX)
85 return sync_pb::SessionHeader_DeviceType_TYPE_LINUX;
86 #elif defined(OS_MACOSX)
87 return sync_pb::SessionHeader_DeviceType_TYPE_MAC;
88 #elif defined(OS_WIN)
89 return sync_pb::SessionHeader_DeviceType_TYPE_WIN;
90 #elif defined(OS_ANDROID)
91 return IsTabletUI() ?
92 sync_pb::SessionHeader_DeviceType_TYPE_TABLET :
93 sync_pb::SessionHeader_DeviceType_TYPE_PHONE;
94 #else
95 return sync_pb::SessionHeader_DeviceType_TYPE_OTHER;
96 #endif
97 }
98
99 } // namespace 78 } // namespace
100 79
101 SessionModelAssociator::SessionModelAssociator(ProfileSyncService* sync_service, 80 SessionModelAssociator::SessionModelAssociator(ProfileSyncService* sync_service,
102 DataTypeErrorHandler* error_handler) 81 DataTypeErrorHandler* error_handler)
103 : tab_pool_(sync_service), 82 : tab_pool_(sync_service),
104 local_session_syncid_(syncer::kInvalidId), 83 local_session_syncid_(syncer::kInvalidId),
105 sync_service_(sync_service), 84 sync_service_(sync_service),
106 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays), 85 stale_session_threshold_days_(kDefaultStaleSessionThresholdDays),
107 setup_for_test_(false), 86 setup_for_test_(false),
108 waiting_for_change_(false), 87 waiting_for_change_(false),
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 syncer::SyncError* error) { 175 syncer::SyncError* error) {
197 DCHECK(CalledOnValidThread()); 176 DCHECK(CalledOnValidThread());
198 std::string local_tag = GetCurrentMachineTag(); 177 std::string local_tag = GetCurrentMachineTag();
199 sync_pb::SessionSpecifics specifics; 178 sync_pb::SessionSpecifics specifics;
200 specifics.set_session_tag(local_tag); 179 specifics.set_session_tag(local_tag);
201 sync_pb::SessionHeader* header_s = specifics.mutable_header(); 180 sync_pb::SessionHeader* header_s = specifics.mutable_header();
202 SyncedSession* current_session = 181 SyncedSession* current_session =
203 synced_session_tracker_.GetSession(local_tag); 182 synced_session_tracker_.GetSession(local_tag);
204 current_session->modified_time = base::Time::Now(); 183 current_session->modified_time = base::Time::Now();
205 header_s->set_client_name(current_session_name_); 184 header_s->set_client_name(current_session_name_);
206 header_s->set_device_type(GetLocalDeviceType()); 185 header_s->set_device_type(DeviceInfo::GetLocalDeviceType());
207 186
208 synced_session_tracker_.ResetSessionTracking(local_tag); 187 synced_session_tracker_.ResetSessionTracking(local_tag);
209 std::set<SyncedWindowDelegate*> windows = 188 std::set<SyncedWindowDelegate*> windows =
210 SyncedWindowDelegate::GetSyncedWindowDelegates(); 189 SyncedWindowDelegate::GetSyncedWindowDelegates();
211 for (std::set<SyncedWindowDelegate*>::const_iterator i = 190 for (std::set<SyncedWindowDelegate*>::const_iterator i =
212 windows.begin(); i != windows.end(); ++i) { 191 windows.begin(); i != windows.end(); ++i) {
213 // Make sure the window has tabs and a viewable window. The viewable window 192 // Make sure the window has tabs and a viewable window. The viewable window
214 // check is necessary because, for example, when a browser is closed the 193 // check is necessary because, for example, when a browser is closed the
215 // destructor is not necessarily run immediately. This means its possible 194 // destructor is not necessarily run immediately. This means its possible
216 // for us to get a handle to a browser that is about to be removed. If 195 // for us to get a handle to a browser that is about to be removed. If
(...skipping 539 matching lines...) Expand 10 before | Expand all | Expand 10 after
756 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(model_type())) != 735 if (root.InitByTagLookup(syncer::ModelTypeToRootTag(model_type())) !=
757 syncer::BaseNode::INIT_OK) { 736 syncer::BaseNode::INIT_OK) {
758 return error_handler_->CreateAndUploadError( 737 return error_handler_->CreateAndUploadError(
759 FROM_HERE, 738 FROM_HERE,
760 kNoSessionsFolderError, 739 kNoSessionsFolderError,
761 model_type()); 740 model_type());
762 } 741 }
763 742
764 // Make sure we have a machine tag. 743 // Make sure we have a machine tag.
765 if (current_machine_tag_.empty()) { 744 if (current_machine_tag_.empty()) {
766 InitializeCurrentMachineTag(&trans); 745 InitializeCurrentMachineTag(sync_service_->GetUserShare());
767 // The session name is retrieved asynchronously so it might not come back 746 // The session name is retrieved asynchronously so it might not come back
768 // for the writing of the session. However, we write to the session often 747 // for the writing of the session. However, we write to the session often
769 // enough (on every navigation) that we'll pick it up quickly. 748 // enough (on every navigation) that we'll pick it up quickly.
770 InitializeCurrentSessionName(); 749 InitializeCurrentSessionName();
771 } 750 }
772 synced_session_tracker_.SetLocalSessionTag(current_machine_tag_); 751 synced_session_tracker_.SetLocalSessionTag(current_machine_tag_);
773 if (!UpdateAssociationsFromSyncModel(root, &trans, &error)) { 752 if (!UpdateAssociationsFromSyncModel(root, &trans, &error)) {
774 DCHECK(error.IsSet()); 753 DCHECK(error.IsSet());
775 return error; 754 return error;
776 } 755 }
(...skipping 13 matching lines...) Expand all
790 model_type()); 769 model_type());
791 } 770 }
792 771
793 // Write the initial values to the specifics so that in case of a crash or 772 // Write the initial values to the specifics so that in case of a crash or
794 // error we don't persist a half-written node. 773 // error we don't persist a half-written node.
795 write_node.SetTitle(UTF8ToWide(current_machine_tag_)); 774 write_node.SetTitle(UTF8ToWide(current_machine_tag_));
796 sync_pb::SessionSpecifics base_specifics; 775 sync_pb::SessionSpecifics base_specifics;
797 base_specifics.set_session_tag(current_machine_tag_); 776 base_specifics.set_session_tag(current_machine_tag_);
798 sync_pb::SessionHeader* header_s = base_specifics.mutable_header(); 777 sync_pb::SessionHeader* header_s = base_specifics.mutable_header();
799 header_s->set_client_name(current_session_name_); 778 header_s->set_client_name(current_session_name_);
800 header_s->set_device_type(GetLocalDeviceType()); 779 header_s->set_device_type(DeviceInfo::GetLocalDeviceType());
801 write_node.SetSessionSpecifics(base_specifics); 780 write_node.SetSessionSpecifics(base_specifics);
802 781
803 local_session_syncid_ = write_node.GetId(); 782 local_session_syncid_ = write_node.GetId();
804 } 783 }
805 } 784 }
806 785
807 // Check if anything has changed on the client side. 786 // Check if anything has changed on the client side.
808 if (!UpdateSyncModelDataFromClient(&error)) { 787 if (!UpdateSyncModelDataFromClient(&error)) {
809 DCHECK(error.IsSet()); 788 DCHECK(error.IsSet());
810 return error; 789 return error;
(...skipping 20 matching lines...) Expand all
831 // There is no local model stored with which to disassociate, just notify 810 // There is no local model stored with which to disassociate, just notify
832 // foreign session handlers. 811 // foreign session handlers.
833 content::NotificationService::current()->Notify( 812 content::NotificationService::current()->Notify(
834 chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED, 813 chrome::NOTIFICATION_FOREIGN_SESSION_DISABLED,
835 content::Source<Profile>(sync_service_->profile()), 814 content::Source<Profile>(sync_service_->profile()),
836 content::NotificationService::NoDetails()); 815 content::NotificationService::NoDetails());
837 return syncer::SyncError(); 816 return syncer::SyncError();
838 } 817 }
839 818
840 void SessionModelAssociator::InitializeCurrentMachineTag( 819 void SessionModelAssociator::InitializeCurrentMachineTag(
841 syncer::WriteTransaction* trans) { 820 syncer::UserShare* share) {
842 DCHECK(CalledOnValidThread()); 821 DCHECK(CalledOnValidThread());
843 DCHECK(current_machine_tag_.empty()); 822 DCHECK(current_machine_tag_.empty());
844 std::string persisted_guid; 823 std::string persisted_guid;
845 if (pref_service_) 824 if (pref_service_)
846 persisted_guid = pref_service_->GetString(kSyncSessionsGUID); 825 persisted_guid = pref_service_->GetString(kSyncSessionsGUID);
847 if (!persisted_guid.empty()) { 826 if (!persisted_guid.empty()) {
848 current_machine_tag_ = persisted_guid; 827 current_machine_tag_ = persisted_guid;
849 DVLOG(1) << "Restoring persisted session sync guid: " 828 DVLOG(1) << "Restoring persisted session sync guid: "
850 << persisted_guid; 829 << persisted_guid;
851 } else { 830 } else {
852 syncer::syncable::Directory* dir =
853 trans->GetWrappedWriteTrans()->directory();
854 current_machine_tag_ = "session_sync"; 831 current_machine_tag_ = "session_sync";
855 #if defined(OS_ANDROID) 832 #if defined(OS_ANDROID)
856 const std::string android_id = syncer::internal::GetAndroidId(); 833 const std::string android_id = syncer::internal::GetAndroidId();
857 // There are reports that sometimes the android_id can't be read. Those 834 // There are reports that sometimes the android_id can't be read. Those
858 // are supposed to be fixed as of Gingerbread, but if it happens we fall 835 // are supposed to be fixed as of Gingerbread, but if it happens we fall
859 // back to use the same GUID generation as on other platforms. 836 // back to use the same GUID generation as on other platforms.
860 current_machine_tag_.append(android_id.empty() ? 837 current_machine_tag_.append(android_id.empty() ?
861 dir->cache_guid() : android_id); 838 share->cache_guid() : android_id);
862 #else 839 #else
863 current_machine_tag_.append(dir->cache_guid()); 840 current_machine_tag_.append(share->cache_guid());
864 #endif 841 #endif
865 DVLOG(1) << "Creating session sync guid: " << current_machine_tag_; 842 DVLOG(1) << "Creating session sync guid: " << current_machine_tag_;
866 if (pref_service_) 843 if (pref_service_)
867 pref_service_->SetString(kSyncSessionsGUID, current_machine_tag_); 844 pref_service_->SetString(kSyncSessionsGUID, current_machine_tag_);
868 } 845 }
869 846
870 tab_pool_.set_machine_tag(current_machine_tag_); 847 tab_pool_.set_machine_tag(current_machine_tag_);
871 } 848 }
872 849
873 void SessionModelAssociator::OnSessionNameInitialized( 850 void SessionModelAssociator::OnSessionNameInitialized(
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after
1116 // Static 1093 // Static
1117 void SessionModelAssociator::PopulateSessionHeaderFromSpecifics( 1094 void SessionModelAssociator::PopulateSessionHeaderFromSpecifics(
1118 const sync_pb::SessionHeader& header_specifics, 1095 const sync_pb::SessionHeader& header_specifics,
1119 const base::Time& mtime, 1096 const base::Time& mtime,
1120 SyncedSession* session_header) { 1097 SyncedSession* session_header) {
1121 if (header_specifics.has_client_name()) { 1098 if (header_specifics.has_client_name()) {
1122 session_header->session_name = header_specifics.client_name(); 1099 session_header->session_name = header_specifics.client_name();
1123 } 1100 }
1124 if (header_specifics.has_device_type()) { 1101 if (header_specifics.has_device_type()) {
1125 switch (header_specifics.device_type()) { 1102 switch (header_specifics.device_type()) {
1126 case sync_pb::SessionHeader_DeviceType_TYPE_WIN: 1103 case sync_pb::SyncEnums_DeviceType_TYPE_WIN:
1127 session_header->device_type = SyncedSession::TYPE_WIN; 1104 session_header->device_type = SyncedSession::TYPE_WIN;
1128 break; 1105 break;
1129 case sync_pb::SessionHeader_DeviceType_TYPE_MAC: 1106 case sync_pb::SyncEnums_DeviceType_TYPE_MAC:
1130 session_header->device_type = SyncedSession::TYPE_MACOSX; 1107 session_header->device_type = SyncedSession::TYPE_MACOSX;
1131 break; 1108 break;
1132 case sync_pb::SessionHeader_DeviceType_TYPE_LINUX: 1109 case sync_pb::SyncEnums_DeviceType_TYPE_LINUX:
1133 session_header->device_type = SyncedSession::TYPE_LINUX; 1110 session_header->device_type = SyncedSession::TYPE_LINUX;
1134 break; 1111 break;
1135 case sync_pb::SessionHeader_DeviceType_TYPE_CROS: 1112 case sync_pb::SyncEnums_DeviceType_TYPE_CROS:
1136 session_header->device_type = SyncedSession::TYPE_CHROMEOS; 1113 session_header->device_type = SyncedSession::TYPE_CHROMEOS;
1137 break; 1114 break;
1138 case sync_pb::SessionHeader_DeviceType_TYPE_PHONE: 1115 case sync_pb::SyncEnums_DeviceType_TYPE_PHONE:
1139 session_header->device_type = SyncedSession::TYPE_PHONE; 1116 session_header->device_type = SyncedSession::TYPE_PHONE;
1140 break; 1117 break;
1141 case sync_pb::SessionHeader_DeviceType_TYPE_TABLET: 1118 case sync_pb::SyncEnums_DeviceType_TYPE_TABLET:
1142 session_header->device_type = SyncedSession::TYPE_TABLET; 1119 session_header->device_type = SyncedSession::TYPE_TABLET;
1143 break; 1120 break;
1144 case sync_pb::SessionHeader_DeviceType_TYPE_OTHER: 1121 case sync_pb::SyncEnums_DeviceType_TYPE_OTHER:
1145 // Intentionally fall-through 1122 // Intentionally fall-through
1146 default: 1123 default:
1147 session_header->device_type = SyncedSession::TYPE_OTHER; 1124 session_header->device_type = SyncedSession::TYPE_OTHER;
1148 break; 1125 break;
1149 } 1126 }
1150 } 1127 }
1151 session_header->modified_time = mtime; 1128 session_header->modified_time = mtime;
1152 } 1129 }
1153 1130
1154 // Static 1131 // Static
(...skipping 442 matching lines...) Expand 10 before | Expand all | Expand 10 after
1597 1574
1598 bool SessionModelAssociator::CryptoReadyIfNecessary() { 1575 bool SessionModelAssociator::CryptoReadyIfNecessary() {
1599 // We only access the cryptographer while holding a transaction. 1576 // We only access the cryptographer while holding a transaction.
1600 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare()); 1577 syncer::ReadTransaction trans(FROM_HERE, sync_service_->GetUserShare());
1601 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes(); 1578 const syncer::ModelTypeSet encrypted_types = trans.GetEncryptedTypes();
1602 return !encrypted_types.Has(SESSIONS) || 1579 return !encrypted_types.Has(SESSIONS) ||
1603 sync_service_->IsCryptographerReady(&trans); 1580 sync_service_->IsCryptographerReady(&trans);
1604 } 1581 }
1605 1582
1606 } // namespace browser_sync 1583 } // namespace browser_sync
OLDNEW
« no previous file with comments | « chrome/browser/sync/glue/session_model_associator.h ('k') | chrome/browser/sync/glue/session_model_associator_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698