Home | History | Annotate | Download | only in safe_browsing
      1 // Copyright (c) 2011 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_H_
      6 #define CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_
      7 #pragma once
      8 
      9 // A class that encapsulates the detailed malware reports sent when
     10 // users opt-in to do so from the malware warning page.
     11 
     12 // An instance of this class is generated when a malware warning page
     13 // is shown (SafeBrowsingBlockingPage).
     14 
     15 #include <string>
     16 #include <vector>
     17 
     18 #include "base/hash_tables.h"
     19 #include "base/memory/linked_ptr.h"
     20 #include "base/memory/ref_counted.h"
     21 #include "base/memory/scoped_ptr.h"
     22 #include "chrome/browser/safe_browsing/report.pb.h"
     23 #include "chrome/browser/safe_browsing/safe_browsing_service.h"
     24 #include "content/browser/tab_contents/tab_contents_observer.h"
     25 #include "net/base/completion_callback.h"
     26 
     27 class TabContents;
     28 struct SafeBrowsingHostMsg_MalwareDOMDetails_Node;
     29 
     30 class MalwareDetailsCacheCollector;
     31 class MalwareDetailsFactory;
     32 
     33 namespace safe_browsing {
     34 // Maps a URL to its Resource.
     35 typedef base::hash_map<
     36   std::string,
     37   linked_ptr<safe_browsing::ClientMalwareReportRequest::Resource> > ResourceMap;
     38 }
     39 
     40 class MalwareDetails : public base::RefCountedThreadSafe<MalwareDetails>,
     41                        public TabContentsObserver {
     42  public:
     43   // Constructs a new MalwareDetails instance, using the factory.
     44   static MalwareDetails* NewMalwareDetails(
     45       SafeBrowsingService* sb_service,
     46       TabContents* tab_contents,
     47       const SafeBrowsingService::UnsafeResource& resource);
     48 
     49   // Makes the passed |factory| the factory used to instanciate
     50   // SafeBrowsingBlockingPage objects. Useful for tests.
     51   static void RegisterFactory(MalwareDetailsFactory* factory) {
     52     factory_ = factory;
     53   }
     54 
     55   // The SafeBrowsingBlockingPage calls this from the IO thread when
     56   // the user is leaving the blocking page and has opted-in to sending
     57   // the report. We start the cache collection, and when we are done,
     58   // we send the report.
     59   void FinishCollection();
     60 
     61   void OnCacheCollectionReady();
     62 
     63   // TabContentsObserver implementation.
     64   virtual bool OnMessageReceived(const IPC::Message& message);
     65 
     66  protected:
     67   friend class MalwareDetailsFactoryImpl;
     68 
     69   MalwareDetails(SafeBrowsingService* sb_service,
     70                  TabContents* tab_contents,
     71                  const SafeBrowsingService::UnsafeResource& resource);
     72 
     73   virtual ~MalwareDetails();
     74 
     75   // Called on the IO thread with the DOM details.
     76   virtual void AddDOMDetails(
     77       const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params);
     78 
     79   // The report protocol buffer.
     80   scoped_ptr<safe_browsing::ClientMalwareReportRequest> report_;
     81 
     82   // Used to get a pointer to the HTTP cache.
     83   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
     84 
     85  private:
     86   friend class base::RefCountedThreadSafe<MalwareDetails>;
     87 
     88   // Starts the collection of the report.
     89   void StartCollection();
     90 
     91   // Whether the url is "public" so we can add it to the report.
     92   bool IsPublicUrl(const GURL& url) const;
     93 
     94   // Finds an existing Resource for the given url, or creates a new
     95   // one if not found, and adds it to |resources_|. Returns the
     96   // found/created resource.
     97   safe_browsing::ClientMalwareReportRequest::Resource* FindOrCreateResource(
     98       const GURL& url);
     99 
    100   // Adds a Resource to resources_ with the given parent-child
    101   // relationship. |parent| and |tagname| can be empty, |children| can be NULL.
    102   void AddUrl(const GURL& url,
    103               const GURL& parent,
    104               const std::string& tagname,
    105               const std::vector<GURL>* children);
    106 
    107   // Message handler.
    108   void OnReceivedMalwareDOMDetails(
    109       const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params);
    110 
    111   scoped_refptr<SafeBrowsingService> sb_service_;
    112 
    113   const SafeBrowsingService::UnsafeResource resource_;
    114 
    115   // For every Url we collect we create a Resource message. We keep
    116   // them in a map so we can avoid duplicates.
    117   safe_browsing::ResourceMap resources_;
    118 
    119   // Result from the cache extractor.
    120   bool cache_result_;
    121 
    122   // The factory used to instanciate SafeBrowsingBlockingPage objects.
    123   // Usefull for tests, so they can provide their own implementation of
    124   // SafeBrowsingBlockingPage.
    125   static MalwareDetailsFactory* factory_;
    126 
    127   // Used to collect details from the HTTP Cache.
    128   scoped_refptr<MalwareDetailsCacheCollector> cache_collector_;
    129 
    130   FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, MalwareDOMDetails);
    131   FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCache);
    132   FRIEND_TEST_ALL_PREFIXES(MalwareDetailsTest, HTTPCacheNoEntries);
    133 
    134   DISALLOW_COPY_AND_ASSIGN(MalwareDetails);
    135 };
    136 
    137 // Factory for creating MalwareDetails.  Useful for tests.
    138 class MalwareDetailsFactory {
    139  public:
    140   virtual ~MalwareDetailsFactory() { }
    141 
    142   virtual MalwareDetails* CreateMalwareDetails(
    143       SafeBrowsingService* sb_service,
    144       TabContents* tab_contents,
    145       const SafeBrowsingService::UnsafeResource& unsafe_resource) = 0;
    146 };
    147 
    148 #endif  // CHROME_BROWSER_SAFE_BROWSING_MALWARE_DETAILS_H_
    149