Bluetooth: fix issues with discovery API
[chromium-blink-merge.git] / sync / internal_api / syncapi_server_connection_manager.cc
blob91900b24f239f2f4fd2998be9ec7a8b5721bcf8f
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.
5 #include "sync/internal_api/syncapi_server_connection_manager.h"
7 #include "net/base/net_errors.h"
8 #include "net/http/http_status_code.h"
9 #include "sync/internal_api/public/http_post_provider_factory.h"
10 #include "sync/internal_api/public/http_post_provider_interface.h"
12 namespace syncer {
14 SyncAPIBridgedConnection::SyncAPIBridgedConnection(
15 ServerConnectionManager* scm,
16 HttpPostProviderFactory* factory)
17 : Connection(scm), factory_(factory) {
18 post_provider_ = factory_->Create();
21 SyncAPIBridgedConnection::~SyncAPIBridgedConnection() {
22 DCHECK(post_provider_);
23 factory_->Destroy(post_provider_);
24 post_provider_ = NULL;
27 bool SyncAPIBridgedConnection::Init(const char* path,
28 const std::string& auth_token,
29 const std::string& payload,
30 HttpResponse* response) {
31 std::string sync_server;
32 int sync_server_port = 0;
33 bool use_ssl = false;
34 GetServerParams(&sync_server, &sync_server_port, &use_ssl);
35 std::string connection_url = MakeConnectionURL(sync_server, path, use_ssl);
37 HttpPostProviderInterface* http = post_provider_;
38 http->SetURL(connection_url.c_str(), sync_server_port);
40 if (!auth_token.empty()) {
41 const std::string& headers =
42 "Authorization: GoogleLogin auth=" + auth_token;
43 http->SetExtraRequestHeaders(headers.c_str());
46 // Must be octet-stream, or the payload may be parsed for a cookie.
47 http->SetPostPayload("application/octet-stream", payload.length(),
48 payload.data());
50 // Issue the POST, blocking until it finishes.
51 int error_code = 0;
52 int response_code = 0;
53 if (!http->MakeSynchronousPost(&error_code, &response_code)) {
54 response->server_status = error_code == net::ERR_ABORTED ?
55 HttpResponse::CONNECTION_UNAVAILABLE : HttpResponse::IO_ERROR;
56 LOG_IF(WARNING, error_code != net::ERR_ABORTED)
57 << "Net error during post: " << error_code;
58 return false;
61 // We got a server response, copy over response codes and content.
62 response->response_code = response_code;
63 response->content_length =
64 static_cast<int64>(http->GetResponseContentLength());
65 response->payload_length =
66 static_cast<int64>(http->GetResponseContentLength());
67 if (response->response_code < 400)
68 response->server_status = HttpResponse::SERVER_CONNECTION_OK;
69 else if (response->response_code == net::HTTP_UNAUTHORIZED)
70 response->server_status = HttpResponse::SYNC_AUTH_ERROR;
71 else
72 response->server_status = HttpResponse::SYNC_SERVER_ERROR;
74 response->update_client_auth_header =
75 http->GetResponseHeaderValue("Update-Client-Auth");
77 // Write the content into our buffer.
78 buffer_.assign(http->GetResponseContent(), http->GetResponseContentLength());
79 return true;
82 void SyncAPIBridgedConnection::Abort() {
83 DCHECK(post_provider_);
84 post_provider_->Abort();
87 SyncAPIServerConnectionManager::SyncAPIServerConnectionManager(
88 const std::string& server,
89 int port,
90 bool use_ssl,
91 HttpPostProviderFactory* factory)
92 : ServerConnectionManager(server, port, use_ssl),
93 post_provider_factory_(factory) {
94 DCHECK(post_provider_factory_.get());
97 SyncAPIServerConnectionManager::~SyncAPIServerConnectionManager() {}
99 ServerConnectionManager::Connection*
100 SyncAPIServerConnectionManager::MakeConnection() {
101 return new SyncAPIBridgedConnection(this, post_provider_factory_.get());
104 } // namespace syncer