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 #include "remoting/host/server_log_entry.h" 6 7 #include "base/logging.h" 8 #include "base/strings/stringize_macros.h" 9 #include "base/sys_info.h" 10 #include "remoting/base/constants.h" 11 #include "remoting/protocol/session.h" 12 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h" 13 14 using base::SysInfo; 15 using buzz::QName; 16 using buzz::XmlElement; 17 using remoting::protocol::Session; 18 19 namespace remoting { 20 21 namespace { 22 const char kLogCommand[] = "log"; 23 24 const char kLogEntry[] = "entry"; 25 26 const char kKeyEventName[] = "event-name"; 27 const char kValueEventNameSessionState[] = "session-state"; 28 const char kValueEventNameHeartbeat[] = "heartbeat"; 29 const char kValueEventNameHostStatus[] = "host-status"; 30 31 const char kKeyRole[] = "role"; 32 const char kValueRoleHost[] = "host"; 33 34 const char kKeyMode[] = "mode"; 35 const char kValueModeIt2Me[] = "it2me"; 36 const char kValueModeMe2Me[] = "me2me"; 37 38 const char kKeySessionState[] = "session-state"; 39 const char kValueSessionStateConnected[] = "connected"; 40 const char kValueSessionStateClosed[] = "closed"; 41 42 const char kStatusName[] = "status"; 43 const char kExitCodeName[] = "exit-code"; 44 45 const char kKeyOsName[] = "os-name"; 46 const char kValueOsNameWindows[] = "Windows"; 47 const char kValueOsNameLinux[] = "Linux"; 48 const char kValueOsNameMac[] = "Mac"; 49 const char kValueOsNameChromeOS[] = "ChromeOS"; 50 51 const char kKeyOsVersion[] = "os-version"; 52 53 const char kKeyHostVersion[] = "host-version"; 54 55 const char kKeyCpu[] = "cpu"; 56 57 const char kKeyConnectionType[] = "connection-type"; 58 59 } // namespace 60 61 ServerLogEntry::ServerLogEntry() { 62 } 63 64 ServerLogEntry::~ServerLogEntry() { 65 } 66 67 // static 68 scoped_ptr<buzz::XmlElement> ServerLogEntry::MakeStanza() { 69 return scoped_ptr<buzz::XmlElement>( 70 new XmlElement(QName(kChromotingXmlNamespace, kLogCommand))); 71 } 72 73 // static 74 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForSessionStateChange( 75 bool connected) { 76 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); 77 entry->Set(kKeyRole, kValueRoleHost); 78 entry->Set(kKeyEventName, kValueEventNameSessionState); 79 entry->Set(kKeySessionState, GetValueSessionState(connected)); 80 return entry.Pass(); 81 } 82 83 // static 84 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForHeartbeat() { 85 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); 86 entry->Set(kKeyRole, kValueRoleHost); 87 entry->Set(kKeyEventName, kValueEventNameHeartbeat); 88 return entry.Pass(); 89 } 90 91 // static 92 scoped_ptr<ServerLogEntry> ServerLogEntry::MakeForHostStatus( 93 HostStatusSender::HostStatus host_status, HostExitCodes exit_code) { 94 scoped_ptr<ServerLogEntry> entry(new ServerLogEntry()); 95 entry->Set(kKeyRole, kValueRoleHost); 96 entry->Set(kKeyEventName, kValueEventNameHostStatus); 97 entry->Set(kStatusName, HostStatusSender::HostStatusToString(host_status)); 98 if (host_status == HostStatusSender::OFFLINE) 99 entry->Set(kExitCodeName, ExitCodeToString(exit_code)); 100 return entry.Pass(); 101 } 102 103 void ServerLogEntry::AddHostFields() { 104 #if defined(OS_WIN) 105 Set(kKeyOsName, kValueOsNameWindows); 106 #elif defined(OS_MACOSX) 107 Set(kKeyOsName, kValueOsNameMac); 108 #elif defined(OS_CHROMEOS) 109 Set(kKeyOsName, kValueOsNameChromeOS); 110 #elif defined(OS_LINUX) 111 Set(kKeyOsName, kValueOsNameLinux); 112 #endif 113 114 // SysInfo::OperatingSystemVersionNumbers is only defined for the following 115 // OSes: see base/sys_info_unittest.cc. 116 #if defined(OS_WIN) || defined(OS_MACOSX) || defined(OS_CHROMEOS) 117 std::stringstream os_version; 118 int32 os_major_version = 0; 119 int32 os_minor_version = 0; 120 int32 os_bugfix_version = 0; 121 SysInfo::OperatingSystemVersionNumbers(&os_major_version, &os_minor_version, 122 &os_bugfix_version); 123 os_version << os_major_version << "." << os_minor_version << "." 124 << os_bugfix_version; 125 Set(kKeyOsVersion, os_version.str()); 126 #endif 127 128 Set(kKeyHostVersion, STRINGIZE(VERSION)); 129 Set(kKeyCpu, SysInfo::OperatingSystemArchitecture()); 130 }; 131 132 void ServerLogEntry::AddModeField(ServerLogEntry::Mode mode) { 133 Set(kKeyMode, GetValueMode(mode)); 134 } 135 136 void ServerLogEntry::AddConnectionTypeField( 137 protocol::TransportRoute::RouteType type) { 138 Set(kKeyConnectionType, protocol::TransportRoute::GetTypeString(type)); 139 } 140 141 // static 142 const char* ServerLogEntry::GetValueMode(ServerLogEntry::Mode mode) { 143 switch (mode) { 144 case IT2ME: 145 return kValueModeIt2Me; 146 case ME2ME: 147 return kValueModeMe2Me; 148 default: 149 NOTREACHED(); 150 return NULL; 151 } 152 } 153 154 scoped_ptr<XmlElement> ServerLogEntry::ToStanza() const { 155 scoped_ptr<XmlElement> stanza(new XmlElement(QName( 156 kChromotingXmlNamespace, kLogEntry))); 157 ValuesMap::const_iterator iter; 158 for (iter = values_map_.begin(); iter != values_map_.end(); ++iter) { 159 stanza->AddAttr(QName(std::string(), iter->first), iter->second); 160 } 161 return stanza.Pass(); 162 } 163 164 // static 165 const char* ServerLogEntry::GetValueSessionState(bool connected) { 166 return connected ? kValueSessionStateConnected : kValueSessionStateClosed; 167 } 168 169 void ServerLogEntry::Set(const std::string& key, const std::string& value) { 170 values_map_[key] = value; 171 } 172 173 } // namespace remoting 174