| 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 <set> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/message_loop/message_loop.h" |
| 8 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 9 #include "base/values.h" | 12 #include "base/values.h" |
| 10 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" | 13 #include "chrome/browser/local_discovery/privet_device_lister_impl.h" |
| 11 #include "chrome/browser/local_discovery/privet_http_impl.h" | 14 #include "chrome/browser/local_discovery/privet_http_impl.h" |
| 15 #include "chrome/browser/printing/cloud_print/cloud_print_url.h" |
| 12 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/browser/signin/profile_oauth2_token_service.h" | 17 #include "chrome/browser/signin/profile_oauth2_token_service.h" |
| 14 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" | 18 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h" |
| 15 #include "chrome/browser/signin/signin_manager.h" | 19 #include "chrome/browser/signin/signin_manager.h" |
| 16 #include "chrome/browser/signin/signin_manager_base.h" | 20 #include "chrome/browser/signin/signin_manager_base.h" |
| 17 #include "chrome/browser/signin/signin_manager_factory.h" | 21 #include "chrome/browser/signin/signin_manager_factory.h" |
| 22 #include "chrome/browser/ui/browser_finder.h" |
| 23 #include "chrome/browser/ui/browser_tabstrip.h" |
| 18 #include "content/public/browser/web_ui.h" | 24 #include "content/public/browser/web_ui.h" |
| 25 #include "content/public/common/page_transition_types.h" |
| 19 #include "net/base/host_port_pair.h" | 26 #include "net/base/host_port_pair.h" |
| 20 #include "net/base/net_util.h" | 27 #include "net/base/net_util.h" |
| 21 #include "net/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
| 22 | 29 |
| 23 namespace local_discovery { | 30 namespace local_discovery { |
| 24 | 31 |
| 25 namespace { | 32 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"; | 33 const char kPrivetAutomatedClaimURLFormat[] = "%s/confirm?token=%s"; |
| 34 const int kRegistrationAnnouncementTimeoutSeconds = 5; |
| 29 | 35 |
| 30 LocalDiscoveryUIHandler::Factory* g_factory = NULL; | 36 LocalDiscoveryUIHandler::Factory* g_factory = NULL; |
| 31 int g_num_visible = 0; | 37 int g_num_visible = 0; |
| 38 |
| 32 } // namespace | 39 } // namespace |
| 33 | 40 |
| 34 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) { | 41 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler() : is_visible_(false) { |
| 35 } | 42 } |
| 36 | 43 |
| 37 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler( | 44 LocalDiscoveryUIHandler::LocalDiscoveryUIHandler( |
| 38 scoped_ptr<PrivetDeviceLister> privet_lister) { | 45 scoped_ptr<PrivetDeviceLister> privet_lister) { |
| 39 privet_lister.swap(privet_lister_); | 46 privet_lister.swap(privet_lister_); |
| 40 } | 47 } |
| 41 | 48 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 67 void LocalDiscoveryUIHandler::RegisterMessages() { | 74 void LocalDiscoveryUIHandler::RegisterMessages() { |
| 68 web_ui()->RegisterMessageCallback("start", base::Bind( | 75 web_ui()->RegisterMessageCallback("start", base::Bind( |
| 69 &LocalDiscoveryUIHandler::HandleStart, | 76 &LocalDiscoveryUIHandler::HandleStart, |
| 70 base::Unretained(this))); | 77 base::Unretained(this))); |
| 71 web_ui()->RegisterMessageCallback("isVisible", base::Bind( | 78 web_ui()->RegisterMessageCallback("isVisible", base::Bind( |
| 72 &LocalDiscoveryUIHandler::HandleIsVisible, | 79 &LocalDiscoveryUIHandler::HandleIsVisible, |
| 73 base::Unretained(this))); | 80 base::Unretained(this))); |
| 74 web_ui()->RegisterMessageCallback("registerDevice", base::Bind( | 81 web_ui()->RegisterMessageCallback("registerDevice", base::Bind( |
| 75 &LocalDiscoveryUIHandler::HandleRegisterDevice, | 82 &LocalDiscoveryUIHandler::HandleRegisterDevice, |
| 76 base::Unretained(this))); | 83 base::Unretained(this))); |
| 77 web_ui()->RegisterMessageCallback("chooseUser", base::Bind( | |
| 78 &LocalDiscoveryUIHandler::HandleChooseUser, | |
| 79 base::Unretained(this))); | |
| 80 web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind( | 84 web_ui()->RegisterMessageCallback("cancelRegistration", base::Bind( |
| 81 &LocalDiscoveryUIHandler::HandleCancelRegistration, | 85 &LocalDiscoveryUIHandler::HandleCancelRegistration, |
| 82 base::Unretained(this))); | 86 base::Unretained(this))); |
| 87 web_ui()->RegisterMessageCallback("requestPrinterList", base::Bind( |
| 88 &LocalDiscoveryUIHandler::HandleRequestPrinterList, |
| 89 base::Unretained(this))); |
| 90 web_ui()->RegisterMessageCallback("openCloudPrintURL", base::Bind( |
| 91 &LocalDiscoveryUIHandler::HandleOpenCloudPrintURL, |
| 92 base::Unretained(this))); |
| 83 } | 93 } |
| 84 | 94 |
| 85 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { | 95 void LocalDiscoveryUIHandler::HandleStart(const base::ListValue* args) { |
| 96 Profile* profile = Profile::FromWebUI(web_ui()); |
| 97 |
| 86 // If privet_lister_ is already set, it is a mock used for tests or the result | 98 // If privet_lister_ is already set, it is a mock used for tests or the result |
| 87 // of a reload. | 99 // of a reload. |
| 88 if (!privet_lister_) { | 100 if (!privet_lister_) { |
| 89 service_discovery_client_ = ServiceDiscoveryHostClientFactory::GetClient(); | 101 service_discovery_client_ = ServiceDiscoveryHostClientFactory::GetClient(); |
| 90 privet_lister_.reset(new PrivetDeviceListerImpl( | 102 privet_lister_.reset(new PrivetDeviceListerImpl( |
| 91 service_discovery_client_.get(), this)); | 103 service_discovery_client_.get(), this)); |
| 92 privet_http_factory_.reset(new PrivetHTTPAsynchronousFactoryImpl( | 104 privet_http_factory_.reset(new PrivetHTTPAsynchronousFactoryImpl( |
| 93 service_discovery_client_.get(), | 105 service_discovery_client_.get(), |
| 94 Profile::FromWebUI(web_ui())->GetRequestContext())); | 106 profile->GetRequestContext())); |
| 95 } | 107 } |
| 96 | 108 |
| 97 privet_lister_->Start(); | 109 privet_lister_->Start(); |
| 98 privet_lister_->DiscoverNewDevices(false); | 110 privet_lister_->DiscoverNewDevices(false); |
| 99 } | 111 } |
| 100 | 112 |
| 101 void LocalDiscoveryUIHandler::HandleRegisterDevice( | |
| 102 const base::ListValue* args) { | |
| 103 std::string device_name; | |
| 104 | |
| 105 bool rv = args->GetString(0, &device_name); | |
| 106 DCHECK(rv); | |
| 107 | |
| 108 current_register_device_ = device_name; | |
| 109 | |
| 110 cloud_print_account_manager_.reset(new CloudPrintAccountManager( | |
| 111 Profile::FromWebUI(web_ui())->GetRequestContext(), | |
| 112 GetCloudPrintBaseUrl(device_name), | |
| 113 0 /* Get XSRF token for primary user */, | |
| 114 base::Bind(&LocalDiscoveryUIHandler::OnCloudPrintAccountsResolved, | |
| 115 base::Unretained(this)))); | |
| 116 | |
| 117 cloud_print_account_manager_->Start(); | |
| 118 } | |
| 119 | |
| 120 void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue* args) { | 113 void LocalDiscoveryUIHandler::HandleIsVisible(const base::ListValue* args) { |
| 121 bool is_visible = false; | 114 bool is_visible = false; |
| 122 bool rv = args->GetBoolean(0, &is_visible); | 115 bool rv = args->GetBoolean(0, &is_visible); |
| 123 DCHECK(rv); | 116 DCHECK(rv); |
| 124 SetIsVisible(is_visible); | 117 SetIsVisible(is_visible); |
| 125 } | 118 } |
| 126 | 119 |
| 127 void LocalDiscoveryUIHandler::HandleChooseUser(const base::ListValue* args) { | 120 void LocalDiscoveryUIHandler::HandleRegisterDevice( |
| 128 std::string user; | 121 const base::ListValue* args) { |
| 122 std::string device; |
| 129 | 123 |
| 130 bool rv = args->GetInteger(0, ¤t_register_user_index_); | 124 bool rv = args->GetString(0, &device); |
| 131 DCHECK(rv); | |
| 132 rv = args->GetString(1, &user); | |
| 133 DCHECK(rv); | 125 DCHECK(rv); |
| 134 | 126 |
| 135 privet_resolution_ = privet_http_factory_->CreatePrivetHTTP( | 127 privet_resolution_ = privet_http_factory_->CreatePrivetHTTP( |
| 136 current_register_device_, | 128 device, |
| 137 device_descriptions_[current_register_device_].address, | 129 device_descriptions_[device].address, |
| 138 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP, | 130 base::Bind(&LocalDiscoveryUIHandler::StartRegisterHTTP, |
| 139 base::Unretained(this), user)); | 131 base::Unretained(this))); |
| 140 privet_resolution_->Start(); | 132 privet_resolution_->Start(); |
| 141 } | 133 } |
| 142 | 134 |
| 143 void LocalDiscoveryUIHandler::HandleCancelRegistration( | 135 void LocalDiscoveryUIHandler::HandleCancelRegistration( |
| 144 const base::ListValue* args) { | 136 const base::ListValue* args) { |
| 145 ResetCurrentRegistration(); | 137 ResetCurrentRegistration(); |
| 146 } | 138 } |
| 147 | 139 |
| 140 void LocalDiscoveryUIHandler::HandleRequestPrinterList( |
| 141 const base::ListValue* args) { |
| 142 Profile* profile = Profile::FromWebUI(web_ui()); |
| 143 OAuth2TokenService* token_service = |
| 144 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 145 |
| 146 cloud_print_printer_list_.reset(new CloudPrintPrinterList( |
| 147 profile->GetRequestContext(), |
| 148 GetCloudPrintBaseUrl(), |
| 149 token_service, |
| 150 this)); |
| 151 cloud_print_printer_list_->Start(); |
| 152 } |
| 153 |
| 154 void LocalDiscoveryUIHandler::HandleOpenCloudPrintURL( |
| 155 const base::ListValue* args) { |
| 156 std::string url; |
| 157 bool rv = args->GetString(0, &url); |
| 158 DCHECK(rv); |
| 159 |
| 160 GURL url_full(GetCloudPrintBaseUrl() + url); |
| 161 |
| 162 Browser* browser = chrome::FindBrowserWithWebContents( |
| 163 web_ui()->GetWebContents()); |
| 164 DCHECK(browser); |
| 165 |
| 166 chrome::AddSelectedTabWithURL(browser, |
| 167 url_full, |
| 168 content::PAGE_TRANSITION_FROM_API); |
| 169 } |
| 170 |
| 148 void LocalDiscoveryUIHandler::StartRegisterHTTP( | 171 void LocalDiscoveryUIHandler::StartRegisterHTTP( |
| 149 const std::string& user, | |
| 150 scoped_ptr<PrivetHTTPClient> http_client) { | 172 scoped_ptr<PrivetHTTPClient> http_client) { |
| 151 current_http_client_.swap(http_client); | 173 current_http_client_.swap(http_client); |
| 152 | 174 |
| 175 std::string user = GetSyncAccount(); |
| 176 |
| 153 if (!current_http_client_) { | 177 if (!current_http_client_) { |
| 154 SendRegisterError(); | 178 SendRegisterError(); |
| 155 return; | 179 return; |
| 156 } | 180 } |
| 157 | 181 |
| 158 current_register_operation_ = | 182 current_register_operation_ = |
| 159 current_http_client_->CreateRegisterOperation(user, this); | 183 current_http_client_->CreateRegisterOperation(user, this); |
| 160 current_register_operation_->Start(); | 184 current_register_operation_->Start(); |
| 161 } | 185 } |
| 162 | 186 |
| 163 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( | 187 void LocalDiscoveryUIHandler::OnPrivetRegisterClaimToken( |
| 164 PrivetRegisterOperation* operation, | 188 PrivetRegisterOperation* operation, |
| 165 const std::string& token, | 189 const std::string& token, |
| 166 const GURL& url) { | 190 const GURL& url) { |
| 167 web_ui()->CallJavascriptFunction( | 191 web_ui()->CallJavascriptFunction( |
| 168 "local_discovery.registrationConfirmedOnPrinter"); | 192 "local_discovery.onRegistrationConfirmedOnPrinter"); |
| 169 if (device_descriptions_.count(current_http_client_->GetName()) == 0) { | 193 if (device_descriptions_.count(current_http_client_->GetName()) == 0) { |
| 170 SendRegisterError(); | 194 SendRegisterError(); |
| 171 return; | 195 return; |
| 172 } | 196 } |
| 173 | 197 |
| 174 std::string base_url = GetCloudPrintBaseUrl(current_http_client_->GetName()); | 198 std::string base_url = GetCloudPrintBaseUrl(); |
| 175 | 199 |
| 176 GURL automated_claim_url(base::StringPrintf( | 200 GURL automated_claim_url(base::StringPrintf( |
| 177 kPrivetAutomatedClaimURLFormat, | 201 kPrivetAutomatedClaimURLFormat, |
| 178 base_url.c_str(), | 202 base_url.c_str(), |
| 179 token.c_str())); | 203 token.c_str())); |
| 180 | 204 |
| 181 Profile* profile = Profile::FromWebUI(web_ui()); | 205 Profile* profile = Profile::FromWebUI(web_ui()); |
| 182 | 206 |
| 183 if (current_register_user_index_ == kAccountIndexUseOAuth2) { | 207 OAuth2TokenService* token_service = |
| 184 OAuth2TokenService* token_service = | 208 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); |
| 185 ProfileOAuth2TokenServiceFactory::GetForProfile(profile); | |
| 186 | 209 |
| 187 if (!token_service) { | 210 if (!token_service) { |
| 188 SendRegisterError(); | 211 SendRegisterError(); |
| 189 return; | 212 return; |
| 190 } | 213 } |
| 191 | 214 |
| 192 confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( | 215 confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( |
| 193 profile->GetRequestContext(), | 216 profile->GetRequestContext(), |
| 194 token_service, | 217 token_service, |
| 195 automated_claim_url, | 218 automated_claim_url, |
| 196 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, | 219 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, |
| 197 base::Unretained(this)))); | 220 base::Unretained(this)))); |
| 198 confirm_api_call_flow_->Start(); | 221 confirm_api_call_flow_->Start(); |
| 199 } else { | |
| 200 if (current_register_user_index_ == 0) { | |
| 201 StartCookieConfirmFlow(current_register_user_index_, | |
| 202 xsrf_token_for_primary_user_, | |
| 203 automated_claim_url); | |
| 204 } else { | |
| 205 cloud_print_account_manager_.reset(new CloudPrintAccountManager( | |
| 206 Profile::FromWebUI(web_ui())->GetRequestContext(), | |
| 207 base_url, | |
| 208 current_register_user_index_, | |
| 209 base::Bind(&LocalDiscoveryUIHandler::OnXSRFTokenForSecondaryAccount, | |
| 210 base::Unretained(this), automated_claim_url))); | |
| 211 | |
| 212 cloud_print_account_manager_->Start(); | |
| 213 } | |
| 214 } | |
| 215 } | 222 } |
| 216 | 223 |
| 217 void LocalDiscoveryUIHandler::OnPrivetRegisterError( | 224 void LocalDiscoveryUIHandler::OnPrivetRegisterError( |
| 218 PrivetRegisterOperation* operation, | 225 PrivetRegisterOperation* operation, |
| 219 const std::string& action, | 226 const std::string& action, |
| 220 PrivetRegisterOperation::FailureReason reason, | 227 PrivetRegisterOperation::FailureReason reason, |
| 221 int printer_http_code, | 228 int printer_http_code, |
| 222 const DictionaryValue* json) { | 229 const DictionaryValue* json) { |
| 223 // TODO(noamsml): Add detailed error message. | 230 // TODO(noamsml): Add detailed error message. |
| 224 SendRegisterError(); | 231 SendRegisterError(); |
| 225 } | 232 } |
| 226 | 233 |
| 227 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( | 234 void LocalDiscoveryUIHandler::OnPrivetRegisterDone( |
| 228 PrivetRegisterOperation* operation, | 235 PrivetRegisterOperation* operation, |
| 229 const std::string& device_id) { | 236 const std::string& device_id) { |
| 237 std::string name = operation->GetHTTPClient()->GetName(); |
| 238 |
| 230 current_register_operation_.reset(); | 239 current_register_operation_.reset(); |
| 231 current_http_client_.reset(); | 240 current_http_client_.reset(); |
| 232 | 241 |
| 233 SendRegisterDone(); | 242 DeviceDescriptionMap::iterator found = device_descriptions_.find(name); |
| 243 |
| 244 if (found == device_descriptions_.end() || found->second.id.empty()) { |
| 245 new_register_device_ = name; |
| 246 registration_announce_timeout_.Reset(base::Bind( |
| 247 &LocalDiscoveryUIHandler::OnAnnouncementTimeoutReached, |
| 248 base::Unretained(this))); |
| 249 |
| 250 base::MessageLoop::current()->PostDelayedTask( |
| 251 FROM_HERE, |
| 252 registration_announce_timeout_.callback(), |
| 253 base::TimeDelta::FromSeconds(kRegistrationAnnouncementTimeoutSeconds)); |
| 254 } |
| 255 } |
| 256 |
| 257 void LocalDiscoveryUIHandler::OnAnnouncementTimeoutReached() { |
| 258 new_register_device_.clear(); |
| 259 registration_announce_timeout_.Cancel(); |
| 260 |
| 261 SendRegisterError(); |
| 234 } | 262 } |
| 235 | 263 |
| 236 void LocalDiscoveryUIHandler::OnConfirmDone( | 264 void LocalDiscoveryUIHandler::OnConfirmDone( |
| 237 CloudPrintBaseApiFlow::Status status) { | 265 CloudPrintBaseApiFlow::Status status) { |
| 238 if (status == CloudPrintBaseApiFlow::SUCCESS) { | 266 if (status == CloudPrintBaseApiFlow::SUCCESS) { |
| 239 confirm_api_call_flow_.reset(); | 267 confirm_api_call_flow_.reset(); |
| 240 current_register_operation_->CompleteRegistration(); | 268 current_register_operation_->CompleteRegistration(); |
| 241 } else { | 269 } else { |
| 242 // TODO(noamsml): Add detailed error message. | 270 // TODO(noamsml): Add detailed error message. |
| 243 SendRegisterError(); | 271 SendRegisterError(); |
| 244 } | 272 } |
| 245 } | 273 } |
| 246 | 274 |
| 247 void LocalDiscoveryUIHandler::DeviceChanged( | 275 void LocalDiscoveryUIHandler::DeviceChanged( |
| 248 bool added, | 276 bool added, |
| 249 const std::string& name, | 277 const std::string& name, |
| 250 const DeviceDescription& description) { | 278 const DeviceDescription& description) { |
| 251 device_descriptions_[name] = description; | 279 device_descriptions_[name] = description; |
| 252 | 280 |
| 253 base::DictionaryValue info; | 281 base::DictionaryValue info; |
| 254 | 282 |
| 255 base::StringValue service_name(name); | 283 base::StringValue service_name(name); |
| 284 scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); |
| 256 | 285 |
| 257 info.SetString("service_name", name); | 286 if (description.id.empty()) { |
| 258 info.SetString("human_readable_name", description.name); | 287 info.SetString("service_name", name); |
| 259 info.SetString("description", description.description); | 288 info.SetString("human_readable_name", description.name); |
| 260 info.SetBoolean("registered", !description.id.empty()); | 289 info.SetString("description", description.description); |
| 261 info.SetBoolean("is_mine", !description.id.empty()); | |
| 262 | 290 |
| 263 web_ui()->CallJavascriptFunction("local_discovery.onDeviceUpdate", | 291 web_ui()->CallJavascriptFunction( |
| 264 service_name, info); | 292 "local_discovery.onUnregisteredDeviceUpdate", |
| 293 service_name, info); |
| 294 } else { |
| 295 web_ui()->CallJavascriptFunction( |
| 296 "local_discovery.onUnregisteredDeviceUpdate", |
| 297 service_name, *null_value); |
| 298 |
| 299 if (name == new_register_device_) { |
| 300 new_register_device_.clear(); |
| 301 SendRegisterDone(); |
| 302 } |
| 303 } |
| 265 } | 304 } |
| 266 | 305 |
| 267 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { | 306 void LocalDiscoveryUIHandler::DeviceRemoved(const std::string& name) { |
| 268 device_descriptions_.erase(name); | 307 device_descriptions_.erase(name); |
| 269 scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); | 308 scoped_ptr<base::Value> null_value(base::Value::CreateNullValue()); |
| 270 base::StringValue name_value(name); | 309 base::StringValue name_value(name); |
| 271 | 310 |
| 272 web_ui()->CallJavascriptFunction("local_discovery.onDeviceUpdate", | 311 web_ui()->CallJavascriptFunction("local_discovery.onUnregisteredDeviceUpdate", |
| 273 name_value, *null_value); | 312 name_value, *null_value); |
| 274 } | 313 } |
| 275 | 314 |
| 315 void LocalDiscoveryUIHandler::OnCloudPrintPrinterListReady() { |
| 316 base::ListValue printer_object_list; |
| 317 std::set<std::string> local_ids; |
| 318 |
| 319 for (DeviceDescriptionMap::iterator i = device_descriptions_.begin(); |
| 320 i != device_descriptions_.end(); |
| 321 i++) { |
| 322 std::string device_id = i->second.id; |
| 323 if (!device_id.empty()) { |
| 324 const CloudPrintPrinterList::PrinterDetails* details = |
| 325 cloud_print_printer_list_->GetDetailsFor(device_id); |
| 326 |
| 327 if (details) { |
| 328 local_ids.insert(device_id); |
| 329 printer_object_list.Append(CreatePrinterInfo(*details).release()); |
| 330 } |
| 331 } |
| 332 } |
| 333 |
| 334 for (CloudPrintPrinterList::iterator i = cloud_print_printer_list_->begin(); |
| 335 i != cloud_print_printer_list_->end(); i++) { |
| 336 if (local_ids.count(i->id) == 0) { |
| 337 printer_object_list.Append(CreatePrinterInfo(*i).release()); |
| 338 } |
| 339 } |
| 340 |
| 341 web_ui()->CallJavascriptFunction( |
| 342 "local_discovery.onCloudDeviceListAvailable", printer_object_list); |
| 343 } |
| 344 |
| 345 void LocalDiscoveryUIHandler::OnCloudPrintPrinterListUnavailable() { |
| 346 } |
| 347 |
| 348 |
| 276 void LocalDiscoveryUIHandler::SendRegisterError() { | 349 void LocalDiscoveryUIHandler::SendRegisterError() { |
| 277 web_ui()->CallJavascriptFunction("local_discovery.registrationFailed"); | 350 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationFailed"); |
| 278 } | 351 } |
| 279 | 352 |
| 280 void LocalDiscoveryUIHandler::SendRegisterDone() { | 353 void LocalDiscoveryUIHandler::SendRegisterDone() { |
| 281 web_ui()->CallJavascriptFunction("local_discovery.registrationSuccess"); | 354 web_ui()->CallJavascriptFunction("local_discovery.onRegistrationSuccess"); |
| 282 } | |
| 283 | |
| 284 void LocalDiscoveryUIHandler::OnCloudPrintAccountsResolved( | |
| 285 const std::vector<std::string>& accounts, | |
| 286 const std::string& xsrf_token) { | |
| 287 xsrf_token_for_primary_user_ = xsrf_token; | |
| 288 | |
| 289 std::string sync_account = GetSyncAccount(); | |
| 290 base::ListValue accounts_annotated_list; | |
| 291 | |
| 292 if (!sync_account.empty()) { | |
| 293 scoped_ptr<base::ListValue> account_annotated(new base::ListValue); | |
| 294 account_annotated->AppendInteger(kAccountIndexUseOAuth2); | |
| 295 account_annotated->AppendString(sync_account); | |
| 296 accounts_annotated_list.Append(account_annotated.release()); | |
| 297 } | |
| 298 | |
| 299 int account_index = 0; | |
| 300 for (std::vector<std::string>::const_iterator i = accounts.begin(); | |
| 301 i != accounts.end(); i++, account_index++) { | |
| 302 if (*i == sync_account) continue; | |
| 303 | |
| 304 scoped_ptr<base::ListValue> account_annotated(new base::ListValue); | |
| 305 account_annotated->AppendInteger(account_index); | |
| 306 account_annotated->AppendString(*i); | |
| 307 accounts_annotated_list.Append(account_annotated.release()); | |
| 308 } | |
| 309 | |
| 310 base::StringValue device_name(current_register_device_); | |
| 311 web_ui()->CallJavascriptFunction("local_discovery.requestUser", | |
| 312 accounts_annotated_list, device_name); | |
| 313 } | |
| 314 | |
| 315 void LocalDiscoveryUIHandler::OnXSRFTokenForSecondaryAccount( | |
| 316 const GURL& automated_claim_url, | |
| 317 const std::vector<std::string>& accounts, | |
| 318 const std::string& xsrf_token) { | |
| 319 StartCookieConfirmFlow(current_register_user_index_, | |
| 320 xsrf_token, | |
| 321 automated_claim_url); | |
| 322 } | 355 } |
| 323 | 356 |
| 324 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { | 357 void LocalDiscoveryUIHandler::SetIsVisible(bool visible) { |
| 325 if (visible != is_visible_) { | 358 if (visible != is_visible_) { |
| 326 g_num_visible += visible ? 1 : -1; | 359 g_num_visible += visible ? 1 : -1; |
| 327 is_visible_ = visible; | 360 is_visible_ = visible; |
| 328 } | 361 } |
| 329 } | 362 } |
| 330 | 363 |
| 331 std::string LocalDiscoveryUIHandler::GetSyncAccount() { | 364 std::string LocalDiscoveryUIHandler::GetSyncAccount() { |
| 332 Profile* profile = Profile::FromWebUI(web_ui()); | 365 Profile* profile = Profile::FromWebUI(web_ui()); |
| 333 SigninManagerBase* signin_manager = | 366 SigninManagerBase* signin_manager = |
| 334 SigninManagerFactory::GetForProfileIfExists(profile); | 367 SigninManagerFactory::GetForProfileIfExists(profile); |
| 335 | 368 |
| 336 if (!signin_manager) { | 369 if (!signin_manager) { |
| 337 return ""; | 370 return ""; |
| 338 } | 371 } |
| 339 | 372 |
| 340 return signin_manager->GetAuthenticatedUsername(); | 373 return signin_manager->GetAuthenticatedUsername(); |
| 341 } | 374 } |
| 342 | 375 |
| 343 const std::string& LocalDiscoveryUIHandler::GetCloudPrintBaseUrl( | 376 std::string LocalDiscoveryUIHandler::GetCloudPrintBaseUrl() { |
| 344 const std::string& device_name) { | 377 CloudPrintURL cloud_print_url(Profile::FromWebUI(web_ui())); |
| 345 return device_descriptions_[device_name].url; | |
| 346 } | |
| 347 | 378 |
| 348 void LocalDiscoveryUIHandler::StartCookieConfirmFlow( | 379 return cloud_print_url.GetCloudPrintServiceURL().spec(); |
| 349 int user_index, | |
| 350 const std::string& xsrf_token, | |
| 351 const GURL& automated_claim_url) { | |
| 352 confirm_api_call_flow_.reset(new PrivetConfirmApiCallFlow( | |
| 353 Profile::FromWebUI(web_ui())->GetRequestContext(), | |
| 354 user_index, | |
| 355 xsrf_token, | |
| 356 automated_claim_url, | |
| 357 base::Bind(&LocalDiscoveryUIHandler::OnConfirmDone, | |
| 358 base::Unretained(this)))); | |
| 359 | |
| 360 confirm_api_call_flow_->Start(); | |
| 361 } | 380 } |
| 362 | 381 |
| 363 // TODO(noamsml): Create master object for registration flow. | 382 // TODO(noamsml): Create master object for registration flow. |
| 364 void LocalDiscoveryUIHandler::ResetCurrentRegistration() { | 383 void LocalDiscoveryUIHandler::ResetCurrentRegistration() { |
| 365 current_register_device_.clear(); | |
| 366 if (current_register_operation_.get()) { | 384 if (current_register_operation_.get()) { |
| 367 current_register_operation_->Cancel(); | 385 current_register_operation_->Cancel(); |
| 368 current_register_operation_.reset(); | 386 current_register_operation_.reset(); |
| 369 } | 387 } |
| 370 | 388 |
| 371 confirm_api_call_flow_.reset(); | 389 confirm_api_call_flow_.reset(); |
| 372 privet_resolution_.reset(); | 390 privet_resolution_.reset(); |
| 373 cloud_print_account_manager_.reset(); | |
| 374 xsrf_token_for_primary_user_.clear(); | |
| 375 current_register_user_index_ = 0; | |
| 376 current_http_client_.reset(); | 391 current_http_client_.reset(); |
| 377 } | 392 } |
| 378 | 393 |
| 394 scoped_ptr<base::DictionaryValue> LocalDiscoveryUIHandler::CreatePrinterInfo( |
| 395 const CloudPrintPrinterList::PrinterDetails& description) { |
| 396 scoped_ptr<base::DictionaryValue> return_value(new base::DictionaryValue); |
| 397 |
| 398 return_value->SetString("id", description.id); |
| 399 return_value->SetString("display_name", description.display_name); |
| 400 return_value->SetString("description", description.description); |
| 401 |
| 402 return return_value.Pass(); |
| 403 } |
| 404 |
| 379 } // namespace local_discovery | 405 } // namespace local_discovery |
| OLD | NEW |