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_CLIENT_SIDE_DETECTION_HOST_H_
6#define CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_HOST_H_
7#pragma once
8
9#include "base/basictypes.h"
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_callback_factory.h"
12#include "base/memory/scoped_ptr.h"
13#include "chrome/browser/safe_browsing/safe_browsing_service.h"
14#include "content/browser/tab_contents/tab_contents_observer.h"
15#include "googleurl/src/gurl.h"
16
17class TabContents;
18
19namespace safe_browsing {
20
21class ClientSideDetectionService;
22
23// This class is used to receive the IPC from the renderer which
24// notifies the browser that a URL was classified as phishing.  This
25// class relays this information to the client-side detection service
26// class which sends a ping to a server to validate the verdict.
27// TODO(noelutz): move all client-side detection IPCs to this class.
28class ClientSideDetectionHost : public TabContentsObserver {
29 public:
30  // The caller keeps ownership of the tab object and is responsible for
31  // ensuring that it stays valid for the entire lifetime of this object.
32  explicit ClientSideDetectionHost(TabContents* tab);
33  virtual ~ClientSideDetectionHost();
34
35  // From TabContentsObserver.
36  virtual bool OnMessageReceived(const IPC::Message& message);
37
38  // From TabContentsObserver.  If we navigate away we cancel all pending
39  // callbacks that could show an interstitial, and check to see whether
40  // we should classify the new URL.
41  virtual void DidNavigateMainFramePostCommit(
42      const NavigationController::LoadCommittedDetails& details,
43      const ViewHostMsg_FrameNavigate_Params& params);
44
45 private:
46  friend class ClientSideDetectionHostTest;
47  class ShouldClassifyUrlRequest;
48  friend class ShouldClassifyUrlRequest;
49
50  // Verdict is an encoded ClientPhishingRequest protocol message.
51  void OnDetectedPhishingSite(const std::string& verdict);
52
53  // Callback that is called when the server ping back is
54  // done. Display an interstitial if |is_phishing| is true.
55  // Otherwise, we do nothing.  Called in UI thread.
56  void MaybeShowPhishingWarning(GURL phishing_url, bool is_phishing);
57
58  // Used for testing.  This function does not take ownership of the service
59  // class.
60  void set_client_side_detection_service(ClientSideDetectionService* service);
61
62  // Used for testing.  This function does not take ownership of the service
63  // class.
64  void set_safe_browsing_service(SafeBrowsingService* service);
65
66  // This pointer may be NULL if client-side phishing detection is disabled.
67  ClientSideDetectionService* csd_service_;
68  // This pointer may be NULL if SafeBrowsing is disabled.
69  scoped_refptr<SafeBrowsingService> sb_service_;
70  // Keep a handle to the latest classification request so that we can cancel
71  // it if necessary.
72  scoped_refptr<ShouldClassifyUrlRequest> classification_request_;
73
74  base::ScopedCallbackFactory<ClientSideDetectionHost> cb_factory_;
75
76  DISALLOW_COPY_AND_ASSIGN(ClientSideDetectionHost);
77};
78
79}  // namespace safe_browsing
80
81#endif  // CHROME_BROWSER_SAFE_BROWSING_CLIENT_SIDE_DETECTION_HOST_H_
82