| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. Use of this |
| 2 // source code is governed by a BSD-style license that can be found in the | 2 // source code is governed by a BSD-style license that can be found in the |
| 3 // LICENSE file. | 3 // LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/in_process_webkit/webkit_thread.h" | 5 #include "chrome/browser/in_process_webkit/webkit_thread.h" |
| 6 | 6 |
| 7 #include "base/command_line.h" |
| 7 #include "chrome/browser/in_process_webkit/browser_webkitclient_impl.h" | 8 #include "chrome/browser/in_process_webkit/browser_webkitclient_impl.h" |
| 9 #include "chrome/common/chrome_switches.h" |
| 8 #include "webkit/api/public/WebKit.h" | 10 #include "webkit/api/public/WebKit.h" |
| 9 | 11 |
| 10 base::LazyInstance<Lock> WebKitThread::global_webkit_lock_( | 12 // This happens on the UI thread before the IO thread has been shut down. |
| 11 base::LINKER_INITIALIZED); | 13 WebKitThread::WebKitThread() { |
| 12 int WebKitThread::global_webkit_ref_count_ = 0; | 14 // The thread is started lazily by InitializeThread() on the IO thread. |
| 13 WebKitThread::InternalWebKitThread* WebKitThread::global_webkit_thread_ = NULL; | 15 } |
| 14 | 16 |
| 15 WebKitThread::WebKitThread() | 17 // This happens on the UI thread after the IO thread has been shut down. |
| 16 : cached_webkit_thread_(NULL) { | 18 WebKitThread::~WebKitThread() { |
| 17 // The thread is started lazily by InitializeThread(). | 19 DCHECK(!ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
| 18 } | 20 } |
| 19 | 21 |
| 20 WebKitThread::InternalWebKitThread::InternalWebKitThread() | 22 WebKitThread::InternalWebKitThread::InternalWebKitThread() |
| 21 : base::Thread("WebKit"), | 23 : ChromeThread(ChromeThread::WEBKIT), |
| 22 webkit_client_(NULL) { | 24 webkit_client_(NULL) { |
| 23 } | 25 } |
| 24 | 26 |
| 25 void WebKitThread::InternalWebKitThread::Init() { | 27 void WebKitThread::InternalWebKitThread::Init() { |
| 26 DCHECK(!webkit_client_); | 28 DCHECK(!webkit_client_); |
| 27 webkit_client_ = new BrowserWebKitClientImpl; | 29 webkit_client_ = new BrowserWebKitClientImpl; |
| 28 DCHECK(webkit_client_); | 30 DCHECK(webkit_client_); |
| 29 WebKit::initialize(webkit_client_); | 31 WebKit::initialize(webkit_client_); |
| 30 // Don't do anything heavyweight here since this can block the IO thread from | 32 // Don't do anything heavyweight here since this can block the IO thread from |
| 31 // executing (since InitializeThread() is often called on the IO thread). | 33 // executing (since InitializeThread() is called on the IO thread). |
| 32 } | 34 } |
| 33 | 35 |
| 34 void WebKitThread::InternalWebKitThread::CleanUp() { | 36 void WebKitThread::InternalWebKitThread::CleanUp() { |
| 37 // Don't do anything heavyweight here since this can block the IO thread from |
| 38 // executing (since the thread is shutdown from the IO thread). |
| 35 DCHECK(webkit_client_); | 39 DCHECK(webkit_client_); |
| 36 WebKit::shutdown(); | 40 WebKit::shutdown(); |
| 37 delete webkit_client_; | 41 delete webkit_client_; |
| 38 webkit_client_ = NULL; | |
| 39 } | 42 } |
| 40 | 43 |
| 41 WebKitThread::~WebKitThread() { | 44 MessageLoop* WebKitThread::InitializeThread() { |
| 42 AutoLock lock(global_webkit_lock_.Get()); | 45 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) |
| 43 if (cached_webkit_thread_) { | 46 return NULL; |
| 44 DCHECK(global_webkit_ref_count_ > 0); | 47 |
| 45 if (--global_webkit_ref_count_ == 0) { | 48 DCHECK(!webkit_thread_.get()); |
| 46 // TODO(jorlow): Make this safe. | 49 webkit_thread_.reset(new InternalWebKitThread); |
| 47 DCHECK(MessageLoop::current() != global_webkit_thread_->message_loop()); | 50 bool started = webkit_thread_->Start(); |
| 48 global_webkit_thread_->Stop(); | 51 DCHECK(started); |
| 49 delete global_webkit_thread_; | 52 return webkit_thread_->message_loop(); |
| 50 global_webkit_thread_ = NULL; | |
| 51 } | |
| 52 } | |
| 53 } | 53 } |
| 54 | |
| 55 void WebKitThread::InitializeThread() { | |
| 56 AutoLock lock(global_webkit_lock_.Get()); | |
| 57 if (!cached_webkit_thread_) { | |
| 58 if (!global_webkit_thread_) { | |
| 59 global_webkit_thread_ = new InternalWebKitThread; | |
| 60 DCHECK(global_webkit_thread_); | |
| 61 bool started = global_webkit_thread_->Start(); | |
| 62 DCHECK(started); | |
| 63 } | |
| 64 ++global_webkit_ref_count_; | |
| 65 // The cached version can be accessed outside of global_webkit_lock_. | |
| 66 cached_webkit_thread_ = global_webkit_thread_; | |
| 67 } | |
| 68 DCHECK(cached_webkit_thread_->IsRunning()); | |
| 69 } | |
| OLD | NEW |