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
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_FEEDBACK_SERVICE_H_
6#define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_FEEDBACK_SERVICE_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/scoped_vector.h"
13#include "base/threading/non_thread_safe.h"
14#include "chrome/browser/safe_browsing/download_protection_service.h"
15#include "content/public/browser/download_danger_type.h"
16
17namespace base {
18class TaskRunner;
19}
20
21namespace content {
22class DownloadItem;
23}
24
25namespace net {
26class URLRequestContextGetter;
27}
28
29namespace safe_browsing {
30
31class DownloadFeedback;
32
33// Tracks active DownloadFeedback objects, provides interface for storing ping
34// data for malicious downloads.
35class DownloadFeedbackService : public base::NonThreadSafe {
36 public:
37  DownloadFeedbackService(net::URLRequestContextGetter* request_context_getter,
38                          base::TaskRunner* file_task_runner);
39  ~DownloadFeedbackService();
40
41  // Stores the request and response ping data from the download check, if the
42  // check result and file size are eligible. This must be called after a
43  // download has been flagged as malicious in order for the download to be
44  // enabled for uploading.
45  static void MaybeStorePingsForDownload(
46      DownloadProtectionService::DownloadCheckResult result,
47      content::DownloadItem* download,
48      const std::string& ping,
49      const std::string& response);
50
51  // Test if pings have been stored for |download|.
52  static bool IsEnabledForDownload(const content::DownloadItem& download);
53
54  // Get the ping values stored in |download|. Returns false if no ping values
55  // are present.
56  static bool GetPingsForDownloadForTesting(
57      const content::DownloadItem& download,
58      std::string* ping,
59      std::string* response);
60
61  // Records histogram for download feedback option shown to user.
62  static void RecordEligibleDownloadShown(
63      content::DownloadDangerType danger_type);
64
65  // Begin download feedback for |download|. The |download| will be deleted
66  // when this function returns. This must only be called if
67  // IsEnabledForDownload is true for |download|.
68  void BeginFeedbackForDownload(content::DownloadItem* download);
69
70 private:
71  static void BeginFeedbackOrDeleteFile(
72      const scoped_refptr<base::TaskRunner>& file_task_runner,
73      const base::WeakPtr<DownloadFeedbackService>& service,
74      const std::string& ping_request,
75      const std::string& ping_response,
76      const base::FilePath& path);
77  void StartPendingFeedback();
78  void BeginFeedback(const std::string& ping_request,
79                     const std::string& ping_response,
80                     const base::FilePath& path);
81  void FeedbackComplete();
82
83  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
84  scoped_refptr<base::TaskRunner> file_task_runner_;
85
86  // Currently active & pending uploads. The first item is active, remaining
87  // items are pending.
88  ScopedVector<DownloadFeedback> active_feedback_;
89
90  base::WeakPtrFactory<DownloadFeedbackService> weak_ptr_factory_;
91
92  DISALLOW_COPY_AND_ASSIGN(DownloadFeedbackService);
93};
94}  // namespace safe_browsing
95
96#endif  // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_FEEDBACK_SERVICE_H_
97