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

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

Issue 2507073002: Split ArcSessionManager from ArcAuthService. (Closed)
Patch Set: Fix rebase mistake 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 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/chromeos/arc/extensions/arc_support_message_host.h" 5 #include "chrome/browser/chromeos/arc/extensions/arc_support_message_host.h"
6 6
7 #include "base/json/json_reader.h" 7 #include "base/json/json_reader.h"
8 #include "base/json/json_writer.h" 8 #include "base/json/json_writer.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/thread_task_runner_handle.h"
10 #include "chrome/browser/chromeos/arc/arc_auth_service.h" 10 #include "chrome/browser/chromeos/arc/arc_session_manager.h"
11 #include "chrome/browser/chromeos/arc/arc_support_host.h" 11 #include "chrome/browser/chromeos/arc/arc_support_host.h"
12 12
13 namespace arc { 13 namespace arc {
14 14
15 // static 15 // static
16 const char ArcSupportMessageHost::kHostName[] = "com.google.arc_support"; 16 const char ArcSupportMessageHost::kHostName[] = "com.google.arc_support";
17 17
18 // static 18 // static
19 const char* const ArcSupportMessageHost::kHostOrigin[] = { 19 const char* const ArcSupportMessageHost::kHostOrigin[] = {
20 "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"}; 20 "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/"};
21 21
22 // static 22 // static
23 std::unique_ptr<extensions::NativeMessageHost> ArcSupportMessageHost::Create() { 23 std::unique_ptr<extensions::NativeMessageHost> ArcSupportMessageHost::Create() {
24 return std::unique_ptr<NativeMessageHost>(new ArcSupportMessageHost()); 24 return std::unique_ptr<NativeMessageHost>(new ArcSupportMessageHost());
25 } 25 }
26 26
27 ArcSupportMessageHost::ArcSupportMessageHost() = default; 27 ArcSupportMessageHost::ArcSupportMessageHost() = default;
28 28
29 ArcSupportMessageHost::~ArcSupportMessageHost() { 29 ArcSupportMessageHost::~ArcSupportMessageHost() {
30 // On shutdown, ArcAuthService may be already deleted. In that case, 30 // On shutdown, ArcSessionManager may be already deleted. In that case,
31 // ArcAuthService::Get() returns nullptr. Note that, ArcSupportHost 31 // ArcSessionManager::Get() returns nullptr. Note that, ArcSupportHost
32 // disconnects to this instance on shutdown already. 32 // disconnects to this instance on shutdown already.
33 ArcAuthService* auth_service = ArcAuthService::Get(); 33 ArcSessionManager* arc_session_manager = ArcSessionManager::Get();
34 if (auth_service) { 34 if (arc_session_manager) {
35 DCHECK(auth_service->support_host()); 35 DCHECK(arc_session_manager->support_host());
36 auth_service->support_host()->UnsetMessageHost(this); 36 arc_session_manager->support_host()->UnsetMessageHost(this);
37 } 37 }
38 } 38 }
39 39
40 void ArcSupportMessageHost::SendMessage(const base::Value& message) { 40 void ArcSupportMessageHost::SendMessage(const base::Value& message) {
41 if (!client_) 41 if (!client_)
42 return; 42 return;
43 43
44 std::string message_string; 44 std::string message_string;
45 base::JSONWriter::Write(message, &message_string); 45 base::JSONWriter::Write(message, &message_string);
46 client_->PostMessageFromNativeHost(message_string); 46 client_->PostMessageFromNativeHost(message_string);
47 } 47 }
48 48
49 void ArcSupportMessageHost::SetObserver(Observer* observer) { 49 void ArcSupportMessageHost::SetObserver(Observer* observer) {
50 // We assume that the observer instance is only ArcSupportHost, which is 50 // We assume that the observer instance is only ArcSupportHost, which is
51 // currently system unique. This is also used to reset the observere, 51 // currently system unique. This is also used to reset the observere,
52 // so |observer| xor |observer_| needs to be nullptr. 52 // so |observer| xor |observer_| needs to be nullptr.
53 DCHECK(!observer != !observer_); 53 DCHECK(!observer != !observer_);
54 observer_ = observer; 54 observer_ = observer;
55 } 55 }
56 56
57 void ArcSupportMessageHost::Start(Client* client) { 57 void ArcSupportMessageHost::Start(Client* client) {
58 DCHECK(!client_); 58 DCHECK(!client_);
59 client_ = client; 59 client_ = client;
60 60
61 ArcAuthService* auth_service = ArcAuthService::Get(); 61 ArcSessionManager* arc_session_manager = ArcSessionManager::Get();
62 DCHECK(auth_service); 62 DCHECK(arc_session_manager);
63 DCHECK(auth_service->support_host()); 63 DCHECK(arc_session_manager->support_host());
64 auth_service->support_host()->SetMessageHost(this); 64 arc_session_manager->support_host()->SetMessageHost(this);
65 } 65 }
66 66
67 void ArcSupportMessageHost::OnMessage(const std::string& message_string) { 67 void ArcSupportMessageHost::OnMessage(const std::string& message_string) {
68 if (!observer_) 68 if (!observer_)
69 return; 69 return;
70 70
71 std::unique_ptr<base::Value> message_value = 71 std::unique_ptr<base::Value> message_value =
72 base::JSONReader::Read(message_string); 72 base::JSONReader::Read(message_string);
73 base::DictionaryValue* message; 73 base::DictionaryValue* message;
74 if (!message_value || !message_value->GetAsDictionary(&message)) { 74 if (!message_value || !message_value->GetAsDictionary(&message)) {
75 NOTREACHED(); 75 NOTREACHED();
76 return; 76 return;
77 } 77 }
78 78
79 observer_->OnMessage(*message); 79 observer_->OnMessage(*message);
80 } 80 }
81 81
82 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportMessageHost::task_runner() 82 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportMessageHost::task_runner()
83 const { 83 const {
84 return base::ThreadTaskRunnerHandle::Get(); 84 return base::ThreadTaskRunnerHandle::Get();
85 } 85 }
86 86
87 } // namespace arc 87 } // namespace arc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698