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/dom_storage_dispatcher_host.h" | 5 #include "chrome/browser/in_process_webkit/dom_storage_dispatcher_host.h" |
6 | 6 |
7 #include "chrome/browser/chrome_thread.h" | 7 #include "chrome/browser/chrome_thread.h" |
8 #include "chrome/browser/in_process_webkit/webkit_context.h" | 8 #include "chrome/browser/in_process_webkit/webkit_context.h" |
9 #include "chrome/browser/in_process_webkit/webkit_thread.h" | 9 #include "chrome/browser/in_process_webkit/webkit_thread.h" |
10 | 10 |
11 DOMStorageDispatcherHost::DOMStorageDispatcherHost( | 11 DOMStorageDispatcherHost::DOMStorageDispatcherHost( |
12 IPC::Message::Sender* message_sender, | 12 IPC::Message::Sender* message_sender, |
13 WebKitContext* webkit_context, | 13 WebKitContext* webkit_context, |
14 WebKitThread* webkit_thread) | 14 WebKitThread* webkit_thread) |
15 : webkit_context_(webkit_context), | 15 : webkit_context_(webkit_context), |
16 webkit_thread_(webkit_thread), | 16 webkit_thread_(webkit_thread), |
17 message_sender_(message_sender) { | 17 message_sender_(message_sender) { |
18 DCHECK(webkit_context_.get()); | 18 DCHECK(webkit_context_.get()); |
19 DCHECK(webkit_thread_.get()); | 19 DCHECK(webkit_thread_); |
20 DCHECK(message_sender_); | 20 DCHECK(message_sender_); |
21 } | 21 } |
22 | 22 |
23 DOMStorageDispatcherHost::~DOMStorageDispatcherHost() { | 23 DOMStorageDispatcherHost::~DOMStorageDispatcherHost() { |
24 DCHECK(!message_sender_); | 24 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::IO)); |
25 } | |
26 | |
27 void DOMStorageDispatcherHost::Shutdown() { | |
28 DCHECK(IsOnIOThread()); | |
29 AutoLock lock(message_sender_lock_); | |
30 message_sender_ = NULL; | 25 message_sender_ = NULL; |
31 } | 26 } |
32 | 27 |
33 bool DOMStorageDispatcherHost::OnMessageReceived(const IPC::Message& msg) { | 28 bool DOMStorageDispatcherHost::OnMessageReceived(const IPC::Message& msg) { |
34 // TODO(jorlow): Implement DOM Storage's message handler...and the rest | 29 // TODO(jorlow): Implement DOM Storage's message handler...and the rest |
35 // of DOM Storage. :-) | 30 // of DOM Storage. :-) |
36 return false; | 31 return false; |
37 } | 32 } |
38 | 33 |
39 void DOMStorageDispatcherHost::Send(IPC::Message* message) { | 34 void DOMStorageDispatcherHost::Send(IPC::Message* message) { |
40 if (IsOnIOThread()) { | |
41 if (message_sender_) | |
42 message_sender_->Send(message); | |
43 else | |
44 delete message; | |
45 } | |
46 | |
47 // If message_sender_ is NULL, the IO thread has either gone away | |
48 // or will do so soon. By holding this lock until we finish posting to the | |
49 // thread, we block the IO thread from completely shutting down benieth us. | |
50 AutoLock lock(message_sender_lock_); | |
51 if (!message_sender_) { | 35 if (!message_sender_) { |
52 delete message; | 36 delete message; |
53 return; | 37 return; |
54 } | 38 } |
55 | 39 |
| 40 if (ChromeThread::CurrentlyOn(ChromeThread::IO)) { |
| 41 message_sender_->Send(message); |
| 42 return; |
| 43 } |
| 44 |
| 45 // The IO thread can't dissapear while the WebKit thread is still running. |
| 46 DCHECK(ChromeThread::CurrentlyOn(ChromeThread::WEBKIT)); |
56 MessageLoop* io_loop = ChromeThread::GetMessageLoop(ChromeThread::IO); | 47 MessageLoop* io_loop = ChromeThread::GetMessageLoop(ChromeThread::IO); |
57 CancelableTask* task = NewRunnableMethod(this, | 48 CancelableTask* task = NewRunnableMethod(this, |
58 &DOMStorageDispatcherHost::Send, | 49 &DOMStorageDispatcherHost::Send, |
59 message); | 50 message); |
60 io_loop->PostTask(FROM_HERE, task); | 51 io_loop->PostTask(FROM_HERE, task); |
61 } | 52 } |
62 | |
63 bool DOMStorageDispatcherHost::IsOnIOThread() const { | |
64 MessageLoop* io_loop = ChromeThread::GetMessageLoop(ChromeThread::IO); | |
65 return MessageLoop::current() == io_loop; | |
66 } | |
67 | |
68 bool DOMStorageDispatcherHost::IsOnWebKitThread() const { | |
69 return MessageLoop::current() == webkit_thread_->GetMessageLoop(); | |
70 } | |
OLD | NEW |