Commit 3ecf8289 authored by erg@chromium.org's avatar erg@chromium.org

mojo: Make InterfacePtr<> testable in if() statements without .get().

This also goes and changes a lot of "if (obj.get())" to "if (obj)".

BUG=394639

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

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@284269 0039d316-1c4b-4281-b951-d872f2087c98
parent 1f6c86af
...@@ -72,7 +72,7 @@ class NestingApp : public ApplicationDelegate, ...@@ -72,7 +72,7 @@ class NestingApp : public ApplicationDelegate,
connection->ConnectToService(&window_manager_); connection->ConnectToService(&window_manager_);
connection->AddService<Navigator>(this); connection->AddService<Navigator>(this);
// TODO(davemoore): Is this ok? // TODO(davemoore): Is this ok?
if (!navigator_.get()) { if (!navigator_) {
connection->ConnectToApplication( connection->ConnectToApplication(
kEmbeddedAppURL)->ConnectToService(&navigator_); kEmbeddedAppURL)->ConnectToService(&navigator_);
} }
......
...@@ -464,7 +464,7 @@ class WindowManager : public ApplicationDelegate, ...@@ -464,7 +464,7 @@ class WindowManager : public ApplicationDelegate,
navigation::NavigationDetailsPtr nav_details, navigation::NavigationDetailsPtr nav_details,
navigation::ResponseDetailsPtr response) { navigation::ResponseDetailsPtr response) {
node->Embed(app_url); node->Embed(app_url);
if (nav_details.get()) { if (nav_details) {
navigation::NavigatorPtr navigator; navigation::NavigatorPtr navigator;
app_->ConnectToService(app_url, &navigator); app_->ConnectToService(app_url, &navigator);
navigator->Navigate(node->id(), nav_details.Pass(), response.Pass()); navigator->Navigate(node->id(), nav_details.Pass(), response.Pass());
......
...@@ -288,7 +288,7 @@ void CommandBufferClientImpl::MakeProgressAndUpdateState() { ...@@ -288,7 +288,7 @@ void CommandBufferClientImpl::MakeProgressAndUpdateState() {
command_buffer_->MakeProgress(last_state_.get_offset); command_buffer_->MakeProgress(last_state_.get_offset);
CommandBufferStatePtr state = sync_client_impl_->WaitForProgress(); CommandBufferStatePtr state = sync_client_impl_->WaitForProgress();
if (!state.get()) { if (!state) {
VLOG(1) << "Channel encountered error while waiting for command buffer"; VLOG(1) << "Channel encountered error while waiting for command buffer";
// TODO(piman): is it ok for this to re-enter? // TODO(piman): is it ok for this to re-enter?
DidDestroy(); DidDestroy();
......
...@@ -102,6 +102,16 @@ class InterfacePtr { ...@@ -102,6 +102,16 @@ class InterfacePtr {
return &internal_state_; return &internal_state_;
} }
// Allow InterfacePtr<> to be used in boolean expressions, but not
// implicitly convertible to a real bool (which is dangerous).
private:
typedef internal::InterfacePtrState<Interface> InterfacePtr::*Testable;
public:
operator Testable() const {
return internal_state_.is_bound() ? &InterfacePtr::internal_state_ : NULL;
}
private: private:
typedef internal::InterfacePtrState<Interface> State; typedef internal::InterfacePtrState<Interface> State;
mutable State internal_state_; mutable State internal_state_;
......
...@@ -69,6 +69,10 @@ class InterfacePtrState { ...@@ -69,6 +69,10 @@ class InterfacePtrState {
return handle_.Pass(); return handle_.Pass();
} }
bool is_bound() const {
return handle_.is_valid() || router_;
}
void set_client(typename Interface::Client* client) { void set_client(typename Interface::Client* client) {
ConfigureProxyIfNecessary(); ConfigureProxyIfNecessary();
......
...@@ -94,7 +94,7 @@ class SampleFactoryImpl : public InterfaceImpl<sample::Factory> { ...@@ -94,7 +94,7 @@ class SampleFactoryImpl : public InterfaceImpl<sample::Factory> {
response->pipe = pipe0.Pass(); response->pipe = pipe0.Pass();
client()->DidStuff(response.Pass(), text1); client()->DidStuff(response.Pass(), text1);
if (request->obj.get()) if (request->obj)
request->obj->DoSomething(); request->obj->DoSomething();
} }
...@@ -335,14 +335,14 @@ TEST_F(HandlePassingTest, CreateNamedObject) { ...@@ -335,14 +335,14 @@ TEST_F(HandlePassingTest, CreateNamedObject) {
BindToProxy(new SampleFactoryImpl(), &factory); BindToProxy(new SampleFactoryImpl(), &factory);
sample::NamedObjectPtr object1; sample::NamedObjectPtr object1;
EXPECT_FALSE(object1.get()); EXPECT_FALSE(object1);
InterfaceRequest<sample::NamedObject> object1_request = Get(&object1); InterfaceRequest<sample::NamedObject> object1_request = Get(&object1);
EXPECT_TRUE(object1_request.is_pending()); EXPECT_TRUE(object1_request.is_pending());
factory->CreateNamedObject(object1_request.Pass()); factory->CreateNamedObject(object1_request.Pass());
EXPECT_FALSE(object1_request.is_pending()); // We've passed the request. EXPECT_FALSE(object1_request.is_pending()); // We've passed the request.
ASSERT_TRUE(object1.get()); ASSERT_TRUE(object1);
object1->SetName("object1"); object1->SetName("object1");
sample::NamedObjectPtr object2; sample::NamedObjectPtr object2;
......
...@@ -252,19 +252,19 @@ TEST_F(InterfacePtrTest, Movable) { ...@@ -252,19 +252,19 @@ TEST_F(InterfacePtrTest, Movable) {
math::CalculatorPtr b; math::CalculatorPtr b;
BindToProxy(new MathCalculatorImpl(), &b); BindToProxy(new MathCalculatorImpl(), &b);
EXPECT_TRUE(!a.get()); EXPECT_TRUE(!a);
EXPECT_FALSE(!b.get()); EXPECT_FALSE(!b);
a = b.Pass(); a = b.Pass();
EXPECT_FALSE(!a.get()); EXPECT_FALSE(!a);
EXPECT_TRUE(!b.get()); EXPECT_TRUE(!b);
} }
TEST_F(InterfacePtrTest, Resettable) { TEST_F(InterfacePtrTest, Resettable) {
math::CalculatorPtr a; math::CalculatorPtr a;
EXPECT_TRUE(!a.get()); EXPECT_TRUE(!a);
MessagePipe pipe; MessagePipe pipe;
...@@ -273,11 +273,11 @@ TEST_F(InterfacePtrTest, Resettable) { ...@@ -273,11 +273,11 @@ TEST_F(InterfacePtrTest, Resettable) {
a = MakeProxy<math::Calculator>(pipe.handle0.Pass()); a = MakeProxy<math::Calculator>(pipe.handle0.Pass());
EXPECT_FALSE(!a.get()); EXPECT_FALSE(!a);
a.reset(); a.reset();
EXPECT_TRUE(!a.get()); EXPECT_TRUE(!a);
EXPECT_FALSE(a.internal_state()->router_for_testing()); EXPECT_FALSE(a.internal_state()->router_for_testing());
// Test that handle was closed. // Test that handle was closed.
......
...@@ -52,7 +52,7 @@ CommandBufferImpl::CommandBufferImpl(gfx::AcceleratedWidget widget, ...@@ -52,7 +52,7 @@ CommandBufferImpl::CommandBufferImpl(gfx::AcceleratedWidget widget,
CommandBufferImpl::~CommandBufferImpl() { CommandBufferImpl::~CommandBufferImpl() {
client()->DidDestroy(); client()->DidDestroy();
if (decoder_.get()) { if (decoder_) {
bool have_context = decoder_->MakeCurrent(); bool have_context = decoder_->MakeCurrent();
decoder_->Destroy(have_context); decoder_->Destroy(have_context);
} }
...@@ -130,7 +130,7 @@ bool CommandBufferImpl::DoInitialize( ...@@ -130,7 +130,7 @@ bool CommandBufferImpl::DoInitialize(
const size_t kSize = sizeof(gpu::CommandBufferSharedState); const size_t kSize = sizeof(gpu::CommandBufferSharedState);
scoped_ptr<gpu::BufferBacking> backing( scoped_ptr<gpu::BufferBacking> backing(
gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize)); gles2::MojoBufferBacking::Create(shared_state.Pass(), kSize));
if (!backing.get()) if (!backing)
return false; return false;
command_buffer_->SetSharedStateBuffer(backing.Pass()); command_buffer_->SetSharedStateBuffer(backing.Pass());
...@@ -159,7 +159,7 @@ void CommandBufferImpl::RegisterTransferBuffer( ...@@ -159,7 +159,7 @@ void CommandBufferImpl::RegisterTransferBuffer(
// This validates the size. // This validates the size.
scoped_ptr<gpu::BufferBacking> backing( scoped_ptr<gpu::BufferBacking> backing(
gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size)); gles2::MojoBufferBacking::Create(transfer_buffer.Pass(), size));
if (!backing.get()) { if (!backing) {
DVLOG(0) << "Failed to map shared memory."; DVLOG(0) << "Failed to map shared memory.";
return; return;
} }
......
...@@ -83,7 +83,7 @@ class HTMLViewer : public ApplicationDelegate, ...@@ -83,7 +83,7 @@ class HTMLViewer : public ApplicationDelegate,
} }
void MaybeLoad() { void MaybeLoad() {
if (document_view_ && response_.get()) if (document_view_ && response_)
document_view_->Load(response_.Pass()); document_view_->Load(response_.Pass());
} }
......
...@@ -73,7 +73,7 @@ class NativeViewportImpl ...@@ -73,7 +73,7 @@ class NativeViewportImpl
virtual void CreateGLES2Context( virtual void CreateGLES2Context(
InterfaceRequest<CommandBuffer> command_buffer_request) OVERRIDE { InterfaceRequest<CommandBuffer> command_buffer_request) OVERRIDE {
if (command_buffer_.get() || command_buffer_request_.is_pending()) { if (command_buffer_ || command_buffer_request_.is_pending()) {
LOG(ERROR) << "Can't create multiple contexts on a NativeViewport"; LOG(ERROR) << "Can't create multiple contexts on a NativeViewport";
return; return;
} }
......
...@@ -44,7 +44,7 @@ void ViewManagerInitServiceImpl::MaybeEmbedRoot( ...@@ -44,7 +44,7 @@ void ViewManagerInitServiceImpl::MaybeEmbedRoot(
void ViewManagerInitServiceImpl::EmbedRoot( void ViewManagerInitServiceImpl::EmbedRoot(
const String& url, const String& url,
const Callback<void(bool)>& callback) { const Callback<void(bool)>& callback) {
if (connect_params_.get()) { if (connect_params_) {
DVLOG(1) << "Ignoring second connect"; DVLOG(1) << "Ignoring second connect";
callback.Run(false); callback.Run(false);
return; return;
...@@ -58,7 +58,7 @@ void ViewManagerInitServiceImpl::EmbedRoot( ...@@ -58,7 +58,7 @@ void ViewManagerInitServiceImpl::EmbedRoot(
void ViewManagerInitServiceImpl::OnRootViewManagerWindowTreeHostCreated() { void ViewManagerInitServiceImpl::OnRootViewManagerWindowTreeHostCreated() {
DCHECK(!is_tree_host_ready_); DCHECK(!is_tree_host_ready_);
is_tree_host_ready_ = true; is_tree_host_ready_ = true;
if (connect_params_.get()) if (connect_params_)
MaybeEmbedRoot(connect_params_->url, connect_params_->callback); MaybeEmbedRoot(connect_params_->url, connect_params_->callback);
} }
......
...@@ -167,7 +167,7 @@ void DynamicServiceLoader::LoadService(ServiceManager* manager, ...@@ -167,7 +167,7 @@ void DynamicServiceLoader::LoadService(ServiceManager* manager,
if (resolved_url.SchemeIsFile()) { if (resolved_url.SchemeIsFile()) {
loader = new LocalLoader(runner.Pass()); loader = new LocalLoader(runner.Pass());
} else { } else {
if (!network_service_.get()) { if (!network_service_) {
context_->service_manager()->ConnectToService( context_->service_manager()->ConnectToService(
GURL("mojo:mojo_network_service"), GURL("mojo:mojo_network_service"),
&network_service_); &network_service_);
......
...@@ -239,7 +239,7 @@ class SpyInterceptor : public mojo::ServiceManager::Interceptor { ...@@ -239,7 +239,7 @@ class SpyInterceptor : public mojo::ServiceManager::Interceptor {
// You can get an invalid handle if the app (or service) is // You can get an invalid handle if the app (or service) is
// created by unconventional means, for example the command line. // created by unconventional means, for example the command line.
if (!real_client.get()) if (!real_client)
return real_client.Pass(); return real_client.Pass();
mojo::ScopedMessagePipeHandle faux_client; mojo::ScopedMessagePipeHandle faux_client;
......
...@@ -287,7 +287,7 @@ HandleSignalsState LocalDataPipe::ConsumerGetHandleSignalsStateNoLock() const { ...@@ -287,7 +287,7 @@ HandleSignalsState LocalDataPipe::ConsumerGetHandleSignalsStateNoLock() const {
void LocalDataPipe::EnsureBufferNoLock() { void LocalDataPipe::EnsureBufferNoLock() {
DCHECK(producer_open_no_lock()); DCHECK(producer_open_no_lock());
if (buffer_.get()) if (buffer_)
return; return;
buffer_.reset(static_cast<char*>( buffer_.reset(static_cast<char*>(
base::AlignedAlloc(capacity_num_bytes(), kDataPipeBufferAlignmentBytes))); base::AlignedAlloc(capacity_num_bytes(), kDataPipeBufferAlignmentBytes)));
...@@ -297,7 +297,7 @@ void LocalDataPipe::DestroyBufferNoLock() { ...@@ -297,7 +297,7 @@ void LocalDataPipe::DestroyBufferNoLock() {
#ifndef NDEBUG #ifndef NDEBUG
// Scribble on the buffer to help detect use-after-frees. (This also helps the // Scribble on the buffer to help detect use-after-frees. (This also helps the
// unit test detect certain bugs without needing ASAN or similar.) // unit test detect certain bugs without needing ASAN or similar.)
if (buffer_.get()) if (buffer_)
memset(buffer_.get(), 0xcd, capacity_num_bytes()); memset(buffer_.get(), 0xcd, capacity_num_bytes());
#endif #endif
buffer_.reset(); buffer_.reset();
......
...@@ -162,7 +162,7 @@ void NativeWidgetViewManager::OnNodeActiveViewChanged( ...@@ -162,7 +162,7 @@ void NativeWidgetViewManager::OnNodeActiveViewChanged(
void NativeWidgetViewManager::OnViewInputEvent(view_manager::View* view, void NativeWidgetViewManager::OnViewInputEvent(view_manager::View* view,
const EventPtr& event) { const EventPtr& event) {
scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event> >()); scoped_ptr<ui::Event> ui_event(event.To<scoped_ptr<ui::Event> >());
if (ui_event.get()) if (ui_event)
window_tree_host_->SendEventToProcessor(ui_event.get()); window_tree_host_->SendEventToProcessor(ui_event.get());
} }
......
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