Upstream version 10.39.225.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/chrome_notification_types.h"
8 #include "chrome/browser/history/history_notifications.h"
9 #include "chrome/browser/history/history_service.h"
10 #include "chrome/browser/history/history_service_factory.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "components/search_engines/template_url_service.h"
13 #include "content/public/browser/notification_details.h"
14 #include "content/public/browser/notification_source.h"
15 #include "extensions/common/constants.h"
16
17 ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient(Profile* profile)
18     : profile_(profile),
19       owner_(NULL) {
20   DCHECK(profile);
21
22   // Register for notifications.
23   // TODO(sky): bug 1166191. The keywords should be moved into the history
24   // db, which will mean we no longer need this notification and the history
25   // backend can handle automatically adding the search terms as the user
26   // navigates.
27   content::Source<Profile> profile_source(profile->GetOriginalProfile());
28   notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED,
29                               profile_source);
30 }
31
32 ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() {
33 }
34
35 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) {
36   DCHECK(!owner_);
37   owner_ = owner;
38 }
39
40 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword(
41     TemplateURLID id) {
42   HistoryService* history_service =
43       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
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   HistoryService* history_service =
53       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
54   if (history_service)
55     history_service->SetKeywordSearchTermsForURL(url, id, term);
56 }
57
58 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) {
59   HistoryService* history_service =
60       HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS);
61   if (history_service)
62     history_service->AddPage(url, base::Time::Now(), NULL, 0, GURL(),
63                              history::RedirectList(),
64                              ui::PAGE_TRANSITION_KEYWORD_GENERATED,
65                              history::SOURCE_BROWSED, false);
66 }
67
68 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary(
69     TemplateURL* template_url) {
70   const TemplateURLData& data = template_url->data();
71   GURL url(data.url());
72   if (url.SchemeIs(extensions::kExtensionScheme)) {
73     const std::string& extension_id = url.host();
74     template_url->set_extension_info(make_scoped_ptr(
75         new TemplateURL::AssociatedExtensionInfo(
76             TemplateURL::OMNIBOX_API_EXTENSION, extension_id)));
77   }
78 }
79
80 void ChromeTemplateURLServiceClient::Observe(
81     int type,
82     const content::NotificationSource& source,
83     const content::NotificationDetails& details) {
84   DCHECK_EQ(type, chrome::NOTIFICATION_HISTORY_URL_VISITED);
85
86   if (!owner_)
87     return;
88
89   content::Details<history::URLVisitedDetails> history_details(details);
90   TemplateURLService::URLVisitedDetails visited_details;
91   visited_details.url = history_details->row.url();
92   visited_details.is_keyword_transition =
93       ui::PageTransitionStripQualifier(history_details->transition) ==
94       ui::PAGE_TRANSITION_KEYWORD;
95   owner_->OnHistoryURLVisited(visited_details);
96 }