OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQUEST _H_ | 5 #ifndef COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQUEST _H_ |
6 #define COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQUEST _H_ | 6 #define COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQUEST _H_ |
7 | 7 |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/memory/ref_counted.h" | 9 #include "base/memory/ref_counted.h" |
10 #include "base/task/cancelable_task_tracker.h" | |
10 #include "components/safe_browsing/password_protection/password_protection_servi ce.h" | 11 #include "components/safe_browsing/password_protection/password_protection_servi ce.h" |
12 #include "content/public/browser/browser_thread.h" | |
11 #include "net/url_request/url_fetcher.h" | 13 #include "net/url_request/url_fetcher.h" |
12 #include "net/url_request/url_fetcher_delegate.h" | 14 #include "net/url_request/url_fetcher_delegate.h" |
13 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
14 | 16 |
15 class GURL; | 17 class GURL; |
16 | 18 |
17 namespace safe_browsing { | 19 namespace safe_browsing { |
18 | 20 |
19 // A request for checking if an unfamiliar login form or a password reuse event | 21 // A request for checking if an unfamiliar login form or a password reuse event |
20 // is safe. PasswordProtectionRequest objects are owned by | 22 // is safe. PasswordProtectionRequest objects are owned by |
21 // PasswordProtectionService indicated by |password_protection_service_|. | 23 // PasswordProtectionService indicated by |password_protection_service_|. |
22 class PasswordProtectionRequest : public net::URLFetcherDelegate { | 24 class PasswordProtectionRequest |
Nathan Parker
2017/04/18 23:07:26
Add a comment as to why it's recounted (destructio
Jialiu Lin
2017/04/19 00:21:14
Thanks for the reminder! Actually, it should only
| |
25 : public base::RefCountedThreadSafe<PasswordProtectionRequest>, | |
26 public net::URLFetcherDelegate { | |
23 public: | 27 public: |
24 // The outcome of the request. These values are used for UMA. | 28 // The outcome of the request. These values are used for UMA. |
25 // DO NOT CHANGE THE ORDERING OF THESE VALUES. | 29 // DO NOT CHANGE THE ORDERING OF THESE VALUES. |
26 enum RequestOutcome { | 30 enum RequestOutcome { |
27 UNKNOWN = 0, | 31 UNKNOWN = 0, |
28 SUCCEEDED = 1, | 32 SUCCEEDED = 1, |
29 CANCELED = 2, | 33 CANCELED = 2, |
30 TIMEDOUT = 3, | 34 TIMEDOUT = 3, |
31 MATCHED_WHITELIST = 4, | 35 MATCHED_WHITELIST = 4, |
32 RESPONSE_ALREADY_CACHED = 5, | 36 RESPONSE_ALREADY_CACHED = 5, |
33 NO_EXTENDED_REPORTING = 6, | 37 NO_EXTENDED_REPORTING = 6, |
34 INCOGNITO = 7, | 38 INCOGNITO = 7, |
35 REQUEST_MALFORMED = 8, | 39 REQUEST_MALFORMED = 8, |
36 FETCH_FAILED = 9, | 40 FETCH_FAILED = 9, |
37 RESPONSE_MALFORMED = 10, | 41 RESPONSE_MALFORMED = 10, |
38 SERVICE_DESTROYED = 11, | 42 SERVICE_DESTROYED = 11, |
39 MAX_OUTCOME | 43 MAX_OUTCOME |
40 }; | 44 }; |
41 | 45 |
42 PasswordProtectionRequest(const GURL& main_frame_url, | 46 PasswordProtectionRequest( |
43 LoginReputationClientRequest::TriggerType type, | 47 const GURL& main_frame_url, |
44 bool is_extended_reporting, | 48 LoginReputationClientRequest::TriggerType type, |
45 bool is_incognito, | 49 std::unique_ptr<PasswordProtectionFrameList> pending_password_frames, |
46 base::WeakPtr<PasswordProtectionService> pps, | 50 base::WeakPtr<PasswordProtectionService> pps, |
47 int request_timeout_in_ms); | 51 int request_timeout_in_ms); |
48 | |
49 ~PasswordProtectionRequest() override; | |
50 | 52 |
51 base::WeakPtr<PasswordProtectionRequest> GetWeakPtr() { | 53 base::WeakPtr<PasswordProtectionRequest> GetWeakPtr() { |
52 return weakptr_factory_.GetWeakPtr(); | 54 return weakptr_factory_.GetWeakPtr(); |
53 } | 55 } |
54 | 56 |
55 // Starts processing request by checking extended reporting and incognito | 57 // Starts processing request by checking extended reporting and incognito |
56 // conditions. | 58 // conditions. |
57 void Start(); | 59 void Start(); |
58 | 60 |
59 // Cancels the current request. |timed_out| indicates if this cancellation is | 61 // Cancels the current request. |timed_out| indicates if this cancellation is |
60 // due to timeout. This function will call Finish() to destroy |this|. | 62 // due to timeout. This function will call Finish() to destroy |this|. |
61 void Cancel(bool timed_out); | 63 void Cancel(bool timed_out); |
62 | 64 |
63 // net::URLFetcherDelegate override. | 65 // net::URLFetcherDelegate override. |
64 // Processes the received response. | 66 // Processes the received response. |
65 void OnURLFetchComplete(const net::URLFetcher* source) override; | 67 void OnURLFetchComplete(const net::URLFetcher* source) override; |
66 | 68 |
67 GURL main_frame_url() const { return main_frame_url_; } | 69 GURL main_frame_url() const { return main_frame_url_; } |
68 | 70 |
69 bool is_incognito() const { return is_incognito_; } | 71 protected: |
72 ~PasswordProtectionRequest() override; | |
70 | 73 |
71 private: | 74 private: |
75 friend class base::RefCountedThreadSafe<PasswordProtectionRequest>; | |
76 | |
77 void CheckWhitelistOnUIThread(); | |
78 | |
72 // If |main_frame_url_| matches whitelist, call Finish() immediately; | 79 // If |main_frame_url_| matches whitelist, call Finish() immediately; |
73 // otherwise call CheckCachedVerdicts(). | 80 // otherwise call CheckCachedVerdicts(). It is the task posted back to UI |
74 void OnWhitelistCheckDone(bool match_whitelist); | 81 // thread by the PostTaskAndReply() in CheckWhitelistOnUIThread(). |
82 // |match_whitelist| boolean pointer is used to pass whitelist checking result | |
83 // between UI and IO thread. The object it points to will be deleted at the | |
84 // end of OnWhitelistCheckDone(), since base::Owned() transfers its ownership | |
85 // to this callback function. | |
86 void OnWhitelistCheckDone(const bool* match_whitelist); | |
75 | 87 |
76 // Looks up cached verdicts. If verdict is already cached, call SendRequest(); | 88 // Looks up cached verdicts. If verdict is already cached, call SendRequest(); |
77 // otherwise call Finish(). | 89 // otherwise call Finish(). |
78 void CheckCachedVerdicts(); | 90 void CheckCachedVerdicts(); |
79 | 91 |
80 // Fill |request_proto_| with appropriate values. | 92 // Fill |request_proto_| with appropriate values. |
81 void FillRequestProto(); | 93 void FillRequestProto(); |
82 | 94 |
83 // Initiates network request to Safe Browsing backend. | 95 // Initiates network request to Safe Browsing backend. |
84 void SendRequest(); | 96 void SendRequest(); |
85 | 97 |
86 // Start a timer to cancel the request if it takes too long. | 98 // Start a timer to cancel the request if it takes too long. |
87 void StartTimeout(); | 99 void StartTimeout(); |
88 | 100 |
89 // |this| will be destroyed after calling this function. | 101 // |this| will be destroyed after calling this function. |
90 void Finish(RequestOutcome outcome, | 102 void Finish(RequestOutcome outcome, |
91 std::unique_ptr<LoginReputationClientResponse> response); | 103 std::unique_ptr<LoginReputationClientResponse> response); |
92 | 104 |
93 void CheckWhitelistsOnUIThread(); | |
94 | |
95 // Main frame URL of the login form. | 105 // Main frame URL of the login form. |
96 GURL main_frame_url_; | 106 GURL main_frame_url_; |
97 | 107 |
98 // If this request is for unfamiliar login page or for a password reuse event. | 108 // If this request is for unfamiliar login page or for a password reuse event. |
99 const LoginReputationClientRequest::TriggerType request_type_; | 109 const LoginReputationClientRequest::TriggerType request_type_; |
100 | 110 |
101 // If user is opted-in Safe Browsing Extended Reporting. | 111 std::unique_ptr<PasswordProtectionFrameList> pending_password_frames_; |
Nathan Parker
2017/04/18 23:07:26
Add a comment. It's not obvious to me what pendin
Jialiu Lin
2017/04/19 00:21:14
"pending_" is irrelevant here. Removed "pending_"
| |
102 const bool is_extended_reporting_; | |
103 | |
104 // If current session is in incognito mode. | |
105 const bool is_incognito_; | |
106 | 112 |
107 // When request is sent. | 113 // When request is sent. |
108 base::TimeTicks request_start_time_; | 114 base::TimeTicks request_start_time_; |
109 | 115 |
110 // URLFetcher instance for sending request and receiving response. | 116 // URLFetcher instance for sending request and receiving response. |
111 std::unique_ptr<net::URLFetcher> fetcher_; | 117 std::unique_ptr<net::URLFetcher> fetcher_; |
112 | 118 |
113 // The PasswordProtectionService instance owns |this|. | 119 // The PasswordProtectionService instance owns |this|. |
120 // Can only be accessed on UI thread. | |
114 base::WeakPtr<PasswordProtectionService> password_protection_service_; | 121 base::WeakPtr<PasswordProtectionService> password_protection_service_; |
115 | 122 |
123 // Safe Browsing database manager used to look up CSD whitelist. | |
124 // Can only be accessed on IO thread. | |
125 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | |
126 | |
116 // If we haven't receive response after this period of time, we cancel this | 127 // If we haven't receive response after this period of time, we cancel this |
117 // request. | 128 // request. |
118 const int request_timeout_in_ms_; | 129 const int request_timeout_in_ms_; |
119 | 130 |
120 std::unique_ptr<LoginReputationClientRequest> request_proto_; | 131 std::unique_ptr<LoginReputationClientRequest> request_proto_; |
121 | 132 |
133 // Needed for canceling tasks posted to different threads. | |
134 base::CancelableTaskTracker tracker_; | |
135 | |
122 base::WeakPtrFactory<PasswordProtectionRequest> weakptr_factory_; | 136 base::WeakPtrFactory<PasswordProtectionRequest> weakptr_factory_; |
123 DISALLOW_COPY_AND_ASSIGN(PasswordProtectionRequest); | 137 DISALLOW_COPY_AND_ASSIGN(PasswordProtectionRequest); |
124 }; | 138 }; |
125 | 139 |
126 } // namespace safe_browsing | 140 } // namespace safe_browsing |
127 | 141 |
128 #endif // COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQU EST_H_ | 142 #endif // COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQU EST_H_ |
OLD | NEW |