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

Side by Side Diff: chrome/browser/ui/webui/copresence_ui_handler.cc

Issue 2130803002: Deleting the copresence API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: . Created 4 years, 5 months 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
« no previous file with comments | « chrome/browser/ui/webui/copresence_ui_handler.h ('k') | chrome/chrome_browser_extensions.gypi » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2014 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/ui/webui/copresence_ui_handler.h"
6
7 #include <map>
8 #include <memory>
9 #include <string>
10 #include <utility>
11 #include <vector>
12
13 #include "base/bind.h"
14 #include "base/i18n/time_formatting.h"
15 #include "base/time/time.h"
16 #include "base/values.h"
17 #include "chrome/browser/extensions/api/copresence/copresence_api.h"
18 #include "components/copresence/proto/data.pb.h"
19 #include "components/copresence/public/copresence_manager.h"
20 #include "components/copresence/public/copresence_state.h"
21 #include "components/copresence/tokens.h"
22 #include "content/public/browser/web_contents.h"
23 #include "content/public/browser/web_ui.h"
24 #include "ui/base/l10n/time_format.h"
25
26 using base::ListValue;
27 using base::DictionaryValue;
28 using content::WebUI;
29 using copresence::Directive;
30 using copresence::ReceivedToken;
31 using copresence::TransmittedToken;
32 using extensions::CopresenceService;
33
34 // TODO(ckehoe): Make debug strings translatable?
35
36 namespace {
37
38 std::string FormatInstructionType(
39 copresence::TokenInstructionType directive_type) {
40 switch (directive_type) {
41 case copresence::TRANSMIT:
42 return "Transmit";
43
44 case copresence::RECEIVE:
45 return "Receive";
46
47 default:
48 NOTREACHED();
49 return "Unknown";
50 }
51 }
52
53 std::string FormatMedium(copresence::TokenMedium medium) {
54 switch (medium) {
55 case copresence::AUDIO_ULTRASOUND_PASSBAND:
56 return "Ultrasound";
57
58 case copresence::AUDIO_AUDIBLE_DTMF:
59 return "Audible";
60
61 default:
62 NOTREACHED();
63 return "Unknown";
64 }
65 }
66
67 std::string ConvertStatus(const TransmittedToken& token) {
68 bool done = token.stop_time < base::Time::Now();
69 std::string status = done ? "done" : "active";
70 if (token.broadcast_confirmed)
71 status += " confirmed";
72 return status;
73 }
74
75 std::string ConvertStatus(const ReceivedToken& token) {
76 switch (token.valid) {
77 case ReceivedToken::VALID:
78 return "valid";
79
80 case ReceivedToken::INVALID:
81 return "invalid";
82
83 case ReceivedToken::UNKNOWN:
84 return std::string();
85
86 default:
87 NOTREACHED();
88 return std::string();
89 }
90 }
91
92 template <class T>
93 std::unique_ptr<DictionaryValue> FormatToken(const T& token) {
94 std::unique_ptr<DictionaryValue> js_token(new DictionaryValue);
95
96 js_token->SetString("id", token.id);
97 js_token->SetString("statuses", ConvertStatus(token));
98 js_token->SetString("medium", FormatMedium(token.medium));
99 DCHECK(!token.start_time.is_null());
100 js_token->SetString("time",
101 base::TimeFormatTimeOfDay(token.start_time));
102
103 return js_token;
104 }
105
106 // Retrieve the CopresenceService, if any.
107 CopresenceService* GetCopresenceService(WebUI* web_ui) {
108 DCHECK(web_ui);
109 return CopresenceService::GetFactoryInstance()->Get(
110 web_ui->GetWebContents()->GetBrowserContext());
111 }
112
113 // Safely retrieve the CopresenceState, if any.
114 copresence::CopresenceState* GetCopresenceState(CopresenceService* service) {
115 // During shutdown, there may be no CopresenceService.
116 return service && service->manager() ? service->manager()->state() : nullptr;
117 }
118
119 // Safely retrieve the CopresenceState, if any. It would be cleaner if we could
120 // put this into CopresenceUIHandler and call WebUIMessageHandler::web_ui()
121 // instead of taking an argument. However, it turns out that web_ui() returns
122 // null when called in the constructor. So we pass in the web_ui explicitly.
123 copresence::CopresenceState* GetCopresenceState(WebUI* web_ui) {
124 return GetCopresenceState(GetCopresenceService(web_ui));
125 }
126
127 } // namespace
128
129
130 // Public functions.
131
132 CopresenceUIHandler::CopresenceUIHandler(WebUI* web_ui)
133 : state_(GetCopresenceState(web_ui)) {
134 DCHECK(state_);
135 state_->AddObserver(this);
136 }
137
138 CopresenceUIHandler::~CopresenceUIHandler() {
139 // Check if the CopresenceService is still up before unregistering.
140 state_ = GetCopresenceState(web_ui());
141 if (state_)
142 state_->RemoveObserver(this);
143 }
144
145
146 // Private functions.
147
148 void CopresenceUIHandler::RegisterMessages() {
149 web_ui()->RegisterMessageCallback(
150 "populateCopresenceState",
151 base::Bind(&CopresenceUIHandler::HandlePopulateState,
152 base::Unretained(this)));
153 web_ui()->RegisterMessageCallback(
154 "clearCopresenceState",
155 base::Bind(&CopresenceUIHandler::HandleClearState,
156 base::Unretained(this)));
157 }
158
159 void CopresenceUIHandler::DirectivesUpdated() {
160 ListValue js_directives;
161 for (const Directive& directive : state_->active_directives()) {
162 std::unique_ptr<DictionaryValue> js_directive(new DictionaryValue);
163
164 js_directive->SetString("type", FormatInstructionType(
165 directive.token_instruction().token_instruction_type()));
166 js_directive->SetString("medium", FormatMedium(
167 directive.token_instruction().medium()));
168 js_directive->SetString("duration", ui::TimeFormat::Simple(
169 ui::TimeFormat::FORMAT_DURATION,
170 ui::TimeFormat::LENGTH_LONG,
171 base::TimeDelta::FromMilliseconds(directive.ttl_millis())));
172
173 js_directives.Append(std::move(js_directive));
174 }
175
176 web_ui()->CallJavascriptFunctionUnsafe("refreshDirectives", js_directives);
177 }
178
179 void CopresenceUIHandler::TokenTransmitted(const TransmittedToken& token) {
180 web_ui()->CallJavascriptFunctionUnsafe("updateTransmittedToken",
181 *FormatToken(token));
182 }
183
184 void CopresenceUIHandler::TokenReceived(const ReceivedToken& token) {
185 web_ui()->CallJavascriptFunctionUnsafe("updateReceivedToken",
186 *FormatToken(token));
187 }
188
189 void CopresenceUIHandler::HandlePopulateState(const ListValue* args) {
190 DCHECK(args->empty());
191 DirectivesUpdated();
192 // TODO(ckehoe): Pass tokens to JS as a batch.
193 for (const auto& token_entry : state_->transmitted_tokens())
194 TokenTransmitted(token_entry.second);
195 for (const auto& token_entry : state_->received_tokens())
196 TokenReceived(token_entry.second);
197 }
198
199 void CopresenceUIHandler::HandleClearState(const ListValue* args) {
200 DCHECK(args->empty());
201
202 CopresenceService* service = GetCopresenceService(web_ui());
203 DCHECK(service);
204 service->ResetState();
205
206 // CopresenceService::ResetState() deletes the CopresenceState object
207 // we were using. We have to get the new one and reconnect to it.
208 state_ = GetCopresenceState(service);
209 DCHECK(state_);
210 state_->AddObserver(this);
211
212 web_ui()->CallJavascriptFunctionUnsafe("clearTokens");
213 DirectivesUpdated();
214 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/copresence_ui_handler.h ('k') | chrome/chrome_browser_extensions.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698