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