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