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#include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
6
7#include "base/prefs/pref_service.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/chrome_notification_types.h"
10#include "chrome/browser/profiles/profile.h"
11#include "chrome/common/pref_names.h"
12#include "chrome/common/render_messages.h"
13#include "content/public/browser/notification_details.h"
14#include "content/public/browser/notification_source.h"
15#include "content/public/browser/render_view_host.h"
16#include "content/public/browser/web_contents.h"
17
18#if defined(FULL_SAFE_BROWSING)
19#include "chrome/browser/safe_browsing/client_side_detection_host.h"
20#endif
21
22DEFINE_WEB_CONTENTS_USER_DATA_KEY(safe_browsing::SafeBrowsingTabObserver);
23
24namespace safe_browsing {
25
26#if !defined(FULL_SAFE_BROWSING)
27// Provide a dummy implementation so that scoped_ptr<ClientSideDetectionHost>
28// has a concrete destructor to call. This is necessary because it is used
29// as a member of SafeBrowsingTabObserver, even if it only ever contains NULL.
30class ClientSideDetectionHost { };
31#endif
32
33SafeBrowsingTabObserver::SafeBrowsingTabObserver(
34    content::WebContents* web_contents) : web_contents_(web_contents) {
35#if defined(FULL_SAFE_BROWSING)
36  Profile* profile =
37      Profile::FromBrowserContext(web_contents->GetBrowserContext());
38  PrefService* prefs = profile->GetPrefs();
39  if (prefs) {
40    pref_change_registrar_.Init(prefs);
41    pref_change_registrar_.Add(
42        prefs::kSafeBrowsingEnabled,
43        base::Bind(&SafeBrowsingTabObserver::UpdateSafebrowsingDetectionHost,
44                   base::Unretained(this)));
45
46    if (prefs->GetBoolean(prefs::kSafeBrowsingEnabled) &&
47        g_browser_process->safe_browsing_detection_service()) {
48      safebrowsing_detection_host_.reset(
49          ClientSideDetectionHost::Create(web_contents));
50    }
51  }
52#endif
53}
54
55SafeBrowsingTabObserver::~SafeBrowsingTabObserver() {
56}
57
58////////////////////////////////////////////////////////////////////////////////
59// Internal helpers
60
61void SafeBrowsingTabObserver::UpdateSafebrowsingDetectionHost() {
62#if defined(FULL_SAFE_BROWSING)
63  Profile* profile =
64      Profile::FromBrowserContext(web_contents_->GetBrowserContext());
65  PrefService* prefs = profile->GetPrefs();
66  bool safe_browsing = prefs->GetBoolean(prefs::kSafeBrowsingEnabled);
67  if (safe_browsing &&
68      g_browser_process->safe_browsing_detection_service()) {
69    if (!safebrowsing_detection_host_.get()) {
70      safebrowsing_detection_host_.reset(
71          ClientSideDetectionHost::Create(web_contents_));
72    }
73  } else {
74    safebrowsing_detection_host_.reset();
75  }
76
77  content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
78  rvh->Send(new ChromeViewMsg_SetClientSidePhishingDetection(
79      rvh->GetRoutingID(), safe_browsing));
80#endif
81}
82
83}  // namespace safe_browsing
84