summaryrefslogtreecommitdiffstats
path: root/chrome/browser/search_engines/chrome_template_url_service_client.cc
blob: a79f2fc7ef9ef84760946f47ccd4e4c90be3427d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/search_engines/chrome_template_url_service_client.h"

#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/history/history_notifications.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_source.h"
#include "extensions/common/constants.h"

ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient(Profile* profile)
    : profile_(profile),
      owner_(NULL) {
  DCHECK(profile);

  // Register for notifications.
  // TODO(sky): bug 1166191. The keywords should be moved into the history
  // db, which will mean we no longer need this notification and the history
  // backend can handle automatically adding the search terms as the user
  // navigates.
  content::Source<Profile> profile_source(profile->GetOriginalProfile());
  notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
                              profile_source);
}

ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() {
}

void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) {
  DCHECK(!owner_);
  owner_ = owner;
}

void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword(
    TemplateURLID id) {
  HistoryService* history_service =
      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
  if (history_service)
    history_service->DeleteAllSearchTermsForKeyword(id);
}

void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL(
    const GURL& url,
    TemplateURLID id,
    const base::string16& term) {
  HistoryService* history_service =
      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
  if (history_service)
    history_service->SetKeywordSearchTermsForURL(url, id, term);
}

void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) {
  HistoryService* history_service =
      HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
  if (history_service)
    history_service->AddPage(url, base::Time::Now(), NULL, 0, GURL(),
                             history::RedirectList(),
                             ui::PAGE_TRANSITION_KEYWORD_GENERATED,
                             history::SOURCE_BROWSED, false);
}

void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary(
    TemplateURL* template_url) {
  const TemplateURLData& data = template_url->data();
  GURL url(data.url());
  if (url.SchemeIs(extensions::kExtensionScheme)) {
    const std::string& extension_id = url.host();
    template_url->set_extension_info(make_scoped_ptr(
        new TemplateURL::AssociatedExtensionInfo(
            TemplateURL::OMNIBOX_API_EXTENSION, extension_id)));
  }
}

void ChromeTemplateURLServiceClient::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  DCHECK_EQ(type, chrome::NOTIFICATION_HISTORY_URL_VISITED);

  if (!owner_)
    return;

  content::Details<history::URLVisitedDetails> history_details(details);
  TemplateURLService::URLVisitedDetails visited_details;
  visited_details.url = history_details->row.url();
  visited_details.is_keyword_transition =
      ui::PageTransitionStripQualifier(history_details->transition) ==
      ui::PAGE_TRANSITION_KEYWORD;
  owner_->OnHistoryURLVisited(visited_details);
}