OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "mojo/services/test_service/test_service_impl.h" | 5 #include "mojo/services/test_service/test_service_impl.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/i18n/time_formatting.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" |
| 11 #include "mojo/services/test_service/monitoring_uploader.h" |
7 #include "mojo/services/test_service/test_service_application.h" | 12 #include "mojo/services/test_service/test_service_application.h" |
| 13 #include "mojo/services/test_service/test_time_service_impl.h" |
| 14 #include "url/gurl.h" |
8 | 15 |
9 namespace mojo { | 16 namespace mojo { |
10 namespace test { | 17 namespace test { |
11 | 18 |
12 TestServiceImpl::TestServiceImpl(ApplicationConnection* connection, | 19 TestServiceImpl::TestServiceImpl(ApplicationConnection* connection, |
13 TestServiceApplication* application) | 20 TestServiceApplication* application) |
14 : application_(application) { | 21 : application_(application), connection_(connection) { |
15 } | 22 } |
16 | 23 |
17 TestServiceImpl::~TestServiceImpl() { | 24 TestServiceImpl::~TestServiceImpl() { |
18 } | 25 } |
19 | 26 |
20 void TestServiceImpl::OnConnectionEstablished() { | 27 void TestServiceImpl::OnConnectionEstablished() { |
21 application_->AddRef(); | 28 application_->AddRef(); |
22 } | 29 } |
23 | 30 |
24 void TestServiceImpl::OnConnectionError() { | 31 void TestServiceImpl::OnConnectionError() { |
25 application_->ReleaseRef(); | 32 application_->ReleaseRef(); |
26 } | 33 } |
27 | 34 |
28 void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) { | 35 void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) { |
| 36 if (monitoring_) |
| 37 monitoring_->RecordNewRequest(); |
29 callback.Run(); | 38 callback.Run(); |
30 } | 39 } |
31 | 40 |
| 41 void SendWelcomeMessageWithTime( |
| 42 const mojo::Callback<void(mojo::String)>& requestor_callback, |
| 43 int64_t time_usec) { |
| 44 std::string friendly_msg(TestServiceImpl::kWelcomeMessagePrefix); |
| 45 friendly_msg += UTF16ToUTF8(base::TimeFormatFriendlyDateAndTime( |
| 46 base::Time::FromInternalValue(time_usec))); |
| 47 requestor_callback.Run(friendly_msg); |
| 48 } |
| 49 |
| 50 void TestServiceImpl::GetFriendlyWelcomeMessage( |
| 51 const mojo::String& time_app_url, |
| 52 const mojo::Callback<void(mojo::String)>& callback) { |
| 53 connection_->ConnectToService(time_app_url, &time_service_); |
| 54 if (monitoring_) { |
| 55 monitoring_->RecordNewRequest(); |
| 56 time_service_->StartMonitoring(mojo::Callback<void()>()); |
| 57 } |
| 58 time_service_->GetPartyTime(base::Bind( |
| 59 &SendWelcomeMessageWithTime, |
| 60 callback)); |
| 61 } |
| 62 |
| 63 void TestServiceImpl::StartMonitoring(const mojo::Callback<void()>& callback) { |
| 64 ToyMonitoringServiceStatsRecorderPtr recorder; |
| 65 connection_->ConnectToService("mojo:mojo_test_monitoring_app", &recorder); |
| 66 monitoring_.reset(new MonitoringUploader(recorder.Pass(), Name_, callback)); |
| 67 } |
| 68 |
32 } // namespace test | 69 } // namespace test |
33 } // namespace mojo | 70 } // namespace mojo |
OLD | NEW |