Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(50)

Side by Side Diff: components/safe_browsing/password_protection/password_protection_request.cc

Issue 2773483003: Create PasswordProtectionRequest to handle password pings (Closed)
Patch Set: Add one more unittest Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "components/safe_browsing/password_protection/password_protection_reque st.h"
5
6 #include "base/memory/ptr_util.h"
7 #include "base/memory/weak_ptr.h"
8 #include "base/metrics/histogram_macros.h"
9 #include "components/data_use_measurement/core/data_use_user_data.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "net/base/escape.h"
12 #include "net/base/load_flags.h"
13 #include "net/base/url_util.h"
14 #include "net/http/http_status_code.h"
15
16 using content::BrowserThread;
17
18 namespace safe_browsing {
19
20 PasswordProtectionRequest::PasswordProtectionRequest(
21 const GURL& main_frame_url,
22 LoginReputationClientRequest::TriggerType type,
23 bool is_extended_reporting,
24 bool is_incognito,
25 base::WeakPtr<PasswordProtectionService> pps,
26 int request_timeout_in_ms)
27 : main_frame_url_(main_frame_url),
28 request_type_(type),
29 is_extended_reporting_(is_extended_reporting),
30 is_incognito_(is_incognito),
31 password_protection_service_(pps),
32 request_timeout_in_ms_(request_timeout_in_ms),
33 weakptr_factory_(this) {
34 DCHECK_CURRENTLY_ON(BrowserThread::UI);
35 }
36
37 PasswordProtectionRequest::~PasswordProtectionRequest() {
38 weakptr_factory_.InvalidateWeakPtrs();
39 }
40
41 void PasswordProtectionRequest::Start() {
42 DCHECK_CURRENTLY_ON(BrowserThread::UI);
43 // Initially we only send ping for Safe Browsing Extended Reporting users when
44 // they are not in incognito mode. We may loose these conditions later.
45 if (is_incognito_) {
46 Finish(RequestOutcome::INCOGNITO, nullptr);
47 return;
48 }
49 if (!is_extended_reporting_) {
50 Finish(RequestOutcome::NO_EXTENDED_REPORTING, nullptr);
51 return;
52 }
53
54 // In case the request take too long, we set a timer to cancel this request.
55 StartTimeout();
lpz 2017/03/24 15:40:48 This timer will also count the time to look at the
Jialiu Lin 2017/03/24 18:10:18 Agree, it makes more sense to only time SendReques
lpz 2017/03/24 18:55:58 ..and just one other thought - is it safe to assum
56
57 CheckWhitelistsOnUIThread();
58 }
59
60 void PasswordProtectionRequest::Cancel(bool timed_out) {
61 DCHECK_CURRENTLY_ON(BrowserThread::UI);
62 fetcher_.reset();
63
64 Finish(timed_out ? TIMEDOUT : CANCELED, nullptr);
65 }
66
67 void PasswordProtectionRequest::StartTimeout() {
68 DCHECK_CURRENTLY_ON(BrowserThread::UI);
69
70 // If request is not done withing 10 seconds, we cancel this request.
71 // The weak pointer used for the timeout will be invalidated (and
72 // hence would prevent the timeout) if the check completes on time and
73 // execution reaches Finish().
74 BrowserThread::PostDelayedTask(
75 BrowserThread::UI, FROM_HERE,
76 base::Bind(&PasswordProtectionRequest::Cancel, GetWeakPtr(), true),
77 base::TimeDelta::FromMilliseconds(request_timeout_in_ms_));
78 }
79
80 void PasswordProtectionRequest::Finish(
lpz 2017/03/24 15:40:48 nit: reorder these functions to more-closely match
Jialiu Lin 2017/03/24 18:10:18 Done.
81 RequestOutcome outcome,
82 std::unique_ptr<LoginReputationClientResponse> response) {
83 DCHECK_CURRENTLY_ON(BrowserThread::UI);
84
85 UMA_HISTOGRAM_ENUMERATION("PasswordProtection.RequestOutcome", outcome,
86 RequestOutcome::MAX_OUTCOME);
87
88 if (response) {
89 switch (request_type_) {
90 case LoginReputationClientRequest::UNFAMILIAR_LOGIN_PAGE:
91 UMA_HISTOGRAM_ENUMERATION(
92 "PasswordProtection.UnfamiliarLoginPageVerdict",
93 response->verdict_type(),
94 LoginReputationClientResponse_VerdictType_VerdictType_MAX + 1);
95 break;
96 case LoginReputationClientRequest::PASSWORD_REUSE_EVENT:
97 UMA_HISTOGRAM_ENUMERATION(
98 "PasswordProtection.PasswordReuseEventVerdict",
99 response->verdict_type(),
100 LoginReputationClientResponse_VerdictType_VerdictType_MAX + 1);
101 break;
102 default:
103 NOTREACHED();
104 }
105 }
106
107 DCHECK(password_protection_service_);
108 password_protection_service_->RequestFinished(this, std::move(response));
109 }
110
111 // TODO(jialiul): there are some duplicated code
112 void PasswordProtectionRequest::CheckWhitelistsOnUIThread() {
113 DCHECK_CURRENTLY_ON(BrowserThread::UI);
114 DCHECK(password_protection_service_);
115
116 BrowserThread::PostTask(
117 BrowserThread::IO, FROM_HERE,
118 base::Bind(&PasswordProtectionService::CheckCsdWhitelistOnIOThread,
119 password_protection_service_, main_frame_url_,
120 base::Bind(&PasswordProtectionRequest::OnWhitelistCheckDone,
121 GetWeakPtr())));
122 }
123
124 void PasswordProtectionRequest::OnWhitelistCheckDone(bool match_whitelist) {
125 DCHECK_CURRENTLY_ON(BrowserThread::UI);
126 if (match_whitelist)
127 Finish(RequestOutcome::MATCHED_WHITELIST, nullptr);
128 else
129 CheckCachedVerdicts();
130 }
131
132 void PasswordProtectionRequest::CheckCachedVerdicts() {
133 DCHECK_CURRENTLY_ON(BrowserThread::UI);
134 if (!password_protection_service_) {
135 Finish(RequestOutcome::SERVICE_DESTROYED, nullptr);
136 return;
137 }
138
139 std::unique_ptr<LoginReputationClientResponse> cached_response =
140 base::MakeUnique<LoginReputationClientResponse>();
141 auto verdict = password_protection_service_->GetCachedVerdict(
142 password_protection_service_->GetSettingMapForActiveProfile(),
143 main_frame_url_, cached_response.get());
144 if (verdict != LoginReputationClientResponse::VERDICT_TYPE_UNSPECIFIED)
145 Finish(RequestOutcome::RESPONSE_ALREADY_CACHED, std::move(cached_response));
146 else
147 SendRequest();
148 }
149
150 void PasswordProtectionRequest::OnURLFetchComplete(
151 const net::URLFetcher* source) {
152 DCHECK_CURRENTLY_ON(BrowserThread::UI);
153 net::URLRequestStatus status = source->GetStatus();
154 const bool is_success = status.is_success();
155 const int response_code = source->GetResponseCode();
156
157 UMA_HISTOGRAM_SPARSE_SLOWLY(
158 "PasswordProtection.PasswordProtectionResponseOrErrorCode",
159 is_success ? response_code : status.error());
160
161 if (!is_success || net::HTTP_OK != response_code) {
162 Finish(RequestOutcome::FETCH_FAILED, nullptr);
163 return;
164 }
165
166 std::unique_ptr<LoginReputationClientResponse> response =
167 base::MakeUnique<LoginReputationClientResponse>();
168 std::string response_body;
169 bool received_data = source->GetResponseAsString(&response_body);
170 DCHECK(received_data);
171 fetcher_.reset(); // We don't need it anymore.
172 UMA_HISTOGRAM_TIMES("PasswordProtection.RequestNetworkDuration",
173 base::TimeTicks::Now() - request_start_time_);
lpz 2017/03/24 15:40:48 ...further to my other comment, this will also inc
Jialiu Lin 2017/03/24 18:10:18 request_start_time is set in SendRequest() functio
174 if (response->ParseFromString(response_body)) {
175 Finish(RequestOutcome::SUCCEEDED, std::move(response));
176 } else {
177 Finish(RequestOutcome::RESPONSE_MALFORMED, nullptr);
178 }
179 }
180
181 void PasswordProtectionRequest::SendRequest() {
182 DCHECK_CURRENTLY_ON(BrowserThread::UI);
183
184 LoginReputationClientRequest request;
185 request.set_page_url(main_frame_url_.spec());
186 request.set_trigger_type(request_type_);
187 request.set_stored_verdict_cnt(
188 password_protection_service_->GetStoredVerdictCount());
189
190 std::string serialized_request;
191 if (!request.SerializeToString(&serialized_request)) {
192 Finish(RequestOutcome::REQUEST_MALFORMED, nullptr);
193 return;
194 }
195
196 fetcher_ = net::URLFetcher::Create(
197 0, PasswordProtectionService::GetPasswordProtectionRequestUrl(),
198 net::URLFetcher::POST, this);
199 data_use_measurement::DataUseUserData::AttachToFetcher(
200 fetcher_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING);
201 fetcher_->SetLoadFlags(net::LOAD_DISABLE_CACHE);
202 fetcher_->SetAutomaticallyRetryOn5xx(false);
203 fetcher_->SetRequestContext(
204 password_protection_service_->request_context_getter().get());
205 fetcher_->SetUploadData("application/octet-stream", serialized_request);
206 request_start_time_ = base::TimeTicks::Now();
207 fetcher_->Start();
208 }
209
210 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698