| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 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 | |
| 5 #ifndef COMPONENTS_SAFE_BROWSING_BASE_RESOURCE_THROTTLE_H_ | |
| 6 #define COMPONENTS_SAFE_BROWSING_BASE_RESOURCE_THROTTLE_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/time/time.h" | |
| 13 #include "base/timer/timer.h" | |
| 14 #include "components/safe_browsing/base_ui_manager.h" | |
| 15 #include "components/safe_browsing_db/database_manager.h" | |
| 16 #include "components/security_interstitials/content/unsafe_resource.h" | |
| 17 #include "content/public/browser/resource_throttle.h" | |
| 18 #include "content/public/common/resource_type.h" | |
| 19 #include "net/log/net_log_event_type.h" | |
| 20 #include "net/log/net_log_with_source.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace content { | |
| 24 class ResourceRequestInfo; | |
| 25 } | |
| 26 | |
| 27 namespace net { | |
| 28 class URLRequest; | |
| 29 } | |
| 30 | |
| 31 namespace safe_browsing { | |
| 32 | |
| 33 // BaseResourceThrottle checks that URLs are "safe" before | |
| 34 // navigating to them. To be considered "safe", a URL must not appear in the | |
| 35 // malware/phishing blacklists (see SafeBrowsingService for details). | |
| 36 // | |
| 37 // Note that the safe browsing check takes at most kCheckUrlTimeoutMs | |
| 38 // milliseconds. If it takes longer than this, then the system defaults to | |
| 39 // treating the URL as safe. | |
| 40 // | |
| 41 // If the URL is classified as dangerous, a warning page is thrown up and | |
| 42 // the request remains suspended. If the user clicks "proceed" on warning | |
| 43 // page, we resume the request. | |
| 44 // | |
| 45 // Note: The ResourceThrottle interface is called in this order: | |
| 46 // WillStartRequest once, WillRedirectRequest zero or more times, and then | |
| 47 // WillProcessReponse once. | |
| 48 class BaseResourceThrottle | |
| 49 : public content::ResourceThrottle, | |
| 50 public SafeBrowsingDatabaseManager::Client, | |
| 51 public base::SupportsWeakPtr<BaseResourceThrottle> { | |
| 52 public: | |
| 53 // Construct a BaseResourceThrottle, or return nullptr if we | |
| 54 // cannot access the safe browsing API on Android | |
| 55 static BaseResourceThrottle* MaybeCreate( | |
| 56 net::URLRequest* request, | |
| 57 content::ResourceType resource_type, | |
| 58 scoped_refptr<SafeBrowsingDatabaseManager> | |
| 59 database_manager, | |
| 60 scoped_refptr<BaseUIManager> ui_manager); | |
| 61 | |
| 62 // content::ResourceThrottle implementation (called on IO thread): | |
| 63 void WillStartRequest(bool* defer) override; | |
| 64 void WillRedirectRequest(const net::RedirectInfo& redirect_info, | |
| 65 bool* defer) override; | |
| 66 void WillProcessResponse(bool* defer) override; | |
| 67 bool MustProcessResponseBeforeReadingBody() override; | |
| 68 | |
| 69 const char* GetNameForLogging() const override; | |
| 70 | |
| 71 // SafeBrowsingDatabaseManager::Client implementation (called on IO thread): | |
| 72 void OnCheckBrowseUrlResult( | |
| 73 const GURL& url, | |
| 74 SBThreatType threat_type, | |
| 75 const ThreatMetadata& metadata) override; | |
| 76 | |
| 77 protected: | |
| 78 BaseResourceThrottle( | |
| 79 const net::URLRequest* request, | |
| 80 content::ResourceType resource_type, | |
| 81 scoped_refptr<SafeBrowsingDatabaseManager> | |
| 82 database_manager, | |
| 83 scoped_refptr<BaseUIManager> ui_manager); | |
| 84 | |
| 85 ~BaseResourceThrottle() override; | |
| 86 | |
| 87 // If our blocked resource is the main frame, this calls | |
| 88 // ContentSubresourceFilterDriverFactory's | |
| 89 // OnMainResourceMatchedSafeBrowsingBlacklist method. | |
| 90 static void NotifySubresourceFilterOfBlockedResource( | |
| 91 const security_interstitials::UnsafeResource& resource); | |
| 92 | |
| 93 // Does nothing in the base class. Override this to destroy prerender contents | |
| 94 // in chrome. | |
| 95 virtual void MaybeDestroyPrerenderContents( | |
| 96 const content::ResourceRequestInfo* info); | |
| 97 | |
| 98 // Posts a task for StartDisplayingBlockingPage | |
| 99 virtual void StartDisplayingBlockingPageHelper( | |
| 100 security_interstitials::UnsafeResource resource); | |
| 101 | |
| 102 // Called by OnBlockingPageComplete when proceed == false. This removes the | |
| 103 // blocking page. This calls ResourceThrottle::Cancel() to show the previous | |
| 104 // page, but may be overridden in a subclass. | |
| 105 virtual void CancelResourceLoad(); | |
| 106 | |
| 107 scoped_refptr<BaseUIManager> ui_manager(); | |
| 108 | |
| 109 private: | |
| 110 // Describes what phase of the check a throttle is in. | |
| 111 enum State { | |
| 112 // Haven't started checking or checking is complete. Not deferred. | |
| 113 STATE_NONE, | |
| 114 // We have one outstanding URL-check. Could be deferred. | |
| 115 STATE_CHECKING_URL, | |
| 116 // We're displaying a blocking page. Could be deferred. | |
| 117 STATE_DISPLAYING_BLOCKING_PAGE, | |
| 118 }; | |
| 119 | |
| 120 // Describes what stage of the request got paused by the check. | |
| 121 enum DeferState { | |
| 122 DEFERRED_NONE, | |
| 123 DEFERRED_START, | |
| 124 DEFERRED_REDIRECT, | |
| 125 DEFERRED_UNCHECKED_REDIRECT, // unchecked_redirect_url_ is populated. | |
| 126 DEFERRED_PROCESSING, | |
| 127 }; | |
| 128 | |
| 129 scoped_refptr<BaseUIManager> ui_manager_; | |
| 130 | |
| 131 // Called on the IO thread when the user has decided to proceed with the | |
| 132 // current request, or go back. | |
| 133 void OnBlockingPageComplete(bool proceed); | |
| 134 | |
| 135 // Starts running |url| through the safe browsing check. Returns true if the | |
| 136 // URL is safe to visit. Otherwise returns false and will call | |
| 137 // OnBrowseUrlResult() when the check has completed. | |
| 138 bool CheckUrl(const GURL& url); | |
| 139 | |
| 140 // Callback for when the safe browsing check (which was initiated by | |
| 141 // StartCheckingUrl()) has taken longer than kCheckUrlTimeoutMs. | |
| 142 void OnCheckUrlTimeout(); | |
| 143 | |
| 144 // Starts displaying the safe browsing interstitial page. Called on the UI | |
| 145 // thread. | |
| 146 static void StartDisplayingBlockingPage( | |
| 147 const base::WeakPtr<BaseResourceThrottle>& throttle, | |
| 148 scoped_refptr<BaseUIManager> ui_manager, | |
| 149 const security_interstitials::UnsafeResource& resource); | |
| 150 | |
| 151 void ResumeRequest(); | |
| 152 | |
| 153 // For marking network events. |name| and |value| can be null. | |
| 154 void BeginNetLogEvent(net::NetLogEventType type, | |
| 155 const GURL& url, | |
| 156 const char* name, | |
| 157 const char* value); | |
| 158 void EndNetLogEvent(net::NetLogEventType type, | |
| 159 const char* name, | |
| 160 const char* value); | |
| 161 | |
| 162 // The result of the most recent safe browsing check. Only valid to read this | |
| 163 // when state_ != STATE_CHECKING_URL. | |
| 164 safe_browsing::SBThreatType threat_type_; | |
| 165 | |
| 166 // The time when we started deferring the request. | |
| 167 base::TimeTicks defer_start_time_; | |
| 168 | |
| 169 // Timer to abort the safe browsing check if it takes too long. | |
| 170 base::OneShotTimer timer_; | |
| 171 | |
| 172 // The redirect chain for this resource | |
| 173 std::vector<GURL> redirect_urls_; | |
| 174 | |
| 175 // If in DEFERRED_UNCHECKED_REDIRECT state, this is the | |
| 176 // URL we still need to check before resuming. | |
| 177 GURL unchecked_redirect_url_; | |
| 178 GURL url_being_checked_; | |
| 179 | |
| 180 scoped_refptr<SafeBrowsingDatabaseManager> database_manager_; | |
| 181 const net::URLRequest* request_; | |
| 182 | |
| 183 State state_; | |
| 184 DeferState defer_state_; | |
| 185 | |
| 186 const content::ResourceType resource_type_; | |
| 187 net::NetLogWithSource net_log_with_source_; | |
| 188 | |
| 189 DISALLOW_COPY_AND_ASSIGN(BaseResourceThrottle); | |
| 190 }; | |
| 191 | |
| 192 } // namespace safe_browsing | |
| 193 | |
| 194 #endif // COMPONENTS_SAFE_BROWSING_BASE_RESOURCE_THROTTLE_H_ | |
| OLD | NEW |