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 "content/child/blink_platform_impl.h" | 5 #include "content/child/blink_platform_impl.h" |
6 | 6 |
7 #include <math.h> | 7 #include <math.h> |
8 | 8 |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 // for the added symbol here. | 425 // for the added symbol here. |
426 default: | 426 default: |
427 break; | 427 break; |
428 } | 428 } |
429 return -1; | 429 return -1; |
430 } | 430 } |
431 | 431 |
432 // TODO(skyostil): Ensure that we always have an active task runner when | 432 // TODO(skyostil): Ensure that we always have an active task runner when |
433 // constructing the platform. | 433 // constructing the platform. |
434 BlinkPlatformImpl::BlinkPlatformImpl() | 434 BlinkPlatformImpl::BlinkPlatformImpl() |
435 : main_thread_task_runner_(base::ThreadTaskRunnerHandle::IsSet() | 435 : BlinkPlatformImpl(base::ThreadTaskRunnerHandle::IsSet() |
436 ? base::ThreadTaskRunnerHandle::Get() | 436 ? base::ThreadTaskRunnerHandle::Get() |
437 : nullptr), | 437 : nullptr) { |
438 shared_timer_func_(NULL), | |
439 shared_timer_fire_time_(0.0), | |
440 shared_timer_fire_time_was_set_while_suspended_(false), | |
441 shared_timer_suspended_(0) { | |
442 InternalInit(); | |
443 } | 438 } |
444 | 439 |
445 BlinkPlatformImpl::BlinkPlatformImpl( | 440 BlinkPlatformImpl::BlinkPlatformImpl( |
446 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) | 441 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) |
447 : main_thread_task_runner_(main_thread_task_runner), | 442 : main_thread_task_runner_(main_thread_task_runner) { |
448 shared_timer_func_(NULL), | |
449 shared_timer_fire_time_(0.0), | |
450 shared_timer_fire_time_was_set_while_suspended_(false), | |
451 shared_timer_suspended_(0) { | |
452 // TODO(alexclarke): Use c++11 delegated constructors when allowed. | |
453 InternalInit(); | 443 InternalInit(); |
454 } | 444 } |
455 | 445 |
456 void BlinkPlatformImpl::InternalInit() { | 446 void BlinkPlatformImpl::InternalInit() { |
457 // ChildThread may not exist in some tests. | 447 // ChildThread may not exist in some tests. |
458 if (ChildThreadImpl::current()) { | 448 if (ChildThreadImpl::current()) { |
459 geofencing_provider_.reset(new WebGeofencingProviderImpl( | 449 geofencing_provider_.reset(new WebGeofencingProviderImpl( |
460 ChildThreadImpl::current()->thread_safe_sender())); | 450 ChildThreadImpl::current()->thread_safe_sender())); |
461 bluetooth_.reset( | 451 bluetooth_.reset( |
462 new WebBluetoothImpl(ChildThreadImpl::current()->thread_safe_sender())); | 452 new WebBluetoothImpl(ChildThreadImpl::current()->thread_safe_sender())); |
463 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender(); | 453 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender(); |
464 notification_dispatcher_ = | 454 notification_dispatcher_ = |
465 ChildThreadImpl::current()->notification_dispatcher(); | 455 ChildThreadImpl::current()->notification_dispatcher(); |
466 push_dispatcher_ = ChildThreadImpl::current()->push_dispatcher(); | 456 push_dispatcher_ = ChildThreadImpl::current()->push_dispatcher(); |
467 permission_client_.reset(new PermissionDispatcher( | 457 permission_client_.reset(new PermissionDispatcher( |
468 ChildThreadImpl::current()->service_registry())); | 458 ChildThreadImpl::current()->service_registry())); |
469 sync_provider_.reset(new BackgroundSyncProvider( | 459 sync_provider_.reset(new BackgroundSyncProvider( |
470 ChildThreadImpl::current()->service_registry())); | 460 ChildThreadImpl::current()->service_registry())); |
471 } | 461 } |
472 | |
473 if (main_thread_task_runner_.get()) { | |
474 shared_timer_.SetTaskRunner(main_thread_task_runner_); | |
475 } | |
476 } | 462 } |
477 | 463 |
478 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) { | 464 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) { |
479 DCHECK(!current_thread_slot_.Get()); | 465 DCHECK(!current_thread_slot_.Get()); |
480 current_thread_slot_.Set(thread); | 466 current_thread_slot_.Set(thread); |
481 } | 467 } |
482 | 468 |
483 BlinkPlatformImpl::~BlinkPlatformImpl() { | 469 BlinkPlatformImpl::~BlinkPlatformImpl() { |
484 } | 470 } |
485 | 471 |
(...skipping 613 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1099 | 1085 |
1100 double BlinkPlatformImpl::systemTraceTime() { | 1086 double BlinkPlatformImpl::systemTraceTime() { |
1101 return (base::TraceTicks::Now() - base::TraceTicks()).InSecondsF(); | 1087 return (base::TraceTicks::Now() - base::TraceTicks()).InSecondsF(); |
1102 } | 1088 } |
1103 | 1089 |
1104 void BlinkPlatformImpl::cryptographicallyRandomValues( | 1090 void BlinkPlatformImpl::cryptographicallyRandomValues( |
1105 unsigned char* buffer, size_t length) { | 1091 unsigned char* buffer, size_t length) { |
1106 base::RandBytes(buffer, length); | 1092 base::RandBytes(buffer, length); |
1107 } | 1093 } |
1108 | 1094 |
1109 void BlinkPlatformImpl::setSharedTimerFiredFunction(void (*func)()) { | |
1110 shared_timer_func_ = func; | |
1111 } | |
1112 | |
1113 void BlinkPlatformImpl::setSharedTimerFireInterval( | |
1114 double interval_seconds) { | |
1115 shared_timer_fire_time_ = interval_seconds + monotonicallyIncreasingTime(); | |
1116 if (shared_timer_suspended_) { | |
1117 shared_timer_fire_time_was_set_while_suspended_ = true; | |
1118 return; | |
1119 } | |
1120 | |
1121 // By converting between double and int64 representation, we run the risk | |
1122 // of losing precision due to rounding errors. Performing computations in | |
1123 // microseconds reduces this risk somewhat. But there still is the potential | |
1124 // of us computing a fire time for the timer that is shorter than what we | |
1125 // need. | |
1126 // As the event loop will check event deadlines prior to actually firing | |
1127 // them, there is a risk of needlessly rescheduling events and of | |
1128 // needlessly looping if sleep times are too short even by small amounts. | |
1129 // This results in measurable performance degradation unless we use ceil() to | |
1130 // always round up the sleep times. | |
1131 int64 interval = static_cast<int64>( | |
1132 ceil(interval_seconds * base::Time::kMillisecondsPerSecond) | |
1133 * base::Time::kMicrosecondsPerMillisecond); | |
1134 | |
1135 if (interval < 0) | |
1136 interval = 0; | |
1137 | |
1138 shared_timer_.Stop(); | |
1139 shared_timer_.Start(FROM_HERE, base::TimeDelta::FromMicroseconds(interval), | |
1140 this, &BlinkPlatformImpl::DoTimeout); | |
1141 OnStartSharedTimer(base::TimeDelta::FromMicroseconds(interval)); | |
1142 } | |
1143 | |
1144 void BlinkPlatformImpl::stopSharedTimer() { | |
1145 shared_timer_.Stop(); | |
1146 } | |
1147 | |
1148 blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve( | 1095 blink::WebGestureCurve* BlinkPlatformImpl::createFlingAnimationCurve( |
1149 blink::WebGestureDevice device_source, | 1096 blink::WebGestureDevice device_source, |
1150 const blink::WebFloatPoint& velocity, | 1097 const blink::WebFloatPoint& velocity, |
1151 const blink::WebSize& cumulative_scroll) { | 1098 const blink::WebSize& cumulative_scroll) { |
1152 return ui::WebGestureCurveImpl::CreateFromDefaultPlatformCurve( | 1099 return ui::WebGestureCurveImpl::CreateFromDefaultPlatformCurve( |
1153 gfx::Vector2dF(velocity.x, velocity.y), | 1100 gfx::Vector2dF(velocity.x, velocity.y), |
1154 gfx::Vector2dF(cumulative_scroll.width, cumulative_scroll.height), | 1101 gfx::Vector2dF(cumulative_scroll.width, cumulative_scroll.height), |
1155 IsMainThread()).release(); | 1102 IsMainThread()).release(); |
1156 } | 1103 } |
1157 | 1104 |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1350 // be reserved for direct use by certain hardware. Thus, we set the limit so | 1297 // be reserved for direct use by certain hardware. Thus, we set the limit so |
1351 // that 1.6GB of reported physical memory on a 2GB device is enough to set the | 1298 // that 1.6GB of reported physical memory on a 2GB device is enough to set the |
1352 // limit at 16M pixels, which is a desirable value since 4K*4K is a relatively | 1299 // limit at 16M pixels, which is a desirable value since 4K*4K is a relatively |
1353 // common texture size. | 1300 // common texture size. |
1354 return base::SysInfo::AmountOfPhysicalMemory() / 25; | 1301 return base::SysInfo::AmountOfPhysicalMemory() / 25; |
1355 #else | 1302 #else |
1356 return noDecodedImageByteLimit; | 1303 return noDecodedImageByteLimit; |
1357 #endif | 1304 #endif |
1358 } | 1305 } |
1359 | 1306 |
1360 void BlinkPlatformImpl::SuspendSharedTimer() { | |
1361 ++shared_timer_suspended_; | |
1362 } | |
1363 | |
1364 void BlinkPlatformImpl::ResumeSharedTimer() { | |
1365 DCHECK_GT(shared_timer_suspended_, 0); | |
1366 | |
1367 // The shared timer may have fired or been adjusted while we were suspended. | |
1368 if (--shared_timer_suspended_ == 0 && | |
1369 (!shared_timer_.IsRunning() || | |
1370 shared_timer_fire_time_was_set_while_suspended_)) { | |
1371 shared_timer_fire_time_was_set_while_suspended_ = false; | |
1372 setSharedTimerFireInterval( | |
1373 shared_timer_fire_time_ - monotonicallyIncreasingTime()); | |
1374 } | |
1375 } | |
1376 | |
1377 scoped_refptr<base::SingleThreadTaskRunner> | 1307 scoped_refptr<base::SingleThreadTaskRunner> |
1378 BlinkPlatformImpl::MainTaskRunnerForCurrentThread() { | 1308 BlinkPlatformImpl::MainTaskRunnerForCurrentThread() { |
1379 if (main_thread_task_runner_.get() && | 1309 if (main_thread_task_runner_.get() && |
1380 main_thread_task_runner_->BelongsToCurrentThread()) { | 1310 main_thread_task_runner_->BelongsToCurrentThread()) { |
1381 return main_thread_task_runner_; | 1311 return main_thread_task_runner_; |
1382 } else { | 1312 } else { |
1383 return base::ThreadTaskRunnerHandle::Get(); | 1313 return base::ThreadTaskRunnerHandle::Get(); |
1384 } | 1314 } |
1385 } | 1315 } |
1386 | 1316 |
1387 bool BlinkPlatformImpl::IsMainThread() const { | 1317 bool BlinkPlatformImpl::IsMainThread() const { |
1388 return main_thread_task_runner_.get() && | 1318 return main_thread_task_runner_.get() && |
1389 main_thread_task_runner_->BelongsToCurrentThread(); | 1319 main_thread_task_runner_->BelongsToCurrentThread(); |
1390 } | 1320 } |
1391 | 1321 |
1392 WebString BlinkPlatformImpl::domCodeStringFromEnum(int dom_code) { | 1322 WebString BlinkPlatformImpl::domCodeStringFromEnum(int dom_code) { |
1393 return WebString::fromUTF8(ui::KeycodeConverter::DomCodeToCodeString( | 1323 return WebString::fromUTF8(ui::KeycodeConverter::DomCodeToCodeString( |
1394 static_cast<ui::DomCode>(dom_code))); | 1324 static_cast<ui::DomCode>(dom_code))); |
1395 } | 1325 } |
1396 | 1326 |
1397 int BlinkPlatformImpl::domEnumFromCodeString(const WebString& code) { | 1327 int BlinkPlatformImpl::domEnumFromCodeString(const WebString& code) { |
1398 return static_cast<int>(ui::KeycodeConverter::CodeStringToDomCode( | 1328 return static_cast<int>(ui::KeycodeConverter::CodeStringToDomCode( |
1399 code.utf8().data())); | 1329 code.utf8().data())); |
1400 } | 1330 } |
1401 | 1331 |
1402 } // namespace content | 1332 } // namespace content |
OLD | NEW |