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

Side by Side Diff: content/renderer/renderer_webkitplatformsupport_impl.cc

Issue 446603002: Refactor code listening to platform events in content/renderer/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@webkitplatform_impl_start_stop
Patch Set: apply review comments Created 6 years, 4 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 unified diff | Download patch
OLDNEW
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_webkitplatformsupport_impl.h" 5 #include "content/renderer/renderer_webkitplatformsupport_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 20 matching lines...) Expand all
31 #include "content/common/gpu/client/context_provider_command_buffer.h" 31 #include "content/common/gpu/client/context_provider_command_buffer.h"
32 #include "content/common/gpu/client/gpu_channel_host.h" 32 #include "content/common/gpu/client/gpu_channel_host.h"
33 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h" 33 #include "content/common/gpu/client/webgraphicscontext3d_command_buffer_impl.h"
34 #include "content/common/gpu/gpu_process_launch_causes.h" 34 #include "content/common/gpu/gpu_process_launch_causes.h"
35 #include "content/common/mime_registry_messages.h" 35 #include "content/common/mime_registry_messages.h"
36 #include "content/common/view_messages.h" 36 #include "content/common/view_messages.h"
37 #include "content/public/common/content_switches.h" 37 #include "content/public/common/content_switches.h"
38 #include "content/public/common/webplugininfo.h" 38 #include "content/public/common/webplugininfo.h"
39 #include "content/public/renderer/content_renderer_client.h" 39 #include "content/public/renderer/content_renderer_client.h"
40 #include "content/renderer/battery_status/battery_status_dispatcher.h" 40 #include "content/renderer/battery_status/battery_status_dispatcher.h"
41 #include "content/renderer/battery_status/fake_battery_status_dispatcher.h"
42 #include "content/renderer/device_sensors/device_light_event_pump.h" 41 #include "content/renderer/device_sensors/device_light_event_pump.h"
43 #include "content/renderer/device_sensors/device_motion_event_pump.h" 42 #include "content/renderer/device_sensors/device_motion_event_pump.h"
44 #include "content/renderer/device_sensors/device_orientation_event_pump.h" 43 #include "content/renderer/device_sensors/device_orientation_event_pump.h"
45 #include "content/renderer/dom_storage/webstoragenamespace_impl.h" 44 #include "content/renderer/dom_storage/webstoragenamespace_impl.h"
46 #include "content/renderer/gamepad_shared_memory_reader.h" 45 #include "content/renderer/gamepad_shared_memory_reader.h"
47 #include "content/renderer/media/audio_decoder.h" 46 #include "content/renderer/media/audio_decoder.h"
48 #include "content/renderer/media/crypto/key_systems.h" 47 #include "content/renderer/media/crypto/key_systems.h"
49 #include "content/renderer/media/renderer_webaudiodevice_impl.h" 48 #include "content/renderer/media/renderer_webaudiodevice_impl.h"
50 #include "content/renderer/media/renderer_webmidiaccessor_impl.h" 49 #include "content/renderer/media/renderer_webmidiaccessor_impl.h"
51 #include "content/renderer/media/webcontentdecryptionmodule_impl.h" 50 #include "content/renderer/media/webcontentdecryptionmodule_impl.h"
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 namespace content { 139 namespace content {
141 140
142 namespace { 141 namespace {
143 142
144 bool g_sandbox_enabled = true; 143 bool g_sandbox_enabled = true;
145 double g_test_device_light_data = -1; 144 double g_test_device_light_data = -1;
146 base::LazyInstance<blink::WebDeviceMotionData>::Leaky 145 base::LazyInstance<blink::WebDeviceMotionData>::Leaky
147 g_test_device_motion_data = LAZY_INSTANCE_INITIALIZER; 146 g_test_device_motion_data = LAZY_INSTANCE_INITIALIZER;
148 base::LazyInstance<blink::WebDeviceOrientationData>::Leaky 147 base::LazyInstance<blink::WebDeviceOrientationData>::Leaky
149 g_test_device_orientation_data = LAZY_INSTANCE_INITIALIZER; 148 g_test_device_orientation_data = LAZY_INSTANCE_INITIALIZER;
150 base::LazyInstance<FakeBatteryStatusDispatcher>::Leaky
151 g_test_battery_status_dispatcher = LAZY_INSTANCE_INITIALIZER;
152 149
153 } // namespace 150 } // namespace
154 151
155 //------------------------------------------------------------------------------ 152 //------------------------------------------------------------------------------
156 153
157 class RendererWebKitPlatformSupportImpl::MimeRegistry 154 class RendererWebKitPlatformSupportImpl::MimeRegistry
158 : public SimpleWebMimeRegistryImpl { 155 : public SimpleWebMimeRegistryImpl {
159 public: 156 public:
160 virtual blink::WebMimeRegistry::SupportsType supportsMediaMIMEType( 157 virtual blink::WebMimeRegistry::SupportsType supportsMediaMIMEType(
161 const blink::WebString& mime_type, 158 const blink::WebString& mime_type,
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
221 218
222 //------------------------------------------------------------------------------ 219 //------------------------------------------------------------------------------
223 220
224 RendererWebKitPlatformSupportImpl::RendererWebKitPlatformSupportImpl() 221 RendererWebKitPlatformSupportImpl::RendererWebKitPlatformSupportImpl()
225 : clipboard_client_(new RendererClipboardClient), 222 : clipboard_client_(new RendererClipboardClient),
226 clipboard_(new WebClipboardImpl(clipboard_client_.get())), 223 clipboard_(new WebClipboardImpl(clipboard_client_.get())),
227 mime_registry_(new RendererWebKitPlatformSupportImpl::MimeRegistry), 224 mime_registry_(new RendererWebKitPlatformSupportImpl::MimeRegistry),
228 sudden_termination_disables_(0), 225 sudden_termination_disables_(0),
229 plugin_refresh_allowed_(true), 226 plugin_refresh_allowed_(true),
230 child_thread_loop_(base::MessageLoopProxy::current()), 227 child_thread_loop_(base::MessageLoopProxy::current()),
231 web_scrollbar_behavior_(new WebScrollbarBehaviorImpl), 228 web_scrollbar_behavior_(new WebScrollbarBehaviorImpl) {
232 gamepad_provider_(NULL) {
233 if (g_sandbox_enabled && sandboxEnabled()) { 229 if (g_sandbox_enabled && sandboxEnabled()) {
234 sandbox_support_.reset( 230 sandbox_support_.reset(
235 new RendererWebKitPlatformSupportImpl::SandboxSupport); 231 new RendererWebKitPlatformSupportImpl::SandboxSupport);
236 } else { 232 } else {
237 DVLOG(1) << "Disabling sandbox support for testing."; 233 DVLOG(1) << "Disabling sandbox support for testing.";
238 } 234 }
239 235
240 // ChildThread may not exist in some tests. 236 // ChildThread may not exist in some tests.
241 if (ChildThread::current()) { 237 if (ChildThread::current()) {
242 sync_message_filter_ = ChildThread::current()->sync_message_filter(); 238 sync_message_filter_ = ChildThread::current()->sync_message_filter();
(...skipping 634 matching lines...) Expand 10 before | Expand all | Expand 10 after
877 //------------------------------------------------------------------------------ 873 //------------------------------------------------------------------------------
878 874
879 WebBlobRegistry* RendererWebKitPlatformSupportImpl::blobRegistry() { 875 WebBlobRegistry* RendererWebKitPlatformSupportImpl::blobRegistry() {
880 // blob_registry_ can be NULL when running some tests. 876 // blob_registry_ can be NULL when running some tests.
881 return blob_registry_.get(); 877 return blob_registry_.get();
882 } 878 }
883 879
884 //------------------------------------------------------------------------------ 880 //------------------------------------------------------------------------------
885 881
886 void RendererWebKitPlatformSupportImpl::sampleGamepads(WebGamepads& gamepads) { 882 void RendererWebKitPlatformSupportImpl::sampleGamepads(WebGamepads& gamepads) {
887 DCHECK(gamepad_provider_); 883 PlatformEventObserverBase* observer =
888 gamepad_provider_->SampleGamepads(gamepads); 884 platform_event_observers_.Lookup(blink::WebPlatformEventGamepad);
885 if (!observer)
886 return;
887 static_cast<RendererGamepadProvider*>(observer)->SampleGamepads(gamepads);
889 } 888 }
890 889
891 //------------------------------------------------------------------------------ 890 //------------------------------------------------------------------------------
892 891
893 WebRTCPeerConnectionHandler* 892 WebRTCPeerConnectionHandler*
894 RendererWebKitPlatformSupportImpl::createRTCPeerConnectionHandler( 893 RendererWebKitPlatformSupportImpl::createRTCPeerConnectionHandler(
895 WebRTCPeerConnectionHandlerClient* client) { 894 WebRTCPeerConnectionHandlerClient* client) {
896 RenderThreadImpl* render_thread = RenderThreadImpl::current(); 895 RenderThreadImpl* render_thread = RenderThreadImpl::current();
897 DCHECK(render_thread); 896 DCHECK(render_thread);
898 if (!render_thread) 897 if (!render_thread)
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
1043 RenderThread::Get()->Send( 1042 RenderThread::Get()->Send(
1044 new ViewHostMsg_Vibrate(base::checked_cast<int64>(milliseconds))); 1043 new ViewHostMsg_Vibrate(base::checked_cast<int64>(milliseconds)));
1045 } 1044 }
1046 1045
1047 void RendererWebKitPlatformSupportImpl::cancelVibration() { 1046 void RendererWebKitPlatformSupportImpl::cancelVibration() {
1048 RenderThread::Get()->Send(new ViewHostMsg_CancelVibration()); 1047 RenderThread::Get()->Send(new ViewHostMsg_CancelVibration());
1049 } 1048 }
1050 1049
1051 //------------------------------------------------------------------------------ 1050 //------------------------------------------------------------------------------
1052 1051
1053 void RendererWebKitPlatformSupportImpl::startListening( 1052 // static
1054 blink::WebPlatformEventType type, 1053 PlatformEventObserverBase*
1055 blink::WebPlatformEventListener* listener) { 1054 RendererWebKitPlatformSupportImpl::CreatePlatformEventObserverFromType(
1055 blink::WebPlatformEventType type) {
1056 RenderThread* thread = RenderThreadImpl::current();
1057
1058 // When running layout tests, those observers should not listen to the actual
1059 // hardware changes. In order to make that happen, they will receive a null
1060 // thread.
1061 if (thread && RenderThreadImpl::current()->layout_test_mode())
1062 thread = 0;
1063
1056 switch (type) { 1064 switch (type) {
1057 case blink::WebPlatformEventDeviceMotion: 1065 case blink::WebPlatformEventDeviceMotion: {
1058 SetDeviceMotionListener( 1066 return new DeviceMotionEventPump(thread);
1059 static_cast<blink::WebDeviceMotionListener*>(listener)); 1067 }
1060 break; 1068 case blink::WebPlatformEventDeviceOrientation: {
1061 case blink::WebPlatformEventDeviceOrientation: 1069 return new DeviceOrientationEventPump(thread);
1062 SetDeviceOrientationListener( 1070 }
1063 static_cast<blink::WebDeviceOrientationListener*>(listener)); 1071 case blink::WebPlatformEventDeviceLight: {
1064 break; 1072 return new DeviceLightEventPump(thread);
1065 case blink::WebPlatformEventDeviceLight: 1073 }
1066 SetDeviceLightListener( 1074 case blink::WebPlatformEventBattery: {
1067 static_cast<blink::WebDeviceLightListener*>(listener)); 1075 return new BatteryStatusDispatcher(thread);
1068 break; 1076 }
1069 case blink::WebPlatformEventBattery:
1070 SetBatteryStatusListener(
1071 static_cast<blink::WebBatteryStatusListener*>(listener));
1072 break;
1073 case blink::WebPlatformEventGamepad: 1077 case blink::WebPlatformEventGamepad:
1074 SetGamepadListener( 1078 return new GamepadSharedMemoryReader(thread);
1075 static_cast<blink::WebGamepadListener*>(listener));
1076 break;
1077 default: 1079 default:
1078 // A default statement is required to prevent compilation errors when Blink 1080 // A default statement is required to prevent compilation errors when Blink
1079 // adds a new type. 1081 // adds a new type.
1080 VLOG(1) << "RendererWebKitPlatformSupportImpl::startListening() with " 1082 VLOG(1) << "RendererWebKitPlatformSupportImpl::startListening() with "
1081 "unknown type."; 1083 "unknown type.";
1082 } 1084 }
1085
1086 return 0;
1087 }
1088
1089 void RendererWebKitPlatformSupportImpl::SetPlatformEventObserverForTesting(
1090 blink::WebPlatformEventType type, PlatformEventObserverBase* observer) {
1091 if (platform_event_observers_.Lookup(type))
1092 platform_event_observers_.Remove(type);
1093 platform_event_observers_.AddWithID(observer, type);
1094 }
1095
1096
1097 void RendererWebKitPlatformSupportImpl::startListening(
1098 blink::WebPlatformEventType type,
1099 blink::WebPlatformEventListener* listener) {
1100 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type);
1101 if (!observer) {
1102 observer = CreatePlatformEventObserverFromType(type);
1103 if (!observer)
1104 return;
1105 platform_event_observers_.AddWithID(observer, static_cast<int32>(type));
1106 }
1107 observer->Start(listener);
1108
1109 // Device events (motion, orientation and light) expect to get an event fired
1110 // as soon as a listener is registered if a fake data was passed before.
1111 // TODO(mlamouri,timvolodine): make those send mock values directly instead of
1112 // using this broken pattern.
1113 if (RenderThreadImpl::current() &&
1114 RenderThreadImpl::current()->layout_test_mode() &&
1115 (type == blink::WebPlatformEventDeviceMotion ||
1116 type == blink::WebPlatformEventDeviceOrientation ||
1117 type == blink::WebPlatformEventDeviceLight)) {
1118 SendFakeDeviceEventDataForTesting(type);
1119 }
1120 }
1121
1122 void RendererWebKitPlatformSupportImpl::SendFakeDeviceEventDataForTesting(
1123 blink::WebPlatformEventType type) {
1124 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type);
1125 CHECK(observer);
1126
1127 void* data = 0;
1128
1129 switch (type) {
1130 case blink::WebPlatformEventDeviceMotion:
1131 if (!(g_test_device_motion_data == 0))
1132 data = &g_test_device_motion_data.Get();
1133 break;
1134 case blink::WebPlatformEventDeviceOrientation:
1135 if (!(g_test_device_orientation_data == 0))
1136 data = &g_test_device_orientation_data.Get();
1137 break;
1138 case blink::WebPlatformEventDeviceLight:
1139 if (g_test_device_light_data >= 0)
1140 data = &g_test_device_light_data;
1141 break;
1142 default:
1143 NOTREACHED();
1144 break;
1145 }
1146
1147 if (!data)
1148 return;
1149
1150 base::MessageLoopProxy::current()->PostTask(
1151 FROM_HERE,
1152 base::Bind(&PlatformEventObserverBase::SendFakeDataForTesting,
1153 base::Unretained(observer), data));
1083 } 1154 }
1084 1155
1085 void RendererWebKitPlatformSupportImpl::stopListening( 1156 void RendererWebKitPlatformSupportImpl::stopListening(
1086 blink::WebPlatformEventType type) { 1157 blink::WebPlatformEventType type) {
1087 switch (type) { 1158 PlatformEventObserverBase* observer = platform_event_observers_.Lookup(type);
1088 case blink::WebPlatformEventDeviceMotion: 1159 if (!observer)
1089 SetDeviceMotionListener(0);
1090 break;
1091 case blink::WebPlatformEventDeviceOrientation:
1092 SetDeviceOrientationListener(0);
1093 break;
1094 case blink::WebPlatformEventDeviceLight:
1095 SetDeviceLightListener(0);
1096 break;
1097 case blink::WebPlatformEventBattery:
1098 SetBatteryStatusListener(0);
1099 break;
1100 case blink::WebPlatformEventGamepad:
1101 SetGamepadListener(0);
1102 break;
1103 default:
1104 // A default statement is required to prevent compilation errors when Blink
1105 // adds a new type.
1106 VLOG(1) << "RendererWebKitPlatformSupportImpl::stopListening() with "
1107 "unknown type.";
1108 }
1109 }
1110
1111 void RendererWebKitPlatformSupportImpl::SetDeviceMotionListener(
1112 blink::WebDeviceMotionListener* listener) {
1113 if (g_test_device_motion_data == 0) {
1114 if (!device_motion_event_pump_) {
1115 device_motion_event_pump_.reset(new DeviceMotionEventPump);
1116 device_motion_event_pump_->Attach(RenderThreadImpl::current());
1117 }
1118 device_motion_event_pump_->SetListener(listener);
1119 } else if (listener) {
1120 // Testing mode: just echo the test data to the listener.
1121 base::MessageLoopProxy::current()->PostTask(
1122 FROM_HERE,
1123 base::Bind(&blink::WebDeviceMotionListener::didChangeDeviceMotion,
1124 base::Unretained(listener),
1125 g_test_device_motion_data.Get()));
1126 }
1127 }
1128
1129 void RendererWebKitPlatformSupportImpl::SetDeviceOrientationListener(
1130 blink::WebDeviceOrientationListener* listener) {
1131 if (g_test_device_orientation_data == 0) {
1132 if (!device_orientation_event_pump_) {
1133 device_orientation_event_pump_.reset(new DeviceOrientationEventPump);
1134 device_orientation_event_pump_->Attach(RenderThreadImpl::current());
1135 }
1136 device_orientation_event_pump_->SetListener(listener);
1137 } else if (listener) {
1138 // Testing mode: just echo the test data to the listener.
1139 base::MessageLoopProxy::current()->PostTask(
1140 FROM_HERE,
1141 base::Bind(
1142 &blink::WebDeviceOrientationListener::didChangeDeviceOrientation,
1143 base::Unretained(listener),
1144 g_test_device_orientation_data.Get()));
1145 }
1146 }
1147
1148 void RendererWebKitPlatformSupportImpl::SetDeviceLightListener(
1149 blink::WebDeviceLightListener* listener) {
1150 if (g_test_device_light_data < 0) {
1151 if (!device_light_event_pump_) {
1152 device_light_event_pump_.reset(new DeviceLightEventPump);
1153 device_light_event_pump_->Attach(RenderThreadImpl::current());
1154 }
1155 device_light_event_pump_->SetListener(listener);
1156 } else if (listener) {
1157 // Testing mode: just echo the test data to the listener.
1158 base::MessageLoopProxy::current()->PostTask(
1159 FROM_HERE,
1160 base::Bind(&blink::WebDeviceLightListener::didChangeDeviceLight,
1161 base::Unretained(listener),
1162 g_test_device_light_data));
1163 }
1164 }
1165
1166 void RendererWebKitPlatformSupportImpl::SetGamepadListener(
1167 blink::WebGamepadListener* listener) {
1168 DCHECK(gamepad_provider_);
1169 gamepad_provider_->SetGamepadListener(listener);
1170 }
1171
1172 void RendererWebKitPlatformSupportImpl::SetBatteryStatusListener(
1173 blink::WebBatteryStatusListener* listener) {
1174 if (RenderThreadImpl::current() &&
1175 RenderThreadImpl::current()->layout_test_mode()) {
1176 // If we are in test mode, we want to use a fake battery status dispatcher,
1177 // which does not communicate with the browser process. Battery status
1178 // changes are signalled by invoking MockBatteryStatusChangedForTesting().
1179 g_test_battery_status_dispatcher.Get().SetListener(listener);
1180 return; 1160 return;
1181 } 1161 observer->Stop();
1182
1183 if (!battery_status_dispatcher_) {
1184 battery_status_dispatcher_.reset(
1185 new BatteryStatusDispatcher(RenderThreadImpl::current()));
1186 }
1187 battery_status_dispatcher_->SetListener(listener);
1188 } 1162 }
1189 1163
1190 //------------------------------------------------------------------------------ 1164 //------------------------------------------------------------------------------
1191 1165
1192 void RendererWebKitPlatformSupportImpl::queryStorageUsageAndQuota( 1166 void RendererWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
1193 const blink::WebURL& storage_partition, 1167 const blink::WebURL& storage_partition,
1194 blink::WebStorageQuotaType type, 1168 blink::WebStorageQuotaType type,
1195 blink::WebStorageQuotaCallbacks callbacks) { 1169 blink::WebStorageQuotaCallbacks callbacks) {
1196 if (!thread_safe_sender_.get() || !quota_message_filter_.get()) 1170 if (!thread_safe_sender_.get() || !quota_message_filter_.get())
1197 return; 1171 return;
1198 QuotaDispatcher::ThreadSpecificInstance( 1172 QuotaDispatcher::ThreadSpecificInstance(
1199 thread_safe_sender_.get(), 1173 thread_safe_sender_.get(),
1200 quota_message_filter_.get())->QueryStorageUsageAndQuota( 1174 quota_message_filter_.get())->QueryStorageUsageAndQuota(
1201 storage_partition, 1175 storage_partition,
1202 static_cast<quota::StorageType>(type), 1176 static_cast<quota::StorageType>(type),
1203 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks)); 1177 QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
1204 } 1178 }
1205 1179
1206 //------------------------------------------------------------------------------ 1180 //------------------------------------------------------------------------------
1207 1181
1208 // static
1209 void RendererWebKitPlatformSupportImpl::MockBatteryStatusChangedForTesting( 1182 void RendererWebKitPlatformSupportImpl::MockBatteryStatusChangedForTesting(
1210 const blink::WebBatteryStatus& status) { 1183 const blink::WebBatteryStatus& status) {
1211 g_test_battery_status_dispatcher.Get().PostBatteryStatusChange(status); 1184 PlatformEventObserverBase* observer =
1185 platform_event_observers_.Lookup(blink::WebPlatformEventBattery);
1186 if (!observer)
1187 return;
1188 observer->SendFakeDataForTesting((void*)&status);
1212 } 1189 }
1213 1190
1214 } // namespace content 1191 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698