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/extensions/api/messaging/arc_support_host.h" | |
6 | |
7 #include "ash/system/chromeos/devicetype_utils.h" | |
8 #include "base/json/json_reader.h" | |
9 #include "base/json/json_writer.h" | |
10 #include "base/thread_task_runner_handle.h" | |
11 #include "base/values.h" | |
12 #include "chrome/browser/browser_process.h" | |
13 #include "chrome/grit/generated_resources.h" | |
14 #include "ui/base/l10n/l10n_util.h" | |
15 #include "ui/base/webui/web_ui_util.h" | |
16 | |
17 namespace { | |
18 const char kAction[] = "action"; | |
19 const char kStatus[] = "status"; | |
20 const char kData[] = "data"; | |
21 const char kPage[] = "page"; | |
22 const char kActionSetLocalization[] = "setLocalization"; | |
23 const char kActionCheckAuthCode[] = "checkAuthCode"; | |
24 const char kActionCancelAuthCode[] = "cancelAuthCode"; | |
25 const char kActionCloseUI[] = "closeUI"; | |
26 const char kActionShowPage[] = "showPage"; | |
27 } // namespace | |
28 | |
29 // static | |
30 const char ArcSupportHost::kHostName[] = "com.google.arc_support"; | |
31 | |
32 // static | |
33 const char* const ArcSupportHost::kHostOrigin[] = { | |
34 "chrome-extension://cnbgggchhmkkdmeppjobngjoejnihlei/" | |
35 }; | |
36 | |
37 // static | |
38 scoped_ptr<extensions::NativeMessageHost> ArcSupportHost::Create() { | |
39 return scoped_ptr<NativeMessageHost>(new ArcSupportHost()); | |
40 } | |
41 | |
42 ArcSupportHost::ArcSupportHost() { | |
43 arc::ArcAuthService::Get()->AddObserver(this); | |
44 } | |
45 | |
46 ArcSupportHost::~ArcSupportHost() { | |
47 arc::ArcAuthService::Get()->RemoveObserver(this); | |
48 } | |
49 | |
50 void ArcSupportHost::Start(Client* client) { | |
51 DCHECK(!client_); | |
52 client_ = client; | |
53 | |
54 SendLocalization(); | |
55 | |
56 arc::ArcAuthService* arc_auth_service = arc::ArcAuthService::Get(); | |
57 OnOptInUIShowPage(arc_auth_service->ui_page(), | |
58 arc_auth_service->ui_page_status()); | |
59 } | |
60 | |
61 void ArcSupportHost::SendLocalization() { | |
62 DCHECK(client_); | |
63 scoped_ptr<base::DictionaryValue> localized_strings( | |
64 new base::DictionaryValue()); | |
65 base::string16 device_name = ash::GetChromeOSDeviceName(); | |
66 localized_strings->SetString( | |
67 "greetingHeader", | |
68 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_HEADER, device_name)); | |
69 localized_strings->SetString( | |
70 "greetingDescription", | |
71 l10n_util::GetStringFUTF16(IDS_ARC_OPT_IN_DIALOG_DESCRIPTION, | |
72 device_name)); | |
73 localized_strings->SetString( | |
74 "greetingLegacy", | |
75 l10n_util::GetStringUTF16(IDS_ARC_OPT_IN_DIALOG_LEGACY)); | |
76 localized_strings->SetString( | |
77 "buttonGetStarted", | |
78 l10n_util::GetStringUTF16(IDS_ARC_OPT_IN_DIALOG_BUTTON_GET_STARTED)); | |
79 localized_strings->SetString( | |
80 "buttonRetry", | |
81 l10n_util::GetStringUTF16(IDS_ARC_OPT_IN_DIALOG_BUTTON_RETRY)); | |
82 localized_strings->SetString( | |
83 "progressLSOLoading", | |
84 l10n_util::GetStringUTF16(IDS_ARC_OPT_IN_DIALOG_PROGRESS_LSO)); | |
85 localized_strings->SetString( | |
86 "progressAndroidLoading", | |
87 l10n_util::GetStringUTF16(IDS_ARC_OPT_IN_DIALOG_PROGRESS_ANDROID)); | |
88 | |
89 const std::string& app_locale = g_browser_process->GetApplicationLocale(); | |
90 webui::SetLoadTimeDataDefaults(app_locale, localized_strings.get()); | |
91 | |
92 base::DictionaryValue request; | |
93 std::string request_string; | |
94 request.SetString(kAction, kActionSetLocalization); | |
95 request.Set(kData, std::move(localized_strings)); | |
96 base::JSONWriter::Write(request, &request_string); | |
97 client_->PostMessageFromNativeHost(request_string); | |
98 } | |
99 | |
100 void ArcSupportHost::OnOptInUIClose() { | |
101 if (!client_) | |
102 return; | |
103 | |
104 base::DictionaryValue response; | |
105 response.SetString(kAction, kActionCloseUI); | |
106 std::string response_string; | |
107 base::JSONWriter::Write(response, &response_string); | |
108 client_->PostMessageFromNativeHost(response_string); | |
109 } | |
110 | |
111 void ArcSupportHost::OnOptInUIShowPage(arc::ArcAuthService::UIPage page, | |
112 const base::string16& status) { | |
113 if (!client_) | |
114 return; | |
115 | |
116 base::DictionaryValue response; | |
117 response.SetString(kAction, kActionShowPage); | |
118 response.SetInteger(kPage, static_cast<int>(page)); | |
119 response.SetString(kStatus, status); | |
120 std::string response_string; | |
121 base::JSONWriter::Write(response, &response_string); | |
122 client_->PostMessageFromNativeHost(response_string); | |
123 } | |
124 | |
125 void ArcSupportHost::OnMessage(const std::string& request_string) { | |
126 scoped_ptr<base::Value> request_value = | |
127 base::JSONReader::Read(request_string); | |
128 scoped_ptr<base::DictionaryValue> request( | |
129 static_cast<base::DictionaryValue*>(request_value.release())); | |
130 if (!request.get()) { | |
131 NOTREACHED(); | |
132 return; | |
133 } | |
134 | |
135 std::string action; | |
136 if (!request->GetString(kAction, &action)) { | |
137 NOTREACHED(); | |
138 return; | |
139 } | |
140 | |
141 if (action == kActionCheckAuthCode) { | |
142 arc::ArcAuthService::Get()->CheckAuthCode(); | |
143 } else if (action == kActionCancelAuthCode) { | |
144 arc::ArcAuthService::Get()->CancelAuthCode(); | |
145 } else { | |
146 NOTREACHED(); | |
147 } | |
148 } | |
149 | |
150 scoped_refptr<base::SingleThreadTaskRunner> ArcSupportHost::task_runner() | |
151 const { | |
152 return base::ThreadTaskRunnerHandle::Get(); | |
153 } | |
OLD | NEW |