Chromium Code Reviews| Index: chrome/browser/chromeos/arc/ui/arc_support_message_host.cc |
| diff --git a/chrome/browser/chromeos/arc/ui/arc_support_message_host.cc b/chrome/browser/chromeos/arc/ui/arc_support_message_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8c09a242116cd0001615a56e6dae4e8d4f3c6b26 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/ui/arc_support_message_host.cc |
| @@ -0,0 +1,77 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/arc/ui/arc_support_message_host.h" |
| + |
| +#include "base/json/json_reader.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/threading/thread_task_runner_handle.h" |
| +#include "chrome/browser/chromeos/arc/arc_auth_service.h" |
| +#include "chrome/browser/chromeos/arc/arc_support_host.h" |
| + |
| +namespace arc { |
| + |
| +// static |
| +const char ArcSupportMessageHost::kHostName[] = "com.google.arc_support"; |
| + |
| +// static |
| +const char* const ArcSupportMessageHost::kHostOrigin[] = { |
| + "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"}; |
| + |
| +// static |
| +std::unique_ptr<extensions::NativeMessageHost> ArcSupportMessageHost::Create() { |
| + return std::unique_ptr<NativeMessageHost>(new ArcSupportMessageHost()); |
| +} |
| + |
| +ArcSupportMessageHost::ArcSupportMessageHost() = default; |
| + |
| +ArcSupportMessageHost::~ArcSupportMessageHost() { |
| + ArcAuthService* auth_service = ArcAuthService::Get(); |
| + if (auth_service) |
| + auth_service->support_host()->UnsetMessageHost(this); |
| +} |
| + |
| +void ArcSupportMessageHost::SendMessage(const base::Value& message) { |
| + if (!client_) |
| + return; |
| + |
| + std::string message_string; |
| + base::JSONWriter::Write(message, &message_string); |
| + client_->PostMessageFromNativeHost(message_string); |
| +} |
| + |
| +void ArcSupportMessageHost::SetObserver(Observer* observer) { |
| + observer_ = observer; |
|
khmel
2016/10/21 03:34:49
nit: DCHECK(!observer_), meaning we cannot overrid
hidehiko
2016/10/21 07:40:27
This also can be used to detach the observer. Adde
|
| +} |
| + |
| +void ArcSupportMessageHost::Start(Client* client) { |
| + DCHECK(!client_); |
| + client_ = client; |
| + |
| + ArcAuthService* auth_service = ArcAuthService::Get(); |
| + if (auth_service) |
|
Luis Héctor Chávez
2016/10/21 02:24:08
This looks racy. If |auth_service| is null, then A
khmel
2016/10/21 03:34:49
It seems ArcAuthService::Get() cannot be nullptr i
hidehiko
2016/10/21 07:40:27
Indeed, here DCHECK is appropriate, but in dtor, i
|
| + auth_service->support_host()->SetMessageHost(this); |
| +} |
| + |
| +void ArcSupportMessageHost::OnMessage(const std::string& message_string) { |
| + if (!observer_) |
| + return; |
| + |
| + std::unique_ptr<base::Value> message_value = |
| + base::JSONReader::Read(message_string); |
| + base::DictionaryValue* message; |
| + if (!message_value || !message_value->GetAsDictionary(&message)) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + observer_->OnMessage(*message); |
| +} |
| + |
| +scoped_refptr<base::SingleThreadTaskRunner> ArcSupportMessageHost::task_runner() |
| + const { |
| + return base::ThreadTaskRunnerHandle::Get(); |
| +} |
| + |
| +} // namespace arc |