| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 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 #include "chrome/browser/template_url_fetcher.h" | |
| 6 | |
| 7 #include "chrome/browser/profile.h" | |
| 8 #include "chrome/browser/template_url.h" | |
| 9 #include "chrome/browser/template_url_model.h" | |
| 10 #include "chrome/browser/template_url_parser.h" | |
| 11 #include "chrome/browser/views/edit_keyword_controller.h" | |
| 12 | |
| 13 // RequestDelegate ------------------------------------------------------------ | |
| 14 | |
| 15 void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete( | |
| 16 const URLFetcher* source, | |
| 17 const GURL& url, | |
| 18 const URLRequestStatus& status, | |
| 19 int response_code, | |
| 20 const ResponseCookies& cookies, | |
| 21 const std::string& data) { | |
| 22 // Make sure we can still replace the keyword. | |
| 23 if (response_code != 200) { | |
| 24 fetcher_->RequestCompleted(this); | |
| 25 // WARNING: RequestCompleted deletes us. | |
| 26 return; | |
| 27 } | |
| 28 | |
| 29 scoped_ptr<TemplateURL> template_url(new TemplateURL()); | |
| 30 if (TemplateURLParser::Parse( | |
| 31 reinterpret_cast<const unsigned char*>(data.c_str()), | |
| 32 data.length(), | |
| 33 NULL, | |
| 34 template_url.get()) && | |
| 35 template_url->url() && template_url->url()->SupportsReplacement()) { | |
| 36 TemplateURLModel* model = fetcher_->profile()->GetTemplateURLModel(); | |
| 37 const TemplateURL* existing_url; | |
| 38 if (!model || !model->loaded() || | |
| 39 !model->CanReplaceKeyword(keyword_, template_url->url()->url(), | |
| 40 &existing_url)) { | |
| 41 // TODO(pamg): If we're coming from JS (not autodetected) and this URL | |
| 42 // already exists in the model, consider bringing up the | |
| 43 // EditKeywordController to edit it. This would be helpful feedback in | |
| 44 // the case of clicking a button twice, and annoying in the case of a | |
| 45 // page that calls AddSearchProvider() in JS without a user action. | |
| 46 fetcher_->RequestCompleted(this); | |
| 47 // WARNING: RequestCompleted deletes us. | |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 if (existing_url) | |
| 52 model->Remove(existing_url); | |
| 53 | |
| 54 // The short name is what is shown to the user. We reset it to make sure | |
| 55 // we don't display random text from the web. | |
| 56 template_url->set_short_name(keyword_); | |
| 57 template_url->set_keyword(keyword_); | |
| 58 template_url->set_originating_url(osdd_url_); | |
| 59 | |
| 60 // The page may have specified a URL to use for favicons, if not, set it. | |
| 61 if (!template_url->GetFavIconURL().is_valid()) | |
| 62 template_url->SetFavIconURL(favicon_url_); | |
| 63 | |
| 64 if (autodetected_) { | |
| 65 // Mark the keyword as replaceable so it can be removed if necessary. | |
| 66 template_url->set_safe_for_autoreplace(true); | |
| 67 model->Add(template_url.release()); | |
| 68 } else { | |
| 69 // Confirm addition and allow user to edit default choices. It's ironic | |
| 70 // that only *non*-autodetected additions get confirmed, but the user | |
| 71 // expects feedback that his action did something. | |
| 72 // The edit controller will take care of adding the URL to the model, | |
| 73 // which takes ownership, or of deleting it if the add is cancelled. | |
| 74 EditKeywordController* controller = | |
| 75 new EditKeywordController(parent_window_, | |
| 76 template_url.release(), | |
| 77 NULL, // no KeywordEditorView | |
| 78 fetcher_->profile()); | |
| 79 controller->Show(); | |
| 80 } | |
| 81 } | |
| 82 fetcher_->RequestCompleted(this); | |
| 83 // WARNING: RequestCompleted deletes us. | |
| 84 } | |
| 85 | |
| 86 // TemplateURLFetcher --------------------------------------------------------- | |
| 87 | |
| 88 TemplateURLFetcher::TemplateURLFetcher(Profile* profile) : profile_(profile) { | |
| 89 DCHECK(profile_); | |
| 90 } | |
| 91 | |
| 92 void TemplateURLFetcher::ScheduleDownload(const std::wstring& keyword, | |
| 93 const GURL& osdd_url, | |
| 94 const GURL& favicon_url, | |
| 95 const HWND parent_window, | |
| 96 bool autodetected) { | |
| 97 DCHECK(!keyword.empty() && osdd_url.is_valid()); | |
| 98 // Make sure we aren't already downloading this request. | |
| 99 for (std::vector<RequestDelegate*>::iterator i = requests_->begin(); | |
| 100 i != requests_->end(); ++i) { | |
| 101 if ((*i)->url() == osdd_url || (*i)->keyword() == keyword) | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 requests_->push_back( | |
| 106 new RequestDelegate(this, keyword, osdd_url, favicon_url, parent_window, | |
| 107 autodetected)); | |
| 108 } | |
| 109 | |
| 110 void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) { | |
| 111 DCHECK(find(requests_->begin(), requests_->end(), request) != | |
| 112 requests_->end()); | |
| 113 requests_->erase(find(requests_->begin(), requests_->end(), request)); | |
| 114 delete request; | |
| 115 } | |
| 116 | |
| OLD | NEW |