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

Side by Side Diff: chrome/browser/extensions/api/messaging/native_message_port.cc

Issue 591463003: Remote Assistance on Chrome OS Part III - NativeMessageHost (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@native_messaging
Patch Set: Fix NativeMessagingBasic test on Release builds Created 6 years, 2 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 (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"
8 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h" 9 #include "chrome/browser/extensions/api/messaging/native_message_process_host.h"
9 #include "content/public/browser/browser_thread.h" 10 #include "content/public/browser/browser_thread.h"
10 11
11 namespace extensions { 12 namespace extensions {
12 13
13 NativeMessagePort::NativeMessagePort(NativeMessageProcessHost* native_process) 14 // Handles jumping between the |host_task_runner| and the
14 : native_process_(native_process) { 15 // |message_service_task_runner|.
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()));
15 } 95 }
16 96
17 NativeMessagePort::~NativeMessagePort() { 97 NativeMessagePort::~NativeMessagePort() {
18 content::BrowserThread::DeleteSoon( 98 DCHECK(thread_checker_.CalledOnValidThread());
19 content::BrowserThread::IO, FROM_HERE, native_process_); 99 host_task_runner_->DeleteSoon(FROM_HERE, core_.release());
20 } 100 }
21 101
22 void NativeMessagePort::DispatchOnMessage( 102 void NativeMessagePort::DispatchOnMessage(
23 const Message& message, 103 const Message& message,
24 int target_port_id) { 104 int target_port_id) {
25 content::BrowserThread::PostTask( 105 DCHECK(thread_checker_.CalledOnValidThread());
26 content::BrowserThread::IO, FROM_HERE, 106 core_->OnMessageFromChrome(message.data);
27 base::Bind(&NativeMessageProcessHost::Send, 107 }
28 base::Unretained(native_process_), message.data)); 108
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 }
29 } 122 }
30 123
31 } // namespace extensions 124 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698