safe_browsing_blocking_page.h revision c407dc5cd9bdc5668497f21b26b09d988ab439de
1c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Classes for managing the SafeBrowsing interstitial pages.
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// When a user is about to visit a page the SafeBrowsing system has deemed to
8c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// be malicious, either as malware or a phishing page, we show an interstitial
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// page with some options (go back, continue) to give the user a chance to avoid
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// the harmful page.
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The SafeBrowsingBlockingPage is created by the SafeBrowsingService on the UI
13c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// thread when we've determined that a page is malicious. The operation of the
14c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// blocking page occurs on the UI thread, where it waits for the user to make a
15c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// decision about what to do: either go back or continue on.
16c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The blocking page forwards the result of the user's choice back to the
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// SafeBrowsingService so that we can cancel the request for the new page, or
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// or allow it to continue.
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch//
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// A web page may contain several resources flagged as malware/phishing.  This
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// results into more than one interstitial being shown.  On the first unsafe
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// resource received we show an interstitial.  Any subsequent unsafe resource
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// notifications while the first interstitial is showing is queued.  If the user
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// decides to proceed in the first interstitial, we display all queued unsafe
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// resources in a new interstitial.
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#ifndef CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_BLOCKING_PAGE_H_
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#define CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_BLOCKING_PAGE_H_
30c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <map>
32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <vector>
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/tab_contents/interstitial_page.h"
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/safe_browsing/safe_browsing_service.h"
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "googleurl/src/gurl.h"
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass DictionaryValue;
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass MessageLoop;
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass NavigationController;
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SafeBrowsingBlockingPageFactory;
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass TabContents;
43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SafeBrowsingBlockingPage : public InterstitialPage {
45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual ~SafeBrowsingBlockingPage();
47c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Shows a blocking page warning the user about phishing/malware for a
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // specific resource.
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // You can call this method several times, if an interstitial is already
51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // showing, the new one will be queued and displayed if the user decides
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // to proceed on the currently showing interstitial.
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void ShowBlockingPage(
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      SafeBrowsingService* service,
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const SafeBrowsingService::UnsafeResource& resource);
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Makes the passed |factory| the factory used to instanciate
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // SafeBrowsingBlockingPage objects. Usefull for tests.
59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void RegisterFactory(SafeBrowsingBlockingPageFactory* factory) {
60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    factory_ = factory;
61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  }
62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // InterstitialPage method:
64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual std::string GetHTMLContents();
65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void Proceed();
66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void DontProceed();
67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::vector<SafeBrowsingService::UnsafeResource> UnsafeResourceList;
69c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected:
71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // InterstitialPage method:
72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual void CommandReceived(const std::string& command);
73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
74c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Don't instanciate this class directly, use ShowBlockingPage instead.
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SafeBrowsingBlockingPage(SafeBrowsingService* service,
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           TabContents* tab_contents,
77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                           const UnsafeResourceList& unsafe_resources);
78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  enum BlockingPageEvent {
81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    SHOW,
82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    PROCEED,
83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    DONT_PROCEED,
84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  };
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Fills the passed dictionary with the strings passed to JS Template when
87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // creating the HTML.
88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void PopulateMultipleThreatStringDictionary(DictionaryValue* strings);
89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void PopulateMalwareStringDictionary(DictionaryValue* strings);
90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void PopulatePhishingStringDictionary(DictionaryValue* strings);
91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // A helper method used by the Populate methods above used to populate common
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // fields.
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void PopulateStringDictionary(DictionaryValue* strings,
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                const std::wstring& title,
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                const std::wstring& headline,
97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                const std::wstring& description1,
98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                const std::wstring& description2,
99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                const std::wstring& description3);
100c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
101c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Records a user action for this interstitial, using the form
102c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // SBInterstitial[Phishing|Malware|Multiple][Show|Proceed|DontProceed].
103c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  void RecordUserAction(BlockingPageEvent event);
104c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // A list of SafeBrowsingService::UnsafeResource for a tab that the user
106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // should be warned about.  They are queued when displaying more than one
107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // interstitial at a time.
108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  typedef std::map<TabContents*, UnsafeResourceList> UnsafeResourceMap;
109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static UnsafeResourceMap* GetUnsafeResourcesMap();
110c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
111c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Notifies the SafeBrowsingService on the IO thread whether to proceed or not
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // for the |resources|.
113c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static void NotifySafeBrowsingService(SafeBrowsingService* sb_service,
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                        const UnsafeResourceList& resources,
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                                        bool proceed);
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Returns true if the passed |unsafe_resources| is for the main page.
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static bool IsMainPage(const UnsafeResourceList& unsafe_resources);
119c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private:
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  friend class SafeBrowsingBlockingPageFactoryImpl;
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // For reporting back user actions.
124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  SafeBrowsingService* sb_service_;
125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  MessageLoop* report_loop_;
126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Whether the flagged resource is the main page (or a sub-resource is false).
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool is_main_frame_;
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The index of a navigation entry that should be removed when DontProceed()
131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // is invoked, -1 if not entry should be removed.
132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  int navigation_entry_index_to_remove_;
133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The list of unsafe resources this page is warning about.
135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  UnsafeResourceList unsafe_resources_;
136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // The factory used to instanciate SafeBrowsingBlockingPage objects.
138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // Usefull for tests, so they can provide their own implementation of
139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  // SafeBrowsingBlockingPage.
140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  static SafeBrowsingBlockingPageFactory* factory_;
141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPage);
143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Factory for creating SafeBrowsingBlockingPage.  Useful for tests.
146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SafeBrowsingBlockingPageFactory {
147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public:
148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ~SafeBrowsingBlockingPageFactory() { }
149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage(
151c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      SafeBrowsingService* service,
152c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      TabContents* tab_contents,
153c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) = 0;
154c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch};
155c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif  // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_BLOCKING_PAGE_H_
157