Chromium Code Reviews| 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" | |
| 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 typedef NativeMessageHost::Client Client; |
|
Sergey Ulanov
2014/10/01 23:23:59
Don't need this typedef: Client is used in only on
kelvinp
2014/10/02 03:12:16
Done.
| |
| 14 : native_process_(native_process) { | 15 |
| 16 // Handles jumping between the |host_task_runner| and the | |
| 17 // |message_service_task_runner|. | |
| 18 // All methods on the host interface should be called on |host_task_runner|. | |
| 19 // All methods on |weak_port| (that calls into MessageServices) should be called | |
| 20 // on |message_service_task_runner|. | |
| 21 class NativeMessagePort::Core : public Client { | |
| 22 public: | |
| 23 Core( | |
| 24 scoped_ptr<NativeMessageHost> host, | |
| 25 base::WeakPtr<NativeMessagePort> weak_port, | |
|
Sergey Ulanov
2014/10/01 23:23:59
don't need 'weak_' prefix.
kelvinp
2014/10/02 03:12:17
Done.
| |
| 26 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_); | |
| 27 virtual ~Core(); | |
| 28 | |
| 29 void OnMessageFromChrome(const std::string& message); | |
| 30 | |
| 31 // NativeMessageHost::Client implementation. | |
| 32 virtual void PostMessageFromNativeHost(const std::string& message) OVERRIDE; | |
| 33 virtual void CloseChannel(const std::string& error_message) OVERRIDE; | |
| 34 | |
| 35 private: | |
| 36 scoped_ptr<NativeMessageHost> host_; | |
| 37 base::WeakPtr<NativeMessagePort> weak_port_; | |
|
Sergey Ulanov
2014/10/01 23:23:58
port_
kelvinp
2014/10/02 03:12:17
Done.
| |
| 38 | |
| 39 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner_; | |
| 40 scoped_refptr<base::SingleThreadTaskRunner> host_task_runner_; | |
| 41 }; | |
| 42 | |
| 43 NativeMessagePort::Core::Core( | |
| 44 scoped_ptr<NativeMessageHost> host, | |
| 45 base::WeakPtr<NativeMessagePort> weak_port, | |
| 46 scoped_refptr<base::SingleThreadTaskRunner> message_service_task_runner) | |
| 47 : host_(host.Pass()), | |
| 48 weak_port_(weak_port), | |
| 49 message_service_task_runner_(message_service_task_runner), | |
| 50 host_task_runner_(host_->task_runner()) { | |
| 51 DCHECK(message_service_task_runner_->BelongsToCurrentThread()); | |
| 52 host_task_runner_->PostTask(FROM_HERE, | |
| 53 base::Bind(&NativeMessageHost::set_client, | |
| 54 base::Unretained(host_.get()), | |
| 55 base::Unretained(this))); | |
| 56 } | |
| 57 | |
| 58 NativeMessagePort::Core::~Core() { | |
|
Sergey Ulanov
2014/10/01 23:23:59
This object must always be destroyed on |host_task
kelvinp
2014/10/02 03:12:16
Done.
| |
| 59 if (!host_task_runner_->BelongsToCurrentThread()) { | |
| 60 host_task_runner_->DeleteSoon(FROM_HERE, host_.release()); | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 void NativeMessagePort::Core::OnMessageFromChrome(const std::string& message) { | |
| 65 DCHECK(message_service_task_runner_->BelongsToCurrentThread()); | |
| 66 host_task_runner_->PostTask(FROM_HERE, | |
| 67 base::Bind(&NativeMessageHost::OnMessage, | |
| 68 base::Unretained(host_.get()), | |
| 69 message)); | |
| 70 } | |
| 71 | |
| 72 void NativeMessagePort::Core::PostMessageFromNativeHost( | |
| 73 const std::string& message) { | |
| 74 DCHECK(host_task_runner_->BelongsToCurrentThread()); | |
| 75 message_service_task_runner_->PostTask( | |
| 76 FROM_HERE, | |
| 77 base::Bind( | |
| 78 &NativeMessagePort::PostMessageFromNativeHost, weak_port_, message)); | |
| 79 } | |
| 80 | |
| 81 void NativeMessagePort::Core::CloseChannel(const std::string& error_message) { | |
| 82 DCHECK(host_task_runner_->BelongsToCurrentThread()); | |
| 83 message_service_task_runner_->PostTask( | |
| 84 FROM_HERE, | |
| 85 base::Bind(&NativeMessagePort::CloseChannel, weak_port_, error_message)); | |
| 86 } | |
| 87 | |
| 88 NativeMessagePort::NativeMessagePort( | |
| 89 base::WeakPtr<MessageService> message_service, | |
| 90 int port_id, | |
| 91 scoped_ptr<NativeMessageHost> native_message_host) | |
| 92 : weak_message_service_(message_service), | |
| 93 port_id_(port_id), | |
| 94 weak_factory_(this) { | |
| 95 core_.reset(new Core(native_message_host.Pass(), | |
| 96 weak_factory_.GetWeakPtr(), | |
| 97 base::MessageLoopProxy::current())); | |
| 15 } | 98 } |
| 16 | 99 |
| 17 NativeMessagePort::~NativeMessagePort() { | 100 NativeMessagePort::~NativeMessagePort() { |
| 18 content::BrowserThread::DeleteSoon( | |
| 19 content::BrowserThread::IO, FROM_HERE, native_process_); | |
| 20 } | 101 } |
| 21 | 102 |
| 22 void NativeMessagePort::DispatchOnMessage( | 103 void NativeMessagePort::DispatchOnMessage( |
| 23 const Message& message, | 104 const Message& message, |
| 24 int target_port_id) { | 105 int target_port_id) { |
| 25 content::BrowserThread::PostTask( | 106 DCHECK(thread_checker_.CalledOnValidThread()); |
| 26 content::BrowserThread::IO, FROM_HERE, | 107 core_->OnMessageFromChrome(message.data); |
| 27 base::Bind(&NativeMessageProcessHost::Send, | 108 } |
| 28 base::Unretained(native_process_), message.data)); | 109 |
| 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 */)); | |
| 115 } | |
| 116 } | |
| 117 | |
| 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); | |
| 122 } | |
| 29 } | 123 } |
| 30 | 124 |
| 31 } // namespace extensions | 125 } // namespace extensions |
| OLD | NEW |