OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" | 5 #include "chrome/browser/search_engines/chrome_template_url_service_client.h" |
6 | 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" | 7 #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" | 8 #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" | 9 #include "extensions/common/constants.h" |
16 | 10 |
17 ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient(Profile* profile) | 11 ChromeTemplateURLServiceClient::ChromeTemplateURLServiceClient( |
18 : profile_(profile), | 12 HistoryService* history_service) |
19 owner_(NULL) { | 13 : owner_(NULL), history_service_(history_service) { |
20 DCHECK(profile); | |
21 | |
22 // Register for notifications. | |
23 // TODO(sky): bug 1166191. The keywords should be moved into the history | 14 // 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 | 15 // 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 | 16 // backend can handle automatically adding the search terms as the user |
26 // navigates. | 17 // navigates. |
27 content::Source<Profile> profile_source(profile->GetOriginalProfile()); | 18 if (history_service_) |
28 notification_registrar_.Add(this, chrome::NOTIFICATION_HISTORY_URL_VISITED, | 19 history_service_->AddObserver(this); |
29 profile_source); | |
30 } | 20 } |
31 | 21 |
32 ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() { | 22 ChromeTemplateURLServiceClient::~ChromeTemplateURLServiceClient() { |
33 } | 23 } |
34 | 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 |
35 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) { | 37 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) { |
36 DCHECK(!owner_); | 38 DCHECK(!owner_); |
37 owner_ = owner; | 39 owner_ = owner; |
38 } | 40 } |
39 | 41 |
40 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword( | 42 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword( |
41 TemplateURLID id) { | 43 TemplateURLID id) { |
42 HistoryService* history_service = | 44 if (history_service_) |
43 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 45 history_service_->DeleteAllSearchTermsForKeyword(id); |
44 if (history_service) | |
45 history_service->DeleteAllSearchTermsForKeyword(id); | |
46 } | 46 } |
47 | 47 |
48 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL( | 48 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL( |
49 const GURL& url, | 49 const GURL& url, |
50 TemplateURLID id, | 50 TemplateURLID id, |
51 const base::string16& term) { | 51 const base::string16& term) { |
52 HistoryService* history_service = | 52 if (history_service_) |
53 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 53 history_service_->SetKeywordSearchTermsForURL(url, id, term); |
54 if (history_service) | |
55 history_service->SetKeywordSearchTermsForURL(url, id, term); | |
56 } | 54 } |
57 | 55 |
58 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) { | 56 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) { |
59 HistoryService* history_service = | 57 if (history_service_) |
60 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 58 history_service_->AddPage(url, base::Time::Now(), NULL, 0, GURL(), |
61 if (history_service) | 59 history::RedirectList(), |
62 history_service->AddPage(url, base::Time::Now(), NULL, 0, GURL(), | 60 ui::PAGE_TRANSITION_KEYWORD_GENERATED, |
63 history::RedirectList(), | 61 history::SOURCE_BROWSED, false); |
64 ui::PAGE_TRANSITION_KEYWORD_GENERATED, | |
65 history::SOURCE_BROWSED, false); | |
66 } | 62 } |
67 | 63 |
68 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary( | 64 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary( |
69 TemplateURL* template_url) { | 65 TemplateURL* template_url) { |
70 const TemplateURLData& data = template_url->data(); | 66 const TemplateURLData& data = template_url->data(); |
71 GURL url(data.url()); | 67 GURL url(data.url()); |
72 if (url.SchemeIs(extensions::kExtensionScheme)) { | 68 if (url.SchemeIs(extensions::kExtensionScheme)) { |
73 const std::string& extension_id = url.host(); | 69 const std::string& extension_id = url.host(); |
74 template_url->set_extension_info(make_scoped_ptr( | 70 template_url->set_extension_info(make_scoped_ptr( |
75 new TemplateURL::AssociatedExtensionInfo( | 71 new TemplateURL::AssociatedExtensionInfo( |
76 TemplateURL::OMNIBOX_API_EXTENSION, extension_id))); | 72 TemplateURL::OMNIBOX_API_EXTENSION, extension_id))); |
77 } | 73 } |
78 } | 74 } |
79 | 75 |
80 void ChromeTemplateURLServiceClient::Observe( | 76 void ChromeTemplateURLServiceClient::OnURLVisited( |
81 int type, | 77 HistoryService* history_service, |
82 const content::NotificationSource& source, | 78 ui::PageTransition transition, |
83 const content::NotificationDetails& details) { | 79 const history::URLRow& row, |
84 DCHECK_EQ(type, chrome::NOTIFICATION_HISTORY_URL_VISITED); | 80 const history::RedirectList& redirects, |
85 | 81 base::Time visit_time) { |
| 82 DCHECK_EQ(history_service_, history_service); |
86 if (!owner_) | 83 if (!owner_) |
87 return; | 84 return; |
88 | 85 |
89 content::Details<history::URLVisitedDetails> history_details(details); | |
90 TemplateURLService::URLVisitedDetails visited_details; | 86 TemplateURLService::URLVisitedDetails visited_details; |
91 visited_details.url = history_details->row.url(); | 87 visited_details.url = row.url(); |
92 visited_details.is_keyword_transition = | 88 visited_details.is_keyword_transition = |
93 ui::PageTransitionStripQualifier(history_details->transition) == | 89 ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_KEYWORD); |
94 ui::PAGE_TRANSITION_KEYWORD; | |
95 owner_->OnHistoryURLVisited(visited_details); | 90 owner_->OnHistoryURLVisited(visited_details); |
96 } | 91 } |
OLD | NEW |