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#ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_INCIDENT_REPORT_UPLOADER_IMPL_H_
6#define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_INCIDENT_REPORT_UPLOADER_IMPL_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/macros.h"
12#include "base/memory/scoped_ptr.h"
13#include "base/time/time.h"
14#include "chrome/browser/safe_browsing/incident_reporting/incident_report_uploader.h"
15#include "net/url_request/url_fetcher_delegate.h"
16#include "url/gurl.h"
17
18namespace net {
19class URLFetcher;
20class URLRequestContextGetter;
21}  // namespace net
22
23namespace safe_browsing {
24
25class ClientIncidentReport;
26
27// An uploader of incident reports. A report is issued via the UploadReport
28// method. The upload can be cancelled by deleting the returned instance. The
29// instance is no longer usable after the delegate is notified, and may safely
30// be destroyed by the delegate.
31class IncidentReportUploaderImpl : public IncidentReportUploader,
32                                   public net::URLFetcherDelegate {
33 public:
34  // The id associated with the URLFetcher, for use by tests.
35  static const int kTestUrlFetcherId;
36
37  virtual ~IncidentReportUploaderImpl();
38
39  // Uploads a report with a caller-provided URL context. |callback| will be run
40  // when the upload is complete. Returns NULL if |report| cannot be serialized
41  // for transmission, in which case the delegate is not notified.
42  static scoped_ptr<IncidentReportUploader> UploadReport(
43      const OnResultCallback& callback,
44      const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
45      const ClientIncidentReport& report);
46
47 private:
48  IncidentReportUploaderImpl(
49      const OnResultCallback& callback,
50      const scoped_refptr<net::URLRequestContextGetter>& request_context_getter,
51      const std::string& post_data);
52
53  // Returns the URL to which incident reports are to be sent.
54  static GURL GetIncidentReportUrl();
55
56  // net::URLFetcherDelegate methods.
57  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
58
59  // The underlying URL fetcher. The instance is alive from construction through
60  // OnURLFetchComplete.
61  scoped_ptr<net::URLFetcher> url_fetcher_;
62
63  // The time at which the upload was initiated.
64  base::TimeTicks time_begin_;
65
66  DISALLOW_COPY_AND_ASSIGN(IncidentReportUploaderImpl);
67};
68
69}  // namespace safe_browsing
70
71#endif  // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_INCIDENT_REPORT_UPLOADER_IMPL_H_
72