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.
4
5#ifndef CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_HISTORY_H_
6#define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_HISTORY_H_
7
8// This class gets redirect chain for urls from the history service.
9
10#include <string>
11#include <vector>
12
13#include "base/callback.h"
14#include "base/containers/hash_tables.h"
15#include "base/memory/linked_ptr.h"
16#include "base/memory/ref_counted.h"
17#include "base/sequenced_task_runner_helpers.h"
18#include "chrome/browser/history/history_service.h"
19#include "content/public/browser/browser_thread.h"
20#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.h"
22#include "net/base/completion_callback.h"
23
24namespace safe_browsing {
25typedef std::vector<GURL> RedirectChain;
26}
27
28class MalwareDetailsRedirectsCollector
29    : public base::RefCountedThreadSafe<
30          MalwareDetailsRedirectsCollector,
31          content::BrowserThread::DeleteOnUIThread>,
32      public content::NotificationObserver {
33 public:
34  explicit MalwareDetailsRedirectsCollector(Profile* profile);
35
36  // Collects urls' redirects chain information from the history service.
37  // We get access to history service via web_contents in UI thread.
38  // Notice the callback will be posted to the IO thread.
39  void StartHistoryCollection(const std::vector<GURL>& urls,
40                              const base::Closure& callback);
41
42  // Returns whether or not StartCacheCollection has been called.
43  bool HasStarted() const;
44
45  // Returns the redirect urls we get from history service
46  const std::vector<safe_browsing::RedirectChain>& GetCollectedUrls() const;
47
48  // content::NotificationObserver
49  virtual void Observe(int type,
50                       const content::NotificationSource& source,
51                       const content::NotificationDetails& details) OVERRIDE;
52
53 private:
54  friend struct content::BrowserThread::DeleteOnThread<
55      content::BrowserThread::UI>;
56  friend class base::DeleteHelper<MalwareDetailsRedirectsCollector>;
57
58  virtual ~MalwareDetailsRedirectsCollector();
59
60  void StartGetRedirects(const std::vector<GURL>& urls);
61  void GetRedirects(const GURL& url);
62  void OnGotQueryRedirectsTo(HistoryService::Handle handle,
63                             GURL url,
64                             bool success,
65                             history::RedirectList* redirect_list);
66
67  // Posts the callback method back to IO thread when redirects collecting
68  // is all done.
69  void AllDone();
70
71  Profile* profile_;
72  CancelableRequestConsumer request_consumer_;
73
74  // Method we call when we are done. The caller must be alive for the
75  // whole time, we are modifying its state (see above).
76  base::Closure callback_;
77
78  // Sets to true once StartHistoryCollection is called
79  bool has_started_;
80
81  // The urls we need to get redirects for
82  std::vector<GURL> urls_;
83  // The iterator goes over urls_
84  std::vector<GURL>::iterator urls_it_;
85  // The collected directs from history service
86  std::vector<safe_browsing::RedirectChain> redirects_urls_;
87
88  content::NotificationRegistrar registrar_;
89
90  DISALLOW_COPY_AND_ASSIGN(MalwareDetailsRedirectsCollector);
91};
92
93#endif  // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_HISTORY_H_
94