Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / safe_browsing / safe_browsing_tab_observer.cc
blob00df92ed09b3964283186d4c6d0b0b2f268b3bbf
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.
5 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h"
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"
18 #if defined(SAFE_BROWSING_CSD)
19 #include "chrome/browser/safe_browsing/client_side_detection_host.h"
20 #endif
22 DEFINE_WEB_CONTENTS_USER_DATA_KEY(safe_browsing::SafeBrowsingTabObserver);
24 namespace safe_browsing {
26 #if !defined(SAFE_BROWSING_CSD)
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.
30 // TODO(shess): This is weird, why not just guard the instance variable?
31 class ClientSideDetectionHost { };
32 #endif
34 SafeBrowsingTabObserver::SafeBrowsingTabObserver(
35 content::WebContents* web_contents) : web_contents_(web_contents) {
36 #if defined(SAFE_BROWSING_CSD)
37 Profile* profile =
38 Profile::FromBrowserContext(web_contents->GetBrowserContext());
39 PrefService* prefs = profile->GetPrefs();
40 if (prefs) {
41 pref_change_registrar_.Init(prefs);
42 pref_change_registrar_.Add(
43 prefs::kSafeBrowsingEnabled,
44 base::Bind(&SafeBrowsingTabObserver::UpdateSafebrowsingDetectionHost,
45 base::Unretained(this)));
47 if (prefs->GetBoolean(prefs::kSafeBrowsingEnabled) &&
48 g_browser_process->safe_browsing_detection_service()) {
49 safebrowsing_detection_host_.reset(
50 ClientSideDetectionHost::Create(web_contents));
53 #endif
56 SafeBrowsingTabObserver::~SafeBrowsingTabObserver() {
59 ////////////////////////////////////////////////////////////////////////////////
60 // Internal helpers
62 void SafeBrowsingTabObserver::UpdateSafebrowsingDetectionHost() {
63 #if defined(SAFE_BROWSING_CSD)
64 Profile* profile =
65 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
66 PrefService* prefs = profile->GetPrefs();
67 bool safe_browsing = prefs->GetBoolean(prefs::kSafeBrowsingEnabled);
68 if (safe_browsing &&
69 g_browser_process->safe_browsing_detection_service()) {
70 if (!safebrowsing_detection_host_.get()) {
71 safebrowsing_detection_host_.reset(
72 ClientSideDetectionHost::Create(web_contents_));
74 } else {
75 safebrowsing_detection_host_.reset();
78 content::RenderViewHost* rvh = web_contents_->GetRenderViewHost();
79 rvh->Send(new ChromeViewMsg_SetClientSidePhishingDetection(
80 rvh->GetRoutingID(), safe_browsing));
81 #endif
84 bool SafeBrowsingTabObserver::DidPageReceiveSafeBrowsingMatch() const {
85 #if defined(SAFE_BROWSING_CSD)
86 return safebrowsing_detection_host_ &&
87 safebrowsing_detection_host_->DidPageReceiveSafeBrowsingMatch();
88 #else
89 return false;
90 #endif
93 } // namespace safe_browsing