OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 // Implementation of the MalwareDetails class. | 5 // Implementation of the MalwareDetails class. |
6 | 6 |
7 #include "chrome/browser/safe_browsing/malware_details.h" | 7 #include "chrome/browser/safe_browsing/malware_details.h" |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
11 #include "chrome/browser/net/chrome_url_request_context.h" | 11 #include "chrome/browser/net/chrome_url_request_context.h" |
12 #include "chrome/browser/profiles/profile.h" | 12 #include "chrome/browser/profiles/profile.h" |
13 #include "chrome/browser/safe_browsing/malware_details_cache.h" | 13 #include "chrome/browser/safe_browsing/malware_details_cache.h" |
14 #include "chrome/browser/safe_browsing/malware_details_history.h" | 14 #include "chrome/browser/safe_browsing/malware_details_history.h" |
15 #include "chrome/browser/safe_browsing/report.pb.h" | 15 #include "chrome/browser/safe_browsing/report.pb.h" |
16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" | 16 #include "chrome/browser/safe_browsing/safe_browsing_service.h" |
17 #include "chrome/common/safe_browsing/safebrowsing_messages.h" | 17 #include "chrome/common/safe_browsing/safebrowsing_messages.h" |
18 #include "content/browser/renderer_host/render_view_host.h" | 18 #include "content/browser/renderer_host/render_view_host.h" |
19 #include "content/browser/tab_contents/navigation_entry.h" | 19 #include "content/browser/tab_contents/navigation_entry.h" |
20 #include "content/browser/tab_contents/tab_contents.h" | |
21 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/web_contents.h" |
22 #include "net/base/io_buffer.h" | 22 #include "net/base/io_buffer.h" |
23 #include "net/disk_cache/disk_cache.h" | 23 #include "net/disk_cache/disk_cache.h" |
24 #include "net/url_request/url_request_context_getter.h" | 24 #include "net/url_request/url_request_context_getter.h" |
25 | 25 |
26 using content::BrowserThread; | 26 using content::BrowserThread; |
| 27 using content::WebContents; |
27 using safe_browsing::ClientMalwareReportRequest; | 28 using safe_browsing::ClientMalwareReportRequest; |
28 | 29 |
29 // Keep in sync with KMaxNodes in renderer/safe_browsing/malware_dom_details | 30 // Keep in sync with KMaxNodes in renderer/safe_browsing/malware_dom_details |
30 static const uint32 kMaxDomNodes = 500; | 31 static const uint32 kMaxDomNodes = 500; |
31 | 32 |
32 // static | 33 // static |
33 MalwareDetailsFactory* MalwareDetails::factory_ = NULL; | 34 MalwareDetailsFactory* MalwareDetails::factory_ = NULL; |
34 | 35 |
35 // The default MalwareDetailsFactory. Global, made a singleton so we | 36 // The default MalwareDetailsFactory. Global, made a singleton so we |
36 // don't leak it. | 37 // don't leak it. |
37 class MalwareDetailsFactoryImpl | 38 class MalwareDetailsFactoryImpl |
38 : public MalwareDetailsFactory { | 39 : public MalwareDetailsFactory { |
39 public: | 40 public: |
40 MalwareDetails* CreateMalwareDetails( | 41 MalwareDetails* CreateMalwareDetails( |
41 SafeBrowsingService* sb_service, | 42 SafeBrowsingService* sb_service, |
42 TabContents* tab_contents, | 43 WebContents* web_contents, |
43 const SafeBrowsingService::UnsafeResource& unsafe_resource) { | 44 const SafeBrowsingService::UnsafeResource& unsafe_resource) { |
44 return new MalwareDetails(sb_service, tab_contents, unsafe_resource); | 45 return new MalwareDetails(sb_service, web_contents, unsafe_resource); |
45 } | 46 } |
46 | 47 |
47 private: | 48 private: |
48 friend struct base::DefaultLazyInstanceTraits< | 49 friend struct base::DefaultLazyInstanceTraits< |
49 MalwareDetailsFactoryImpl>; | 50 MalwareDetailsFactoryImpl>; |
50 | 51 |
51 MalwareDetailsFactoryImpl() { } | 52 MalwareDetailsFactoryImpl() { } |
52 | 53 |
53 DISALLOW_COPY_AND_ASSIGN(MalwareDetailsFactoryImpl); | 54 DISALLOW_COPY_AND_ASSIGN(MalwareDetailsFactoryImpl); |
54 }; | 55 }; |
55 | 56 |
56 static base::LazyInstance<MalwareDetailsFactoryImpl> | 57 static base::LazyInstance<MalwareDetailsFactoryImpl> |
57 g_malware_details_factory_impl = LAZY_INSTANCE_INITIALIZER; | 58 g_malware_details_factory_impl = LAZY_INSTANCE_INITIALIZER; |
58 | 59 |
59 // Create a MalwareDetails for the given tab. | 60 // Create a MalwareDetails for the given tab. |
60 /* static */ | 61 /* static */ |
61 MalwareDetails* MalwareDetails::NewMalwareDetails( | 62 MalwareDetails* MalwareDetails::NewMalwareDetails( |
62 SafeBrowsingService* sb_service, | 63 SafeBrowsingService* sb_service, |
63 TabContents* tab_contents, | 64 WebContents* web_contents, |
64 const SafeBrowsingService::UnsafeResource& resource) { | 65 const SafeBrowsingService::UnsafeResource& resource) { |
65 // Set up the factory if this has not been done already (tests do that | 66 // Set up the factory if this has not been done already (tests do that |
66 // before this method is called). | 67 // before this method is called). |
67 if (!factory_) | 68 if (!factory_) |
68 factory_ = g_malware_details_factory_impl.Pointer(); | 69 factory_ = g_malware_details_factory_impl.Pointer(); |
69 return factory_->CreateMalwareDetails(sb_service, tab_contents, resource); | 70 return factory_->CreateMalwareDetails(sb_service, web_contents, resource); |
70 } | 71 } |
71 | 72 |
72 // Create a MalwareDetails for the given tab. Runs in the UI thread. | 73 // Create a MalwareDetails for the given tab. Runs in the UI thread. |
73 MalwareDetails::MalwareDetails( | 74 MalwareDetails::MalwareDetails( |
74 SafeBrowsingService* sb_service, | 75 SafeBrowsingService* sb_service, |
75 TabContents* tab_contents, | 76 content::WebContents* web_contents, |
76 const SafeBrowsingService::UnsafeResource& resource) | 77 const SafeBrowsingService::UnsafeResource& resource) |
77 : content::WebContentsObserver(tab_contents), | 78 : content::WebContentsObserver(web_contents), |
78 profile_(Profile::FromBrowserContext(tab_contents->GetBrowserContext())), | 79 profile_(Profile::FromBrowserContext(web_contents->GetBrowserContext())), |
79 request_context_getter_(profile_->GetRequestContext()), | 80 request_context_getter_(profile_->GetRequestContext()), |
80 sb_service_(sb_service), | 81 sb_service_(sb_service), |
81 resource_(resource), | 82 resource_(resource), |
82 cache_result_(false), | 83 cache_result_(false), |
83 cache_collector_(new MalwareDetailsCacheCollector), | 84 cache_collector_(new MalwareDetailsCacheCollector), |
84 redirects_collector_( | 85 redirects_collector_( |
85 new MalwareDetailsRedirectsCollector(profile_)) { | 86 new MalwareDetailsRedirectsCollector(profile_)) { |
86 StartCollection(); | 87 StartCollection(); |
87 } | 88 } |
88 | 89 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 } | 155 } |
155 | 156 |
156 void MalwareDetails::StartCollection() { | 157 void MalwareDetails::StartCollection() { |
157 DVLOG(1) << "Starting to compute malware details."; | 158 DVLOG(1) << "Starting to compute malware details."; |
158 report_.reset(new ClientMalwareReportRequest()); | 159 report_.reset(new ClientMalwareReportRequest()); |
159 | 160 |
160 if (IsPublicUrl(resource_.url)) { | 161 if (IsPublicUrl(resource_.url)) { |
161 report_->set_malware_url(resource_.url.spec()); | 162 report_->set_malware_url(resource_.url.spec()); |
162 } | 163 } |
163 | 164 |
164 GURL page_url = tab_contents()->GetURL(); | 165 GURL page_url = web_contents()->GetURL(); |
165 if (IsPublicUrl(page_url)) { | 166 if (IsPublicUrl(page_url)) { |
166 report_->set_page_url(page_url.spec()); | 167 report_->set_page_url(page_url.spec()); |
167 } | 168 } |
168 | 169 |
169 GURL referrer_url; | 170 GURL referrer_url; |
170 NavigationEntry* nav_entry = tab_contents()->GetController().GetActiveEntry(); | 171 NavigationEntry* nav_entry = web_contents()->GetController().GetActiveEntry(); |
171 if (nav_entry) { | 172 if (nav_entry) { |
172 referrer_url = nav_entry->GetReferrer().url; | 173 referrer_url = nav_entry->GetReferrer().url; |
173 if (IsPublicUrl(referrer_url)) { | 174 if (IsPublicUrl(referrer_url)) { |
174 report_->set_referrer_url(referrer_url.spec()); | 175 report_->set_referrer_url(referrer_url.spec()); |
175 } | 176 } |
176 } | 177 } |
177 | 178 |
178 // Add the nodes, starting from the page url. | 179 // Add the nodes, starting from the page url. |
179 AddUrl(page_url, GURL(), "", NULL); | 180 AddUrl(page_url, GURL(), "", NULL); |
180 | 181 |
(...skipping 22 matching lines...) Expand all Loading... |
203 parent_url = resource_.redirect_urls[i]; | 204 parent_url = resource_.redirect_urls[i]; |
204 } | 205 } |
205 | 206 |
206 // Add the referrer url. | 207 // Add the referrer url. |
207 if (nav_entry && !referrer_url.is_empty()) { | 208 if (nav_entry && !referrer_url.is_empty()) { |
208 AddUrl(referrer_url, GURL(), "", NULL); | 209 AddUrl(referrer_url, GURL(), "", NULL); |
209 } | 210 } |
210 | 211 |
211 // Get URLs of frames, scripts etc from the DOM. | 212 // Get URLs of frames, scripts etc from the DOM. |
212 // OnReceivedMalwareDOMDetails will be called when the renderer replies. | 213 // OnReceivedMalwareDOMDetails will be called when the renderer replies. |
213 RenderViewHost* view = tab_contents()->GetRenderViewHost(); | 214 RenderViewHost* view = web_contents()->GetRenderViewHost(); |
214 view->Send(new SafeBrowsingMsg_GetMalwareDOMDetails(view->routing_id())); | 215 view->Send(new SafeBrowsingMsg_GetMalwareDOMDetails(view->routing_id())); |
215 } | 216 } |
216 | 217 |
217 // When the renderer is done, this is called. | 218 // When the renderer is done, this is called. |
218 void MalwareDetails::OnReceivedMalwareDOMDetails( | 219 void MalwareDetails::OnReceivedMalwareDOMDetails( |
219 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { | 220 const std::vector<SafeBrowsingHostMsg_MalwareDOMDetails_Node>& params) { |
220 // Schedule this in IO thread, so it doesn't conflict with future users | 221 // Schedule this in IO thread, so it doesn't conflict with future users |
221 // of our data structures (eg GetSerializedReport). | 222 // of our data structures (eg GetSerializedReport). |
222 BrowserThread::PostTask( | 223 BrowserThread::PostTask( |
223 BrowserThread::IO, FROM_HERE, | 224 BrowserThread::IO, FROM_HERE, |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
303 | 304 |
304 // Send the report, using the SafeBrowsingService. | 305 // Send the report, using the SafeBrowsingService. |
305 std::string serialized; | 306 std::string serialized; |
306 if (!report_->SerializeToString(&serialized)) { | 307 if (!report_->SerializeToString(&serialized)) { |
307 DLOG(ERROR) << "Unable to serialize the malware report."; | 308 DLOG(ERROR) << "Unable to serialize the malware report."; |
308 return; | 309 return; |
309 } | 310 } |
310 | 311 |
311 sb_service_->SendSerializedMalwareDetails(serialized); | 312 sb_service_->SendSerializedMalwareDetails(serialized); |
312 } | 313 } |
OLD | NEW |