Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / messaging / native_message_port.cc
blob8d26d306b144f2f5716cc62c38121c83cb696394
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
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/api/messaging/native_message_port.h"
7 #include "base/bind.h"
8 #include "base/single_thread_task_runner.h"
9 #include "base/thread_task_runner_handle.h"
10 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
11 #include "content/public/browser/browser_thread.h"
13 namespace extensions {
15 // Handles jumping between the |host_task_runner| and the
16 // |message_service_task_runner|.
17 // All methods on the host interface should be called on |host_task_runner|.
18 // All methods on |port| (that calls into MessageServices) should be called
19 // on |message_service_task_runner|.
20 class NativeMessagePort::Core : public NativeMessageHost::Client {
21 public:
22 Core(
23 scoped_ptr<NativeMessageHost> host,
24 base::WeakPtr<NativeMessagePort> port,
25 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_);
26 ~Core() override;
28 void OnMessageFromChrome(const std::string& message);
30 // NativeMessageHost::Client implementation.
31 void PostMessageFromNativeHost(const std::string& message) override;
32 void CloseChannel(const std::string& error_message) override;
34 private:
35 scoped_ptr<NativeMessageHost> host_;
36 base::WeakPtr<NativeMessagePort> port_;
38 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_;
39 scoped_refptr<base::SingleThreadTaskRunner> host_task_runner_;
42 NativeMessagePort::Core::Core(
43 scoped_ptr<NativeMessageHost> host,
44 base::WeakPtr<NativeMessagePort> port,
45 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner)
46 : host_(host.Pass()),
47 port_(port),
48 message_service_task_runner_(message_service_task_runner),
49 host_task_runner_(host_->task_runner()) {
50 DCHECK(message_service_task_runner_->BelongsToCurrentThread());
51 host_task_runner_->PostTask(FROM_HERE,
52 base::Bind(&NativeMessageHost::Start,
53 base::Unretained(host_.get()),
54 base::Unretained(this)));
57 NativeMessagePort::Core::~Core() {
58 DCHECK(host_task_runner_->BelongsToCurrentThread());
61 void NativeMessagePort::Core::OnMessageFromChrome(const std::string& message) {
62 DCHECK(message_service_task_runner_->BelongsToCurrentThread());
63 host_task_runner_->PostTask(FROM_HERE,
64 base::Bind(&NativeMessageHost::OnMessage,
65 base::Unretained(host_.get()),
66 message));
69 void NativeMessagePort::Core::PostMessageFromNativeHost(
70 const std::string& message) {
71 DCHECK(host_task_runner_->BelongsToCurrentThread());
72 message_service_task_runner_->PostTask(
73 FROM_HERE,
74 base::Bind(
75 &NativeMessagePort::PostMessageFromNativeHost, port_, message));
78 void NativeMessagePort::Core::CloseChannel(const std::string& error_message) {
79 DCHECK(host_task_runner_->BelongsToCurrentThread());
80 message_service_task_runner_->PostTask(
81 FROM_HERE,
82 base::Bind(&NativeMessagePort::CloseChannel, port_, error_message));
85 NativeMessagePort::NativeMessagePort(
86 base::WeakPtr<MessageService> message_service,
87 int port_id,
88 scoped_ptr<NativeMessageHost> native_message_host)
89 : weak_message_service_(message_service),
90 host_task_runner_(native_message_host->task_runner()),
91 port_id_(port_id),
92 weak_factory_(this) {
93 core_.reset(new Core(native_message_host.Pass(),
94 weak_factory_.GetWeakPtr(),
95 base::ThreadTaskRunnerHandle::Get()));
98 NativeMessagePort::~NativeMessagePort() {
99 DCHECK(thread_checker_.CalledOnValidThread());
100 host_task_runner_->DeleteSoon(FROM_HERE, core_.release());
103 void NativeMessagePort::DispatchOnMessage(
104 const Message& message,
105 int target_port_id) {
106 DCHECK(thread_checker_.CalledOnValidThread());
107 core_->OnMessageFromChrome(message.data);
110 void NativeMessagePort::PostMessageFromNativeHost(const std::string& message) {
111 DCHECK(thread_checker_.CalledOnValidThread());
112 if (weak_message_service_) {
113 weak_message_service_->PostMessage(
114 port_id_, Message(message, false /* user_gesture */));
118 void NativeMessagePort::CloseChannel(const std::string& error_message) {
119 DCHECK(thread_checker_.CalledOnValidThread());
120 if (weak_message_service_) {
121 weak_message_service_->CloseChannel(port_id_, error_message);
125 } // namespace extensions