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

Unified Diff: services/device/device_service.cc

Issue 2730333002: Make DeviceSensorService implement MainLoop::DestructionObserver (Closed)
Patch Set: shutdown in UI thread for Android. Created 3 years, 9 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
« no previous file with comments | « services/device/device_service.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: services/device/device_service.cc
diff --git a/services/device/device_service.cc b/services/device/device_service.cc
index 0044460a1d9c1dd4bb4d99313ae24f2582b861bb..ab7d66e69414d0231f266c2b9653c7c1fd1d4aef 100644
--- a/services/device/device_service.cc
+++ b/services/device/device_service.cc
@@ -7,7 +7,10 @@
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h"
+#include "base/message_loop/message_loop.h"
#include "base/threading/thread_task_runner_handle.h"
+#include "base/trace_event/trace_event.h"
+#include "device/sensors/device_sensor_service.h"
#include "services/device/fingerprint/fingerprint.h"
#include "services/device/power_monitor/power_monitor_message_broadcaster.h"
#include "services/device/time_zone_monitor/time_zone_monitor.h"
@@ -18,10 +21,60 @@
#include "services/device/android/register_jni.h"
#endif
+namespace {
+
+void ShutDownPlatformServices() {
+ TRACE_EVENT0("shutdown", "Subsystem:SensorService");
+ device::DeviceSensorService::GetInstance()->Shutdown();
+}
+
+// IOMessageLoopObserver is responsible for shutdown platform services when it
+// observers IO thread is going to close. It is created by DeviceService in UI
+// thread. When it is notified that the IO thread is closing, it first shut
+// down those platform services, then delete itself. Both the shutdown and self
+// deleting is on IO thread.
+class IOMessageLoopObserver : public base::MessageLoop::DestructionObserver {
+ public:
+ IOMessageLoopObserver(
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
+ : io_task_runner_(io_task_runner) {
+ DCHECK(io_task_runner_);
+ io_task_runner_->PostTask(
+ FROM_HERE, base::Bind(&IOMessageLoopObserver::AddDestructionObserver,
+ base::Unretained(this)));
+ }
+
+ // Run in IO thread.
+ void AddDestructionObserver() {
+ DCHECK(io_task_runner_->BelongsToCurrentThread());
+ base::MessageLoop::current()->AddDestructionObserver(this);
+ }
+
+ // base::MessageLoop::DestructionObserver, run in IO thread:
+ void WillDestroyCurrentMessageLoop() override {
+ DCHECK(io_task_runner_ && io_task_runner_->BelongsToCurrentThread());
+ base::MessageLoop::current()->RemoveDestructionObserver(this);
+#if !defined(OS_ANDROID)
+ ShutDownPlatformServices();
+#endif
+ delete this;
+ }
+
+ private:
+ ~IOMessageLoopObserver() override {}
+
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
+
+ DISALLOW_COPY_AND_ASSIGN(IOMessageLoopObserver);
+};
+
+} // namespace
+
namespace device {
std::unique_ptr<service_manager::Service> CreateDeviceService(
- scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) {
+ scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner) {
#if defined(OS_ANDROID)
if (!EnsureJniRegistered()) {
DLOG(ERROR) << "Failed to register JNI for Device Service";
@@ -29,14 +82,23 @@ std::unique_ptr<service_manager::Service> CreateDeviceService(
}
#endif
- return base::MakeUnique<DeviceService>(std::move(file_task_runner));
+ return base::MakeUnique<DeviceService>(std::move(file_task_runner),
+ std::move(io_task_runner));
}
DeviceService::DeviceService(
- scoped_refptr<base::SingleThreadTaskRunner> file_task_runner)
- : file_task_runner_(std::move(file_task_runner)) {}
+ scoped_refptr<base::SingleThreadTaskRunner> file_task_runner,
+ scoped_refptr<base::SingleThreadTaskRunner> io_task_runner)
+ : file_task_runner_(std::move(file_task_runner)),
+ io_task_runner_(std::move(io_task_runner)) {
+ new IOMessageLoopObserver(io_task_runner_);
ke.he 2017/03/07 09:56:41 Now the IOMessageLoopObserver is created in Device
+}
-DeviceService::~DeviceService() {}
+DeviceService::~DeviceService() {
+#if defined(OS_ANDROID)
+ ShutDownPlatformServices();
ke.he 2017/03/07 09:56:41 For Android, still shutdown in UI thread. It is sa
+#endif
+}
void DeviceService::OnStart() {}
« no previous file with comments | « services/device/device_service.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698