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