17d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
27d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
37d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// found in the LICENSE file.
47d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
57d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#ifndef CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_FEEDBACK_SERVICE_H_
67d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#define CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_FEEDBACK_SERVICE_H_
77d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
87d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include <string>
97d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
107d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/basictypes.h"
117d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/ref_counted.h"
127d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/memory/scoped_vector.h"
137d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "base/threading/non_thread_safe.h"
147d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "chrome/browser/safe_browsing/download_protection_service.h"
157d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#include "content/public/browser/download_danger_type.h"
167d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
177d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace base {
187d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class TaskRunner;
197d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
207d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace content {
227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class DownloadItem;
237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
257d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace net {
267d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class URLRequestContextGetter;
277d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}
287d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
297d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)namespace safe_browsing {
307d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
317d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class DownloadFeedback;
327d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
337d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// Tracks active DownloadFeedback objects, provides interface for storing ping
347d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)// data for malicious downloads.
357d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)class DownloadFeedbackService : public base::NonThreadSafe {
367d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) public:
377d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DownloadFeedbackService(net::URLRequestContextGetter* request_context_getter,
387d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                          base::TaskRunner* file_task_runner);
397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ~DownloadFeedbackService();
407d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
417d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Stores the request and response ping data from the download check, if the
427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // check result and file size are eligible. This must be called after a
437d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // download has been flagged as malicious in order for the download to be
447d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // enabled for uploading.
457d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static void MaybeStorePingsForDownload(
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      DownloadProtectionService::DownloadCheckResult result,
477d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      content::DownloadItem* download,
487d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const std::string& ping,
497d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const std::string& response);
507d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
517d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Test if pings have been stored for |download|.
527d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static bool IsEnabledForDownload(const content::DownloadItem& download);
537d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
547d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Get the ping values stored in |download|. Returns false if no ping values
557d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // are present.
567d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static bool GetPingsForDownloadForTesting(
577d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const content::DownloadItem& download,
587d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      std::string* ping,
597d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      std::string* response);
607d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
617d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Records histogram for download feedback option shown to user.
620f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  static void RecordEligibleDownloadShown(
637d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      content::DownloadDangerType danger_type);
647d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
657d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Begin download feedback for |download|. The |download| will be deleted
667d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // when this function returns. This must only be called if
677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // IsEnabledForDownload is true for |download|.
687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void BeginFeedbackForDownload(content::DownloadItem* download);
697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) private:
717d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  static void BeginFeedbackOrDeleteFile(
727d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const scoped_refptr<base::TaskRunner>& file_task_runner,
737d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const base::WeakPtr<DownloadFeedbackService>& service,
747d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const std::string& ping_request,
757d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const std::string& ping_response,
767d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)      const base::FilePath& path);
777d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void StartPendingFeedback();
787d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void BeginFeedback(const std::string& ping_request,
797d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                     const std::string& ping_response,
807d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)                     const base::FilePath& path);
817d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  void FeedbackComplete();
827d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
837d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
847d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  scoped_refptr<base::TaskRunner> file_task_runner_;
857d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
867d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Currently active & pending uploads. The first item is active, remaining
877d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // items are pending.
887d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  ScopedVector<DownloadFeedback> active_feedback_;
897d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
907d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  base::WeakPtrFactory<DownloadFeedbackService> weak_ptr_factory_;
917d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
927d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(DownloadFeedbackService);
937d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)};
947d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)}  // namespace safe_browsing
957d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
967d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)#endif  // CHROME_BROWSER_SAFE_BROWSING_DOWNLOAD_FEEDBACK_SERVICE_H_
97