Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / search_engines / chrome_template_url_service_client.cc
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.
4
5 #include "chrome/browser/search_engines/chrome_template_url_service_client.h"
6
7 #include "chrome/browser/history/history_service.h"
8 #include "components/search_engines/template_url_service.h"
9 #include "extensions/common/constants.h"
10
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
17   // navigates.
18   if (history_service_)
19     history_service_->AddObserver(this);
20 }
21
22 ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() {
23 }
24
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.
29   //
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.
33   if (history_service_)
34     history_service_->RemoveObserver(this);
35 }
36
37 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) {
38   DCHECK(!owner_);
39   owner_ = owner;
40 }
41
42 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword(
43     TemplateURLID id) {
44   if (history_service_)
45     history_service_->DeleteAllSearchTermsForKeyword(id);
46 }
47
48 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL(
49     const GURL& url,
50     TemplateURLID id,
51     const base::string16& term) {
52   if (history_service_)
53     history_service_->SetKeywordSearchTermsForURL(url, id, term);
54 }
55
56 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) {
57   if (history_service_)
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);
62 }
63
64 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary(
65     TemplateURL* template_url) {
66   const TemplateURLData& data = template_url->data();
67   GURL url(data.url());
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)));
73   }
74 }
75
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);
83   if (!owner_)
84     return;
85
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);
91 }