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

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: 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/message_loop/message_loop.h"
9 #include "base/strings/stringprintf.h"
8 #include "base/values.h" 10 #include "base/values.h"
11 #include "chrome/browser/local_discovery/privet_device_lister_impl.h"
12 #include "chrome/browser/local_discovery/privet_http_impl.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/signin/profile_oauth2_token_service.h"
15 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
16 #include "chrome/browser/signin/signin_manager.h"
17 #include "chrome/browser/signin/signin_manager_base.h"
18 #include "chrome/browser/signin/signin_manager_factory.h"
9 #include "content/public/browser/web_ui.h" 19 #include "content/public/browser/web_ui.h"
20 #include "net/base/host_port_pair.h"
21 #include "net/base/net_util.h"
10 22
11 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() 23 namespace local_discovery {
12 : action_callback_(base::Bind(&LocalDiscoveryUIHandler::OnNewDevice, 24
13 base::Unretained(this))) { 25 namespace {
14 content::AddActionCallback(action_callback_); 26 // TODO(noamsml): This is a temporary shim until automated_url is in the
27 // response.
28 const char kPrivetAutomatedClaimURLFormat[] = "%s/confirm?token=%s";
29 } // namepsace
30
31 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() {
15 } 32 }
16 33
17 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() { 34 LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() {
18 content::RemoveActionCallback(action_callback_);
19 } 35 }
20 36
21 void LocalDiscoveryUIHandler::RegisterMessages() {} 37 void LocalDiscoveryUIHandler::RegisterMessages() {
38 web_ui()->RegisterMessageCallback("start", base::Bind(
39 &LocalDiscoveryUIHandler::OnStart,
40 base::Unretained(this)));
41 web_ui()->RegisterMessageCallback("register", base::Bind(
42 &LocalDiscoveryUIHandler::OnRegisterDevice,
43 base::Unretained(this)));
44 }
22 45
23 void LocalDiscoveryUIHandler::OnNewDevice(const std::string& name) { 46 void LocalDiscoveryUIHandler::OnStart(const base::ListValue* value) {
24 // TODO(gene): Once we receive information about new locally discovered 47 service_discovery_client_ = new ServiceDiscoveryHostClient();
25 // device, we should add it to the page. 48 service_discovery_client_->Start();
26 // Here is an example how to do it: 49 privet_lister_.reset(new PrivetDeviceListerImpl(
27 // 50 service_discovery_client_.get(), this));
28 // base::StringValue service_name(name); 51 privet_lister_->Start();
29 // 52 privet_lister_->DiscoverNewDevices(false);
30 // base::DictionaryValue info;
31 // info.SetString("domain", "domain.local");
32 // info.SetString("port", "80");
33 // info.SetString("ip", "XXX.XXX.XXX.XXX");
34 // info.SetString("metadata", "metadata\nmetadata");
35 // info.SetString("lastSeen", "unknown");
36 // info.SetString("registered", "not registered");
37 //
38 // base::StringValue domain("domain.local");
39 // base::StringValue port("80");
40 // base::StringValue ip("XXX.XXX.XXX.XXX");
41 // base::StringValue metadata("metadata<br>metadata");
42 // base::StringValue lastSeen("unknown");
43 // base::StringValue registered("not registered");
44 //
45 // web_ui()->CallJavascriptFunction("onServiceUpdate", service_name, info);
46 } 53 }
54
55 void LocalDiscoveryUIHandler::OnRegisterDevice(const base::ListValue* data) {
56 std::string device_name;
57 data->GetString(0, &device_name);
Dan Beam 2013/08/06 21:21:38 nit: CHECK() or bool rv = ...; DCHECK(rv); etc.
Noam Samuel 2013/08/06 23:54:05 Done.
58 currently_registering_device_ = device_name;
59
Dan Beam 2013/08/06 21:21:38 nit: what if |device_name| isn't in |device_descri
Noam Samuel 2013/08/06 23:54:05 Hm. I shouldn't actually be looking this up in the
60 if (!device_descriptions_[device_name].ip_address.empty()) {
61 OnDomainResolved(true, device_descriptions_[device_name].ip_address);
62 } else {
63 domain_resolver_ = service_discovery_client_->CreateLocalDomainResolver(
64 device_descriptions_[device_name].address.host(),
65 net::ADDRESS_FAMILY_UNSPECIFIED,
66 base::Bind(&LocalDiscoveryUIHandler::OnDomainResolved,
67 base::Unretained(this)));
68
69 domain_resolver_->Start();
70 }
71 }
72
73 void LocalDiscoveryUIHandler::OnDomainResolved(bool success,
74 const net::IPAddressNumber& address) {
75 if (!success) {
76 LogRegisterErrorToWeb("Resolution failed");
77 return;
78 }
79
80 Profile* profile = Profile::FromWebUI(web_ui());
81 SigninManagerBase* signin_manager =
82 SigninManagerFactory::GetForProfileIfExists(profile);
83
84 if (!signin_manager) {
85 LogRegisterErrorToWeb("You must be signed in");
86 return;
87 }
88
89 std::string username = signin_manager->GetAuthenticatedUsername();
90
91 std::string address_str = net::IPAddressToString(address);
92 int port = device_descriptions_[currently_registering_device_].address.port();
93
94 current_http_client_.reset(new PrivetHTTPClientImpl(
95 net::HostPortPair(address_str, port),
96 Profile::FromWebUI(web_ui())->GetRequestContext()));
97 current_register_operation_ =
98 current_http_client_->CreateRegisterOperation(username, this);
99 current_register_operation_->Start();
100 }
101
102 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken(
103 const std::string& token,
104 const GURL& url) {
105 GURL automated_claim_url(base::StringPrintf(
106 kPrivetAutomatedClaimURLFormat,
107 device_descriptions_[currently_registering_device_].url.c_str(),
108 token.c_str()));
109
110 Profile* profile = Profile::FromWebUI(web_ui());
111
112 OAuth2TokenService* token_service =
113 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
114
115 if (!token_service) {
116 LogRegisterErrorToWeb("Could not get token service");
117 return;
118 }
119
120 confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow(
121 profile->GetRequestContext(),
122 token_service,
123 automated_claim_url,
124 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone,
125 base::Unretained(this))));
126
127 confirm_api_call_flow_->Start();
128 }
129
130 void LocalDiscoveryUIHandler::OnPrivetRegisterError(
131 const std::string& action,
132 PrivetRegisterOperation::FailureReason reason,
133 int printer_http_code,
134 const DictionaryValue* json) {
135 // TODO(noamsml): Add detailed error message.
136 LogRegisterErrorToWeb("Registration error");
137 }
138
139 void LocalDiscoveryUIHandler::OnPrivetRegisterDone(
140 const std::string& device_id) {
141 current_register_operation_.reset();
142 current_http_client_.reset();
143
144 LogRegisterDoneToWeb(device_id);
145 }
146
147 void LocalDiscoveryUIHandler::OnConfirmDone(
148 PrivetConfirmApiCallFlow::Status status) {
149 if (status == PrivetConfirmApiCallFlow::SUCCESS) {
150 LOG(INFO) << "Confirm success.";
Dan Beam 2013/08/06 21:21:38 nit: DLOG
Noam Samuel 2013/08/06 23:54:05 Done.
151 confirm_api_call_flow_.reset();
152 current_register_operation_->CompleteRegistration();
153 } else {
154 // TODO(noamsml): Add detailed error message.
155 LogRegisterErrorToWeb("Confirm error");
156 }
157 }
158
159 void LocalDiscoveryUIHandler::DeviceChanged(
160 bool added,
161 const std::string& name,
162 const DeviceDescription& description) {
163 device_descriptions_[name] = description;
164
165 base::StringValue service_name(name);
166 base::DictionaryValue info;
167 info.SetString("domain", description.address.host());
168 info.SetInteger("port", description.address.port());
169 std::string ip_addr_string;
170 if (!description.ip_address.empty()) {
Dan Beam 2013/08/06 21:21:38 nit: no curlies
Noam Samuel 2013/08/06 23:54:05 Done.
171 ip_addr_string = net::IPAddressToString(description.ip_address);
172 }
173 info.SetString("ip", ip_addr_string);
174 info.SetString("metadata", "");
175 info.SetString("lastSeen", "unknown");
176 info.SetBoolean("registered", !description.id.empty());
177
178 web_ui()->CallJavascriptFunction("onServiceUpdate", service_name, info);
179 }
180
181 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) {
182 device_descriptions_.erase(name);
183 scoped_ptr<base::Value> null_value(base::Value::CreateNullValue());
184 base::StringValue name_value(name);
185
186 web_ui()->CallJavascriptFunction("onServiceUpdate",
187 name_value, *null_value.get());
Dan Beam 2013/08/06 21:21:38 nit: don't think you need the .get()
Noam Samuel 2013/08/06 23:54:05 Done.
188 }
189
190 void LocalDiscoveryUIHandler::LogRegisterErrorToWeb(const std::string& error) {
191 base::StringValue error_value(error);
192 web_ui()->CallJavascriptFunction("registrationFailed", error_value);
193 LOG(ERROR) << error;
Dan Beam 2013/08/06 21:21:38 nit: DLOG
Noam Samuel 2013/08/06 23:54:05 Done.
194 }
195
196 void LocalDiscoveryUIHandler::LogRegisterDoneToWeb(const std::string& id) {
197 base::StringValue id_value(id);
198 web_ui()->CallJavascriptFunction("registrationSuccess", id_value);
199 LOG(INFO) << "Registered " << id;
Dan Beam 2013/08/06 21:21:38 nit: DLOG
Noam Samuel 2013/08/06 23:54:05 Done.
200 }
201
202 } // namespace local_discovery
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698