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

Side by Side Diff: content/child/blink_platform_impl.cc

Issue 959803003: web-threads: Create a single instance of WebThread for each blink thread. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 5 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 unified diff | Download patch
OLDNEW
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
11 #include "base/allocator/allocator_extension.h" 11 #include "base/allocator/allocator_extension.h"
12 #include "base/bind.h" 12 #include "base/bind.h"
13 #include "base/files/file_path.h" 13 #include "base/files/file_path.h"
14 #include "base/memory/scoped_ptr.h" 14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/singleton.h" 15 #include "base/memory/singleton.h"
16 #include "base/message_loop/message_loop.h"
17 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
18 #include "base/metrics/sparse_histogram.h" 17 #include "base/metrics/sparse_histogram.h"
19 #include "base/process/process_metrics.h" 18 #include "base/process/process_metrics.h"
20 #include "base/rand_util.h" 19 #include "base/rand_util.h"
21 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
22 #include "base/strings/string_util.h" 21 #include "base/strings/string_util.h"
23 #include "base/strings/utf_string_conversions.h" 22 #include "base/strings/utf_string_conversions.h"
24 #include "base/synchronization/lock.h" 23 #include "base/synchronization/lock.h"
25 #include "base/synchronization/waitable_event.h" 24 #include "base/synchronization/waitable_event.h"
26 #include "base/sys_info.h" 25 #include "base/sys_info.h"
(...skipping 384 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 } 410 }
412 return -1; 411 return -1;
413 } 412 }
414 413
415 BlinkPlatformImpl::BlinkPlatformImpl() 414 BlinkPlatformImpl::BlinkPlatformImpl()
416 : main_thread_task_runner_(base::MessageLoopProxy::current()), 415 : main_thread_task_runner_(base::MessageLoopProxy::current()),
417 shared_timer_func_(NULL), 416 shared_timer_func_(NULL),
418 shared_timer_fire_time_(0.0), 417 shared_timer_fire_time_(0.0),
419 shared_timer_fire_time_was_set_while_suspended_(false), 418 shared_timer_fire_time_was_set_while_suspended_(false),
420 shared_timer_suspended_(0), 419 shared_timer_suspended_(0),
421 current_thread_slot_(&DestroyCurrentThread) { 420 current_thread_slot_() {
422 InternalInit(); 421 InternalInit();
423 } 422 }
424 423
425 BlinkPlatformImpl::BlinkPlatformImpl( 424 BlinkPlatformImpl::BlinkPlatformImpl(
426 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner) 425 scoped_refptr<base::SingleThreadTaskRunner> main_thread_task_runner)
427 : main_thread_task_runner_(main_thread_task_runner), 426 : main_thread_task_runner_(main_thread_task_runner),
428 shared_timer_func_(NULL), 427 shared_timer_func_(NULL),
429 shared_timer_fire_time_(0.0), 428 shared_timer_fire_time_(0.0),
430 shared_timer_fire_time_was_set_while_suspended_(false), 429 shared_timer_fire_time_was_set_while_suspended_(false),
431 shared_timer_suspended_(0), 430 shared_timer_suspended_(0),
432 current_thread_slot_(&DestroyCurrentThread) { 431 current_thread_slot_() {
433 // TODO(alexclarke): Use c++11 delegated constructors when allowed. 432 // TODO(alexclarke): Use c++11 delegated constructors when allowed.
434 InternalInit(); 433 InternalInit();
435 } 434 }
436 435
437 void BlinkPlatformImpl::InternalInit() { 436 void BlinkPlatformImpl::InternalInit() {
438 // ChildThread may not exist in some tests. 437 // ChildThread may not exist in some tests.
439 if (ChildThreadImpl::current()) { 438 if (ChildThreadImpl::current()) {
440 geofencing_provider_.reset(new WebGeofencingProviderImpl( 439 geofencing_provider_.reset(new WebGeofencingProviderImpl(
441 ChildThreadImpl::current()->thread_safe_sender())); 440 ChildThreadImpl::current()->thread_safe_sender()));
442 bluetooth_.reset( 441 bluetooth_.reset(
443 new WebBluetoothImpl(ChildThreadImpl::current()->thread_safe_sender())); 442 new WebBluetoothImpl(ChildThreadImpl::current()->thread_safe_sender()));
444 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender(); 443 thread_safe_sender_ = ChildThreadImpl::current()->thread_safe_sender();
445 notification_dispatcher_ = 444 notification_dispatcher_ =
446 ChildThreadImpl::current()->notification_dispatcher(); 445 ChildThreadImpl::current()->notification_dispatcher();
447 push_dispatcher_ = ChildThreadImpl::current()->push_dispatcher(); 446 push_dispatcher_ = ChildThreadImpl::current()->push_dispatcher();
448 } 447 }
449 448
450 if (main_thread_task_runner_.get()) { 449 if (main_thread_task_runner_.get()) {
451 shared_timer_.SetTaskRunner(main_thread_task_runner_); 450 shared_timer_.SetTaskRunner(main_thread_task_runner_);
452 } 451 }
453 } 452 }
454 453
454 void BlinkPlatformImpl::UpdateWebThreadTLS(blink::WebThread* thread) {
455 DCHECK(!current_thread_slot_.Get());
456 current_thread_slot_.Set(thread);
457 }
458
455 BlinkPlatformImpl::~BlinkPlatformImpl() { 459 BlinkPlatformImpl::~BlinkPlatformImpl() {
456 } 460 }
457 461
458 WebURLLoader* BlinkPlatformImpl::createURLLoader() { 462 WebURLLoader* BlinkPlatformImpl::createURLLoader() {
459 ChildThreadImpl* child_thread = ChildThreadImpl::current(); 463 ChildThreadImpl* child_thread = ChildThreadImpl::current();
460 // There may be no child thread in RenderViewTests. These tests can still use 464 // There may be no child thread in RenderViewTests. These tests can still use
461 // data URLs to bypass the ResourceDispatcher. 465 // data URLs to bypass the ResourceDispatcher.
462 return new WebURLLoaderImpl( 466 return new WebURLLoaderImpl(
463 child_thread ? child_thread->resource_dispatcher() : NULL, 467 child_thread ? child_thread->resource_dispatcher() : NULL,
464 MainTaskRunnerForCurrentThread()); 468 MainTaskRunnerForCurrentThread());
(...skipping 27 matching lines...) Expand all
492 496
493 bool BlinkPlatformImpl::isReservedIPAddress( 497 bool BlinkPlatformImpl::isReservedIPAddress(
494 const blink::WebString& host) const { 498 const blink::WebString& host) const {
495 net::IPAddressNumber address; 499 net::IPAddressNumber address;
496 if (!net::ParseURLHostnameToNumber(host.utf8(), &address)) 500 if (!net::ParseURLHostnameToNumber(host.utf8(), &address))
497 return false; 501 return false;
498 return net::IsIPAddressReserved(address); 502 return net::IsIPAddressReserved(address);
499 } 503 }
500 504
501 blink::WebThread* BlinkPlatformImpl::createThread(const char* name) { 505 blink::WebThread* BlinkPlatformImpl::createThread(const char* name) {
502 return new WebThreadImpl(name); 506 WebThreadImpl* thread = new WebThreadImpl(name);
507 thread->TaskRunner()->PostTask(
508 FROM_HERE, base::Bind(&BlinkPlatformImpl::UpdateWebThreadTLS,
509 base::Unretained(this), thread));
510 return thread;
503 } 511 }
504 512
505 blink::WebThread* BlinkPlatformImpl::currentThread() { 513 blink::WebThread* BlinkPlatformImpl::currentThread() {
506 WebThreadImplForMessageLoop* thread = 514 return static_cast<blink::WebThread*>(current_thread_slot_.Get());
kinuko 2015/02/27 09:54:27 Hmm. By looking into the callsites in production c
sadrul 2015/03/01 10:21:38 it looks like it's used in some places to add task
kinuko 2015/03/02 06:08:17 They belong to main thread afaik. (I'm ok to keep
sadrul 2015/03/02 07:43:14 If this is only called on the main thread, then in
kinuko 2015/03/03 00:14:14 This is not really true, it's also called on the w
507 static_cast<WebThreadImplForMessageLoop*>(current_thread_slot_.Get());
508 if (thread)
509 return (thread);
510
511 scoped_refptr<base::SingleThreadTaskRunner> task_runner =
512 MainTaskRunnerForCurrentThread();
513 if (!task_runner.get())
514 return NULL;
515
516 thread = new WebThreadImplForMessageLoop(task_runner);
517 current_thread_slot_.Set(thread);
518 return thread;
519 } 515 }
520 516
521 void BlinkPlatformImpl::yieldCurrentThread() { 517 void BlinkPlatformImpl::yieldCurrentThread() {
522 base::PlatformThread::YieldCurrentThread(); 518 base::PlatformThread::YieldCurrentThread();
523 } 519 }
524 520
525 blink::WebWaitableEvent* BlinkPlatformImpl::createWaitableEvent() { 521 blink::WebWaitableEvent* BlinkPlatformImpl::createWaitableEvent() {
526 return new WebWaitableEventImpl(); 522 return new WebWaitableEventImpl();
527 } 523 }
528 524
(...skipping 722 matching lines...) Expand 10 before | Expand all | Expand 10 after
1251 scoped_refptr<base::SingleThreadTaskRunner> 1247 scoped_refptr<base::SingleThreadTaskRunner>
1252 BlinkPlatformImpl::MainTaskRunnerForCurrentThread() { 1248 BlinkPlatformImpl::MainTaskRunnerForCurrentThread() {
1253 if (main_thread_task_runner_.get() && 1249 if (main_thread_task_runner_.get() &&
1254 main_thread_task_runner_->BelongsToCurrentThread()) { 1250 main_thread_task_runner_->BelongsToCurrentThread()) {
1255 return main_thread_task_runner_; 1251 return main_thread_task_runner_;
1256 } else { 1252 } else {
1257 return base::MessageLoopProxy::current(); 1253 return base::MessageLoopProxy::current();
1258 } 1254 }
1259 } 1255 }
1260 1256
1261 // static
1262 void BlinkPlatformImpl::DestroyCurrentThread(void* thread) {
1263 WebThreadImplForMessageLoop* impl =
1264 static_cast<WebThreadImplForMessageLoop*>(thread);
1265 delete impl;
1266 }
1267
1268 WebString BlinkPlatformImpl::domCodeStringFromEnum(int dom_code) { 1257 WebString BlinkPlatformImpl::domCodeStringFromEnum(int dom_code) {
1269 return WebString::fromUTF8(ui::KeycodeConverter::DomCodeToCodeString( 1258 return WebString::fromUTF8(ui::KeycodeConverter::DomCodeToCodeString(
1270 static_cast<ui::DomCode>(dom_code))); 1259 static_cast<ui::DomCode>(dom_code)));
1271 } 1260 }
1272 1261
1273 int BlinkPlatformImpl::domEnumFromCodeString(const WebString& code) { 1262 int BlinkPlatformImpl::domEnumFromCodeString(const WebString& code) {
1274 return static_cast<int>(ui::KeycodeConverter::CodeStringToDomCode( 1263 return static_cast<int>(ui::KeycodeConverter::CodeStringToDomCode(
1275 code.utf8().data())); 1264 code.utf8().data()));
1276 } 1265 }
1277 1266
1278 } // namespace content 1267 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698