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

Side by Side Diff: chrome/browser/chromeos/arc/extensions/arc_support_message_host.cc

Issue 2436903003: Extract ArcSupportMessageHost. (Closed)
Patch Set: Rebase Created 4 years, 1 month 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
(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/extensions/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 // On shutdown, ArcAuthService may be already deleted. In that case,
31 // ArcAuthService::Get() returns nullptr. Note that, ArcSupportHost
32 // disconnects to this instance on shutdown already.
33 ArcAuthService* auth_service = ArcAuthService::Get();
34 if (auth_service) {
35 DCHECK(auth_service->support_host());
36 auth_service->support_host()->UnsetMessageHost(this);
37 }
38 }
39
40 void ArcSupportMessageHost::SendMessage(const base::Value& message) {
41 if (!client_)
42 return;
43
44 std::string message_string;
45 base::JSONWriter::Write(message, &message_string);
46 client_->PostMessageFromNativeHost(message_string);
47 }
48
49 void ArcSupportMessageHost::SetObserver(Observer* observer) {
50 // We assume that the observer instance is only ArcSupportHost, which is
51 // currently system unique. This is also used to reset the observere,
52 // so |observer| xor |observer_| needs to be nullptr.
53 DCHECK(!observer != !observer_);
54 observer_ = observer;
55 }
56
57 void ArcSupportMessageHost::Start(Client* client) {
58 DCHECK(!client_);
59 client_ = client;
60
61 ArcAuthService* auth_service = ArcAuthService::Get();
62 DCHECK(auth_service);
63 DCHECK(auth_service->support_host());
64 auth_service->support_host()->SetMessageHost(this);
65 }
66
67 void ArcSupportMessageHost::OnMessage(const std::string& message_string) {
68 if (!observer_)
69 return;
70
71 std::unique_ptr<base::Value> message_value =
72 base::JSONReader::Read(message_string);
73 base::DictionaryValue* message;
74 if (!message_value || !message_value->GetAsDictionary(&message)) {
75 NOTREACHED();
76 return;
77 }
78
79 observer_->OnMessage(*message);
80 }
81
82 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportMessageHost::task_runner()
83 const {
84 return base::ThreadTaskRunnerHandle::Get();
85 }
86
87 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698