OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/screen/public/screen_manager.h" | 5 #include "athena/screen/public/screen_manager.h" |
6 #include "athena/system/orientation_controller.h" | 6 #include "athena/system/orientation_controller.h" |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
9 #include "base/files/file_path_watcher.h" | 9 #include "base/files/file_path_watcher.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 | 51 |
52 // Ambient light level in SI lux units. | 52 // Ambient light level in SI lux units. |
53 float light; | 53 float light; |
54 }; | 54 }; |
55 }; | 55 }; |
56 | 56 |
57 } // namespace | 57 } // namespace |
58 | 58 |
59 OrientationController::OrientationController() | 59 OrientationController::OrientationController() |
60 : DeviceSocketListener(kSocketPath, sizeof(DeviceSensorEvent)), | 60 : DeviceSocketListener(kSocketPath, sizeof(DeviceSensorEvent)), |
61 last_orientation_change_time_(0) { | 61 last_orientation_change_time_(0), |
| 62 shutdown_(false) { |
62 CHECK(base::MessageLoopForUI::current()); | 63 CHECK(base::MessageLoopForUI::current()); |
63 ui_task_runner_ = base::MessageLoopForUI::current()->task_runner(); | 64 ui_task_runner_ = base::MessageLoopForUI::current()->task_runner(); |
64 } | 65 } |
65 | 66 |
66 void OrientationController::InitWith( | 67 void OrientationController::InitWith( |
67 scoped_refptr<base::TaskRunner> io_task_runner) { | 68 scoped_refptr<base::TaskRunner> file_task_runner) { |
68 io_task_runner->PostTask(FROM_HERE, base::Bind( | 69 file_task_runner_ = file_task_runner; |
69 &OrientationController::WatchForSocketPathOnIO, this)); | 70 file_task_runner->PostTask( |
| 71 FROM_HERE, |
| 72 base::Bind(&OrientationController::WatchForSocketPathOnFILE, this)); |
70 } | 73 } |
71 | 74 |
72 OrientationController::~OrientationController() { | 75 OrientationController::~OrientationController() { |
73 } | 76 } |
74 | 77 |
75 void OrientationController::WatchForSocketPathOnIO() { | 78 void OrientationController::Shutdown() { |
| 79 CHECK(file_task_runner_); |
| 80 StopListening(); |
| 81 file_task_runner_->PostTask( |
| 82 FROM_HERE, |
| 83 base::Bind(&OrientationController::ShutdownOnFILE, this)); |
| 84 } |
| 85 |
| 86 void OrientationController::ShutdownOnFILE() { |
| 87 shutdown_ = true; |
| 88 watcher_.reset(); |
| 89 } |
| 90 |
| 91 void OrientationController::WatchForSocketPathOnFILE() { |
76 CHECK(base::MessageLoopForIO::current()); | 92 CHECK(base::MessageLoopForIO::current()); |
77 if (base::PathExists(base::FilePath(kSocketPath))) { | 93 if (base::PathExists(base::FilePath(kSocketPath))) { |
78 ui_task_runner_->PostTask(FROM_HERE, | 94 ui_task_runner_->PostTask(FROM_HERE, |
79 base::Bind(&OrientationController::StartListening, this)); | 95 base::Bind(&OrientationController::StartListening, this)); |
80 } else { | 96 } else { |
81 watcher_.reset(new base::FilePathWatcher); | 97 watcher_.reset(new base::FilePathWatcher); |
82 watcher_->Watch( | 98 watcher_->Watch( |
83 base::FilePath(kSocketPath), false, | 99 base::FilePath(kSocketPath), |
84 base::Bind(&OrientationController::OnFilePathChangedOnIO, this)); | 100 false, |
| 101 base::Bind(&OrientationController::OnFilePathChangedOnFILE, this)); |
85 } | 102 } |
86 } | 103 } |
87 | 104 |
88 void OrientationController::OnFilePathChangedOnIO(const base::FilePath& path, | 105 void OrientationController::OnFilePathChangedOnFILE(const base::FilePath& path, |
89 bool error) { | 106 bool error) { |
90 watcher_.reset(); | 107 watcher_.reset(); |
91 if (error) | 108 if (error || shutdown_) |
92 return; | 109 return; |
93 | 110 |
94 ui_task_runner_->PostTask(FROM_HERE, | 111 ui_task_runner_->PostTask(FROM_HERE, |
95 base::Bind(&OrientationController::StartListening, this)); | 112 base::Bind(&OrientationController::StartListening, this)); |
96 } | 113 } |
97 | 114 |
98 void OrientationController::OnDataAvailableOnIO(const void* data) { | 115 void OrientationController::OnDataAvailableOnFILE(const void* data) { |
99 const DeviceSensorEvent* event = | 116 const DeviceSensorEvent* event = |
100 static_cast<const DeviceSensorEvent*>(data); | 117 static_cast<const DeviceSensorEvent*>(data); |
101 if (event->type != SENSOR_ACCELEROMETER) | 118 if (event->type != SENSOR_ACCELEROMETER) |
102 return; | 119 return; |
103 | 120 |
104 float gravity_x = event->data[0]; | 121 float gravity_x = event->data[0]; |
105 float gravity_y = event->data[1]; | 122 float gravity_y = event->data[1]; |
106 gfx::Display::Rotation rotation; | 123 gfx::Display::Rotation rotation; |
107 if (gravity_x < -kGravityThreshold) { | 124 if (gravity_x < -kGravityThreshold) { |
108 rotation = gfx::Display::ROTATE_270; | 125 rotation = gfx::Display::ROTATE_270; |
(...skipping 15 matching lines...) Expand all Loading... |
124 } | 141 } |
125 | 142 |
126 last_orientation_change_time_ = event->timestamp; | 143 last_orientation_change_time_ = event->timestamp; |
127 current_rotation_ = rotation; | 144 current_rotation_ = rotation; |
128 ui_task_runner_->PostTask(FROM_HERE, | 145 ui_task_runner_->PostTask(FROM_HERE, |
129 base::Bind(&OrientationController::RotateOnUI, this, rotation)); | 146 base::Bind(&OrientationController::RotateOnUI, this, rotation)); |
130 } | 147 } |
131 | 148 |
132 void OrientationController::RotateOnUI(gfx::Display::Rotation rotation) { | 149 void OrientationController::RotateOnUI(gfx::Display::Rotation rotation) { |
133 ScreenManager* screen_manager = ScreenManager::Get(); | 150 ScreenManager* screen_manager = ScreenManager::Get(); |
134 // Since this is called from the IO thread, the screen manager may no longer | 151 // Since this is called from the FILE thread, the screen manager may no longer |
135 // exist. | 152 // exist. |
136 if (screen_manager) | 153 if (screen_manager) |
137 screen_manager->SetRotation(rotation); | 154 screen_manager->SetRotation(rotation); |
138 } | 155 } |
139 | 156 |
140 } // namespace athena | 157 } // namespace athena |
OLD | NEW |