| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/sync/internal_api/syncapi_server_connection_manager.h" | |
| 6 | |
| 7 #include "chrome/browser/sync/internal_api/http_post_provider_factory.h" | |
| 8 #include "chrome/browser/sync/internal_api/http_post_provider_interface.h" | |
| 9 #include "net/base/net_errors.h" | |
| 10 #include "net/http/http_status_code.h" | |
| 11 | |
| 12 using browser_sync::HttpResponse; | |
| 13 | |
| 14 namespace sync_api { | |
| 15 | |
| 16 SyncAPIBridgedConnection::SyncAPIBridgedConnection( | |
| 17 browser_sync::ServerConnectionManager* scm, | |
| 18 HttpPostProviderFactory* factory) | |
| 19 : Connection(scm), factory_(factory) { | |
| 20 post_provider_ = factory_->Create(); | |
| 21 } | |
| 22 | |
| 23 SyncAPIBridgedConnection::~SyncAPIBridgedConnection() { | |
| 24 DCHECK(post_provider_); | |
| 25 factory_->Destroy(post_provider_); | |
| 26 post_provider_ = NULL; | |
| 27 } | |
| 28 | |
| 29 bool SyncAPIBridgedConnection::Init(const char* path, | |
| 30 const std::string& auth_token, | |
| 31 const std::string& payload, | |
| 32 HttpResponse* response) { | |
| 33 std::string sync_server; | |
| 34 int sync_server_port = 0; | |
| 35 bool use_ssl = false; | |
| 36 GetServerParams(&sync_server, &sync_server_port, &use_ssl); | |
| 37 std::string connection_url = MakeConnectionURL(sync_server, path, use_ssl); | |
| 38 | |
| 39 HttpPostProviderInterface* http = post_provider_; | |
| 40 http->SetUserAgent(scm_->user_agent().c_str()); | |
| 41 http->SetURL(connection_url.c_str(), sync_server_port); | |
| 42 | |
| 43 if (!auth_token.empty()) { | |
| 44 const std::string& headers = | |
| 45 "Authorization: GoogleLogin auth=" + auth_token; | |
| 46 http->SetExtraRequestHeaders(headers.c_str()); | |
| 47 } | |
| 48 | |
| 49 // Must be octet-stream, or the payload may be parsed for a cookie. | |
| 50 http->SetPostPayload("application/octet-stream", payload.length(), | |
| 51 payload.data()); | |
| 52 | |
| 53 // Issue the POST, blocking until it finishes. | |
| 54 int error_code = 0; | |
| 55 int response_code = 0; | |
| 56 if (!http->MakeSynchronousPost(&error_code, &response_code)) { | |
| 57 DVLOG(1) << "Http POST failed, error returns: " << error_code; | |
| 58 response->server_status = error_code == net::ERR_ABORTED ? | |
| 59 HttpResponse::CONNECTION_UNAVAILABLE : HttpResponse::IO_ERROR; | |
| 60 return false; | |
| 61 } | |
| 62 | |
| 63 // We got a server response, copy over response codes and content. | |
| 64 response->response_code = response_code; | |
| 65 response->content_length = | |
| 66 static_cast<int64>(http->GetResponseContentLength()); | |
| 67 response->payload_length = | |
| 68 static_cast<int64>(http->GetResponseContentLength()); | |
| 69 if (response->response_code < 400) | |
| 70 response->server_status = HttpResponse::SERVER_CONNECTION_OK; | |
| 71 else if (response->response_code == net::HTTP_UNAUTHORIZED) | |
| 72 response->server_status = HttpResponse::SYNC_AUTH_ERROR; | |
| 73 else | |
| 74 response->server_status = HttpResponse::SYNC_SERVER_ERROR; | |
| 75 | |
| 76 response->update_client_auth_header = | |
| 77 http->GetResponseHeaderValue("Update-Client-Auth"); | |
| 78 | |
| 79 // Write the content into our buffer. | |
| 80 buffer_.assign(http->GetResponseContent(), http->GetResponseContentLength()); | |
| 81 return true; | |
| 82 } | |
| 83 | |
| 84 void SyncAPIBridgedConnection::Abort() { | |
| 85 DCHECK(post_provider_); | |
| 86 post_provider_->Abort(); | |
| 87 } | |
| 88 | |
| 89 SyncAPIServerConnectionManager::SyncAPIServerConnectionManager( | |
| 90 const std::string& server, | |
| 91 int port, | |
| 92 bool use_ssl, | |
| 93 const std::string& client_version, | |
| 94 HttpPostProviderFactory* factory) | |
| 95 : ServerConnectionManager(server, port, use_ssl, client_version), | |
| 96 post_provider_factory_(factory) { | |
| 97 DCHECK(post_provider_factory_.get()); | |
| 98 } | |
| 99 | |
| 100 SyncAPIServerConnectionManager::~SyncAPIServerConnectionManager() {} | |
| 101 | |
| 102 browser_sync::ServerConnectionManager::Connection* | |
| 103 SyncAPIServerConnectionManager::MakeConnection() { | |
| 104 return new SyncAPIBridgedConnection(this, post_provider_factory_.get()); | |
| 105 } | |
| 106 | |
| 107 } // namespace sync_api | |
| OLD | NEW |