1 // Copyright 2014 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.
5 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
7 #include "chrome/browser/history/history_service.h"
8 #include "components/search_engines/template_url_service.h"
9 #include "extensions/common/constants.h"
11 ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient(
12 HistoryService
* history_service
)
13 : owner_(NULL
), history_service_(history_service
) {
14 // TODO(sky): bug 1166191. The keywords should be moved into the history
15 // db, which will mean we no longer need this notification and the history
16 // backend can handle automatically adding the search terms as the user
19 history_service_
->AddObserver(this);
22 ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() {
25 void ChromeTemplateURLServiceClient::Shutdown() {
26 // ChromeTemplateURLServiceClient is owned by TemplateURLService which is a
27 // KeyedService with a dependency on HistoryService, thus |history_service_|
28 // outlives the ChromeTemplateURLServiceClient.
30 // Remove self from |history_service_| observers in the shutdown phase of the
31 // two-phases since KeyedService are not supposed to use a dependend service
32 // after the Shutdown call.
34 history_service_
->RemoveObserver(this);
37 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService
* owner
) {
42 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword(
45 history_service_
->DeleteAllSearchTermsForKeyword(id
);
48 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL(
51 const base::string16
& term
) {
53 history_service_
->SetKeywordSearchTermsForURL(url
, id
, term
);
56 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL
& url
) {
58 history_service_
->AddPage(url
, base::Time::Now(), NULL
, 0, GURL(),
59 history::RedirectList(),
60 ui::PAGE_TRANSITION_KEYWORD_GENERATED
,
61 history::SOURCE_BROWSED
, false);
64 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary(
65 TemplateURL
* template_url
) {
66 const TemplateURLData
& data
= template_url
->data();
68 if (url
.SchemeIs(extensions::kExtensionScheme
)) {
69 const std::string
& extension_id
= url
.host();
70 template_url
->set_extension_info(make_scoped_ptr(
71 new TemplateURL::AssociatedExtensionInfo(
72 TemplateURL::OMNIBOX_API_EXTENSION
, extension_id
)));
76 void ChromeTemplateURLServiceClient::OnURLVisited(
77 HistoryService
* history_service
,
78 ui::PageTransition transition
,
79 const history::URLRow
& row
,
80 const history::RedirectList
& redirects
,
81 base::Time visit_time
) {
82 DCHECK_EQ(history_service_
, history_service
);
86 TemplateURLService::URLVisitedDetails visited_details
;
87 visited_details
.url
= row
.url();
88 visited_details
.is_keyword_transition
=
89 ui::PageTransitionCoreTypeIs(transition
, ui::PAGE_TRANSITION_KEYWORD
);
90 owner_
->OnHistoryURLVisited(visited_details
);