Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(945)

Unified Diff: media/mojo/services/mojo_media_application.cc

Issue 1200323003: media: Quit MojoMediaApplication when no service is bound. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/mojo/services/mojo_media_application.cc
diff --git a/media/mojo/services/mojo_media_application.cc b/media/mojo/services/mojo_media_application.cc
index 814be21fcc2b013b1f40f2a3f27de2f2f38d8092..178314465e9257ea795dea768735808b8afbfc72 100644
--- a/media/mojo/services/mojo_media_application.cc
+++ b/media/mojo/services/mojo_media_application.cc
@@ -5,6 +5,7 @@
#include "media/mojo/services/mojo_media_application.h"
#include "base/logging.h"
+#include "base/thread_task_runner_handle.h"
#include "media/mojo/services/mojo_cdm_service.h"
#include "media/mojo/services/mojo_renderer_service.h"
#include "mojo/application/public/cpp/application_connection.h"
@@ -13,6 +14,7 @@
namespace media {
const char kMojoMediaAppUrl[] = "mojo:media";
+const int kIdleTimeoutInSeconds = 30;
// static
GURL MojoMediaApplication::AppUrl() {
@@ -31,11 +33,16 @@ MojoMediaApplication::~MojoMediaApplication() {
}
void MojoMediaApplication::Initialize(mojo::ApplicationImpl* app) {
+ app_impl_ = app;
sky 2015/06/24 03:03:31 Add to member initializers and set to null.
xhwang 2015/06/26 16:30:07 Added Class Member Initializers.
+
+ // Enable logging.
logging::LoggingSettings settings;
settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
logging::InitLogging(settings);
// Display process ID, thread ID and timestamp in logs.
logging::SetLogItems(true, true, true, false);
+
+ StartIdleTimer();
}
bool MojoMediaApplication::ConfigureIncomingConnection(
@@ -49,14 +56,57 @@ void MojoMediaApplication::Create(
mojo::ApplicationConnection* connection,
mojo::InterfaceRequest<mojo::ContentDecryptionModule> request) {
// The created object is owned by the pipe.
- new MojoCdmService(&cdm_service_context_, request.Pass());
+ MojoCdmService* mojo_cdm_service =
+ new MojoCdmService(&cdm_service_context_, request.Pass());
+
+ // Passing unretained |this| is safe here because the app is guaranteed to
+ // outlive all services.
+ mojo_cdm_service->set_connection_error_handler(base::Bind(
+ &MojoMediaApplication::OnConnectionError, base::Unretained(this)));
sky 2015/06/24 03:03:31 I'm not familiar with this code. Who owns mojo_cmd
xhwang 2015/06/25 18:09:52 mojo_cmd_service is owned by the pipe since it use
+
+ active_service_count_++;
+ idle_timeout_callback_.Cancel();
}
void MojoMediaApplication::Create(
mojo::ApplicationConnection* connection,
mojo::InterfaceRequest<mojo::MediaRenderer> request) {
// The created object is owned by the pipe.
- new MojoRendererService(request.Pass());
+ MojoRendererService* mojo_renderer_service =
+ new MojoRendererService(request.Pass());
+
+ // Passing unretained |this| is safe here because the app is guaranteed to
+ // outlive all services.
+ mojo_renderer_service->set_connection_error_handler(base::Bind(
+ &MojoMediaApplication::OnConnectionError, base::Unretained(this)));
+
+ active_service_count_++;
+ idle_timeout_callback_.Cancel();
+}
+
+void MojoMediaApplication::Quit() {
+ app_impl_ = nullptr;
+}
+
+void MojoMediaApplication::StartIdleTimer() {
+ // Passing unretained |app_impl_| is safe here because |app_impl_| is
+ // guaranteed to outlive |this|, and the callback is canceled if |this| is
+ // destroyed.
+ idle_timeout_callback_.Reset(base::Bind(&mojo::ApplicationImpl::Terminate,
+ base::Unretained(app_impl_)));
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
+ FROM_HERE, idle_timeout_callback_.callback(),
+ base::TimeDelta::FromSeconds(kIdleTimeoutInSeconds));
+}
+
+void MojoMediaApplication::OnConnectionError() {
+ DCHECK_GT(active_service_count_, 0u);
+ active_service_count_--;
+ if (active_service_count_ == 0) {
+ // If the last service connection has been dropped, kick off an idle timeout
+ // to shut ourselves down.
+ StartIdleTimer();
+ }
}
} // namespace media

Powered by Google App Engine
This is Rietveld 408576698