Chromium Code Reviews| Index: chrome/browser/sync/glue/session_model_associator.cc |
| diff --git a/chrome/browser/sync/glue/session_model_associator.cc b/chrome/browser/sync/glue/session_model_associator.cc |
| index ea4eee306bb0bbd0728373c23140fc6cdd5311fd..04e023a2a89ffd63c15330e8277660a725ef7881 100644 |
| --- a/chrome/browser/sync/glue/session_model_associator.cc |
| +++ b/chrome/browser/sync/glue/session_model_associator.cc |
| @@ -26,6 +26,15 @@ |
| #include "content/browser/tab_contents/navigation_entry.h" |
| #include "content/common/notification_details.h" |
| #include "content/common/notification_service.h" |
| +#if defined(OS_LINUX) |
| +#include "base/linux_util.h" |
| +#elif defined(OS_MACOSX) |
| +#include <sys/sysctl.h> // sysctlbyname() |
| +#include "base/sys_info.h" |
| +#include "base/string_util.h" |
| +#elif defined(OS_WIN) |
| +#include <windows.h> |
| +#endif |
| namespace browser_sync { |
| @@ -38,6 +47,8 @@ static const char kNoSessionsFolderError[] = |
| static const int max_sync_navigation_count = 6; |
| } // namespace |
| +std::string SessionModelAssociator::current_machine_name_ = ""; |
|
Nicolas Zea
2011/08/30 21:29:58
You can remove this line (default value for a stri
Yaron
2011/08/31 03:26:12
But since it's static, I need to have a concrete i
Nicolas Zea
2011/08/31 18:23:00
Woops, I didn't read this properly. Yeah, this def
Yaron
2011/08/31 23:23:21
Done.
|
| + |
| SessionModelAssociator::SessionModelAssociator(ProfileSyncService* sync_service) |
| : tab_pool_(sync_service), |
| local_session_syncid_(sync_api::kInvalidId), |
| @@ -123,6 +134,18 @@ void SessionModelAssociator::ReassociateWindows(bool reload_tabs) { |
| sync_pb::SessionHeader* header_s = specifics.mutable_header(); |
| SyncedSession* current_session = |
| synced_session_tracker_.GetSession(local_tag); |
| + header_s->set_client_name(current_machine_name_); |
| +#if defined(OS_LINUX) |
| + header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_LINUX); |
| +#elif defined(OS_MACOSX) |
| + header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_MAC); |
| +#elif defined(OS_WIN) |
| + header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_WIN); |
| +#elif defined(OS_CHROMEOS) |
| + header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_CROS); |
| +#else |
| + header_s->set_device_type(sync_pb::SessionHeader_DeviceType_TYPE_OTHER); |
| +#endif |
| size_t window_num = 0; |
| std::set<SyncedWindowDelegate*> windows = |
| @@ -427,8 +450,10 @@ bool SessionModelAssociator::AssociateModels(SyncError* error) { |
| } |
| // Make sure we have a machine tag. |
| - if (current_machine_tag_.empty()) |
| + if (current_machine_tag_.empty()) { |
| InitializeCurrentMachineTag(&trans); |
| + InitializeCurrentMachineName(); |
| + } |
| synced_session_tracker_.SetLocalSessionTag(current_machine_tag_); |
| if (!UpdateAssociationsFromSyncModel(root, &trans)) { |
| error->Reset(FROM_HERE, |
| @@ -493,6 +518,40 @@ void SessionModelAssociator::InitializeCurrentMachineTag( |
| tab_pool_.set_machine_tag(current_machine_tag_); |
| } |
| +// Static |
| +void SessionModelAssociator::InitializeCurrentMachineName() { |
| +#if defined(OS_LINUX) |
| + current_machine_name_ = base::GetLinuxDistro(); |
|
Nicolas Zea
2011/08/30 21:29:58
should probably check for the "Unknown" case and r
Yaron
2011/08/31 03:26:12
Done
|
| +#elif defined(OS_MACOSX) |
| + char modelBuffer[256]; |
| + size_t length = sizeof(modelBuffer); |
| + if (!sysctlbyname("hw.model", modelBuffer, &length, NULL, 0)) { |
|
Nicolas Zea
2011/08/30 21:29:58
I'm not familiar with these API's, so my main conc
Yaron
2011/08/31 03:26:12
Split off GetComputerName to win_util and GetHardw
|
| + for (size_t i = 0; i < length; i++) { |
| + if (IsAsciiDigit(modelBuffer[i])) { |
| + current_machine_name_ = std::string(modelBuffer, 0, i); |
| + return; |
| + } |
| + } |
| + // If we didn't find a number in the string, Just use Mac OS X. |
|
Nicolas Zea
2011/08/30 21:29:58
Why not just use the full string?
Yaron
2011/08/31 03:26:12
Hmm. I suppose that could be better
|
| + current_machine_name_ = base::SysInfo::OperatingSystemName(); |
| + } else { |
| + // Can't read hardware model. Just use Mac OS X. |
| + current_machine_name_ = base::SysInfo::OperatingSystemName(); |
| + } |
| +#elif defined(OS_WIN) |
| + char computer_name[MAX_COMPUTERNAME_LENGTH + 1]; |
| + DWORD size = sizeof(computer_name); |
| + if (GetComputerNameA(computer_name, &size)) { |
| + current_machine_name_ = computer_name; |
| + } else { |
| + current_machine_name_ = "Windows"; |
| + } |
| +#elif defined(OS_CHROMEOS) |
| + current_machine_name_ = "Chromebook"; |
| +#endif |
|
Nicolas Zea
2011/08/30 21:29:58
else "Unknown OS"
Yaron
2011/08/31 03:26:12
I added a catch-all case instead (because it also
|
| +} |
| + |
| + |
| bool SessionModelAssociator::UpdateAssociationsFromSyncModel( |
| const sync_api::ReadNode& root, |
| const sync_api::BaseTransaction* trans) { |
| @@ -561,6 +620,7 @@ bool SessionModelAssociator::AssociateForeignSpecifics( |
| synced_session_tracker_.GetSession(foreign_session_tag); |
| const sync_pb::SessionHeader& header = specifics.header(); |
| + PopulateSessionHeaderFromSpecifics(header, foreign_session); |
| foreign_session->windows.reserve(header.window_size()); |
| VLOG(1) << "Associating " << foreign_session_tag << " with " << |
| header.window_size() << " windows."; |
| @@ -605,6 +665,36 @@ void SessionModelAssociator::DisassociateForeignSession( |
| } |
| // Static |
| +void SessionModelAssociator::PopulateSessionHeaderFromSpecifics( |
| + const sync_pb::SessionHeader& header_specifics, |
| + SyncedSession* session_header) { |
| + if (header_specifics.has_client_name()) { |
| + session_header->client_name = header_specifics.client_name(); |
| + } |
| + if (header_specifics.has_device_type()) { |
| + switch (header_specifics.device_type()) { |
| + case sync_pb::SessionHeader_DeviceType_TYPE_WIN: |
| + session_header->device_type = SyncedSession::TYPE_WIN; |
| + break; |
| + case sync_pb::SessionHeader_DeviceType_TYPE_MAC: |
| + session_header->device_type = SyncedSession::TYPE_MAC; |
| + break; |
| + case sync_pb::SessionHeader_DeviceType_TYPE_LINUX: |
| + session_header->device_type = SyncedSession::TYPE_LINUX; |
| + break; |
| + case sync_pb::SessionHeader_DeviceType_TYPE_CROS: |
| + session_header->device_type = SyncedSession::TYPE_CROS; |
| + break; |
| + case sync_pb::SessionHeader_DeviceType_TYPE_OTHER: |
| + // Intentionally fall-through |
| + default: |
| + session_header->device_type = SyncedSession::TYPE_OTHER; |
| + break; |
| + } |
| + } |
| +} |
| + |
| +// Static |
| void SessionModelAssociator::PopulateSessionWindowFromSpecifics( |
| const std::string& session_tag, |
| const sync_pb::SessionWindow& specifics, |