OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h" | 5 #include "chrome/browser/safe_browsing/safe_browsing_tab_observer.h" |
6 | 6 |
7 #include "chrome/browser/browser_process.h" | 7 #include "chrome/browser/browser_process.h" |
8 #include "chrome/browser/prefs/pref_service.h" | 8 #include "chrome/browser/prefs/pref_service.h" |
9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
10 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 10 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
11 #include "chrome/common/chrome_notification_types.h" | 11 #include "chrome/common/chrome_notification_types.h" |
12 #include "chrome/common/pref_names.h" | 12 #include "chrome/common/pref_names.h" |
13 #include "chrome/common/render_messages.h" | 13 #include "chrome/common/render_messages.h" |
14 #include "content/public/browser/notification_details.h" | 14 #include "content/public/browser/notification_details.h" |
15 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
16 #include "content/public/browser/render_view_host.h" | 16 #include "content/public/browser/render_view_host.h" |
17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
18 | 18 |
19 #if defined(ENABLE_SAFE_BROWSING) | 19 #if defined(ENABLE_SAFE_BROWSING) |
20 #include "chrome/browser/safe_browsing/client_side_detection_host.h" | 20 #include "chrome/browser/safe_browsing/client_side_detection_host.h" |
21 #endif | 21 #endif |
22 | 22 |
23 namespace safe_browsing { | 23 namespace safe_browsing { |
24 | 24 |
25 #if !defined(ENABLE_SAFE_BROWSING) | 25 #if !defined(ENABLE_SAFE_BROWSING) |
26 // Provide a dummy implementation so that scoped_ptr<ClientSideDetectionHost> | 26 // Provide a dummy implementation so that scoped_ptr<ClientSideDetectionHost> |
27 // has a concrete destructor to call. This is necessary because it is used | 27 // has a concrete destructor to call. This is necessary because it is used |
28 // as a member of SafeBrowsingTabObserver, even if it only ever contains NULL. | 28 // as a member of SafeBrowsingTabObserver, even if it only ever contains NULL. |
29 class ClientSideDetectionHost { }; | 29 class ClientSideDetectionHost { }; |
30 #endif | 30 #endif |
31 | 31 |
32 SafeBrowsingTabObserver::SafeBrowsingTabObserver( | 32 SafeBrowsingTabObserver::SafeBrowsingTabObserver( |
33 TabContentsWrapper* wrapper) : wrapper_(wrapper) { | 33 TabContents* tab_contents) : tab_contents_(tab_contents) { |
34 #if defined(ENABLE_SAFE_BROWSING) | 34 #if defined(ENABLE_SAFE_BROWSING) |
35 PrefService* prefs = wrapper_->profile()->GetPrefs(); | 35 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
36 if (prefs) { | 36 if (prefs) { |
37 pref_change_registrar_.Init(prefs); | 37 pref_change_registrar_.Init(prefs); |
38 pref_change_registrar_.Add(prefs::kSafeBrowsingEnabled, this); | 38 pref_change_registrar_.Add(prefs::kSafeBrowsingEnabled, this); |
39 | 39 |
40 if (prefs->GetBoolean(prefs::kSafeBrowsingEnabled) && | 40 if (prefs->GetBoolean(prefs::kSafeBrowsingEnabled) && |
41 g_browser_process->safe_browsing_detection_service()) { | 41 g_browser_process->safe_browsing_detection_service()) { |
42 safebrowsing_detection_host_.reset( | 42 safebrowsing_detection_host_.reset( |
43 ClientSideDetectionHost::Create(wrapper_->web_contents())); | 43 ClientSideDetectionHost::Create(tab_contents_->web_contents())); |
44 } | 44 } |
45 } | 45 } |
46 #endif | 46 #endif |
47 } | 47 } |
48 | 48 |
49 SafeBrowsingTabObserver::~SafeBrowsingTabObserver() { | 49 SafeBrowsingTabObserver::~SafeBrowsingTabObserver() { |
50 } | 50 } |
51 | 51 |
52 //////////////////////////////////////////////////////////////////////////////// | 52 //////////////////////////////////////////////////////////////////////////////// |
53 // content::NotificationObserver overrides | 53 // content::NotificationObserver overrides |
54 | 54 |
55 void SafeBrowsingTabObserver::Observe( | 55 void SafeBrowsingTabObserver::Observe( |
56 int type, | 56 int type, |
57 const content::NotificationSource& source, | 57 const content::NotificationSource& source, |
58 const content::NotificationDetails& details) { | 58 const content::NotificationDetails& details) { |
59 switch (type) { | 59 switch (type) { |
60 case chrome::NOTIFICATION_PREF_CHANGED: { | 60 case chrome::NOTIFICATION_PREF_CHANGED: { |
61 std::string* pref_name = content::Details<std::string>(details).ptr(); | 61 std::string* pref_name = content::Details<std::string>(details).ptr(); |
62 DCHECK(content::Source<PrefService>(source).ptr() == | 62 DCHECK(content::Source<PrefService>(source).ptr() == |
63 wrapper_->profile()->GetPrefs()); | 63 tab_contents_->profile()->GetPrefs()); |
64 if (*pref_name == prefs::kSafeBrowsingEnabled) { | 64 if (*pref_name == prefs::kSafeBrowsingEnabled) { |
65 UpdateSafebrowsingDetectionHost(); | 65 UpdateSafebrowsingDetectionHost(); |
66 } else { | 66 } else { |
67 NOTREACHED() << "unexpected pref change notification" << *pref_name; | 67 NOTREACHED() << "unexpected pref change notification" << *pref_name; |
68 } | 68 } |
69 break; | 69 break; |
70 } | 70 } |
71 default: | 71 default: |
72 NOTREACHED(); | 72 NOTREACHED(); |
73 } | 73 } |
74 } | 74 } |
75 | 75 |
76 //////////////////////////////////////////////////////////////////////////////// | 76 //////////////////////////////////////////////////////////////////////////////// |
77 // Internal helpers | 77 // Internal helpers |
78 | 78 |
79 void SafeBrowsingTabObserver::UpdateSafebrowsingDetectionHost() { | 79 void SafeBrowsingTabObserver::UpdateSafebrowsingDetectionHost() { |
80 #if defined(ENABLE_SAFE_BROWSING) | 80 #if defined(ENABLE_SAFE_BROWSING) |
81 PrefService* prefs = wrapper_->profile()->GetPrefs(); | 81 PrefService* prefs = tab_contents_->profile()->GetPrefs(); |
82 bool safe_browsing = prefs->GetBoolean(prefs::kSafeBrowsingEnabled); | 82 bool safe_browsing = prefs->GetBoolean(prefs::kSafeBrowsingEnabled); |
83 if (safe_browsing && | 83 if (safe_browsing && |
84 g_browser_process->safe_browsing_detection_service()) { | 84 g_browser_process->safe_browsing_detection_service()) { |
85 if (!safebrowsing_detection_host_.get()) { | 85 if (!safebrowsing_detection_host_.get()) { |
86 safebrowsing_detection_host_.reset( | 86 safebrowsing_detection_host_.reset( |
87 ClientSideDetectionHost::Create(wrapper_->web_contents())); | 87 ClientSideDetectionHost::Create(tab_contents_->web_contents())); |
88 } | 88 } |
89 } else { | 89 } else { |
90 safebrowsing_detection_host_.reset(); | 90 safebrowsing_detection_host_.reset(); |
91 } | 91 } |
92 | 92 |
93 content::RenderViewHost* rvh = wrapper_->web_contents()->GetRenderViewHost(); | 93 content::RenderViewHost* rvh = |
| 94 tab_contents_->web_contents()->GetRenderViewHost(); |
94 rvh->Send(new ChromeViewMsg_SetClientSidePhishingDetection( | 95 rvh->Send(new ChromeViewMsg_SetClientSidePhishingDetection( |
95 rvh->GetRoutingID(), safe_browsing)); | 96 rvh->GetRoutingID(), safe_browsing)); |
96 #endif | 97 #endif |
97 } | 98 } |
98 | 99 |
99 } // namespace safe_browsing | 100 } // namespace safe_browsing |
OLD | NEW |