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