Home | History | Annotate | Download | only in net
      1 // Copyright 2014 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 "components/metrics/net/net_metrics_log_uploader.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "net/base/load_flags.h"
      9 #include "net/url_request/url_fetcher.h"
     10 #include "url/gurl.h"
     11 
     12 namespace metrics {
     13 
     14 NetMetricsLogUploader::NetMetricsLogUploader(
     15     net::URLRequestContextGetter* request_context_getter,
     16     const std::string& server_url,
     17     const std::string& mime_type,
     18     const base::Callback<void(int)>& on_upload_complete)
     19     : MetricsLogUploader(server_url, mime_type, on_upload_complete),
     20       request_context_getter_(request_context_getter) {
     21 }
     22 
     23 NetMetricsLogUploader::~NetMetricsLogUploader() {
     24 }
     25 
     26 bool NetMetricsLogUploader::UploadLog(const std::string& compressed_log_data,
     27                                       const std::string& log_hash) {
     28   current_fetch_.reset(
     29       net::URLFetcher::Create(GURL(server_url_), net::URLFetcher::POST, this));
     30   current_fetch_->SetRequestContext(request_context_getter_);
     31   current_fetch_->SetUploadData(mime_type_, compressed_log_data);
     32 
     33   // Tell the server that we're uploading gzipped protobufs.
     34   current_fetch_->SetExtraRequestHeaders("content-encoding: gzip");
     35 
     36   DCHECK(!log_hash.empty());
     37   current_fetch_->AddExtraRequestHeader("X-Chrome-UMA-Log-SHA1: " + log_hash);
     38 
     39   // We already drop cookies server-side, but we might as well strip them out
     40   // client-side as well.
     41   current_fetch_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES |
     42                                net::LOAD_DO_NOT_SEND_COOKIES);
     43   current_fetch_->Start();
     44   return true;
     45 }
     46 
     47 void NetMetricsLogUploader::OnURLFetchComplete(const net::URLFetcher* source) {
     48   // We're not allowed to re-use the existing |URLFetcher|s, so free them here.
     49   // Note however that |source| is aliased to the fetcher, so we should be
     50   // careful not to delete it too early.
     51   DCHECK_EQ(current_fetch_.get(), source);
     52 
     53   int response_code = source->GetResponseCode();
     54   if (response_code == net::URLFetcher::RESPONSE_CODE_INVALID)
     55     response_code = -1;
     56   on_upload_complete_.Run(response_code);
     57   current_fetch_.reset();
     58 }
     59 
     60 }  // namespace metrics
     61