Commit message: None
CVE: None
Vulnerability description: None

Project: Chrome

void AssociateURLFetcherWithWebContents(content::WebContents* web_contents,
                                        net::URLFetcher* url_fetcher) {
  content::AssociateURLFetcherWithRenderFrame(
      url_fetcher,
      web_contents->GetURL(),
      web_contents->GetRenderProcessHost()->GetID(),
      web_contents->GetMainFrame()->GetRoutingID());
}

Project: Chrome

void SearchEngineTabHelper::DidNavigateMainFrame(
    const content::LoadCommittedDetails& /*details*/,
    const content::FrameNavigateParams& params) {
  GenerateKeywordIfNecessary(params);
}

Project: Chrome

void SearchEngineTabHelper::GenerateKeywordIfNecessary(
    const content::FrameNavigateParams& params) {
  if (!params.searchable_form_url.is_valid())
    return;

  Profile* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  if (profile->IsOffTheRecord())
    return;

  const NavigationController& controller = web_contents()->GetController();
  int last_index = controller.GetLastCommittedEntryIndex();
  if (last_index <= 0)
    return;

  base::string16 keyword(GenerateKeywordFromNavigationEntry(
      controller.GetEntryAtIndex(last_index - 1),
      profile->GetPrefs()->GetString(prefs::kAcceptLanguages)));
  if (keyword.empty())
    return;

  TemplateURLService* url_service =
      TemplateURLServiceFactory::GetForProfile(profile);
  if (!url_service)
    return;

  if (!url_service->loaded()) {
    url_service->Load();
    return;
  }

  TemplateURL* current_url;
  GURL url = params.searchable_form_url;
  if (!url_service->CanAddAutogeneratedKeyword(keyword, url, &current_url))
    return;

  if (current_url) {
    if (current_url->originating_url().is_valid()) {
      return;
    }
    url_service->Remove(current_url);
  }

  TemplateURLData data;
  data.SetShortName(keyword);
  data.SetKeyword(keyword);
  data.SetURL(url.spec());
  DCHECK(controller.GetLastCommittedEntry());
  const GURL& current_favicon =
      controller.GetLastCommittedEntry()->GetFavicon().url;
  data.favicon_url = current_favicon.is_valid() ?
      current_favicon : TemplateURL::GenerateFaviconURL(params.referrer.url);
  data.safe_for_autoreplace = true;
  data.input_encodings.push_back(params.searchable_form_encoding);
  url_service->Add(new TemplateURL(data));
}

Project: Chrome

void SearchEngineTabHelper::OnDownloadedOSDD(
    scoped_ptr<TemplateURL> template_url) {
  Profile* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  delegate_->ConfirmAddSearchProvider(template_url.release(), profile);
}

Project: Chrome

bool SearchEngineTabHelper::OnMessageReceived(
    const IPC::Message& message,
    content::RenderFrameHost* render_frame_host) {
  bool handled = true;
  IPC_BEGIN_MESSAGE_MAP(SearchEngineTabHelper, message)
    IPC_MESSAGE_HANDLER(ChromeViewHostMsg_PageHasOSDD, OnPageHasOSDD)
    IPC_MESSAGE_UNHANDLED(handled = false)
  IPC_END_MESSAGE_MAP()

  return handled;
}

Project: Chrome

void SearchEngineTabHelper::OnPageHasOSDD(
    const GURL& page_url,
    const GURL& osdd_url,
    const search_provider::OSDDType& msg_provider_type) {

  if (!osdd_url.is_valid() || !osdd_url.SchemeIsHTTPOrHTTPS())
    return;

  Profile* profile =
      Profile::FromBrowserContext(web_contents()->GetBrowserContext());
  if (page_url != web_contents()->GetLastCommittedURL() ||
      !TemplateURLFetcherFactory::GetForProfile(profile) ||
      profile->IsOffTheRecord())
    return;

  TemplateURLFetcher::ProviderType provider_type =
      (msg_provider_type == search_provider::AUTODETECTED_PROVIDER) ?
          TemplateURLFetcher::AUTODETECTED_PROVIDER :
          TemplateURLFetcher::EXPLICIT_PROVIDER;

  const NavigationController& controller = web_contents()->GetController();
  const NavigationEntry* entry = controller.GetLastCommittedEntry();
  for (int index = controller.GetLastCommittedEntryIndex();
       (index > 0) && IsFormSubmit(entry);
       entry = controller.GetEntryAtIndex(index))
    --index;
  if (!entry || IsFormSubmit(entry))
    return;

  base::string16 keyword;
  if (provider_type == TemplateURLFetcher::AUTODETECTED_PROVIDER) {
    keyword = GenerateKeywordFromNavigationEntry(
        entry, profile->GetPrefs()->GetString(prefs::kAcceptLanguages));
    if (keyword.empty())
      return;
  }

  TemplateURLFetcherFactory::GetForProfile(profile)->ScheduleDownload(
      keyword, osdd_url, entry->GetFavicon().url,
      base::Bind(&AssociateURLFetcherWithWebContents, web_contents()),
      base::Bind(&SearchEngineTabHelper::OnDownloadedOSDD,
                 weak_ptr_factory_.GetWeakPtr()),
      provider_type);
}

Project: Chrome

SearchEngineTabHelper::SearchEngineTabHelper(WebContents* web_contents)
    : content::WebContentsObserver(web_contents),
      delegate_(nullptr),
      weak_ptr_factory_(this) {
  DCHECK(web_contents);
}

Project: Chrome

SearchEngineTabHelper::~SearchEngineTabHelper() {
}