Mojo: Make the TestService quit when there are no more connections.

(This makes mojo_shell_tests not hang.)

R=sky@chromium.org

Review URL: https://codereview.chromium.org/336483003

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@276806 0039d316-1c4b-4281-b951-d872f2087c98
parent 1bb5deb7
......@@ -358,6 +358,7 @@
'sources': [
'public/cpp/application/lib/mojo_main_standalone.cc',
'services/test_service/test_service_application.cc',
'services/test_service/test_service_application.h',
'services/test_service/test_service_impl.cc',
'services/test_service/test_service_impl.h',
],
......
......@@ -2,28 +2,38 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/public/cpp/application/application.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/services/test_service/test_service_application.h"
#include <assert.h>
#include "mojo/public/cpp/utility/run_loop.h"
#include "mojo/services/test_service/test_service_impl.h"
namespace mojo {
namespace test {
namespace {
class TestServiceApplication : public Application {
public:
TestServiceApplication() {}
virtual ~TestServiceApplication() {}
TestServiceApplication::TestServiceApplication() : ref_count_(0) {
}
virtual void Initialize() MOJO_OVERRIDE {
AddService<TestServiceImpl>();
}
TestServiceApplication::~TestServiceApplication() {
}
private:
MOJO_DISALLOW_COPY_AND_ASSIGN(TestServiceApplication);
};
void TestServiceApplication::Initialize() {
AddService<TestServiceImpl>(this);
}
void TestServiceApplication::AddRef() {
assert(ref_count_ >= 0);
ref_count_++;
}
void TestServiceApplication::ReleaseRef() {
assert(ref_count_ > 0);
ref_count_--;
if (ref_count_ <= 0)
RunLoop::current()->Quit();
}
} // namespace
} // namespace test
// static
......
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_SERVICES_TEST_SERVICE_TEST_SERVICE_APPLICATION_H_
#define MOJO_SERVICES_TEST_SERVICE_TEST_SERVICE_APPLICATION_H_
#include "mojo/public/cpp/application/application.h"
#include "mojo/public/cpp/system/macros.h"
namespace mojo {
namespace test {
class TestServiceApplication : public Application {
public:
TestServiceApplication();
virtual ~TestServiceApplication();
virtual void Initialize() MOJO_OVERRIDE;
void AddRef();
void ReleaseRef();
private:
int ref_count_;
MOJO_DISALLOW_COPY_AND_ASSIGN(TestServiceApplication);
};
} // namespace test
} // namespace mojo
#endif // MOJO_SERVICES_TEST_SERVICE_TEST_SERVICE_APPLICATION_H_
......@@ -4,15 +4,26 @@
#include "mojo/services/test_service/test_service_impl.h"
#include "mojo/services/test_service/test_service_application.h"
namespace mojo {
namespace test {
TestServiceImpl::TestServiceImpl() {
TestServiceImpl::TestServiceImpl(TestServiceApplication* application)
: application_(application) {
}
TestServiceImpl::~TestServiceImpl() {
}
void TestServiceImpl::OnConnectionEstablished() {
application_->AddRef();
}
void TestServiceImpl::OnConnectionError() {
application_->ReleaseRef();
}
void TestServiceImpl::Ping(const mojo::Callback<void()>& callback) {
callback.Run();
}
......
......@@ -2,25 +2,34 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/public/cpp/application/application.h"
#ifndef MOJO_SERVICES_TEST_SERVICE_TEST_SERVICE_IMPL_H_
#define MOJO_SERVICES_TEST_SERVICE_TEST_SERVICE_IMPL_H_
#include "mojo/public/cpp/system/macros.h"
#include "mojo/services/test_service/test_service.mojom.h"
namespace mojo {
namespace test {
class TestServiceApplication;
class TestServiceImpl : public InterfaceImpl<ITestService> {
public:
TestServiceImpl();
explicit TestServiceImpl(TestServiceApplication* application);
virtual ~TestServiceImpl();
// |ITestService| methods:
virtual void OnConnectionEstablished() MOJO_OVERRIDE;
virtual void OnConnectionError() MOJO_OVERRIDE;
virtual void Ping(const mojo::Callback<void()>& callback) MOJO_OVERRIDE;
private:
TestServiceApplication* const application_;
MOJO_DISALLOW_COPY_AND_ASSIGN(TestServiceImpl);
};
} // namespace test
} // namespace mojo
#endif // MOJO_SERVICES_TEST_SERVICE_TEST_SERVICE_IMPL_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment