OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #include "chrome/browser/search_engines/template_url_fetcher.h" | 7 #include "chrome/browser/search_engines/template_url_fetcher.h" |
8 | 8 |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); | 124 DCHECK(type == chrome::NOTIFICATION_TEMPLATE_URL_SERVICE_LOADED); |
125 | 125 |
126 if (!template_url_.get()) | 126 if (!template_url_.get()) |
127 return; | 127 return; |
128 AddSearchProvider(); | 128 AddSearchProvider(); |
129 // WARNING: AddSearchProvider deletes us. | 129 // WARNING: AddSearchProvider deletes us. |
130 } | 130 } |
131 | 131 |
132 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( | 132 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( |
133 const content::URLFetcher* source) { | 133 const content::URLFetcher* source) { |
134 template_url_.reset(new TemplateURL()); | |
135 | |
136 // Validation checks. | 134 // Validation checks. |
137 // Make sure we can still replace the keyword, i.e. the fetch was successful. | 135 // Make sure we can still replace the keyword, i.e. the fetch was successful. |
138 // If the OSDD file was loaded HTTP, we also have to check the response_code. | 136 // If the OSDD file was loaded HTTP, we also have to check the response_code. |
139 // For other schemes, e.g. when the OSDD file is bundled with an extension, | 137 // For other schemes, e.g. when the OSDD file is bundled with an extension, |
140 // the response_code is not applicable and should be -1. Also, ensure that | 138 // the response_code is not applicable and should be -1. Also, ensure that |
141 // the returned information results in a valid search URL. | 139 // the returned information results in a valid search URL. |
142 std::string data; | 140 std::string data; |
143 if (!source->GetStatus().is_success() || | 141 if (!source->GetStatus().is_success() || |
144 ((source->GetResponseCode() != -1) && | 142 ((source->GetResponseCode() != -1) && |
145 (source->GetResponseCode() != 200)) || | 143 (source->GetResponseCode() != 200)) || |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 | 176 |
179 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { | 177 void TemplateURLFetcher::RequestDelegate::AddSearchProvider() { |
180 DCHECK(template_url_.get()); | 178 DCHECK(template_url_.get()); |
181 DCHECK(!keyword_.empty()); | 179 DCHECK(!keyword_.empty()); |
182 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( | 180 TemplateURLService* model = TemplateURLServiceFactory::GetForProfile( |
183 fetcher_->profile()); | 181 fetcher_->profile()); |
184 DCHECK(model); | 182 DCHECK(model); |
185 DCHECK(model->loaded()); | 183 DCHECK(model->loaded()); |
186 | 184 |
187 const TemplateURL* existing_url = NULL; | 185 const TemplateURL* existing_url = NULL; |
188 GURL gurl(template_url_->url()->url()); | 186 if (model->CanReplaceKeyword(keyword_, GURL(template_url_->url()->url()), |
189 if (!model->CanReplaceKeyword(keyword_, gurl, &existing_url)) { | 187 &existing_url)) { |
190 if (provider_type_ == AUTODETECTED_PROVIDER) { | 188 if (existing_url) |
191 fetcher_->RequestCompleted(this); | 189 model->Remove(existing_url); |
192 // WARNING: RequestCompleted deletes us. | 190 } else if (provider_type_ == AUTODETECTED_PROVIDER) { |
193 return; | 191 fetcher_->RequestCompleted(this); // WARNING: Deletes us! |
194 } | 192 return; |
195 | |
196 existing_url = NULL; | |
197 | |
198 // If we're coming from JS (neither autodetected nor failure to load the | |
199 // template URL model) and this URL already exists in the model, we bring up | |
200 // the EditKeywordController to edit it. This is helpful feedback in the | |
201 // case of clicking a button twice, and annoying in the case of a page that | |
202 // calls AddSearchProvider() in JS without a user action. | |
203 keyword_.clear(); | |
204 } | 193 } |
205 | 194 |
206 if (existing_url) | |
207 model->Remove(existing_url); | |
208 | |
209 // The short name is what is shown to the user. We preserve original names | 195 // The short name is what is shown to the user. We preserve original names |
210 // since it is better when generated keyword in many cases. | 196 // since it is better when generated keyword in many cases. |
211 template_url_->set_keyword(keyword_); | 197 template_url_->set_keyword(keyword_); |
212 template_url_->set_originating_url(osdd_url_); | 198 template_url_->set_originating_url(osdd_url_); |
213 | 199 |
214 // The page may have specified a URL to use for favicons, if not, set it. | 200 // The page may have specified a URL to use for favicons, if not, set it. |
215 if (!template_url_->GetFaviconURL().is_valid()) | 201 if (!template_url_->GetFaviconURL().is_valid()) |
216 template_url_->SetFaviconURL(favicon_url_); | 202 template_url_->SetFaviconURL(favicon_url_); |
217 | 203 |
218 switch (provider_type_) { | 204 switch (provider_type_) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
254 void TemplateURLFetcher::ScheduleDownload( | 240 void TemplateURLFetcher::ScheduleDownload( |
255 const string16& keyword, | 241 const string16& keyword, |
256 const GURL& osdd_url, | 242 const GURL& osdd_url, |
257 const GURL& favicon_url, | 243 const GURL& favicon_url, |
258 content::WebContents* web_contents, | 244 content::WebContents* web_contents, |
259 TemplateURLFetcherCallbacks* callbacks, | 245 TemplateURLFetcherCallbacks* callbacks, |
260 ProviderType provider_type) { | 246 ProviderType provider_type) { |
261 DCHECK(osdd_url.is_valid()); | 247 DCHECK(osdd_url.is_valid()); |
262 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); | 248 scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks); |
263 | 249 |
264 // For JS added OSDD empty keyword is OK because we will generate keyword | |
265 // later from OSDD content. | |
266 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER && | |
267 keyword.empty()) | |
268 return; | |
269 TemplateURLService* url_model = | 250 TemplateURLService* url_model = |
270 TemplateURLServiceFactory::GetForProfile(profile()); | 251 TemplateURLServiceFactory::GetForProfile(profile()); |
271 if (!url_model) | 252 if (!url_model) |
272 return; | 253 return; |
273 | 254 |
274 // For a JS-added OSDD, the provided keyword is irrelevant because we will | 255 // For a JS-added OSDD, the provided keyword is irrelevant because we will |
275 // generate a keyword later from the OSDD content. For the autodetected case, | 256 // generate a keyword later from the OSDD content. For the autodetected case, |
276 // we need a valid keyword up front. | 257 // we need a valid keyword up front. |
277 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { | 258 if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) { |
| 259 DCHECK(!keyword.empty()); |
| 260 |
278 if (!url_model->loaded()) { | 261 if (!url_model->loaded()) { |
279 // We could try to set up a callback to this function again once the model | 262 // We could try to set up a callback to this function again once the model |
280 // is loaded but since this is an auto-add case anyway, meh. | 263 // is loaded but since this is an auto-add case anyway, meh. |
281 url_model->Load(); | 264 url_model->Load(); |
282 return; | 265 return; |
283 } | 266 } |
284 | 267 |
285 const TemplateURL* template_url = | 268 const TemplateURL* template_url = |
286 url_model->GetTemplateURLForKeyword(keyword); | 269 url_model->GetTemplateURLForKeyword(keyword); |
287 if (template_url && (!template_url->safe_for_autoreplace() || | 270 if (template_url && (!template_url->safe_for_autoreplace() || |
288 template_url->originating_url() == osdd_url)) | 271 template_url->originating_url() == osdd_url)) |
289 return; | 272 return; |
290 } | 273 } |
291 | 274 |
292 // Make sure we aren't already downloading this request. | 275 // Make sure we aren't already downloading this request. |
293 for (Requests::iterator i = requests_->begin(); i != requests_->end(); ++i) { | 276 for (Requests::iterator i = requests_->begin(); i != requests_->end(); ++i) { |
294 if ((*i)->url() == osdd_url || (*i)->keyword() == keyword) | 277 if (((*i)->url() == osdd_url) || |
| 278 ((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) && |
| 279 ((*i)->keyword() == keyword))) |
295 return; | 280 return; |
296 } | 281 } |
297 | 282 |
298 requests_->push_back( | 283 requests_->push_back( |
299 new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents, | 284 new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents, |
300 owned_callbacks.release(), provider_type)); | 285 owned_callbacks.release(), provider_type)); |
301 } | 286 } |
302 | 287 |
303 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | 288 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { |
304 Requests::iterator i = | 289 Requests::iterator i = |
305 std::find(requests_->begin(), requests_->end(), request); | 290 std::find(requests_->begin(), requests_->end(), request); |
306 DCHECK(i != requests_->end()); | 291 DCHECK(i != requests_->end()); |
307 requests_->erase(i); | 292 requests_->erase(i); |
308 delete request; | 293 delete request; |
309 } | 294 } |
OLD | NEW |