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 "mojo/services/test_service/test_service_impl.h" 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/test_request_tracker_client_impl.h" 12 #include "mojo/services/test_service/test_service_application.h" 13 #include "mojo/services/test_service/test_time_service_impl.h" 14 15 namespace mojo { 16 namespace test { 17 18 TestServiceImpl::TestServiceImpl(ApplicationConnection* connection, 19 TestServiceApplication* application) 20 : application_(application), connection_(connection) { 21 } 22 23 TestServiceImpl::~TestServiceImpl() { 24 } 25 26 void TestServiceImpl::OnConnectionEstablished() { 27 application_->AddRef(); 28 } 29 30 void TestServiceImpl::OnConnectionError() { 31 application_->ReleaseRef(); 32 } 33 34 void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) { 35 if (tracking_) 36 tracking_->RecordNewRequest(); 37 callback.Run(); 38 } 39 40 void SendTimeResponse( 41 const mojo::Callback<void(int64_t)>& requestor_callback, 42 int64_t time_usec) { 43 requestor_callback.Run(time_usec); 44 } 45 46 void TestServiceImpl::ConnectToAppAndGetTime( 47 const mojo::String& app_url, 48 const mojo::Callback<void(int64_t)>& callback) { 49 connection_->ConnectToService(app_url, &time_service_); 50 if (tracking_) { 51 tracking_->RecordNewRequest(); 52 time_service_->StartTrackingRequests(mojo::Callback<void()>()); 53 } 54 time_service_->GetPartyTime(base::Bind(&SendTimeResponse, callback)); 55 } 56 57 void TestServiceImpl::StartTrackingRequests( 58 const mojo::Callback<void()>& callback) { 59 TestRequestTrackerPtr tracker; 60 connection_->ConnectToService( 61 "mojo:mojo_test_request_tracker_app", &tracker); 62 tracking_.reset( 63 new TestRequestTrackerClientImpl(tracker.Pass(), Name_, callback)); 64 } 65 66 } // namespace test 67 } // namespace mojo 68