blob: bbd6a27e171921bceaafea62bfc617cec9265f7d [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// 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 Robinson63b637a2014-12-03 14:46:39 -08005#include "services/test_service/test_service_impl.h"
James Robinson646469d2014-10-03 15:33:28 -07006
7#include "base/bind.h"
8#include "base/i18n/time_formatting.h"
9#include "base/strings/utf_string_conversions.h"
Viet-Trung Luuc1248b22016-04-26 13:41:55 -070010#include "mojo/public/cpp/application/connect.h"
James Robinson63b637a2014-12-03 14:46:39 -080011#include "services/test_service/test_service_application.h"
12#include "services/test_service/test_time_service_impl.h"
James Robinson51b4cd62015-02-04 11:27:40 -080013#include "services/test_service/tracked_service.h"
James Robinson646469d2014-10-03 15:33:28 -070014
15namespace mojo {
16namespace test {
17
Viet-Trung Luu7d579152016-05-23 13:06:41 -070018TestServiceImpl::TestServiceImpl(TestServiceApplication* application,
James Robinsonab60bd32015-02-12 13:02:24 -080019 InterfaceRequest<TestService> request)
Viet-Trung Luu7d579152016-05-23 13:06:41 -070020 : application_(application), binding_(this, request.Pass()) {
Viet-Trung Luud1892f22015-07-08 13:32:51 -070021 binding_.set_connection_error_handler(
22 [this]() { application_->ReleaseRef(); });
James Robinson646469d2014-10-03 15:33:28 -070023}
24
Viet-Trung Luu7d579152016-05-23 13:06:41 -070025TestServiceImpl::~TestServiceImpl() {}
James Robinson646469d2014-10-03 15:33:28 -070026
James Robinson646469d2014-10-03 15:33:28 -070027void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) {
28 if (tracking_)
29 tracking_->RecordNewRequest();
30 callback.Run();
31}
32
33void SendTimeResponse(
34 const mojo::Callback<void(int64_t)>& requestor_callback,
35 int64_t time_usec) {
36 requestor_callback.Run(time_usec);
37}
38
39void TestServiceImpl::ConnectToAppAndGetTime(
40 const mojo::String& app_url,
41 const mojo::Callback<void(int64_t)>& callback) {
Viet-Trung Luu7d579152016-05-23 13:06:41 -070042 ConnectToService(application_->shell(), app_url, GetProxy(&time_service_));
James Robinson646469d2014-10-03 15:33:28 -070043 if (tracking_) {
44 tracking_->RecordNewRequest();
45 time_service_->StartTrackingRequests(mojo::Callback<void()>());
46 }
47 time_service_->GetPartyTime(base::Bind(&SendTimeResponse, callback));
48}
49
50void TestServiceImpl::StartTrackingRequests(
51 const mojo::Callback<void()>& callback) {
52 TestRequestTrackerPtr tracker;
Viet-Trung Luu7d579152016-05-23 13:06:41 -070053 ConnectToService(application_->shell(), "mojo:test_request_tracker_app",
Viet-Trung Luuc1248b22016-04-26 13:41:55 -070054 GetProxy(&tracker));
James Robinson51b4cd62015-02-04 11:27:40 -080055 tracking_.reset(new TrackedService(tracker.Pass(), Name_, callback));
James Robinson646469d2014-10-03 15:33:28 -070056}
57
58} // namespace test
59} // namespace mojo