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