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. Since it outlive the | |
Peter Kasting
2014/10/17 20:41:02
Nit: outlives
This seems weird, though. If Templ
sdefresne
2014/10/18 17:31:47
You're right. I don't know why I though the opposi
| |
28 // HistoryService, TemplateURLService will call Shutdown as part of the two | |
29 // steps shutdown of the KeyedService so that we can remove the observer | |
Peter Kasting
2014/10/17 20:41:02
Nit: two steps -> two-phase
sdefresne
2014/10/18 17:31:47
Acknowledged.
| |
30 // before the Historyservice becomes invalid. | |
Peter Kasting
2014/10/17 20:41:03
Nit: the Historyservice -> |history_service_|
sdefresne
2014/10/18 17:31:47
Acknowledged.
| |
31 if (history_service_) | |
32 history_service_->RemoveObserver(this); | |
33 } | |
34 | |
35 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) { | 35 void ChromeTemplateURLServiceClient::SetOwner(TemplateURLService* owner) { |
36 DCHECK(!owner_); | 36 DCHECK(!owner_); |
37 owner_ = owner; | 37 owner_ = owner; |
38 } | 38 } |
39 | 39 |
40 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword( | 40 void ChromeTemplateURLServiceClient::DeleteAllSearchTermsForKeyword( |
41 TemplateURLID id) { | 41 TemplateURLID id) { |
42 HistoryService* history_service = | 42 if (history_service_) |
43 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 43 history_service_->DeleteAllSearchTermsForKeyword(id); |
44 if (history_service) | |
45 history_service->DeleteAllSearchTermsForKeyword(id); | |
46 } | 44 } |
47 | 45 |
48 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL( | 46 void ChromeTemplateURLServiceClient::SetKeywordSearchTermsForURL( |
49 const GURL& url, | 47 const GURL& url, |
50 TemplateURLID id, | 48 TemplateURLID id, |
51 const base::string16& term) { | 49 const base::string16& term) { |
52 HistoryService* history_service = | 50 if (history_service_) |
53 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 51 history_service_->SetKeywordSearchTermsForURL(url, id, term); |
54 if (history_service) | |
55 history_service->SetKeywordSearchTermsForURL(url, id, term); | |
56 } | 52 } |
57 | 53 |
58 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) { | 54 void ChromeTemplateURLServiceClient::AddKeywordGeneratedVisit(const GURL& url) { |
59 HistoryService* history_service = | 55 if (history_service_) |
60 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 56 history_service_->AddPage(url, base::Time::Now(), NULL, 0, GURL(), |
61 if (history_service) | 57 history::RedirectList(), |
62 history_service->AddPage(url, base::Time::Now(), NULL, 0, GURL(), | 58 ui::PAGE_TRANSITION_KEYWORD_GENERATED, |
63 history::RedirectList(), | 59 history::SOURCE_BROWSED, false); |
64 ui::PAGE_TRANSITION_KEYWORD_GENERATED, | |
65 history::SOURCE_BROWSED, false); | |
66 } | 60 } |
67 | 61 |
68 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary( | 62 void ChromeTemplateURLServiceClient::RestoreExtensionInfoIfNecessary( |
69 TemplateURL* template_url) { | 63 TemplateURL* template_url) { |
70 const TemplateURLData& data = template_url->data(); | 64 const TemplateURLData& data = template_url->data(); |
71 GURL url(data.url()); | 65 GURL url(data.url()); |
72 if (url.SchemeIs(extensions::kExtensionScheme)) { | 66 if (url.SchemeIs(extensions::kExtensionScheme)) { |
73 const std::string& extension_id = url.host(); | 67 const std::string& extension_id = url.host(); |
74 template_url->set_extension_info(make_scoped_ptr( | 68 template_url->set_extension_info(make_scoped_ptr( |
75 new TemplateURL::AssociatedExtensionInfo( | 69 new TemplateURL::AssociatedExtensionInfo( |
76 TemplateURL::OMNIBOX_API_EXTENSION, extension_id))); | 70 TemplateURL::OMNIBOX_API_EXTENSION, extension_id))); |
77 } | 71 } |
78 } | 72 } |
79 | 73 |
80 void ChromeTemplateURLServiceClient::Observe( | 74 void ChromeTemplateURLServiceClient::OnURLVisited( |
81 int type, | 75 HistoryService* history_service, |
82 const content::NotificationSource& source, | 76 ui::PageTransition transition, |
83 const content::NotificationDetails& details) { | 77 const history::URLRow& row, |
84 DCHECK_EQ(type, chrome::NOTIFICATION_HISTORY_URL_VISITED); | 78 const history::RedirectList& redirects, |
85 | 79 base::Time visit_time) { |
80 DCHECK_EQ(history_service_, history_service); | |
86 if (!owner_) | 81 if (!owner_) |
87 return; | 82 return; |
88 | 83 |
89 content::Details<history::URLVisitedDetails> history_details(details); | |
90 TemplateURLService::URLVisitedDetails visited_details; | 84 TemplateURLService::URLVisitedDetails visited_details; |
91 visited_details.url = history_details->row.url(); | 85 visited_details.url = row.url(); |
92 visited_details.is_keyword_transition = | 86 visited_details.is_keyword_transition = |
93 ui::PageTransitionStripQualifier(history_details->transition) == | 87 ui::PageTransitionCoreTypeIs(transition, ui::PAGE_TRANSITION_KEYWORD); |
94 ui::PAGE_TRANSITION_KEYWORD; | |
95 owner_->OnHistoryURLVisited(visited_details); | 88 owner_->OnHistoryURLVisited(visited_details); |
96 } | 89 } |
OLD | NEW |