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 |
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<PasswordProtectionFrames> pending_password_frames, |
46 base::WeakPtr<PasswordProtectionService> pps, | 50 base::WeakPtr<PasswordProtectionService> pps, |
47 int request_timeout_in_ms); | 51 scoped_refptr<SafeBrowsingDatabaseManager> database_manager, |
48 | 52 int request_timeout_in_ms); |
49 ~PasswordProtectionRequest() override; | |
50 | 53 |
51 base::WeakPtr<PasswordProtectionRequest> GetWeakPtr() { | 54 base::WeakPtr<PasswordProtectionRequest> GetWeakPtr() { |
52 return weakptr_factory_.GetWeakPtr(); | 55 return weakptr_factory_.GetWeakPtr(); |
53 } | 56 } |
54 | 57 |
55 // Starts processing request by checking extended reporting and incognito | 58 // Starts processing request by checking extended reporting and incognito |
56 // conditions. | 59 // conditions. |
57 void Start(); | 60 void Start(); |
58 | 61 |
59 // Cancels the current request. |timed_out| indicates if this cancellation is | 62 // Cancels the current request. |timed_out| indicates if this cancellation is |
60 // due to timeout. This function will call Finish() to destroy |this|. | 63 // due to timeout. This function will call Finish() to destroy |this|. |
61 void Cancel(bool timed_out); | 64 void Cancel(bool timed_out); |
62 | 65 |
63 // net::URLFetcherDelegate override. | 66 // net::URLFetcherDelegate override. |
64 // Processes the received response. | 67 // Processes the received response. |
65 void OnURLFetchComplete(const net::URLFetcher* source) override; | 68 void OnURLFetchComplete(const net::URLFetcher* source) override; |
66 | 69 |
67 GURL main_frame_url() const { return main_frame_url_; } | 70 GURL main_frame_url() const { return main_frame_url_; } |
68 | 71 |
69 bool is_incognito() const { return is_incognito_; } | 72 protected: |
73 ~PasswordProtectionRequest() override; | |
70 | 74 |
71 private: | 75 private: |
76 friend class base::RefCountedThreadSafe<PasswordProtectionRequest>; | |
77 | |
78 void CheckWhitelistOnUIThread(); | |
79 | |
72 // If |main_frame_url_| matches whitelist, call Finish() immediately; | 80 // If |main_frame_url_| matches whitelist, call Finish() immediately; |
73 // otherwise call CheckCachedVerdicts(). | 81 // otherwise call CheckCachedVerdicts(). |
Nathan Parker
2017/04/17 20:28:02
Comment on why this is a ptr. Does null mean some
Jialiu Lin
2017/04/18 00:58:26
Done.
| |
74 void OnWhitelistCheckDone(bool match_whitelist); | 82 void OnWhitelistCheckDone(const bool* match_whitelist); |
75 | 83 |
76 // Looks up cached verdicts. If verdict is already cached, call SendRequest(); | 84 // Looks up cached verdicts. If verdict is already cached, call SendRequest(); |
77 // otherwise call Finish(). | 85 // otherwise call Finish(). |
78 void CheckCachedVerdicts(); | 86 void CheckCachedVerdicts(); |
79 | 87 |
80 // Fill |request_proto_| with appropriate values. | 88 // Fill |request_proto_| with appropriate values. |
81 void FillRequestProto(); | 89 void FillRequestProto(); |
82 | 90 |
83 // Initiates network request to Safe Browsing backend. | 91 // Initiates network request to Safe Browsing backend. |
84 void SendRequest(); | 92 void SendRequest(); |
85 | 93 |
86 // Start a timer to cancel the request if it takes too long. | 94 // Start a timer to cancel the request if it takes too long. |
87 void StartTimeout(); | 95 void StartTimeout(); |
88 | 96 |
89 // |this| will be destroyed after calling this function. | 97 // |this| will be destroyed after calling this function. |
90 void Finish(RequestOutcome outcome, | 98 void Finish(RequestOutcome outcome, |
91 std::unique_ptr<LoginReputationClientResponse> response); | 99 std::unique_ptr<LoginReputationClientResponse> response); |
92 | 100 |
93 void CheckWhitelistsOnUIThread(); | |
94 | |
95 // Main frame URL of the login form. | 101 // Main frame URL of the login form. |
96 GURL main_frame_url_; | 102 GURL main_frame_url_; |
97 | 103 |
98 // If this request is for unfamiliar login page or for a password reuse event. | 104 // If this request is for unfamiliar login page or for a password reuse event. |
99 const LoginReputationClientRequest::TriggerType request_type_; | 105 const LoginReputationClientRequest::TriggerType request_type_; |
100 | 106 |
101 // If user is opted-in Safe Browsing Extended Reporting. | 107 std::unique_ptr<PasswordProtectionFrames> pending_password_frames_; |
102 const bool is_extended_reporting_; | |
103 | |
104 // If current session is in incognito mode. | |
105 const bool is_incognito_; | |
106 | 108 |
107 // When request is sent. | 109 // When request is sent. |
108 base::TimeTicks request_start_time_; | 110 base::TimeTicks request_start_time_; |
109 | 111 |
110 // URLFetcher instance for sending request and receiving response. | 112 // URLFetcher instance for sending request and receiving response. |
111 std::unique_ptr<net::URLFetcher> fetcher_; | 113 std::unique_ptr<net::URLFetcher> fetcher_; |
112 | 114 |
113 // The PasswordProtectionService instance owns |this|. | 115 // The PasswordProtectionService instance owns |this|. |
116 // Can only be accessed on UI thread. | |
114 base::WeakPtr<PasswordProtectionService> password_protection_service_; | 117 base::WeakPtr<PasswordProtectionService> password_protection_service_; |
115 | 118 |
119 // Safe Browsing database manager used to look up CSD whitelist. | |
120 // Can only be accessed on IO thread. | |
121 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | |
122 | |
116 // If we haven't receive response after this period of time, we cancel this | 123 // If we haven't receive response after this period of time, we cancel this |
117 // request. | 124 // request. |
118 const int request_timeout_in_ms_; | 125 const int request_timeout_in_ms_; |
119 | 126 |
120 std::unique_ptr<LoginReputationClientRequest> request_proto_; | 127 std::unique_ptr<LoginReputationClientRequest> request_proto_; |
121 | 128 |
129 // Needed for canceling tasks posted to different threads. | |
130 base::CancelableTaskTracker tracker_; | |
131 | |
122 base::WeakPtrFactory<PasswordProtectionRequest> weakptr_factory_; | 132 base::WeakPtrFactory<PasswordProtectionRequest> weakptr_factory_; |
123 DISALLOW_COPY_AND_ASSIGN(PasswordProtectionRequest); | 133 DISALLOW_COPY_AND_ASSIGN(PasswordProtectionRequest); |
124 }; | 134 }; |
125 | 135 |
126 } // namespace safe_browsing | 136 } // namespace safe_browsing |
127 | 137 |
128 #endif // COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQU EST_H_ | 138 #endif // COMPONENTS_SAFE_BROWSING_PASSWORD_PROTECTION_PASSWORD_PROTECTION_REQU EST_H_ |
OLD | NEW |