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