13345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick// Copyright (c) 2010 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_ 303345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick#pragma once 31c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 32c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <map> 33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include <vector> 34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/safe_browsing/safe_browsing_service.h" 36dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/browser/tab_contents/interstitial_page.h" 37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "googleurl/src/gurl.h" 38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 39c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass DictionaryValue; 40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass MessageLoop; 41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SafeBrowsingBlockingPageFactory; 4221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsenclass MalwareDetails; 43c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass TabContents; 44c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 45c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SafeBrowsingBlockingPage : public InterstitialPage { 46c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 4721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen typedef std::vector<SafeBrowsingService::UnsafeResource> UnsafeResourceList; 4821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen typedef std::map<TabContents*, UnsafeResourceList> UnsafeResourceMap; 4921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual ~SafeBrowsingBlockingPage(); 51c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Shows a blocking page warning the user about phishing/malware for a 53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // specific resource. 54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // You can call this method several times, if an interstitial is already 55c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // showing, the new one will be queued and displayed if the user decides 56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // to proceed on the currently showing interstitial. 57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static void ShowBlockingPage( 58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch SafeBrowsingService* service, 59c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const SafeBrowsingService::UnsafeResource& resource); 60c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 61c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Makes the passed |factory| the factory used to instanciate 62c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // SafeBrowsingBlockingPage objects. Usefull for tests. 63c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static void RegisterFactory(SafeBrowsingBlockingPageFactory* factory) { 64c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch factory_ = factory; 65c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch } 66c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 67c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // InterstitialPage method: 68c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual std::string GetHTMLContents(); 693f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen virtual void SetReportingPreference(bool report); 70c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void Proceed(); 71c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void DontProceed(); 72c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 73c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch protected: 74201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch friend class SafeBrowsingBlockingPageTest; 75201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch 76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // InterstitialPage method: 77c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual void CommandReceived(const std::string& command); 78c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 79c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Don't instanciate this class directly, use ShowBlockingPage instead. 80c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch SafeBrowsingBlockingPage(SafeBrowsingService* service, 81c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch TabContents* tab_contents, 82c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const UnsafeResourceList& unsafe_resources); 83c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 84c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch enum BlockingPageEvent { 86c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch SHOW, 87c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch PROCEED, 88c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DONT_PROCEED, 89c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch }; 90c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 91c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Fills the passed dictionary with the strings passed to JS Template when 92c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // creating the HTML. 93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void PopulateMultipleThreatStringDictionary(DictionaryValue* strings); 94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void PopulateMalwareStringDictionary(DictionaryValue* strings); 95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void PopulatePhishingStringDictionary(DictionaryValue* strings); 96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 97c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // A helper method used by the Populate methods above used to populate common 98c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // fields. 99c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void PopulateStringDictionary(DictionaryValue* strings, 1003f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen const string16& title, 1013f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen const string16& headline, 1023f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen const string16& description1, 1033f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen const string16& description2, 1043f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen const string16& description3); 105c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 106c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Records a user action for this interstitial, using the form 107c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // SBInterstitial[Phishing|Malware|Multiple][Show|Proceed|DontProceed]. 108c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch void RecordUserAction(BlockingPageEvent event); 109c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 1103f50c38dc070f4bb515c1b64450dae14f316474eKristian Monsen // Checks if we should even show the malware details option. For example, we 11121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // don't show it in incognito mode. 11221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen bool CanShowMalwareDetailsOption(); 11321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 11421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // Called when the insterstitial is going away. If there is a 11521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // pending malware details object, we look at the user's 11621d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // preferences, and if the option to send malware details is 11721d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // enabled, the report is scheduled to be sent on the |sb_service_|. 11821d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen void FinishMalwareDetails(); 11921d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // A list of SafeBrowsingService::UnsafeResource for a tab that the user 121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // should be warned about. They are queued when displaying more than one 122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // interstitial at a time. 123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static UnsafeResourceMap* GetUnsafeResourcesMap(); 124c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 125c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Notifies the SafeBrowsingService on the IO thread whether to proceed or not 126c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // for the |resources|. 127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static void NotifySafeBrowsingService(SafeBrowsingService* sb_service, 128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const UnsafeResourceList& resources, 129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool proceed); 130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 131c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Returns true if the passed |unsafe_resources| is for the main page. 132c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static bool IsMainPage(const UnsafeResourceList& unsafe_resources); 133c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 134c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch private: 135c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch friend class SafeBrowsingBlockingPageFactoryImpl; 136c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 137c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // For reporting back user actions. 138c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch SafeBrowsingService* sb_service_; 139c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch MessageLoop* report_loop_; 140c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 141c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Whether the flagged resource is the main page (or a sub-resource is false). 142c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch bool is_main_frame_; 143c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 144c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // The index of a navigation entry that should be removed when DontProceed() 145c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // is invoked, -1 if not entry should be removed. 146c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch int navigation_entry_index_to_remove_; 147c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 148c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // The list of unsafe resources this page is warning about. 149c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch UnsafeResourceList unsafe_resources_; 150c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 15121d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // A MalwareDetails object that we start generating when the 15221d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // blocking page is shown. The object will be sent when the warning 15321d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen // is gone (if the user enables the feature). 15421d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen scoped_refptr<MalwareDetails> malware_details_; 15521d179b334e59e9a3bfcaed4c4430bef1bc5759dKristian Monsen 156c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // The factory used to instanciate SafeBrowsingBlockingPage objects. 157c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // Usefull for tests, so they can provide their own implementation of 158c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch // SafeBrowsingBlockingPage. 159c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch static SafeBrowsingBlockingPageFactory* factory_; 160c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 161c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch DISALLOW_COPY_AND_ASSIGN(SafeBrowsingBlockingPage); 162c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 163c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 164c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Factory for creating SafeBrowsingBlockingPage. Useful for tests. 165c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochclass SafeBrowsingBlockingPageFactory { 166c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch public: 1673345a6884c488ff3a535c2c9acdd33d74b37e311Iain Merrick virtual ~SafeBrowsingBlockingPageFactory() { } 168c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 169c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch virtual SafeBrowsingBlockingPage* CreateSafeBrowsingPage( 170c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch SafeBrowsingService* service, 171c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch TabContents* tab_contents, 172c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch const SafeBrowsingBlockingPage::UnsafeResourceList& unsafe_resources) = 0; 173c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}; 174c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch 175c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#endif // CHROME_BROWSER_SAFE_BROWSING_SAFE_BROWSING_BLOCKING_PAGE_H_ 176