Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / remoting / client / log_to_server.cc
1 // Copyright 2014 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/client/log_to_server.h"
6
7 #include "base/macros.h"
8 #include "base/rand_util.h"
9 #include "remoting/base/constants.h"
10 #include "remoting/client/chromoting_stats.h"
11 #include "remoting/jingle_glue/iq_sender.h"
12 #include "remoting/jingle_glue/signal_strategy.h"
13 #include "third_party/libjingle/source/talk/xmllite/xmlelement.h"
14 #include "third_party/libjingle/source/talk/xmpp/constants.h"
15
16 using buzz::QName;
17 using buzz::XmlElement;
18 using remoting::protocol::ConnectionToHost;
19
20 namespace {
21
22 const char kSessionIdAlphabet[] =
23     "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
24 const int kSessionIdLength = 20;
25
26 const int kMaxSessionIdAgeDays = 1;
27
28 bool IsStartOfSession(ConnectionToHost::State state) {
29   return state == ConnectionToHost::INITIALIZING ||
30       state == ConnectionToHost::CONNECTING ||
31       state == ConnectionToHost::AUTHENTICATED ||
32       state == ConnectionToHost::CONNECTED;
33 }
34
35 bool IsEndOfSession(ConnectionToHost::State state) {
36   return state == ConnectionToHost::FAILED ||
37       state == ConnectionToHost::CLOSED;
38 }
39
40 bool ShouldAddDuration(ConnectionToHost::State state) {
41   // Duration is added to log entries at the end of the session, as well as at
42   // some intermediate states where it is relevant (e.g. to determine how long
43   // it took for a session to become CONNECTED).
44   return IsEndOfSession(state) || state == ConnectionToHost::CONNECTED;
45 }
46
47 }  // namespace
48
49 namespace remoting {
50
51 namespace client {
52
53 LogToServer::LogToServer(ServerLogEntry::Mode mode,
54                          SignalStrategy* signal_strategy,
55                          const std::string& directory_bot_jid)
56     : mode_(mode),
57       signal_strategy_(signal_strategy),
58       directory_bot_jid_(directory_bot_jid) {
59   signal_strategy_->AddListener(this);
60 }
61
62 LogToServer::~LogToServer() {
63   signal_strategy_->RemoveListener(this);
64 }
65
66 void LogToServer::LogSessionStateChange(
67     protocol::ConnectionToHost::State state,
68     protocol::ErrorCode error) {
69   DCHECK(CalledOnValidThread());
70
71   scoped_ptr<ServerLogEntry> entry(
72       ServerLogEntry::MakeForSessionStateChange(state, error));
73   entry->AddClientFields();
74   entry->AddModeField(mode_);
75
76   MaybeExpireSessionId();
77   if (IsStartOfSession(state)) {
78     // Maybe set the session ID and start time.
79     if (session_id_.empty()) {
80       GenerateSessionId();
81     }
82     if (session_start_time_.is_null()) {
83       session_start_time_ = base::TimeTicks::Now();
84     }
85   }
86
87   if (!session_id_.empty()) {
88     entry->AddSessionId(session_id_);
89   }
90
91   // Maybe clear the session start time and log the session duration.
92   if (ShouldAddDuration(state) && !session_start_time_.is_null()) {
93     entry->AddSessionDuration(base::TimeTicks::Now() - session_start_time_);
94   }
95
96   if (IsEndOfSession(state)) {
97     session_start_time_ = base::TimeTicks();
98     session_id_.clear();
99   }
100
101   Log(*entry.get());
102 }
103
104 void LogToServer::LogStatistics(ChromotingStats* statistics) {
105   DCHECK(CalledOnValidThread());
106
107   MaybeExpireSessionId();
108
109   scoped_ptr<ServerLogEntry> entry(
110       ServerLogEntry::MakeForStatistics(statistics));
111   entry->AddClientFields();
112   entry->AddModeField(mode_);
113   entry->AddSessionId(session_id_);
114   Log(*entry.get());
115 }
116
117 void LogToServer::OnSignalStrategyStateChange(SignalStrategy::State state) {
118   DCHECK(CalledOnValidThread());
119
120   if (state == SignalStrategy::CONNECTED) {
121     iq_sender_.reset(new IqSender(signal_strategy_));
122     SendPendingEntries();
123   } else if (state == SignalStrategy::DISCONNECTED) {
124     iq_sender_.reset();
125   }
126 }
127
128 bool LogToServer::OnSignalStrategyIncomingStanza(
129     const buzz::XmlElement* stanza) {
130   return false;
131 }
132
133 void LogToServer::Log(const ServerLogEntry& entry) {
134   pending_entries_.push_back(entry);
135   SendPendingEntries();
136 }
137
138 void LogToServer::SendPendingEntries() {
139   if (iq_sender_ == NULL) {
140     return;
141   }
142   if (pending_entries_.empty()) {
143     return;
144   }
145   // Make one stanza containing all the pending entries.
146   scoped_ptr<XmlElement> stanza(ServerLogEntry::MakeStanza());
147   while (!pending_entries_.empty()) {
148     ServerLogEntry& entry = pending_entries_.front();
149     stanza->AddElement(entry.ToStanza().release());
150     pending_entries_.pop_front();
151   }
152   // Send the stanza to the server.
153   scoped_ptr<IqRequest> req = iq_sender_->SendIq(
154       buzz::STR_SET, directory_bot_jid_, stanza.Pass(),
155       IqSender::ReplyCallback());
156   // We ignore any response, so let the IqRequest be destroyed.
157   return;
158 }
159
160 void LogToServer::GenerateSessionId() {
161   session_id_.resize(kSessionIdLength);
162   for (int i = 0; i < kSessionIdLength; i++) {
163     const int alphabet_size = arraysize(kSessionIdAlphabet) - 1;
164     session_id_[i] = kSessionIdAlphabet[base::RandGenerator(alphabet_size)];
165   }
166   session_id_generation_time_ = base::TimeTicks::Now();
167 }
168
169 void LogToServer::MaybeExpireSessionId() {
170   if (session_id_.empty()) {
171     return;
172   }
173
174   base::TimeDelta max_age = base::TimeDelta::FromDays(kMaxSessionIdAgeDays);
175   if (base::TimeTicks::Now() - session_id_generation_time_ > max_age) {
176     // Log the old session ID.
177     scoped_ptr<ServerLogEntry> entry(
178         ServerLogEntry::MakeForSessionIdOld(session_id_));
179     entry->AddModeField(mode_);
180     Log(*entry.get());
181
182     // Generate a new session ID.
183     GenerateSessionId();
184
185     // Log the new session ID.
186     entry = ServerLogEntry::MakeForSessionIdNew(session_id_);
187     entry->AddModeField(mode_);
188     Log(*entry.get());
189   }
190 }
191
192 }  // namespace client
193
194 }  // namespace remoting