James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 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 | |
James Robinson | 63b637a | 2014-12-03 14:46:39 -0800 | [diff] [blame] | 5 | #include "services/test_service/test_service_impl.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 6 | |
| 7 | #include "base/bind.h" |
| 8 | #include "base/i18n/time_formatting.h" |
| 9 | #include "base/strings/utf_string_conversions.h" |
Viet-Trung Luu | c1248b2 | 2016-04-26 13:41:55 -0700 | [diff] [blame] | 10 | #include "mojo/public/cpp/application/connect.h" |
James Robinson | 63b637a | 2014-12-03 14:46:39 -0800 | [diff] [blame] | 11 | #include "services/test_service/test_service_application.h" |
| 12 | #include "services/test_service/test_time_service_impl.h" |
James Robinson | 51b4cd6 | 2015-02-04 11:27:40 -0800 | [diff] [blame] | 13 | #include "services/test_service/tracked_service.h" |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 14 | |
| 15 | namespace mojo { |
| 16 | namespace test { |
| 17 | |
Viet-Trung Luu | 7d57915 | 2016-05-23 13:06:41 -0700 | [diff] [blame^] | 18 | TestServiceImpl::TestServiceImpl(TestServiceApplication* application, |
James Robinson | ab60bd3 | 2015-02-12 13:02:24 -0800 | [diff] [blame] | 19 | InterfaceRequest<TestService> request) |
Viet-Trung Luu | 7d57915 | 2016-05-23 13:06:41 -0700 | [diff] [blame^] | 20 | : application_(application), binding_(this, request.Pass()) { |
Viet-Trung Luu | d1892f2 | 2015-07-08 13:32:51 -0700 | [diff] [blame] | 21 | binding_.set_connection_error_handler( |
| 22 | [this]() { application_->ReleaseRef(); }); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Viet-Trung Luu | 7d57915 | 2016-05-23 13:06:41 -0700 | [diff] [blame^] | 25 | TestServiceImpl::~TestServiceImpl() {} |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 26 | |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 27 | void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) { |
| 28 | if (tracking_) |
| 29 | tracking_->RecordNewRequest(); |
| 30 | callback.Run(); |
| 31 | } |
| 32 | |
| 33 | void SendTimeResponse( |
| 34 | const mojo::Callback<void(int64_t)>& requestor_callback, |
| 35 | int64_t time_usec) { |
| 36 | requestor_callback.Run(time_usec); |
| 37 | } |
| 38 | |
| 39 | void TestServiceImpl::ConnectToAppAndGetTime( |
| 40 | const mojo::String& app_url, |
| 41 | const mojo::Callback<void(int64_t)>& callback) { |
Viet-Trung Luu | 7d57915 | 2016-05-23 13:06:41 -0700 | [diff] [blame^] | 42 | ConnectToService(application_->shell(), app_url, GetProxy(&time_service_)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 43 | if (tracking_) { |
| 44 | tracking_->RecordNewRequest(); |
| 45 | time_service_->StartTrackingRequests(mojo::Callback<void()>()); |
| 46 | } |
| 47 | time_service_->GetPartyTime(base::Bind(&SendTimeResponse, callback)); |
| 48 | } |
| 49 | |
| 50 | void TestServiceImpl::StartTrackingRequests( |
| 51 | const mojo::Callback<void()>& callback) { |
| 52 | TestRequestTrackerPtr tracker; |
Viet-Trung Luu | 7d57915 | 2016-05-23 13:06:41 -0700 | [diff] [blame^] | 53 | ConnectToService(application_->shell(), "mojo:test_request_tracker_app", |
Viet-Trung Luu | c1248b2 | 2016-04-26 13:41:55 -0700 | [diff] [blame] | 54 | GetProxy(&tracker)); |
James Robinson | 51b4cd6 | 2015-02-04 11:27:40 -0800 | [diff] [blame] | 55 | tracking_.reset(new TrackedService(tracker.Pass(), Name_, callback)); |
James Robinson | 646469d | 2014-10-03 15:33:28 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | } // namespace test |
| 59 | } // namespace mojo |