OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/safe_browsing/client_side_detection_service.h" | 5 #include "chrome/browser/safe_browsing/client_side_detection_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/file_path.h" | 8 #include "base/file_path.h" |
9 #include "base/file_util_proxy.h" | 9 #include "base/file_util_proxy.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 22 matching lines...) Expand all Loading... | |
33 scoped_ptr<ClientReportPhishingRequestCallback> callback; | 33 scoped_ptr<ClientReportPhishingRequestCallback> callback; |
34 GURL phishing_url; | 34 GURL phishing_url; |
35 }; | 35 }; |
36 | 36 |
37 ClientSideDetectionService::ClientSideDetectionService( | 37 ClientSideDetectionService::ClientSideDetectionService( |
38 const FilePath& model_path, | 38 const FilePath& model_path, |
39 URLRequestContextGetter* request_context_getter) | 39 URLRequestContextGetter* request_context_getter) |
40 : model_path_(model_path), | 40 : model_path_(model_path), |
41 model_status_(UNKNOWN_STATUS), | 41 model_status_(UNKNOWN_STATUS), |
42 model_file_(base::kInvalidPlatformFileValue), | 42 model_file_(base::kInvalidPlatformFileValue), |
43 model_fetcher_(NULL), | |
44 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), | 43 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)), |
45 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), | 44 ALLOW_THIS_IN_INITIALIZER_LIST(callback_factory_(this)), |
46 request_context_getter_(request_context_getter) { | 45 request_context_getter_(request_context_getter) { |
47 } | 46 } |
48 | 47 |
49 ClientSideDetectionService::~ClientSideDetectionService() { | 48 ClientSideDetectionService::~ClientSideDetectionService() { |
50 method_factory_.RevokeAll(); | 49 method_factory_.RevokeAll(); |
51 STLDeleteContainerPairPointers(client_phishing_reports_.begin(), | 50 STLDeleteContainerPairPointers(client_phishing_reports_.begin(), |
52 client_phishing_reports_.end()); | 51 client_phishing_reports_.end()); |
53 client_phishing_reports_.clear(); | 52 client_phishing_reports_.clear(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
98 phishing_url, score, callback)); | 97 phishing_url, score, callback)); |
99 } | 98 } |
100 | 99 |
101 void ClientSideDetectionService::OnURLFetchComplete( | 100 void ClientSideDetectionService::OnURLFetchComplete( |
102 const URLFetcher* source, | 101 const URLFetcher* source, |
103 const GURL& url, | 102 const GURL& url, |
104 const net::URLRequestStatus& status, | 103 const net::URLRequestStatus& status, |
105 int response_code, | 104 int response_code, |
106 const ResponseCookies& cookies, | 105 const ResponseCookies& cookies, |
107 const std::string& data) { | 106 const std::string& data) { |
108 if (source == model_fetcher_) { | 107 if (source == model_fetcher_.get()) { |
109 HandleModelResponse(source, url, status, response_code, cookies, data); | 108 HandleModelResponse(source, url, status, response_code, cookies, data); |
110 // The fetcher object will be invalid after this method returns. | |
111 model_fetcher_ = NULL; | |
112 } else if (client_phishing_reports_.find(source) != | 109 } else if (client_phishing_reports_.find(source) != |
113 client_phishing_reports_.end()) { | 110 client_phishing_reports_.end()) { |
114 HandlePhishingVerdict(source, url, status, response_code, cookies, data); | 111 HandlePhishingVerdict(source, url, status, response_code, cookies, data); |
noelutz
2011/01/19 17:14:25
How about deleting source here and then calling re
| |
115 } else { | 112 } else { |
116 NOTREACHED(); | 113 NOTREACHED(); |
117 } | 114 } |
118 delete source; | |
119 } | 115 } |
120 | 116 |
121 void ClientSideDetectionService::SetModelStatus(ModelStatus status) { | 117 void ClientSideDetectionService::SetModelStatus(ModelStatus status) { |
122 DCHECK_NE(READY_STATUS, model_status_); | 118 DCHECK_NE(READY_STATUS, model_status_); |
123 model_status_ = status; | 119 model_status_ = status; |
124 if (READY_STATUS == status || ERROR_STATUS == status) { | 120 if (READY_STATUS == status || ERROR_STATUS == status) { |
125 for (size_t i = 0; i < open_callbacks_.size(); ++i) { | 121 for (size_t i = 0; i < open_callbacks_.size(); ++i) { |
126 open_callbacks_[i]->Run(model_file_); | 122 open_callbacks_[i]->Run(model_file_); |
127 } | 123 } |
128 STLDeleteElements(&open_callbacks_); | 124 STLDeleteElements(&open_callbacks_); |
129 } else { | 125 } else { |
130 NOTREACHED(); | 126 NOTREACHED(); |
131 } | 127 } |
132 } | 128 } |
133 | 129 |
134 void ClientSideDetectionService::OpenModelFileDone( | 130 void ClientSideDetectionService::OpenModelFileDone( |
135 base::PlatformFileError error_code, | 131 base::PlatformFileError error_code, |
136 base::PassPlatformFile file, | 132 base::PassPlatformFile file, |
137 bool created) { | 133 bool created) { |
138 DCHECK(!created); | 134 DCHECK(!created); |
139 if (base::PLATFORM_FILE_OK == error_code) { | 135 if (base::PLATFORM_FILE_OK == error_code) { |
140 // The model file already exists. There is no need to fetch the model. | 136 // The model file already exists. There is no need to fetch the model. |
141 model_file_ = file.ReleaseValue(); | 137 model_file_ = file.ReleaseValue(); |
142 SetModelStatus(READY_STATUS); | 138 SetModelStatus(READY_STATUS); |
143 } else if (base::PLATFORM_FILE_ERROR_NOT_FOUND == error_code) { | 139 } else if (base::PLATFORM_FILE_ERROR_NOT_FOUND == error_code) { |
144 // We need to fetch the model since it does not exist yet. | 140 // We need to fetch the model since it does not exist yet. |
145 model_fetcher_ = URLFetcher::Create(0 /* ID is not used */, | 141 model_fetcher_.reset(URLFetcher::Create(0 /* ID is not used */, |
146 GURL(kClientModelUrl), | 142 GURL(kClientModelUrl), |
147 URLFetcher::GET, | 143 URLFetcher::GET, |
148 this); | 144 this)); |
149 model_fetcher_->set_request_context(request_context_getter_.get()); | 145 model_fetcher_->set_request_context(request_context_getter_.get()); |
150 model_fetcher_->Start(); | 146 model_fetcher_->Start(); |
151 } else { | 147 } else { |
152 // It is not clear what we should do in this case. For now we simply fail. | 148 // It is not clear what we should do in this case. For now we simply fail. |
153 // Hopefully, we'll be able to read the model during the next browser | 149 // Hopefully, we'll be able to read the model during the next browser |
154 // restart. | 150 // restart. |
155 SetModelStatus(ERROR_STATUS); | 151 SetModelStatus(ERROR_STATUS); |
156 } | 152 } |
157 } | 153 } |
158 | 154 |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
297 scoped_ptr<ClientReportInfo> info(client_phishing_reports_[source]); | 293 scoped_ptr<ClientReportInfo> info(client_phishing_reports_[source]); |
298 if (status.is_success() && RC_REQUEST_OK == response_code && | 294 if (status.is_success() && RC_REQUEST_OK == response_code && |
299 response.ParseFromString(data)) { | 295 response.ParseFromString(data)) { |
300 info->callback->Run(info->phishing_url, response.phishy()); | 296 info->callback->Run(info->phishing_url, response.phishy()); |
301 } else { | 297 } else { |
302 DLOG(ERROR) << "Unable to get the server verdict for URL: " | 298 DLOG(ERROR) << "Unable to get the server verdict for URL: " |
303 << info->phishing_url; | 299 << info->phishing_url; |
304 info->callback->Run(info->phishing_url, false); | 300 info->callback->Run(info->phishing_url, false); |
305 } | 301 } |
306 client_phishing_reports_.erase(source); | 302 client_phishing_reports_.erase(source); |
303 delete source; | |
eroman
2011/01/20 00:12:21
Isn't this a double-free?
You changed model_fetche
Brian Ryner
2011/01/20 00:20:37
I don't think so, no. ClientSideDetectionService
| |
307 } | 304 } |
308 | 305 |
309 } // namespace safe_browsing | 306 } // namespace safe_browsing |
OLD | NEW |