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 "components/history/core/browser/history_service.h"
8 #include "components/search_engines/template_url_service.h"
9 #include "extensions/common/constants.h"
11 ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient(
12 history::HistoryService
* history_service
)
14 history_service_observer_(this),
15 history_service_(history_service
) {
16 // TODO(sky): bug 1166191. The keywords should be moved into the history
17 // db, which will mean we no longer need this notification and the history
18 // backend can handle automatically adding the search terms as the user
21 history_service_observer_
.Add(history_service_
);
24 ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() {
27 void ChromeTemplateURLServiceClient::Shutdown() {
28 // ChromeTemplateURLServiceClient is owned by TemplateURLService which is a
29 // KeyedService with a dependency on HistoryService, thus |history_service_|
30 // outlives the ChromeTemplateURLServiceClient.
32 // Remove self from |history_service_| observers in the shutdown phase of the
33 // two-phases since KeyedService are not supposed to use a dependend service
34 // after the Shutdown call.
35 history_service_observer_
.RemoveAll();
38 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService
* owner
) {
43 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword(
46 history_service_
->DeleteAllSearchTermsForKeyword(id
);
49 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL(
52 const base::string16
& term
) {
54 history_service_
->SetKeywordSearchTermsForURL(url
, id
, term
);
57 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL
& url
) {
59 history_service_
->AddPage(url
, base::Time::Now(), NULL
, 0, GURL(),
60 history::RedirectList(),
61 ui::PAGE_TRANSITION_KEYWORD_GENERATED
,
62 history::SOURCE_BROWSED
, false);
65 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary(
66 TemplateURL
* template_url
) {
67 const TemplateURLData
& data
= template_url
->data();
69 if (url
.SchemeIs(extensions::kExtensionScheme
)) {
70 const std::string
& extension_id
= url
.host();
71 template_url
->set_extension_info(make_scoped_ptr(
72 new TemplateURL::AssociatedExtensionInfo(
73 TemplateURL::OMNIBOX_API_EXTENSION
, extension_id
)));
77 void ChromeTemplateURLServiceClient::OnURLVisited(
78 history::HistoryService
* history_service
,
79 ui::PageTransition transition
,
80 const history::URLRow
& row
,
81 const history::RedirectList
& redirects
,
82 base::Time visit_time
) {
83 DCHECK_EQ(history_service_
, history_service
);
87 TemplateURLService::URLVisitedDetails visited_details
;
88 visited_details
.url
= row
.url();
89 visited_details
.is_keyword_transition
=
90 ui::PageTransitionCoreTypeIs(transition
, ui::PAGE_TRANSITION_KEYWORD
);
91 owner_
->OnHistoryURLVisited(visited_details
);