OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/renderer/renderer_blink_platform_impl.h" | 5 #include "content/renderer/renderer_blink_platform_impl.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 1075 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1086 blink::WebPlatformEventType type) { | 1086 blink::WebPlatformEventType type) { |
1087 RenderThread* thread = RenderThreadImpl::current(); | 1087 RenderThread* thread = RenderThreadImpl::current(); |
1088 | 1088 |
1089 // When running layout tests, those observers should not listen to the actual | 1089 // When running layout tests, those observers should not listen to the actual |
1090 // hardware changes. In order to make that happen, they will receive a null | 1090 // hardware changes. In order to make that happen, they will receive a null |
1091 // thread. | 1091 // thread. |
1092 if (thread && RenderThreadImpl::current()->layout_test_mode()) | 1092 if (thread && RenderThreadImpl::current()->layout_test_mode()) |
1093 thread = NULL; | 1093 thread = NULL; |
1094 | 1094 |
1095 switch (type) { | 1095 switch (type) { |
1096 case blink::WebPlatformEventDeviceMotion: | |
1097 return new DeviceMotionEventPump(thread); | |
1098 case blink::WebPlatformEventDeviceOrientation: | |
1099 return new DeviceOrientationEventPump(thread); | |
1100 case blink::WebPlatformEventDeviceLight: | |
1101 return new DeviceLightEventPump(thread); | |
1102 case blink::WebPlatformEventGamepad: | 1096 case blink::WebPlatformEventGamepad: |
1103 return new GamepadSharedMemoryReader(thread); | 1097 return new GamepadSharedMemoryReader(thread); |
1104 case blink::WebPlatformEventScreenOrientation: | 1098 case blink::WebPlatformEventScreenOrientation: |
1105 return new ScreenOrientationObserver(); | 1099 return new ScreenOrientationObserver(); |
1106 default: | 1100 default: |
1107 // A default statement is required to prevent compilation errors when | 1101 // A default statement is required to prevent compilation errors when |
1108 // Blink adds a new type. | 1102 // Blink adds a new type. |
1109 DVLOG(1) << "RendererBlinkPlatformImpl::startListening() with " | 1103 DVLOG(1) << "RendererBlinkPlatformImpl::startListening() with " |
1110 "unknown type."; | 1104 "unknown type."; |
1111 } | 1105 } |
1112 | 1106 |
1113 return NULL; | 1107 return NULL; |
1114 } | 1108 } |
1115 | 1109 |
1116 void RendererBlinkPlatformImpl::SetPlatformEventObserverForTesting( | 1110 void RendererBlinkPlatformImpl::SetPlatformEventObserverForTesting( |
1117 blink::WebPlatformEventType type, | 1111 blink::WebPlatformEventType type, |
1118 scoped_ptr<PlatformEventObserverBase> observer) { | 1112 scoped_ptr<PlatformEventObserverBase> observer) { |
1119 DCHECK(type != blink::WebPlatformEventBattery); | 1113 DCHECK(type != blink::WebPlatformEventBattery); |
| 1114 DCHECK(type != blink::WebPlatformEventDeviceMotion); |
| 1115 DCHECK(type != blink::WebPlatformEventDeviceOrientation); |
| 1116 DCHECK(type != blink::WebPlatformEventDeviceLight); |
1120 | 1117 |
1121 if (platform_event_observers_.Lookup(type)) | 1118 if (platform_event_observers_.Lookup(type)) |
1122 platform_event_observers_.Remove(type); | 1119 platform_event_observers_.Remove(type); |
1123 platform_event_observers_.AddWithID(observer.release(), type); | 1120 platform_event_observers_.AddWithID(observer.release(), type); |
1124 } | 1121 } |
1125 | 1122 |
1126 void RendererBlinkPlatformImpl::startListening( | 1123 void RendererBlinkPlatformImpl::startListening( |
1127 blink::WebPlatformEventType type, | 1124 blink::WebPlatformEventType type, |
1128 blink::WebPlatformEventListener* listener) { | 1125 blink::WebPlatformEventListener* listener) { |
1129 if (type == blink::WebPlatformEventBattery) { | 1126 switch (type) { |
| 1127 case blink::WebPlatformEventBattery: |
1130 if (RenderThreadImpl::current() && | 1128 if (RenderThreadImpl::current() && |
1131 RenderThreadImpl::current()->layout_test_mode()) { | 1129 RenderThreadImpl::current()->layout_test_mode()) { |
1132 g_test_battery_status_listener = | 1130 g_test_battery_status_listener = |
1133 static_cast<blink::WebBatteryStatusListener*>(listener); | 1131 static_cast<blink::WebBatteryStatusListener*>(listener); |
1134 } else { | 1132 } else { |
1135 battery_status_dispatcher_.reset(new BatteryStatusDispatcher( | 1133 battery_status_dispatcher_.reset(new BatteryStatusDispatcher( |
1136 static_cast<blink::WebBatteryStatusListener*>(listener))); | 1134 static_cast<blink::WebBatteryStatusListener*>(listener))); |
1137 } | 1135 } |
1138 return; | 1136 return; |
| 1137 case blink::WebPlatformEventDeviceMotion: |
| 1138 device_motion_event_pump_.reset(new DeviceMotionEventPump( |
| 1139 static_cast<blink::WebDeviceMotionListener*>(listener))); |
| 1140 SendFakeDeviceEventDataForTesting(type); |
| 1141 return; |
| 1142 case blink::WebPlatformEventDeviceOrientation: |
| 1143 device_orientation_event_pump_.reset(new DeviceOrientationEventPump( |
| 1144 static_cast<blink::WebDeviceOrientationListener*>(listener))); |
| 1145 SendFakeDeviceEventDataForTesting(type); |
| 1146 return; |
| 1147 case blink::WebPlatformEventDeviceLight: |
| 1148 device_light_event_pump_.reset(new DeviceLightEventPump( |
| 1149 static_cast<blink::WebDeviceLightListener*>(listener))); |
| 1150 SendFakeDeviceEventDataForTesting(type); |
| 1151 return; |
| 1152 default: |
| 1153 break; |
1139 } | 1154 } |
1140 | 1155 |
1141 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type); | 1156 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type); |
1142 if (!observer) { | 1157 if (!observer) { |
1143 observer = CreatePlatformEventObserverFromType(type); | 1158 observer = CreatePlatformEventObserverFromType(type); |
1144 if (!observer) | 1159 if (!observer) |
1145 return; | 1160 return; |
1146 platform_event_observers_.AddWithID(observer, static_cast<int32>(type)); | 1161 platform_event_observers_.AddWithID(observer, static_cast<int32>(type)); |
1147 } | 1162 } |
1148 observer->Start(listener); | 1163 observer->Start(listener); |
| 1164 } |
1149 | 1165 |
| 1166 void RendererBlinkPlatformImpl::SendFakeDeviceEventDataForTesting( |
| 1167 blink::WebPlatformEventType type) { |
1150 // Device events (motion, orientation and light) expect to get an event fired | 1168 // Device events (motion, orientation and light) expect to get an event fired |
1151 // as soon as a listener is registered if a fake data was passed before. | 1169 // as soon as a listener is registered if a fake data was passed before. |
1152 // TODO(mlamouri,timvolodine): make those send mock values directly instead of | 1170 // TODO(mlamouri,timvolodine): make those send mock values directly instead of |
1153 // using this broken pattern. | 1171 // using this broken pattern. |
1154 if (RenderThreadImpl::current() && | 1172 if (!RenderThreadImpl::current() || |
1155 RenderThreadImpl::current()->layout_test_mode() && | 1173 !RenderThreadImpl::current()->layout_test_mode()) { |
1156 (type == blink::WebPlatformEventDeviceMotion || | 1174 return; |
1157 type == blink::WebPlatformEventDeviceOrientation || | |
1158 type == blink::WebPlatformEventDeviceLight)) { | |
1159 SendFakeDeviceEventDataForTesting(type); | |
1160 } | 1175 } |
1161 } | |
1162 | |
1163 void RendererBlinkPlatformImpl::SendFakeDeviceEventDataForTesting( | |
1164 blink::WebPlatformEventType type) { | |
1165 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type); | |
1166 CHECK(observer); | |
1167 | 1176 |
1168 void* data = 0; | 1177 void* data = 0; |
| 1178 DeviceSensorEventPump* event_pump = nullptr; |
1169 | 1179 |
1170 switch (type) { | 1180 switch (type) { |
1171 case blink::WebPlatformEventDeviceMotion: | 1181 case blink::WebPlatformEventDeviceMotion: |
1172 if (!(g_test_device_motion_data == 0)) | 1182 if (!(g_test_device_motion_data == 0)) |
1173 data = &g_test_device_motion_data.Get(); | 1183 data = &g_test_device_motion_data.Get(); |
| 1184 event_pump = |
| 1185 static_cast<DeviceSensorEventPump*>(device_motion_event_pump_.get()); |
1174 break; | 1186 break; |
1175 case blink::WebPlatformEventDeviceOrientation: | 1187 case blink::WebPlatformEventDeviceOrientation: |
1176 if (!(g_test_device_orientation_data == 0)) | 1188 if (!(g_test_device_orientation_data == 0)) |
1177 data = &g_test_device_orientation_data.Get(); | 1189 data = &g_test_device_orientation_data.Get(); |
| 1190 event_pump = static_cast<DeviceSensorEventPump*>( |
| 1191 device_orientation_event_pump_.get()); |
1178 break; | 1192 break; |
1179 case blink::WebPlatformEventDeviceLight: | 1193 case blink::WebPlatformEventDeviceLight: |
1180 if (g_test_device_light_data >= 0) | 1194 if (g_test_device_light_data >= 0) |
1181 data = &g_test_device_light_data; | 1195 data = &g_test_device_light_data; |
| 1196 event_pump = |
| 1197 static_cast<DeviceSensorEventPump*>(device_light_event_pump_.get()); |
1182 break; | 1198 break; |
1183 default: | 1199 default: |
1184 NOTREACHED(); | 1200 NOTREACHED(); |
1185 break; | 1201 break; |
1186 } | 1202 } |
1187 | 1203 |
1188 if (!data) | 1204 if (!data || !event_pump) |
1189 return; | 1205 return; |
1190 | 1206 |
1191 base::MessageLoopProxy::current()->PostTask( | 1207 base::MessageLoopProxy::current()->PostTask( |
1192 FROM_HERE, | 1208 FROM_HERE, base::Bind(&DeviceSensorEventPump::SendFakeDataForTesting, |
1193 base::Bind(&PlatformEventObserverBase::SendFakeDataForTesting, | 1209 base::Unretained(event_pump), data)); |
1194 base::Unretained(observer), data)); | |
1195 } | 1210 } |
1196 | 1211 |
1197 void RendererBlinkPlatformImpl::stopListening( | 1212 void RendererBlinkPlatformImpl::stopListening( |
1198 blink::WebPlatformEventType type) { | 1213 blink::WebPlatformEventType type) { |
1199 if (type == blink::WebPlatformEventBattery) { | 1214 switch (type) { |
1200 g_test_battery_status_listener = nullptr; | 1215 case blink::WebPlatformEventBattery: |
1201 battery_status_dispatcher_.reset(); | 1216 g_test_battery_status_listener = nullptr; |
1202 return; | 1217 battery_status_dispatcher_.reset(); |
| 1218 return; |
| 1219 case blink::WebPlatformEventDeviceMotion: |
| 1220 device_motion_event_pump_.reset(); |
| 1221 return; |
| 1222 case blink::WebPlatformEventDeviceOrientation: |
| 1223 device_orientation_event_pump_.reset(); |
| 1224 return; |
| 1225 case blink::WebPlatformEventDeviceLight: |
| 1226 device_light_event_pump_.reset(); |
| 1227 return; |
| 1228 default: |
| 1229 break; |
1203 } | 1230 } |
1204 | 1231 |
1205 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type); | 1232 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type); |
1206 if (!observer) | 1233 if (!observer) |
1207 return; | 1234 return; |
1208 observer->Stop(); | 1235 observer->Stop(); |
1209 } | 1236 } |
1210 | 1237 |
1211 //------------------------------------------------------------------------------ | 1238 //------------------------------------------------------------------------------ |
1212 | 1239 |
(...skipping 14 matching lines...) Expand all Loading... |
1227 //------------------------------------------------------------------------------ | 1254 //------------------------------------------------------------------------------ |
1228 | 1255 |
1229 void RendererBlinkPlatformImpl::MockBatteryStatusChangedForTesting( | 1256 void RendererBlinkPlatformImpl::MockBatteryStatusChangedForTesting( |
1230 const blink::WebBatteryStatus& status) { | 1257 const blink::WebBatteryStatus& status) { |
1231 if (!g_test_battery_status_listener) | 1258 if (!g_test_battery_status_listener) |
1232 return; | 1259 return; |
1233 g_test_battery_status_listener->updateBatteryStatus(status); | 1260 g_test_battery_status_listener->updateBatteryStatus(status); |
1234 } | 1261 } |
1235 | 1262 |
1236 } // namespace content | 1263 } // namespace content |
OLD | NEW |