OLD | NEW |
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 "chrome/browser/extensions/api/messaging/native_message_port.h" | 5 #include "chrome/browser/extensions/api/messaging/native_message_port.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/single_thread_task_runner.h" | |
9 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" | 8 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" |
10 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
11 | 10 |
12 namespace extensions { | 11 namespace extensions { |
13 | 12 |
14 // Handles jumping between the |host_task_runner| and the | 13 NativeMessagePort::NativeMessagePort(NativeMessageProcessHost* native_process) |
15 // |message_service_task_runner|. | 14 : native_process_(native_process) { |
16 // All methods on the host interface should be called on |host_task_runner|. | |
17 // All methods on |port| (that calls into MessageServices) should be called | |
18 // on |message_service_task_runner|. | |
19 class NativeMessagePort::Core : public NativeMessageHost::Client { | |
20 public: | |
21 Core( | |
22 scoped_ptr<NativeMessageHost> host, | |
23 base::WeakPtr<NativeMessagePort> port, | |
24 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_); | |
25 virtual ~Core(); | |
26 | |
27 void OnMessageFromChrome(const std::string& message); | |
28 | |
29 // NativeMessageHost::Client implementation. | |
30 virtual void PostMessageFromNativeHost(const std::string& message) OVERRIDE; | |
31 virtual void CloseChannel(const std::string& error_message) OVERRIDE; | |
32 | |
33 private: | |
34 scoped_ptr<NativeMessageHost> host_; | |
35 base::WeakPtr<NativeMessagePort> port_; | |
36 | |
37 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_; | |
38 scoped_refptr<base::SingleThreadTaskRunner> host_task_runner_; | |
39 }; | |
40 | |
41 NativeMessagePort::Core::Core( | |
42 scoped_ptr<NativeMessageHost> host, | |
43 base::WeakPtr<NativeMessagePort> port, | |
44 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner) | |
45 : host_(host.Pass()), | |
46 port_(port), | |
47 message_service_task_runner_(message_service_task_runner), | |
48 host_task_runner_(host_->task_runner()) { | |
49 DCHECK(message_service_task_runner_->BelongsToCurrentThread()); | |
50 host_task_runner_->PostTask(FROM_HERE, | |
51 base::Bind(&NativeMessageHost::Start, | |
52 base::Unretained(host_.get()), | |
53 base::Unretained(this))); | |
54 } | |
55 | |
56 NativeMessagePort::Core::~Core() { | |
57 DCHECK(host_task_runner_->BelongsToCurrentThread()); | |
58 } | |
59 | |
60 void NativeMessagePort::Core::OnMessageFromChrome(const std::string& message) { | |
61 DCHECK(message_service_task_runner_->BelongsToCurrentThread()); | |
62 host_task_runner_->PostTask(FROM_HERE, | |
63 base::Bind(&NativeMessageHost::OnMessage, | |
64 base::Unretained(host_.get()), | |
65 message)); | |
66 } | |
67 | |
68 void NativeMessagePort::Core::PostMessageFromNativeHost( | |
69 const std::string& message) { | |
70 DCHECK(host_task_runner_->BelongsToCurrentThread()); | |
71 message_service_task_runner_->PostTask( | |
72 FROM_HERE, | |
73 base::Bind( | |
74 &NativeMessagePort::PostMessageFromNativeHost, port_, message)); | |
75 } | |
76 | |
77 void NativeMessagePort::Core::CloseChannel(const std::string& error_message) { | |
78 DCHECK(host_task_runner_->BelongsToCurrentThread()); | |
79 message_service_task_runner_->PostTask( | |
80 FROM_HERE, | |
81 base::Bind(&NativeMessagePort::CloseChannel, port_, error_message)); | |
82 } | |
83 | |
84 NativeMessagePort::NativeMessagePort( | |
85 base::WeakPtr<MessageService> message_service, | |
86 int port_id, | |
87 scoped_ptr<NativeMessageHost> native_message_host) | |
88 : weak_message_service_(message_service), | |
89 host_task_runner_(native_message_host->task_runner()), | |
90 port_id_(port_id), | |
91 weak_factory_(this) { | |
92 core_.reset(new Core(native_message_host.Pass(), | |
93 weak_factory_.GetWeakPtr(), | |
94 base::MessageLoopProxy::current())); | |
95 } | 15 } |
96 | 16 |
97 NativeMessagePort::~NativeMessagePort() { | 17 NativeMessagePort::~NativeMessagePort() { |
98 DCHECK(thread_checker_.CalledOnValidThread()); | 18 content::BrowserThread::DeleteSoon( |
99 host_task_runner_->DeleteSoon(FROM_HERE, core_.release()); | 19 content::BrowserThread::IO, FROM_HERE, native_process_); |
100 } | 20 } |
101 | 21 |
102 void NativeMessagePort::DispatchOnMessage( | 22 void NativeMessagePort::DispatchOnMessage( |
103 const Message& message, | 23 const Message& message, |
104 int target_port_id) { | 24 int target_port_id) { |
105 DCHECK(thread_checker_.CalledOnValidThread()); | 25 content::BrowserThread::PostTask( |
106 core_->OnMessageFromChrome(message.data); | 26 content::BrowserThread::IO, FROM_HERE, |
107 } | 27 base::Bind(&NativeMessageProcessHost::Send, |
108 | 28 base::Unretained(native_process_), message.data)); |
109 void NativeMessagePort::PostMessageFromNativeHost(const std::string& message) { | |
110 DCHECK(thread_checker_.CalledOnValidThread()); | |
111 if (weak_message_service_) { | |
112 weak_message_service_->PostMessage( | |
113 port_id_, Message(message, false /* user_gesture */)); | |
114 } | |
115 } | |
116 | |
117 void NativeMessagePort::CloseChannel(const std::string& error_message) { | |
118 DCHECK(thread_checker_.CalledOnValidThread()); | |
119 if (weak_message_service_) { | |
120 weak_message_service_->CloseChannel(port_id_, error_message); | |
121 } | |
122 } | 29 } |
123 | 30 |
124 } // namespace extensions | 31 } // namespace extensions |
OLD | NEW |