Skip to content

Commit

Permalink
Update to mojo dd010e1297c09b351dc82f7def154cfa63d795b1
Browse files Browse the repository at this point in the history
  • Loading branch information
collinjackson committed Nov 6, 2015
1 parent 8f06f34 commit 0847bbc
Show file tree
Hide file tree
Showing 197 changed files with 5,000 additions and 2,342 deletions.
1 change: 0 additions & 1 deletion mojo/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ shared_library("mojo_java_unittests") {
"//mojo/message_pump",
"//mojo/public/cpp/bindings/tests:mojo_public_bindings_test_utils",
"//mojo/public/cpp/environment",
"//mojo/public/cpp/test_support:test_utils",
]
defines = [ "UNIT_TEST" ]
}
Expand Down
29 changes: 16 additions & 13 deletions mojo/common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ source_set("common") {
sources = [
"binding_set.h",
"interface_ptr_set.h",
"task_tracker.cc",
"task_tracker.h",
"strong_binding_set.h",
]

deps = [
Expand All @@ -19,34 +18,38 @@ source_set("common") {
]
}

test("mojo_common_unittests") {
source_set("tests") {
testonly = true

sources = [
"binding_set_unittest.cc",
"callback_binding_unittest.cc",
"interface_ptr_set_unittest.cc",
"task_tracker_unittest.cc",
"strong_binding_set_unittest.cc",
]

deps = [
":common",
":test_interfaces",
"//base",
"//base/test:test_support",
"//mojo/message_pump",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/bindings:callback",
"//mojo/public/cpp/system",
"//testing/gtest",
]
}

test("mojo_common_unittests") {
deps = [
":tests",
"//mojo/converters/array_string:tests",
"//mojo/converters/base:tests",
"//mojo/converters/url:tests",
"//mojo/data_pipe_utils:tests",
"//mojo/edk/test:run_all_unittests",
"//mojo/edk/test:test_support",
"//mojo/environment:chromium",
"//mojo/message_pump",
"//mojo/message_pump:tests",
"//mojo/public/cpp/bindings",
"//mojo/public/cpp/bindings:callback",
"//mojo/public/cpp/system",
"//mojo/public/cpp/test_support:test_utils",
"//testing/gtest",
"//url",
]
}

Expand Down
7 changes: 6 additions & 1 deletion mojo/common/binding_set.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
#define MOJO_COMMON_BINDING_SET_H_

#include <algorithm>
#include <memory>
#include <vector>

#include "base/memory/weak_ptr.h"
#include "base/logging.h"
#include "base/macros.h"
#include "mojo/public/cpp/bindings/binding.h"

namespace mojo {
Expand All @@ -21,6 +23,9 @@ class BindingSet {
BindingSet() {}
~BindingSet() { CloseAllBindings(); }

// Adds a binding to the list and arranges for it to be removed when
// a connection error occurs. Does not take ownership of |impl|, which
// must outlive the binding set.
void AddBinding(Interface* impl, InterfaceRequest<Interface> request) {
bindings_.emplace_back(new Binding<Interface>(impl, request.Pass()));
auto* binding = bindings_.back().get();
Expand Down
65 changes: 65 additions & 0 deletions mojo/common/strong_binding_set.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// 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_COMMON_STRONG_BINDING_SET_H_
#define MOJO_COMMON_STRONG_BINDING_SET_H_

#include <algorithm>
#include <memory>
#include <vector>

#include "base/logging.h"
#include "base/macros.h"
#include "mojo/public/cpp/bindings/binding.h"

namespace mojo {

// Use this class to manage a set of strong bindings each of which is
// owned by the pipe it is bound to. The set takes ownership of the
// interfaces and will delete them when the bindings are closed.
template <typename Interface>
class StrongBindingSet {
public:
StrongBindingSet() {}
~StrongBindingSet() { CloseAllBindings(); }

// Adds a binding to the list and arranges for it to be removed when
// a connection error occurs. Takes ownership of |impl|, which
// will be deleted when the binding is closed.
void AddBinding(Interface* impl, InterfaceRequest<Interface> request) {
bindings_.emplace_back(new Binding<Interface>(impl, request.Pass()));
auto* binding = bindings_.back().get();
// Set the connection error handler for the newly added Binding to be a
// function that will erase it from the vector.
binding->set_connection_error_handler([this, binding]() {
auto it =
std::find_if(bindings_.begin(), bindings_.end(),
[binding](const std::unique_ptr<Binding<Interface>>& b) {
return (b.get() == binding);
});
DCHECK(it != bindings_.end());
delete binding->impl();
bindings_.erase(it);
});
}

// Closes all bindings and deletes their associated interfaces.
void CloseAllBindings() {
for (auto it = bindings_.begin(); it != bindings_.end(); ++it) {
delete (*it)->impl();
}
bindings_.clear();
}

size_t size() const { return bindings_.size(); }

private:
std::vector<std::unique_ptr<Binding<Interface>>> bindings_;

DISALLOW_COPY_AND_ASSIGN(StrongBindingSet);
};

} // namespace mojo

#endif // MOJO_COMMON_STRONG_BINDING_SET_H_
115 changes: 115 additions & 0 deletions mojo/common/strong_binding_set_unittest.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright 2015 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.

#include "mojo/common/strong_binding_set.h"

#include "base/message_loop/message_loop.h"
#include "mojo/common/test_interfaces.mojom.h"
#include "mojo/message_pump/message_pump_mojo.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "mojo/public/cpp/bindings/interface_request.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace mojo {
namespace common {
namespace {

class DummyImpl : public tests::Dummy {
public:
explicit DummyImpl(bool* deleted_flag) : deleted_flag_(deleted_flag) {}
~DummyImpl() override { *deleted_flag_ = true; }

void Foo() override { call_count_++; }

int call_count() const { return call_count_; }

private:
bool* deleted_flag_;
int call_count_ = 0;

DISALLOW_COPY_AND_ASSIGN(DummyImpl);
};

// Tests all of the functionality of StrongBindingSet.
TEST(StrongBindingSetTest, FullLifeCycle) {
base::MessageLoop loop(MessagePumpMojo::Create());

// Create 10 InterfacePtrs and DummyImpls.
const size_t kNumObjects = 10;
InterfacePtr<tests::Dummy> intrfc_ptrs[kNumObjects];
DummyImpl* impls[kNumObjects];
bool deleted_flags[kNumObjects] = {};

// Create 10 message pipes, bind everything together, and add the
// bindings to binding_set.
StrongBindingSet<tests::Dummy> binding_set;
EXPECT_EQ(0u, binding_set.size());
for (size_t i = 0; i < kNumObjects; i++) {
impls[i] = new DummyImpl(&deleted_flags[i]);
binding_set.AddBinding(impls[i], GetProxy(&intrfc_ptrs[i]));
}
EXPECT_EQ(kNumObjects, binding_set.size());

// Check that initially all call counts are zero.
for (const auto& impl : impls) {
EXPECT_EQ(0, impl->call_count());
}

// Invoke method foo() on all 10 InterfacePointers.
for (InterfacePtr<tests::Dummy>& ptr : intrfc_ptrs) {
ptr->Foo();
}

// Check that now all call counts are one.
loop.RunUntilIdle();
for (const auto& impl : impls) {
EXPECT_EQ(1, impl->call_count());
}

// Close the first 5 message pipes and destroy the first five
// InterfacePtrs.
for (size_t i = 0; i < kNumObjects / 2; i++) {
intrfc_ptrs[i].reset();
}

// Check that the set contains only five elements now.
loop.RunUntilIdle();
EXPECT_EQ(kNumObjects / 2, binding_set.size());

// Check that the first 5 interfaces have all been deleted.
for (size_t i = 0; i < kNumObjects; i++) {
bool expected = (i < kNumObjects / 2);
EXPECT_EQ(expected, deleted_flags[i]);
}

// Invoke method foo() on the second five InterfacePointers.
for (size_t i = kNumObjects / 2; i < kNumObjects; i++) {
intrfc_ptrs[i]->Foo();
}
loop.RunUntilIdle();

// Check that now the second five counts are two.
for (size_t i = kNumObjects / 2; i < kNumObjects; i++) {
EXPECT_EQ(2, impls[i]->call_count());
}

// Invoke CloseAllBindings
binding_set.CloseAllBindings();
EXPECT_EQ(0u, binding_set.size());

// Invoke method foo() on the second five InterfacePointers.
for (size_t i = kNumObjects / 2; i < kNumObjects; i++) {
intrfc_ptrs[i]->Foo();
}
loop.RunUntilIdle();

// Check that all interfaces have all been deleted.
for (size_t i = 0; i < kNumObjects; i++) {
EXPECT_TRUE(deleted_flags[i]);
}
}

} // namespace
} // namespace common
} // namespace mojo
102 changes: 0 additions & 102 deletions mojo/common/task_tracker.cc

This file was deleted.

Loading

0 comments on commit 0847bbc

Please sign in to comment.