OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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. | |
4 | |
5 #include "chrome/browser/chromeos/arc/ui/arc_support_message_host.h" | |
6 | |
7 #include "base/json/json_reader.h" | |
8 #include "base/json/json_writer.h" | |
9 #include "base/threading/thread_task_runner_handle.h" | |
10 #include "chrome/browser/chromeos/arc/arc_auth_service.h" | |
11 #include "chrome/browser/chromeos/arc/arc_support_host.h" | |
12 | |
13 namespace arc { | |
14 | |
15 // static | |
16 const char ArcSupportMessageHost::kHostName[] = "com.google.arc_support"; | |
17 | |
18 // static | |
19 const char* const ArcSupportMessageHost::kHostOrigin[] = { | |
20 "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"}; | |
21 | |
22 // static | |
23 std::unique_ptr<extensions::NativeMessageHost> ArcSupportMessageHost::Create() { | |
24 return std::unique_ptr<NativeMessageHost>(new ArcSupportMessageHost()); | |
25 } | |
26 | |
27 ArcSupportMessageHost::ArcSupportMessageHost() = default; | |
28 | |
29 ArcSupportMessageHost::~ArcSupportMessageHost() { | |
30 ArcAuthService* auth_service = ArcAuthService::Get(); | |
31 if (auth_service) | |
32 auth_service->support_host()->UnsetMessageHost(this); | |
33 } | |
34 | |
35 void ArcSupportMessageHost::SendMessage(const base::Value& message) { | |
36 if (!client_) | |
37 return; | |
38 | |
39 std::string message_string; | |
40 base::JSONWriter::Write(message, &message_string); | |
41 client_->PostMessageFromNativeHost(message_string); | |
42 } | |
43 | |
44 void ArcSupportMessageHost::SetObserver(Observer* observer) { | |
45 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
| |
46 } | |
47 | |
48 void ArcSupportMessageHost::Start(Client* client) { | |
49 DCHECK(!client_); | |
50 client_ = client; | |
51 | |
52 ArcAuthService* auth_service = ArcAuthService::Get(); | |
53 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
| |
54 auth_service->support_host()->SetMessageHost(this); | |
55 } | |
56 | |
57 void ArcSupportMessageHost::OnMessage(const std::string& message_string) { | |
58 if (!observer_) | |
59 return; | |
60 | |
61 std::unique_ptr<base::Value> message_value = | |
62 base::JSONReader::Read(message_string); | |
63 base::DictionaryValue* message; | |
64 if (!message_value || !message_value->GetAsDictionary(&message)) { | |
65 NOTREACHED(); | |
66 return; | |
67 } | |
68 | |
69 observer_->OnMessage(*message); | |
70 } | |
71 | |
72 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportMessageHost::task_runner() | |
73 const { | |
74 return base::ThreadTaskRunnerHandle::Get(); | |
75 } | |
76 | |
77 } // namespace arc | |
OLD | NEW |