diff options
author | ben <ben@chromium.org> | 2016-02-25 23:30:25 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-02-26 07:31:39 +0000 |
commit | e4a2bc4fb161d8d2ea673d5f55119d36be2cf06f (patch) | |
tree | a09be63e6724713539bd7065ecdcb38b0233a73e /mojo/shell/public | |
parent | 0d78506780d9bf1e5e80a0bfca6099a80fd355e0 (diff) | |
download | chromium_src-e4a2bc4fb161d8d2ea673d5f55119d36be2cf06f.zip chromium_src-e4a2bc4fb161d8d2ea673d5f55119d36be2cf06f.tar.gz chromium_src-e4a2bc4fb161d8d2ea673d5f55119d36be2cf06f.tar.bz2 |
Replace with mojo::Connector (already exists) now that Shell is gone.
Requires:
- Move AppRefCount to a different class, MessageLoopRef, which is optionally used by applications that wish to quit the current message loop started by application runner when the ref drops to zero.
- Changing the signature of Initialize() to take a Connector. This is what most of the change in this CL is.
BUG=
Review URL: https://codereview.chromium.org/1725353003
Cr-Commit-Position: refs/heads/master@{#377841}
Diffstat (limited to 'mojo/shell/public')
-rw-r--r-- | mojo/shell/public/cpp/BUILD.gn | 2 | ||||
-rw-r--r-- | mojo/shell/public/cpp/application_test_base.h | 13 | ||||
-rw-r--r-- | mojo/shell/public/cpp/lib/message_loop_ref.cc | 98 | ||||
-rw-r--r-- | mojo/shell/public/cpp/lib/shell_client.cc | 4 | ||||
-rw-r--r-- | mojo/shell/public/cpp/lib/shell_connection.cc | 114 | ||||
-rw-r--r-- | mojo/shell/public/cpp/message_loop_ref.h | 57 | ||||
-rw-r--r-- | mojo/shell/public/cpp/shell.h | 58 | ||||
-rw-r--r-- | mojo/shell/public/cpp/shell_client.h | 16 | ||||
-rw-r--r-- | mojo/shell/public/cpp/shell_connection.h | 23 |
9 files changed, 175 insertions, 210 deletions
diff --git a/mojo/shell/public/cpp/BUILD.gn b/mojo/shell/public/cpp/BUILD.gn index 1ad6892..e4342c9 100644 --- a/mojo/shell/public/cpp/BUILD.gn +++ b/mojo/shell/public/cpp/BUILD.gn @@ -36,8 +36,10 @@ source_set("sources") { "lib/connector_impl.h", "lib/interface_factory_binder.h", "lib/interface_registry.cc", + "lib/message_loop_ref.cc", "lib/shell_client.cc", "lib/shell_connection.cc", + "message_loop_ref.h", "shell.h", "shell_client.h", "shell_connection.h", diff --git a/mojo/shell/public/cpp/application_test_base.h b/mojo/shell/public/cpp/application_test_base.h index f8ff6ad..bba5916 100644 --- a/mojo/shell/public/cpp/application_test_base.h +++ b/mojo/shell/public/cpp/application_test_base.h @@ -9,6 +9,7 @@ #include "mojo/public/cpp/bindings/array.h" #include "mojo/public/cpp/bindings/string.h" #include "mojo/public/cpp/system/macros.h" +#include "mojo/shell/public/cpp/connector.h" #include "mojo/shell/public/cpp/shell_client.h" #include "mojo/shell/public/cpp/shell_connection.h" #include "mojo/shell/public/interfaces/shell_client.mojom.h" @@ -32,8 +33,8 @@ class TestHelper { explicit TestHelper(ShellClient* client); ~TestHelper(); - Shell* shell() { return shell_connection_.get(); } - std::string shell_url() { return url_; } + Connector* connector() { return shell_connection_->connector(); } + std::string test_url() { return url_; } private: // The application delegate used if GetShellClient is not overridden. @@ -54,11 +55,11 @@ class ApplicationTestBase : public testing::Test { ~ApplicationTestBase() override; protected: - Shell* shell() { - return test_helper_ ? test_helper_->shell() : nullptr; + Connector* connector() { + return test_helper_ ? test_helper_->connector() : nullptr; } - std::string shell_url() const { - return test_helper_ ? test_helper_->shell_url() : std::string(); + std::string test_url() const { + return test_helper_ ? test_helper_->test_url() : std::string(); } // Get the ShellClient for the application to be tested. diff --git a/mojo/shell/public/cpp/lib/message_loop_ref.cc b/mojo/shell/public/cpp/lib/message_loop_ref.cc new file mode 100644 index 0000000..a285cda --- /dev/null +++ b/mojo/shell/public/cpp/lib/message_loop_ref.cc @@ -0,0 +1,98 @@ +// Copyright 2016 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/shell/public/cpp/message_loop_ref.h" + +#include "base/bind.h" +#include "base/message_loop/message_loop.h" + +namespace mojo { + +class MessageLoopRefImpl : public MessageLoopRef { + public: + MessageLoopRefImpl( + MessageLoopRefFactory* factory, + scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) + : factory_(factory), + app_task_runner_(app_task_runner) {} + ~MessageLoopRefImpl() override { +#ifndef NDEBUG + // Ensure that this object is used on only one thread at a time, or else + // there could be races where the object is being reset on one thread and + // cloned on another. + if (clone_task_runner_) + DCHECK(clone_task_runner_->BelongsToCurrentThread()); +#endif + + if (app_task_runner_->BelongsToCurrentThread()) { + factory_->Release(); + } else { + app_task_runner_->PostTask( + FROM_HERE, + base::Bind(&MessageLoopRefFactory::Release, + base::Unretained(factory_))); + } + } + + private: + // MessageLoopRef: + scoped_ptr<MessageLoopRef> Clone() override { + if (app_task_runner_->BelongsToCurrentThread()) { + factory_->AddRef(); + } else { + app_task_runner_->PostTask( + FROM_HERE, + base::Bind(&MessageLoopRefFactory::AddRef, + base::Unretained(factory_))); + } + +#ifndef NDEBUG + // Ensure that this object is used on only one thread at a time, or else + // there could be races where the object is being reset on one thread and + // cloned on another. + if (clone_task_runner_) { + DCHECK(clone_task_runner_->BelongsToCurrentThread()); + } else { + clone_task_runner_ = base::MessageLoop::current()->task_runner(); + } +#endif + + return make_scoped_ptr(new MessageLoopRefImpl(factory_, app_task_runner_)); + } + + MessageLoopRefFactory* factory_; + scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; + +#ifndef NDEBUG + scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; +#endif + + DISALLOW_COPY_AND_ASSIGN(MessageLoopRefImpl); +}; + +MessageLoopRefFactory::MessageLoopRefFactory() {} +MessageLoopRefFactory::~MessageLoopRefFactory() {} + +scoped_ptr<MessageLoopRef> MessageLoopRefFactory::CreateRef() { + AddRef(); + return make_scoped_ptr(new MessageLoopRefImpl( + this, base::MessageLoop::current()->task_runner())); +} + +void MessageLoopRefFactory::AddRef() { + ++ref_count_; +} + +void MessageLoopRefFactory::Release() { + if (!--ref_count_) { + if (!quit_closure_.is_null()) + quit_closure_.Run(); + if (base::MessageLoop::current() && + base::MessageLoop::current()->is_running()) { + base::MessageLoop::current()->QuitWhenIdle(); + } + } +} + +} // namespace mojo diff --git a/mojo/shell/public/cpp/lib/shell_client.cc b/mojo/shell/public/cpp/lib/shell_client.cc index 6570068..0d553f0 100644 --- a/mojo/shell/public/cpp/lib/shell_client.cc +++ b/mojo/shell/public/cpp/lib/shell_client.cc @@ -9,7 +9,7 @@ namespace mojo { ShellClient::ShellClient() {} ShellClient::~ShellClient() {} -void ShellClient::Initialize(Shell* app, const std::string& url, +void ShellClient::Initialize(Connector* connector, const std::string& url, uint32_t id, uint32_t user_id) { } @@ -21,6 +21,4 @@ bool ShellClient::ShellConnectionLost() { return true; } -void ShellClient::Quit() {} - } // namespace mojo diff --git a/mojo/shell/public/cpp/lib/shell_connection.cc b/mojo/shell/public/cpp/lib/shell_connection.cc index e14cc77..5977cc6 100644 --- a/mojo/shell/public/cpp/lib/shell_connection.cc +++ b/mojo/shell/public/cpp/lib/shell_connection.cc @@ -2,9 +2,6 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include <algorithm> -#include <utility> - #include "base/bind.h" #include "base/logging.h" #include "base/message_loop/message_loop.h" @@ -17,65 +14,6 @@ namespace mojo { -class AppRefCountImpl : public AppRefCount { - public: - AppRefCountImpl(ShellConnection* connection, - scoped_refptr<base::SingleThreadTaskRunner> app_task_runner) - : connection_(connection), - app_task_runner_(app_task_runner) {} - ~AppRefCountImpl() override { -#ifndef NDEBUG - // Ensure that this object is used on only one thread at a time, or else - // there could be races where the object is being reset on one thread and - // cloned on another. - if (clone_task_runner_) - DCHECK(clone_task_runner_->BelongsToCurrentThread()); -#endif - - if (app_task_runner_->BelongsToCurrentThread()) { - connection_->Release(); - } else { - app_task_runner_->PostTask( - FROM_HERE, - base::Bind(&ShellConnection::Release, base::Unretained(connection_))); - } - } - - private: - // AppRefCount: - scoped_ptr<AppRefCount> Clone() override { - if (app_task_runner_->BelongsToCurrentThread()) { - connection_->AddRef(); - } else { - app_task_runner_->PostTask( - FROM_HERE, - base::Bind(&ShellConnection::AddRef, base::Unretained(connection_))); - } - -#ifndef NDEBUG - // Ensure that this object is used on only one thread at a time, or else - // there could be races where the object is being reset on one thread and - // cloned on another. - if (clone_task_runner_) { - DCHECK(clone_task_runner_->BelongsToCurrentThread()); - } else { - clone_task_runner_ = base::MessageLoop::current()->task_runner(); - } -#endif - - return make_scoped_ptr(new AppRefCountImpl(connection_, app_task_runner_)); - } - - ShellConnection* connection_; - scoped_refptr<base::SingleThreadTaskRunner> app_task_runner_; - -#ifndef NDEBUG - scoped_refptr<base::SingleThreadTaskRunner> clone_task_runner_; -#endif - - DISALLOW_COPY_AND_ASSIGN(AppRefCountImpl); -}; - //////////////////////////////////////////////////////////////////////////////// // ShellConnection, public: @@ -84,7 +22,6 @@ ShellConnection::ShellConnection( InterfaceRequest<shell::mojom::ShellClient> request) : client_(client), binding_(this, std::move(request)), - ref_count_(0), weak_factory_(this) {} ShellConnection::~ShellConnection() {} @@ -95,36 +32,6 @@ void ShellConnection::WaitForInitialize() { } //////////////////////////////////////////////////////////////////////////////// -// ShellConnection, Shell implementation: - -scoped_ptr<Connection> ShellConnection::Connect(const std::string& url) { - return connector_->Connect(url); -} - -scoped_ptr<Connection> ShellConnection::Connect( - Connector::ConnectParams* params) { - return connector_->Connect(params); -} - -scoped_ptr<Connector> ShellConnection::CloneConnector() const { - return connector_->Clone(); -} - -scoped_ptr<AppRefCount> ShellConnection::CreateAppRefCount() { - AddRef(); - return make_scoped_ptr( - new AppRefCountImpl(this, base::MessageLoop::current()->task_runner())); -} - -void ShellConnection::Quit() { - client_->Quit(); - if (base::MessageLoop::current() && - base::MessageLoop::current()->is_running()) { - base::MessageLoop::current()->QuitWhenIdle(); - } -} - -//////////////////////////////////////////////////////////////////////////////// // ShellConnection, shell::mojom::ShellClient implementation: void ShellConnection::Initialize(shell::mojom::ConnectorPtr connector, @@ -135,7 +42,7 @@ void ShellConnection::Initialize(shell::mojom::ConnectorPtr connector, std::move(connector), base::Bind(&ShellConnection::OnConnectionError, weak_factory_.GetWeakPtr()))); - client_->Initialize(this, url, id, user_id); + client_->Initialize(connector_.get(), url, id, user_id); } void ShellConnection::AcceptConnection( @@ -162,27 +69,12 @@ void ShellConnection::AcceptConnection( // ShellConnection, private: void ShellConnection::OnConnectionError() { - base::WeakPtr<ShellConnection> ptr(weak_factory_.GetWeakPtr()); - // We give the client notice first, since it might want to do something on // shell connection errors other than immediate termination of the run // loop. The application might want to continue servicing connections other // than the one to the shell. - bool quit_now = client_->ShellConnectionLost(); - if (quit_now) - Quit(); - if (!ptr) - return; - connector_.reset(); -} - -void ShellConnection::AddRef() { - ++ref_count_; -} - -void ShellConnection::Release() { - if (!--ref_count_) - Quit(); + if (client_->ShellConnectionLost()) + connector_.reset(); } } // namespace mojo diff --git a/mojo/shell/public/cpp/message_loop_ref.h b/mojo/shell/public/cpp/message_loop_ref.h new file mode 100644 index 0000000..fffb738 --- /dev/null +++ b/mojo/shell/public/cpp/message_loop_ref.h @@ -0,0 +1,57 @@ +// Copyright 2016 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_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_REFERENCE_H_ +#define MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_REFERENCE_H_ + +#include "base/macros.h" +#include "base/memory/scoped_ptr.h" +#include "base/message_loop/message_loop.h" +#include "mojo/public/cpp/system/core.h" + +namespace mojo { + +class MessageLoopRefImpl; + +// An interface implementation can keep this object as a member variable to +// hold a reference to the ShellConnection, keeping it alive as long as the +// bound implementation exists. +// Since interface implementations can be bound on different threads than the +// ShellConnection, this class is safe to use on any thread. However, each +// instance should only be used on one thread at a time (otherwise there'll be +// races between the AddRef resulting from cloning and destruction). +class MessageLoopRef { + public: + virtual ~MessageLoopRef() {} + + virtual scoped_ptr<MessageLoopRef> Clone() = 0; +}; + +class MessageLoopRefFactory { + public: + MessageLoopRefFactory(); + ~MessageLoopRefFactory(); + + void set_quit_closure(const base::Closure& quit_closure) { + quit_closure_ = quit_closure; + } + + scoped_ptr<MessageLoopRef> CreateRef(); + + private: + friend MessageLoopRefImpl; + + // Called from MessageLoopRefImpl. + void AddRef(); + void Release(); + + base::Closure quit_closure_; + int ref_count_ = 0; + + DISALLOW_COPY_AND_ASSIGN(MessageLoopRefFactory); +}; + +} // namespace mojo + +#endif // MOJO_SHELL_PUBLIC_CPP_APPLICATION_RUNNER_REFERENCE_H_ diff --git a/mojo/shell/public/cpp/shell.h b/mojo/shell/public/cpp/shell.h index cade46a..772d23f 100644 --- a/mojo/shell/public/cpp/shell.h +++ b/mojo/shell/public/cpp/shell.h @@ -5,67 +5,11 @@ #ifndef MOJO_SHELL_PUBLIC_CPP_SHELL_H_ #define MOJO_SHELL_PUBLIC_CPP_SHELL_H_ -#include "mojo/shell/public/cpp/connection.h" #include "mojo/shell/public/cpp/connector.h" -#include "mojo/shell/public/interfaces/shell.mojom.h" -#include "url/gurl.h" namespace mojo { -// An interface implementation can keep this object as a member variable to -// hold a reference to the ShellConnection, keeping it alive as long as the -// bound implementation exists. -// Since interface implementations can be bound on different threads than the -// ShellConnection, this class is safe to use on any thread. However, each -// instance should only be used on one thread at a time (otherwise there'll be -// races between the AddRef resulting from cloning and destruction). -class AppRefCount { - public: - virtual ~AppRefCount() {} - - virtual scoped_ptr<AppRefCount> Clone() = 0; -}; - -// An interface that encapsulates the Mojo Shell's broker interface by which -// connections between applications are established. Implemented by -// ShellConnection, this is the primary interface exposed to clients. -class Shell { - public: - // Requests a new connection to an application. Returns a pointer to the - // connection if the connection is permitted by this application's delegate, - // or nullptr otherwise. Caller takes ownership. - virtual scoped_ptr<Connection> Connect(const std::string& url) = 0; - virtual scoped_ptr<Connection> Connect(Connector::ConnectParams* params) = 0; - - // Connect to application identified by |request->url| and connect to the - // service implementation of the interface identified by |Interface|. - template <typename Interface> - void ConnectToInterface(Connector::ConnectParams* params, - InterfacePtr<Interface>* ptr) { - scoped_ptr<Connection> connection = Connect(params); - if (connection) - connection->GetInterface(ptr); - } - template <typename Interface> - void ConnectToInterface(const std::string& url, - InterfacePtr<Interface>* ptr) { - Connector::ConnectParams params(url); - return ConnectToInterface(¶ms, ptr); - } - - // Returns a clone of the ShellConnection's Connector that can be passed to - // other threads. - virtual scoped_ptr<Connector> CloneConnector() const = 0; - - // Quits the message loop run by the ApplicationRunner, which causes this - // object to be destructed and the application to quit. - virtual void Quit() = 0; - - // Create an object that can be used to refcount the lifetime of the - // application. The returned object may be cloned, and when the refcount falls - // to zero Quit() is called. - virtual scoped_ptr<AppRefCount> CreateAppRefCount() = 0; -}; +using Shell = Connector; } // namespace mojo diff --git a/mojo/shell/public/cpp/shell_client.h b/mojo/shell/public/cpp/shell_client.h index b7a8e0e..0d6fde1 100644 --- a/mojo/shell/public/cpp/shell_client.h +++ b/mojo/shell/public/cpp/shell_client.h @@ -13,7 +13,7 @@ namespace mojo { -class Shell; +class Connector; // An interface representing an instance "known to the Mojo Shell". The // implementation receives lifecycle messages for the instance and gets the @@ -31,7 +31,7 @@ class ShellClient { // instance of the application. // |user_id| identifies the user this instance is run as. // Called exactly once before any other method. - virtual void Initialize(Shell* shell, + virtual void Initialize(Connector* connector, const std::string& url, uint32_t id, uint32_t user_id = 0); @@ -44,20 +44,10 @@ class ShellClient { // Called when ShellConnection's pipe to the Mojo Shell is closed. // - // Returning true from this method will cause the ShellConnection instance to - // call this instance back via Quit(), and then run the termination closure - // passed to it (which may do cleanup like, for example, quitting a run loop). - // Returning false from this method will not do any of this. The client is - // then responsible for calling Shell::QuitNow() when it is ready to close. - // The client may do this if it wishes to continue servicing connections other - // than the Shell. + // Returning true from this method will cause ... // The default implementation returns true. virtual bool ShellConnectionLost(); - // Called before ShellConnection::QuitNow(). After returning from this call - // the delegate can no longer rely on the main run loop still running. - virtual void Quit(); - private: MOJO_DISALLOW_COPY_AND_ASSIGN(ShellClient); }; diff --git a/mojo/shell/public/cpp/shell_connection.h b/mojo/shell/public/cpp/shell_connection.h index d79b788..123bd93 100644 --- a/mojo/shell/public/cpp/shell_connection.h +++ b/mojo/shell/public/cpp/shell_connection.h @@ -14,14 +14,12 @@ #include "mojo/public/cpp/bindings/binding.h" #include "mojo/public/cpp/bindings/callback.h" #include "mojo/public/cpp/system/core.h" -#include "mojo/shell/public/cpp/shell.h" #include "mojo/shell/public/cpp/shell_client.h" #include "mojo/shell/public/interfaces/shell.mojom.h" #include "mojo/shell/public/interfaces/shell_client.mojom.h" namespace mojo { -class AppRefCountImpl; class Connector; // Encapsulates a connection to the Mojo Shell in two parts: @@ -44,10 +42,7 @@ class Connector; // Client Lib's mojo::ShellClient interface. See documentation in shell_client.h // for details. // -// Though this class provides the canonical implementation of the Shell Client -// lib's mojo::Shell interface, this interface should not be reached through -// pointers to this type. -class ShellConnection : public Shell, public shell::mojom::ShellClient { +class ShellConnection : public shell::mojom::ShellClient { public: // Does not take ownership of |delegate|, which must remain valid for the // lifetime of ShellConnection. @@ -59,16 +54,9 @@ class ShellConnection : public Shell, public shell::mojom::ShellClient { // shell. void WaitForInitialize(); - private: - friend AppRefCountImpl; - - // Shell: - scoped_ptr<Connection> Connect(const std::string& url) override; - scoped_ptr<Connection> Connect(Connector::ConnectParams* params) override; - scoped_ptr<Connector> CloneConnector() const override; - scoped_ptr<AppRefCount> CreateAppRefCount() override; - void Quit() override; + Connector* connector() { return connector_.get(); } + private: // shell::mojom::ShellClient: void Initialize(shell::mojom::ConnectorPtr connector, const mojo::String& url, @@ -85,17 +73,12 @@ class ShellConnection : public Shell, public shell::mojom::ShellClient { void OnConnectionError(); - // Called from AppRefCountImpl. - void AddRef(); - void Release(); - // We track the lifetime of incoming connection registries as it more // convenient for the client. ScopedVector<Connection> incoming_connections_; mojo::ShellClient* client_; Binding<shell::mojom::ShellClient> binding_; scoped_ptr<Connector> connector_; - int ref_count_; base::WeakPtrFactory<ShellConnection> weak_factory_; MOJO_DISALLOW_COPY_AND_ASSIGN(ShellConnection); |