Index: chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
diff --git a/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc b/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
index e1f458c257184612ca3ce9de5d3ac3ed398f7fc3..8539ca3f17b0c38d9875174477c34e4ceddae4a9 100644 |
--- a/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
+++ b/chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.cc |
@@ -5,42 +5,208 @@ |
#include "chrome/browser/ui/webui/local_discovery/local_discovery_ui_handler.h" |
#include "base/bind.h" |
+#include "base/message_loop/message_loop.h" |
Dan Beam
2013/08/07 18:43:03
^ where is this used?
Noam Samuel
2013/08/08 18:45:36
Removed.
|
+#include "base/strings/stringprintf.h" |
#include "base/values.h" |
+#include "chrome/browser/local_discovery/privet_device_lister_impl.h" |
+#include "chrome/browser/local_discovery/privet_http_impl.h" |
+#include "chrome/browser/profiles/profile.h" |
+#include "chrome/browser/signin/profile_oauth2_token_service.h" |
+#include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
+#include "chrome/browser/signin/signin_manager.h" |
+#include "chrome/browser/signin/signin_manager_base.h" |
+#include "chrome/browser/signin/signin_manager_factory.h" |
#include "content/public/browser/web_ui.h" |
+#include "net/base/host_port_pair.h" |
+#include "net/base/net_util.h" |
-LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() |
- : action_callback_(base::Bind(&LocalDiscoveryUIHandler::OnNewDevice, |
- base::Unretained(this))) { |
- content::AddActionCallback(action_callback_); |
+namespace local_discovery { |
+ |
+namespace { |
+// TODO(noamsml): This is a temporary shim until automated_url is in the |
+// response. |
+const char kPrivetAutomatedClaimURLFormat[] = "%s/confirm?token=%s"; |
+} // namepsace |
+ |
+LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() { |
} |
LocalDiscoveryUIHandler::~LocalDiscoveryUIHandler() { |
- content::RemoveActionCallback(action_callback_); |
-} |
- |
-void LocalDiscoveryUIHandler::RegisterMessages() {} |
- |
-void LocalDiscoveryUIHandler::OnNewDevice(const std::string& name) { |
- // TODO(gene): Once we receive information about new locally discovered |
- // device, we should add it to the page. |
- // Here is an example how to do it: |
- // |
- // base::StringValue service_name(name); |
- // |
- // base::DictionaryValue info; |
- // info.SetString("domain", "domain.local"); |
- // info.SetString("port", "80"); |
- // info.SetString("ip", "XXX.XXX.XXX.XXX"); |
- // info.SetString("metadata", "metadata\nmetadata"); |
- // info.SetString("lastSeen", "unknown"); |
- // info.SetString("registered", "not registered"); |
- // |
- // base::StringValue domain("domain.local"); |
- // base::StringValue port("80"); |
- // base::StringValue ip("XXX.XXX.XXX.XXX"); |
- // base::StringValue metadata("metadata<br>metadata"); |
- // base::StringValue lastSeen("unknown"); |
- // base::StringValue registered("not registered"); |
- // |
- // web_ui()->CallJavascriptFunction("onServiceUpdate", service_name, info); |
} |
+ |
+void LocalDiscoveryUIHandler::RegisterMessages() { |
+ web_ui()->RegisterMessageCallback("start", base::Bind( |
+ &LocalDiscoveryUIHandler::HandleStart, |
+ base::Unretained(this))); |
+ web_ui()->RegisterMessageCallback("registerDevice", base::Bind( |
+ &LocalDiscoveryUIHandler::HandleRegisterDevice, |
+ base::Unretained(this))); |
+} |
+ |
+void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { |
+ service_discovery_client_ = new ServiceDiscoveryHostClient(); |
+ service_discovery_client_->Start(); |
+ privet_lister_.reset(new PrivetDeviceListerImpl( |
+ service_discovery_client_.get(), this)); |
+ privet_lister_->Start(); |
+ privet_lister_->DiscoverNewDevices(false); |
+} |
+ |
+void LocalDiscoveryUIHandler::HandleRegisterDevice( |
+ const base::ListValue* args) { |
+ std::string device_name; |
+ bool rv = args->GetString(0, &device_name); |
+ DCHECK(rv); |
+ currently_registering_device_ = device_name; |
+ |
+ domain_resolver_ = service_discovery_client_->CreateLocalDomainResolver( |
+ device_descriptions_[device_name].address.host(), |
+ net::ADDRESS_FAMILY_UNSPECIFIED, |
+ base::Bind(&LocalDiscoveryUIHandler::OnDomainResolved, |
+ base::Unretained(this))); |
+ |
+ domain_resolver_->Start(); |
+} |
+ |
+void LocalDiscoveryUIHandler::OnDomainResolved(bool success, |
+ const net::IPAddressNumber& address) { |
+ if (!success) { |
+ LogRegisterErrorToWeb("Resolution failed"); |
+ return; |
+ } |
+ |
+ if (device_descriptions_.count(currently_registering_device_) == 0) { |
+ LogRegisterErrorToWeb("Device no longer exists"); |
+ return; |
+ } |
+ |
+ Profile* profile = Profile::FromWebUI(web_ui()); |
+ SigninManagerBase* signin_manager = |
+ SigninManagerFactory::GetForProfileIfExists(profile); |
+ |
+ if (!signin_manager) { |
+ LogRegisterErrorToWeb("You must be signed in"); |
+ return; |
+ } |
+ |
+ std::string username = signin_manager->GetAuthenticatedUsername(); |
+ |
+ std::string address_str = net::IPAddressToString(address); |
+ int port = device_descriptions_[currently_registering_device_].address.port(); |
+ |
+ current_http_client_.reset(new PrivetHTTPClientImpl( |
+ net::HostPortPair(address_str, port), |
+ Profile::FromWebUI(web_ui())->GetRequestContext())); |
+ current_register_operation_ = |
+ current_http_client_->CreateRegisterOperation(username, this); |
+ current_register_operation_->Start(); |
+} |
+ |
+void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( |
+ const std::string& token, |
+ const GURL& url) { |
+ if (device_descriptions_.count(currently_registering_device_) == 0) { |
+ LogRegisterErrorToWeb("Device no longer exists"); |
+ return; |
+ } |
+ |
+ GURL automated_claim_url(base::StringPrintf( |
+ kPrivetAutomatedClaimURLFormat, |
+ device_descriptions_[currently_registering_device_].url.c_str(), |
+ token.c_str())); |
+ |
+ Profile* profile = Profile::FromWebUI(web_ui()); |
+ |
+ OAuth2TokenService* token_service = |
+ ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
+ |
+ if (!token_service) { |
+ LogRegisterErrorToWeb("Could not get token service"); |
+ return; |
+ } |
+ |
+ confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( |
+ profile->GetRequestContext(), |
+ token_service, |
+ automated_claim_url, |
+ base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
+ base::Unretained(this)))); |
+ |
+ confirm_api_call_flow_->Start(); |
+} |
+ |
+void LocalDiscoveryUIHandler::OnPrivetRegisterError( |
+ const std::string& action, |
+ PrivetRegisterOperation::FailureReason reason, |
+ int printer_http_code, |
+ const DictionaryValue* json) { |
+ // TODO(noamsml): Add detailed error message. |
+ LogRegisterErrorToWeb("Registration error"); |
+} |
+ |
+void LocalDiscoveryUIHandler::OnPrivetRegisterDone( |
+ const std::string& device_id) { |
+ current_register_operation_.reset(); |
+ current_http_client_.reset(); |
+ |
+ LogRegisterDoneToWeb(device_id); |
+} |
+ |
+void LocalDiscoveryUIHandler::OnConfirmDone( |
+ PrivetConfirmApiCallFlow::Status status) { |
+ if (status == PrivetConfirmApiCallFlow::SUCCESS) { |
+ DLOG(INFO) << "Confirm success."; |
+ confirm_api_call_flow_.reset(); |
+ current_register_operation_->CompleteRegistration(); |
+ } else { |
+ // TODO(noamsml): Add detailed error message. |
+ LogRegisterErrorToWeb("Confirm error"); |
+ } |
+} |
+ |
+void LocalDiscoveryUIHandler::DeviceChanged( |
+ bool added, |
+ const std::string& name, |
+ const DeviceDescription& description) { |
+ device_descriptions_[name] = description; |
+ |
+ base::StringValue service_name(name); |
+ base::DictionaryValue info; |
+ info.SetString("domain", description.address.host()); |
+ info.SetInteger("port", description.address.port()); |
+ std::string ip_addr_string; |
+ if (!description.ip_address.empty()) |
+ ip_addr_string = net::IPAddressToString(description.ip_address); |
+ |
+ info.SetString("ip", ip_addr_string); |
+ info.SetString("lastSeen", "unknown"); |
+ info.SetBoolean("registered", !description.id.empty()); |
+ |
+ web_ui()->CallJavascriptFunction("local_discovery.onServiceUpdate", |
+ service_name, info); |
+} |
+ |
+void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { |
+ device_descriptions_.erase(name); |
+ scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); |
+ base::StringValue name_value(name); |
+ |
+ web_ui()->CallJavascriptFunction("local_discovery.onServiceUpdate", |
+ name_value, *null_value); |
+} |
+ |
+void LocalDiscoveryUIHandler::LogRegisterErrorToWeb(const std::string& error) { |
+ base::StringValue error_value(error); |
+ web_ui()->CallJavascriptFunction("local_discovery.registrationFailed", |
+ error_value); |
+ DLOG(ERROR) << error; |
+} |
+ |
+void LocalDiscoveryUIHandler::LogRegisterDoneToWeb(const std::string& id) { |
+ base::StringValue id_value(id); |
+ web_ui()->CallJavascriptFunction("local_discovery.registrationSuccess", |
+ id_value); |
+ DLOG(INFO) << "Registered " << id; |
+} |
+ |
+} // namespace local_discovery |