This source file includes following definitions.
- url
- keyword
- provider_type
- callbacks_
- OnLoaded
- OnURLFetchComplete
- AddSearchProvider
- ScheduleDownload
- RequestCompleted
#include "build/build_config.h"
#include "chrome/browser/search_engines/template_url_fetcher.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url.h"
#include "chrome/browser/search_engines/template_url_fetcher_callbacks.h"
#include "chrome/browser/search_engines/template_url_parser.h"
#include "chrome/browser/search_engines/template_url_service.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/url_fetcher.h"
#include "net/base/load_flags.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_status.h"
class TemplateURLFetcher::RequestDelegate : public net::URLFetcherDelegate {
public:
RequestDelegate(TemplateURLFetcher* fetcher,
const base::string16& keyword,
const GURL& osdd_url,
const GURL& favicon_url,
content::WebContents* web_contents,
TemplateURLFetcherCallbacks* callbacks,
ProviderType provider_type);
virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
GURL url() const { return osdd_url_; }
base::string16 keyword() const { return keyword_; }
ProviderType provider_type() const { return provider_type_; }
private:
void OnLoaded();
void AddSearchProvider();
scoped_ptr<net::URLFetcher> url_fetcher_;
TemplateURLFetcher* fetcher_;
scoped_ptr<TemplateURL> template_url_;
base::string16 keyword_;
const GURL osdd_url_;
const GURL favicon_url_;
const ProviderType provider_type_;
scoped_ptr<TemplateURLFetcherCallbacks> callbacks_;
scoped_ptr<TemplateURLService::Subscription> template_url_subscription_;
DISALLOW_COPY_AND_ASSIGN(RequestDelegate);
};
TemplateURLFetcher::RequestDelegate::RequestDelegate(
TemplateURLFetcher* fetcher,
const base::string16& keyword,
const GURL& osdd_url,
const GURL& favicon_url,
content::WebContents* web_contents,
TemplateURLFetcherCallbacks* callbacks,
ProviderType provider_type)
: url_fetcher_(net::URLFetcher::Create(
osdd_url, net::URLFetcher::GET, this)),
fetcher_(fetcher),
keyword_(keyword),
osdd_url_(osdd_url),
favicon_url_(favicon_url),
provider_type_(provider_type),
callbacks_(callbacks) {
TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(
fetcher_->profile());
DCHECK(model);
if (!model->loaded()) {
template_url_subscription_ = model->RegisterOnLoadedCallback(
base::Bind(&TemplateURLFetcher::RequestDelegate::OnLoaded,
base::Unretained(this)));
model->Load();
}
url_fetcher_->SetRequestContext(fetcher->profile()->GetRequestContext());
if (web_contents) {
content::AssociateURLFetcherWithRenderFrame(
url_fetcher_.get(),
web_contents->GetURL(),
web_contents->GetRenderProcessHost()->GetID(),
web_contents->GetMainFrame()->GetRoutingID());
}
url_fetcher_->Start();
}
void TemplateURLFetcher::RequestDelegate::OnLoaded() {
template_url_subscription_.reset();
if (!template_url_.get())
return;
AddSearchProvider();
}
void TemplateURLFetcher::RequestDelegate::OnURLFetchComplete(
const net::URLFetcher* source) {
std::string data;
if (!source->GetStatus().is_success() ||
((source->GetResponseCode() != -1) &&
(source->GetResponseCode() != 200)) ||
!source->GetResponseAsString(&data)) {
fetcher_->RequestCompleted(this);
return;
}
template_url_.reset(TemplateURLParser::Parse(fetcher_->profile(), false,
data.data(), data.length(), NULL));
if (!template_url_.get() || !template_url_->url_ref().SupportsReplacement()) {
fetcher_->RequestCompleted(this);
return;
}
if (provider_type_ != AUTODETECTED_PROVIDER || keyword_.empty()) {
keyword_ = template_url_->keyword();
DCHECK(!keyword_.empty());
}
TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(
fetcher_->profile());
if (!model->loaded())
return;
AddSearchProvider();
}
void TemplateURLFetcher::RequestDelegate::AddSearchProvider() {
DCHECK(template_url_.get());
DCHECK(!keyword_.empty());
Profile* profile = fetcher_->profile();
TemplateURLService* model = TemplateURLServiceFactory::GetForProfile(profile);
DCHECK(model);
DCHECK(model->loaded());
TemplateURL* existing_url = NULL;
if (model->CanReplaceKeyword(keyword_, GURL(template_url_->url()),
&existing_url)) {
if (existing_url)
model->Remove(existing_url);
} else if (provider_type_ == AUTODETECTED_PROVIDER) {
fetcher_->RequestCompleted(this);
return;
}
TemplateURLData data(template_url_->data());
data.SetKeyword(keyword_);
data.originating_url = osdd_url_;
if (!data.favicon_url.is_valid())
data.favicon_url = favicon_url_;
switch (provider_type_) {
case AUTODETECTED_PROVIDER:
data.safe_for_autoreplace = true;
model->Add(new TemplateURL(profile, data));
break;
case EXPLICIT_PROVIDER:
callbacks_->ConfirmAddSearchProvider(new TemplateURL(profile, data),
profile);
break;
default:
NOTREACHED();
break;
}
fetcher_->RequestCompleted(this);
}
TemplateURLFetcher::TemplateURLFetcher(Profile* profile) : profile_(profile) {
DCHECK(profile_);
}
TemplateURLFetcher::~TemplateURLFetcher() {
}
void TemplateURLFetcher::ScheduleDownload(
const base::string16& keyword,
const GURL& osdd_url,
const GURL& favicon_url,
content::WebContents* web_contents,
TemplateURLFetcherCallbacks* callbacks,
ProviderType provider_type) {
DCHECK(osdd_url.is_valid());
scoped_ptr<TemplateURLFetcherCallbacks> owned_callbacks(callbacks);
TemplateURLService* url_model =
TemplateURLServiceFactory::GetForProfile(profile());
if (!url_model)
return;
if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) {
DCHECK(!keyword.empty());
if (!url_model->loaded()) {
url_model->Load();
return;
}
const TemplateURL* template_url =
url_model->GetTemplateURLForKeyword(keyword);
if (template_url && (!template_url->safe_for_autoreplace() ||
template_url->originating_url() == osdd_url))
return;
}
for (Requests::iterator i = requests_.begin(); i != requests_.end(); ++i) {
if (((*i)->url() == osdd_url) ||
((provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) &&
((*i)->keyword() == keyword)))
return;
}
requests_.push_back(
new RequestDelegate(this, keyword, osdd_url, favicon_url, web_contents,
owned_callbacks.release(), provider_type));
}
void TemplateURLFetcher::RequestCompleted(RequestDelegate* request) {
Requests::iterator i =
std::find(requests_.begin(), requests_.end(), request);
DCHECK(i != requests_.end());
requests_.weak_erase(i);
delete request;
}