OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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/download_feedback_service.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/command_line.h" | |
9 #include "base/files/file_path.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/stl_util.h" | |
12 #include "base/supports_user_data.h" | |
13 #include "base/task_runner.h" | |
14 #include "chrome/browser/safe_browsing/download_feedback.h" | |
15 #include "chrome/common/chrome_switches.h" | |
16 #include "chrome/common/chrome_version_info.h" | |
17 #include "content/public/browser/download_danger_type.h" | |
18 #include "content/public/browser/download_item.h" | |
19 | |
20 namespace safe_browsing { | |
21 | |
22 namespace { | |
23 | |
24 const void* kPingKey = &kPingKey; | |
25 | |
26 bool IsEnabled() { | |
27 CommandLine* cmdline = CommandLine::ForCurrentProcess(); | |
28 if (cmdline->HasSwitch(switches::kSbEnableDownloadFeedback)) | |
29 return true; | |
30 | |
31 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); | |
32 if (channel == chrome::VersionInfo::CHANNEL_UNKNOWN || | |
33 channel == chrome::VersionInfo::CHANNEL_CANARY || | |
34 channel == chrome::VersionInfo::CHANNEL_DEV) | |
35 return true; | |
36 | |
37 return false; | |
38 } | |
39 | |
40 class DownloadFeedbackPings : public base::SupportsUserData::Data { | |
41 public: | |
42 DownloadFeedbackPings(const std::string& ping_request, | |
43 const std::string& ping_response); | |
44 | |
45 // Stores the ping data in the given |download|. | |
46 static void CreateForDownload(content::DownloadItem* download, | |
47 const std::string& ping_request, | |
48 const std::string& ping_response); | |
49 | |
50 // Returns the DownloadFeedbackPings object associated with |download|. May | |
51 // return NULL. | |
52 static DownloadFeedbackPings* FromDownload( | |
53 const content::DownloadItem& download); | |
54 | |
55 | |
56 const std::string& ping_request() const { | |
57 return ping_request_; | |
58 } | |
59 | |
60 const std::string& ping_response() const { | |
61 return ping_response_; | |
62 } | |
63 | |
64 private: | |
65 std::string ping_request_; | |
66 std::string ping_response_; | |
67 }; | |
68 | |
69 DownloadFeedbackPings::DownloadFeedbackPings(const std::string& ping_request, | |
70 const std::string& ping_response) | |
71 : ping_request_(ping_request), | |
72 ping_response_(ping_response) { | |
73 } | |
74 | |
75 // static | |
76 void DownloadFeedbackPings::CreateForDownload( | |
77 content::DownloadItem* download, | |
78 const std::string& ping_request, | |
79 const std::string& ping_response) { | |
80 DownloadFeedbackPings* pings = new DownloadFeedbackPings(ping_request, | |
81 ping_response); | |
82 download->SetUserData(kPingKey, pings); | |
83 } | |
84 | |
85 // static | |
86 DownloadFeedbackPings* DownloadFeedbackPings::FromDownload( | |
87 const content::DownloadItem& download) { | |
88 return static_cast<DownloadFeedbackPings*>(download.GetUserData(kPingKey)); | |
89 } | |
90 | |
91 } // namespace | |
92 | |
93 DownloadFeedbackService::DownloadFeedbackService( | |
94 net::URLRequestContextGetter* request_context_getter, | |
95 base::TaskRunner* file_task_runner) | |
96 : request_context_getter_(request_context_getter), | |
97 file_task_runner_(file_task_runner), | |
98 weak_ptr_factory_(this) { | |
99 } | |
100 | |
101 DownloadFeedbackService::~DownloadFeedbackService() { | |
102 DCHECK(CalledOnValidThread()); | |
103 STLDeleteContainerPointers(active_feedback_.begin(), active_feedback_.end()); | |
104 } | |
105 | |
106 // static | |
107 void DownloadFeedbackService::MaybeStorePingsForDownload( | |
108 DownloadProtectionService::DownloadCheckResult result, | |
109 content::DownloadItem* download, | |
110 const std::string& ping, | |
111 const std::string& response) { | |
112 if (!IsEnabled() || !(result == DownloadProtectionService::UNCOMMON || | |
113 result == DownloadProtectionService::DANGEROUS_HOST)) | |
114 return; | |
115 UMA_HISTOGRAM_COUNTS("SBDownloadFeedback.SizeEligibleKB", | |
116 download->GetReceivedBytes() / 1024); | |
117 if (download->GetReceivedBytes() > DownloadFeedback::kMaxUploadSize) | |
118 return; | |
119 | |
120 DownloadFeedbackPings::CreateForDownload(download, ping, response); | |
121 } | |
122 | |
123 // static | |
124 bool DownloadFeedbackService::IsEnabledForDownload( | |
125 const content::DownloadItem& download) { | |
126 return !!DownloadFeedbackPings::FromDownload(download); | |
127 } | |
128 | |
129 // static | |
130 bool DownloadFeedbackService::GetPingsForDownloadForTesting( | |
131 const content::DownloadItem& download, | |
132 std::string* ping, | |
133 std::string* response) { | |
134 DownloadFeedbackPings* pings = DownloadFeedbackPings::FromDownload(download); | |
135 if (!pings) | |
136 return false; | |
137 | |
138 *ping = pings->ping_request(); | |
139 *response = pings->ping_response(); | |
140 return true; | |
141 } | |
142 | |
143 // static | |
144 void DownloadFeedbackService::RecordFeedbackButtonShown( | |
145 content::DownloadDangerType danger_type) { | |
146 UMA_HISTOGRAM_ENUMERATION("SBDownloadFeedback.Shown", | |
147 danger_type, | |
148 content::DOWNLOAD_DANGER_TYPE_MAX); | |
149 } | |
150 | |
151 | |
152 void DownloadFeedbackService::BeginFeedbackForDownload( | |
153 content::DownloadItem* download) { | |
154 DCHECK(CalledOnValidThread()); | |
155 | |
156 UMA_HISTOGRAM_ENUMERATION("SBDownloadFeedback.Activations", | |
157 download->GetDangerType(), | |
158 content::DOWNLOAD_DANGER_TYPE_MAX); | |
159 | |
160 DownloadFeedbackPings* pings = DownloadFeedbackPings::FromDownload(*download); | |
161 DCHECK(pings); | |
162 | |
163 download->StealDangerousDownload( | |
164 base::Bind(&DownloadFeedbackService::BeginFeedback, | |
165 weak_ptr_factory_.GetWeakPtr(), | |
asanka
2013/06/03 16:18:32
When would the service be destroyed?
StealDangero
mattm
2013/06/07 03:09:02
On shutdown.
asanka
2013/06/07 15:39:58
Thanks. The handover should be happening in respon
| |
166 pings->ping_request(), | |
167 pings->ping_response())); | |
168 } | |
169 | |
170 void DownloadFeedbackService::BeginFeedback( | |
171 const std::string& ping_request, | |
172 const std::string& ping_response, | |
173 const base::FilePath& path) { | |
174 DCHECK(CalledOnValidThread()); | |
175 DownloadFeedback* feedback = DownloadFeedback::Create( | |
176 request_context_getter_, file_task_runner_, path, | |
177 ping_request, ping_response); | |
178 active_feedback_.insert(feedback); | |
179 feedback->Start(base::Bind(&DownloadFeedbackService::FeedbackComplete, | |
180 base::Unretained(this), feedback)); | |
181 } | |
182 | |
183 void DownloadFeedbackService::FeedbackComplete(DownloadFeedback* feedback) { | |
184 DVLOG(1) << __FUNCTION__; | |
185 DCHECK(CalledOnValidThread()); | |
186 active_feedback_.erase(feedback); | |
187 delete feedback; | |
188 } | |
189 | |
190 } // namespace safe_browsing | |
OLD | NEW |