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

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

Issue 734243003: Adding the chrome://copresence page (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@state
Patch Set: Fixing compile errors Created 6 years 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_ui.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 <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/i18n/time_formatting.h"
13 #include "base/time/time.h"
14 #include "base/values.h"
15 #include "chrome/browser/extensions/api/copresence/copresence_api.h"
16 #include "components/copresence/proto/data.pb.h"
17 #include "components/copresence/public/copresence_manager.h"
18 #include "components/copresence/public/copresence_state.h"
19 #include "components/copresence/tokens.h"
20 #include "content/public/browser/web_contents.h"
21 #include "content/public/browser/web_ui.h"
22 #include "ui/base/l10n/time_format.h"
23
24 using base::ListValue;
25 using base::DictionaryValue;
26 using content::WebUI;
27 using copresence::Directive;
28 using copresence::ReceivedToken;
29 using copresence::TransmittedToken;
30 using extensions::CopresenceService;
31
32 // TODO(ckehoe): Make debug strings translatable?
33
34 namespace {
35
36 std::string FormatInstructionType(
37 copresence::TokenInstructionType directive_type) {
38 switch (directive_type) {
39 case copresence::TRANSMIT:
40 return "Transmit";
41
42 case copresence::RECEIVE:
43 return "Receive";
44
45 default:
46 return "Unknown";
47 }
Dan Beam 2014/12/20 00:10:29 instead of swith (...) { default: ret
Charlie 2014/12/20 02:04:51 Done.
48 }
49
50 std::string FormatMedium(copresence::TokenMedium medium) {
51 switch (medium) {
52 case copresence::AUDIO_ULTRASOUND_PASSBAND:
53 return "Ultrasound";
54
55 case copresence::AUDIO_AUDIBLE_DTMF:
56 return "Audible";
57
58 default:
Dan Beam 2014/12/20 00:10:29 NOTREACHED();
Charlie 2014/12/20 02:04:51 Done.
59 return "Unknown";
60 }
61 }
62
63 std::string ConvertStatus(const TransmittedToken& token) {
64 bool done = token.stop_time < base::Time::Now();
65 std::string status = done ? "done" : "active";
66 if (token.broadcast_confirmed)
67 status += " confirmed";
68 return status;
69 }
70
71 std::string ConvertStatus(const ReceivedToken& token) {
72 switch (token.valid) {
73 case copresence::ReceivedToken::VALID:
74 return "valid";
75
76 case copresence::ReceivedToken::INVALID:
77 return "invalid";
78
79 case copresence::ReceivedToken::UNKNOWN:
80 default:
81 return std::string();
82 }
83 }
84
85 template<class T>
86 scoped_ptr<DictionaryValue> FormatToken(const T& token) {
87 scoped_ptr<DictionaryValue> js_token(new DictionaryValue);
88
89 js_token->SetString("id", token.id);
90 js_token->SetString("statuses", ConvertStatus(token));
91 js_token->SetString("medium", FormatMedium(token.medium));
92 DCHECK(!token.start_time.is_null());
93 js_token->SetString("time",
94 base::TimeFormatTimeOfDay(token.start_time));
95
96 return js_token.Pass();
97 }
98
99 // Safely retrieve the CopresenceState, if any.
100 copresence::CopresenceState* GetCopresenceState(WebUI* web_ui) {
101 // This function must be called with a valid web_ui.
102 DCHECK(web_ui);
103 DCHECK(web_ui->GetWebContents());
104
105 // During shutdown, however, there may be no CopresenceService.
106 CopresenceService* service = CopresenceService::GetFactoryInstance()->Get(
107 web_ui->GetWebContents()->GetBrowserContext());
108 return service && service->manager() ? service->manager()->state() : nullptr;
109 }
110
111 } // namespace
112
113
114 // Public functions.
115
116 CopresenceUIHandler::CopresenceUIHandler(WebUI* web_ui)
117 : state_(GetCopresenceState(web_ui)) {
118 DCHECK(state_);
119 state_->AddObserver(this);
120 }
121
122 CopresenceUIHandler::~CopresenceUIHandler() {
123 // Check if the CopresenceService is still up before unregistering.
124 state_ = GetCopresenceState(web_ui());
125 if (state_)
126 state_->RemoveObserver(this);
127 }
128
129
130 // Private functions.
131
132 void CopresenceUIHandler::RegisterMessages() {
133 web_ui()->RegisterMessageCallback(
134 "populateCopresenceState",
135 base::Bind(&CopresenceUIHandler::HandlePopulateState,
136 base::Unretained(this)));
137 }
138
139 void CopresenceUIHandler::DirectivesUpdated() {
140 ListValue js_directives;
141 for (const Directive& directive : state_->active_directives()) {
142 scoped_ptr<DictionaryValue> js_directive(new DictionaryValue);
143
144 js_directive->SetString("type", FormatInstructionType(
145 directive.token_instruction().token_instruction_type()));
146 js_directive->SetString("medium", FormatMedium(
147 directive.token_instruction().medium()));
148 js_directive->SetString("duration", ui::TimeFormat::Simple(
149 ui::TimeFormat::FORMAT_DURATION,
150 ui::TimeFormat::LENGTH_LONG,
151 base::TimeDelta::FromMilliseconds(directive.ttl_millis())));
152
153 js_directives.Append(js_directive.release());
154 }
155
156 web_ui()->CallJavascriptFunction("refreshDirectives", js_directives);
157 }
158
159 void CopresenceUIHandler::TokenTransmitted(const TransmittedToken& token) {
160 web_ui()->CallJavascriptFunction("updateTransmittedToken",
161 *FormatToken(token));
162 }
163
164 void CopresenceUIHandler::TokenReceived(const ReceivedToken& token) {
165 web_ui()->CallJavascriptFunction("updateReceivedToken",
166 *FormatToken(token));
167 }
168
169 void CopresenceUIHandler::HandlePopulateState(const ListValue* args) {
170 DCHECK(args->empty());
171 DirectivesUpdated();
172 // TODO(ckehoe): Pass tokens to JS as a batch.
173 for (const auto& token_entry : state_->transmitted_tokens())
174 TokenTransmitted(token_entry.second);
175 for (const auto& token_entry : state_->received_tokens())
176 TokenReceived(token_entry.second);
177 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/copresence_ui_handler.h ('k') | chrome/chrome_browser_ui.gypi » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698