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

Side by Side Diff: chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc

Issue 20070002: Demo UI for device discovery and registration (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added FILE_PATH_LITERAL macro to fix windows compile problem Created 7 years, 4 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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/ui/webui/local_discovery/local_discovery_ui_handler.h" 5 #include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/strings/stringprintf.h"
8 #include "base/values.h" 9 #include "base/values.h"
10 #include "chrome/browser/local_discovery/privet_device_lister_impl.h"
11 #include "chrome/browser/local_discovery/privet_http_impl.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/signin/profile_oauth2_token_service.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
15 #include "chrome/browser/signin/signin_manager.h"
16 #include "chrome/browser/signin/signin_manager_base.h"
17 #include "chrome/browser/signin/signin_manager_factory.h"
9 #include "content/public/browser/web_ui.h" 18 #include "content/public/browser/web_ui.h"
10 19 #include "net/base/host_port_pair.h"
11 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() 20 #include "net/base/net_util.h"
12 : action_callback_(base::Bind(&LocalDiscoveryUIHandler::OnNewDevice, 21
13 base::Unretained(this))) { 22 namespace local_discovery {
14 content::AddActionCallback(action_callback_); 23
24 namespace {
25 // TODO(noamsml): This is a temporary shim until automated_url is in the
26 // response.
27 const char kPrivetAutomatedClaimURLFormat[] = "%s/confirm?token=%s";
28 LocalDiscoveryUIHandler::Factory* g_factory = NULL;
29 } // namepsace
30
31 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() {
32 }
33
34 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler(
35 scoped_ptr<PrivetDeviceLister> privet_lister) {
36 privet_lister.swap(privet_lister_);
15 } 37 }
16 38
17 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() { 39 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() {
18 content::RemoveActionCallback(action_callback_); 40 }
19 } 41
20 42 // static
21 void LocalDiscoveryUIHandler::RegisterMessages() {} 43 LocalDiscoveryUIHandler* LocalDiscoveryUIHandler::Create() {
22 44 if (g_factory) return g_factory->CreateLocalDiscoveryUIHandler();
23 void LocalDiscoveryUIHandler::OnNewDevice(const std::string& name) { 45 return new LocalDiscoveryUIHandler();
24 // TODO(gene): Once we receive information about new locally discovered 46 }
25 // device, we should add it to the page. 47
26 // Here is an example how to do it: 48 // static
27 // 49 void LocalDiscoveryUIHandler::SetFactory(Factory* factory) {
28 // base::StringValue service_name(name); 50 g_factory = factory;
29 // 51 }
30 // base::DictionaryValue info; 52
31 // info.SetString("domain", "domain.local"); 53 void LocalDiscoveryUIHandler::RegisterMessages() {
32 // info.SetString("port", "80"); 54 web_ui()->RegisterMessageCallback("start", base::Bind(
33 // info.SetString("ip", "XXX.XXX.XXX.XXX"); 55 &LocalDiscoveryUIHandler::HandleStart,
34 // info.SetString("metadata", "metadata\nmetadata"); 56 base::Unretained(this)));
35 // info.SetString("lastSeen", "unknown"); 57 web_ui()->RegisterMessageCallback("registerDevice", base::Bind(
36 // info.SetString("registered", "not registered"); 58 &LocalDiscoveryUIHandler::HandleRegisterDevice,
37 // 59 base::Unretained(this)));
38 // base::StringValue domain("domain.local"); 60 }
39 // base::StringValue port("80"); 61
40 // base::StringValue ip("XXX.XXX.XXX.XXX"); 62 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) {
41 // base::StringValue metadata("metadata<br>metadata"); 63 // If privet_lister_ is already set, it is a mock used for tests.
42 // base::StringValue lastSeen("unknown"); 64 if (!privet_lister_) {
43 // base::StringValue registered("not registered"); 65 service_discovery_client_ = new ServiceDiscoveryHostClient();
44 // 66 service_discovery_client_->Start();
45 // web_ui()->CallJavascriptFunction("onServiceUpdate", service_name, info); 67 privet_lister_.reset(new PrivetDeviceListerImpl(
46 } 68 service_discovery_client_.get(), this));
69 }
70
71 privet_lister_->Start();
72 privet_lister_->DiscoverNewDevices(false);
73 }
74
75 void LocalDiscoveryUIHandler::HandleRegisterDevice(
76 const base::ListValue* args) {
77 std::string device_name;
78 bool rv = args->GetString(0, &device_name);
79 DCHECK(rv);
80 currently_registering_device_ = device_name;
81
82 domain_resolver_ = service_discovery_client_->CreateLocalDomainResolver(
83 device_descriptions_[device_name].address.host(),
84 net::ADDRESS_FAMILY_UNSPECIFIED,
85 base::Bind(&LocalDiscoveryUIHandler::OnDomainResolved,
86 base::Unretained(this)));
87
88 domain_resolver_->Start();
89 }
90
91 void LocalDiscoveryUIHandler::OnDomainResolved(bool success,
92 const net::IPAddressNumber& address) {
93 if (!success) {
94 LogRegisterErrorToWeb("Resolution failed");
95 return;
96 }
97
98 if (device_descriptions_.count(currently_registering_device_) == 0) {
99 LogRegisterErrorToWeb("Device no longer exists");
100 return;
101 }
102
103 Profile* profile = Profile::FromWebUI(web_ui());
104 SigninManagerBase* signin_manager =
105 SigninManagerFactory::GetForProfileIfExists(profile);
106
107 if (!signin_manager) {
108 LogRegisterErrorToWeb("You must be signed in");
109 return;
110 }
111
112 std::string username = signin_manager->GetAuthenticatedUsername();
113
114 std::string address_str = net::IPAddressToString(address);
115 int port = device_descriptions_[currently_registering_device_].address.port();
116
117 current_http_client_.reset(new PrivetHTTPClientImpl(
118 net::HostPortPair(address_str, port),
119 Profile::FromWebUI(web_ui())->GetRequestContext()));
120 current_register_operation_ =
121 current_http_client_->CreateRegisterOperation(username, this);
122 current_register_operation_->Start();
123 }
124
125 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken(
126 const std::string& token,
127 const GURL& url) {
128 if (device_descriptions_.count(currently_registering_device_) == 0) {
129 LogRegisterErrorToWeb("Device no longer exists");
130 return;
131 }
132
133 GURL automated_claim_url(base::StringPrintf(
134 kPrivetAutomatedClaimURLFormat,
135 device_descriptions_[currently_registering_device_].url.c_str(),
136 token.c_str()));
137
138 Profile* profile = Profile::FromWebUI(web_ui());
139
140 OAuth2TokenService* token_service =
141 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
142
143 if (!token_service) {
144 LogRegisterErrorToWeb("Could not get token service");
145 return;
146 }
147
148 confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow(
149 profile->GetRequestContext(),
150 token_service,
151 automated_claim_url,
152 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone,
153 base::Unretained(this))));
154
155 confirm_api_call_flow_->Start();
156 }
157
158 void LocalDiscoveryUIHandler::OnPrivetRegisterError(
159 const std::string& action,
160 PrivetRegisterOperation::FailureReason reason,
161 int printer_http_code,
162 const DictionaryValue* json) {
163 // TODO(noamsml): Add detailed error message.
164 LogRegisterErrorToWeb("Registration error");
165 }
166
167 void LocalDiscoveryUIHandler::OnPrivetRegisterDone(
168 const std::string& device_id) {
169 current_register_operation_.reset();
170 current_http_client_.reset();
171
172 LogRegisterDoneToWeb(device_id);
173 }
174
175 void LocalDiscoveryUIHandler::OnConfirmDone(
176 PrivetConfirmApiCallFlow::Status status) {
177 if (status == PrivetConfirmApiCallFlow::SUCCESS) {
178 DLOG(INFO) << "Confirm success.";
179 confirm_api_call_flow_.reset();
180 current_register_operation_->CompleteRegistration();
181 } else {
182 // TODO(noamsml): Add detailed error message.
183 LogRegisterErrorToWeb("Confirm error");
184 }
185 }
186
187 void LocalDiscoveryUIHandler::DeviceChanged(
188 bool added,
189 const std::string& name,
190 const DeviceDescription& description) {
191 device_descriptions_[name] = description;
192
193 base::StringValue service_name(name);
194 base::DictionaryValue info;
195 info.SetString("domain", description.address.host());
196 info.SetInteger("port", description.address.port());
197 std::string ip_addr_string;
198 if (!description.ip_address.empty())
199 ip_addr_string = net::IPAddressToString(description.ip_address);
200
201 info.SetString("ip", ip_addr_string);
202 info.SetString("lastSeen", "unknown");
203 info.SetBoolean("registered", !description.id.empty());
204
205 web_ui()->CallJavascriptFunction("local_discovery.onServiceUpdate",
206 service_name, info);
207 }
208
209 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) {
210 device_descriptions_.erase(name);
211 scoped_ptr<base::Value> null_value(base::Value::CreateNullValue());
212 base::StringValue name_value(name);
213
214 web_ui()->CallJavascriptFunction("local_discovery.onServiceUpdate",
215 name_value, *null_value);
216 }
217
218 void LocalDiscoveryUIHandler::LogRegisterErrorToWeb(const std::string& error) {
219 base::StringValue error_value(error);
220 web_ui()->CallJavascriptFunction("local_discovery.registrationFailed",
221 error_value);
222 DLOG(ERROR) << error;
223 }
224
225 void LocalDiscoveryUIHandler::LogRegisterDoneToWeb(const std::string& id) {
226 base::StringValue id_value(id);
227 web_ui()->CallJavascriptFunction("local_discovery.registrationSuccess",
228 id_value);
229 DLOG(INFO) << "Registered " << id;
230 }
231
232 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698