| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2011 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/protector/protector.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "chrome/browser/protector/settings_change_global_error.h" |
| 10 #include "chrome/browser/protector/keys.h" |
| 11 #include "chrome/browser/search_engines/template_url.h" |
| 12 #include "chrome/browser/search_engines/template_url_service.h" |
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/webdata/keyword_table.h" |
| 16 #include "chrome/common/url_constants.h" |
| 17 #include "crypto/hmac.h" |
| 18 #include "googleurl/src/gurl.h" |
| 19 |
| 20 namespace protector { |
| 21 |
| 22 DefaultSearchProviderChange::DefaultSearchProviderChange( |
| 23 KeywordTable* table, |
| 24 int64 old_id, |
| 25 int64 new_id) |
| 26 : SettingChange(kSearchEngineChanged), |
| 27 new_id_(new_id), |
| 28 old_name_(table->GetKeywordShortName(old_id)), |
| 29 new_name_(table->GetKeywordShortName(new_id)) { |
| 30 } |
| 31 |
| 32 DefaultSearchProviderChange::~DefaultSearchProviderChange() { |
| 33 } |
| 34 |
| 35 const string16& DefaultSearchProviderChange::GetOldSetting() const { |
| 36 return old_name_; |
| 37 } |
| 38 |
| 39 const string16& DefaultSearchProviderChange::GetNewSetting() const { |
| 40 return new_name_; |
| 41 } |
| 42 |
| 43 void DefaultSearchProviderChange::Accept(Protector* protector) { |
| 44 DCHECK(protector); |
| 45 TemplateURLService* url_service = protector->GetTemplateURLService(); |
| 46 if (!url_service) { |
| 47 LOG(WARNING) << "Can't get TemplateURLService object."; |
| 48 return; |
| 49 } |
| 50 const TemplateURLService::TemplateURLVector& urls = |
| 51 url_service->GetTemplateURLs(); |
| 52 for (size_t i = 0; i < urls.size(); ++i) { |
| 53 if (urls[i]->id() == new_id_) { |
| 54 url_service->SetDefaultSearchProvider(urls[i]); |
| 55 return; |
| 56 } |
| 57 } |
| 58 LOG(WARNING) << "Didn't find search provider with id " << new_id_; |
| 59 // TODO(avayvod): Add histrogram. |
| 60 } |
| 61 |
| 62 void DefaultSearchProviderChange::Revert(Protector* protector) { |
| 63 DCHECK(protector); |
| 64 if (old_name_.empty()) { |
| 65 TemplateURLService* url_service = protector->GetTemplateURLService(); |
| 66 if (url_service) |
| 67 url_service->SetDefaultSearchProvider(NULL); |
| 68 else |
| 69 LOG(WARNING) << "Can't get TemplateURLService object."; |
| 70 // Open settings page in case the original setting was lost. |
| 71 protector->OpenTab( |
| 72 GURL(std::string(chrome::kChromeUISettingsURL) + |
| 73 chrome::kSearchEnginesSubPage)); |
| 74 } |
| 75 // TODO(avayvod): Add histrogram. |
| 76 } |
| 77 |
| 78 |
| 79 Protector::Protector() : error_(NULL) { |
| 80 } |
| 81 |
| 82 Protector::~Protector() { |
| 83 } |
| 84 |
| 85 void Protector::AddChange(scoped_refptr<SettingChange> change) { |
| 86 if (error_) { |
| 87 NOTREACHED() << "Adding setting change after showing the error."; |
| 88 return; |
| 89 } |
| 90 changes_.push_back(change); |
| 91 } |
| 92 |
| 93 void Protector::NotifyUser() { |
| 94 if (changes_.empty()) |
| 95 return; |
| 96 |
| 97 error_ = new SettingsChangeGlobalError( |
| 98 changes_, |
| 99 base::Bind(&Protector::OnChangesAccepted, this), |
| 100 base::Bind(&Protector::OnChangesDeclined, this)); |
| 101 error_->ShowForDefaultProfile(); |
| 102 } |
| 103 |
| 104 void Protector::OnChangesAccepted() { |
| 105 for (size_t i = 0; i < changes_.size(); ++i) |
| 106 changes_[i]->Accept(this); |
| 107 error_ = NULL; |
| 108 } |
| 109 |
| 110 void Protector::OnChangesDeclined() { |
| 111 for (size_t i = 0; i < changes_.size(); ++i) |
| 112 changes_[i]->Revert(this); |
| 113 error_ = NULL; |
| 114 } |
| 115 |
| 116 void Protector::OpenTab(const GURL& url) { |
| 117 if (!error_ || !error_->browser()) { |
| 118 LOG(WARNING) << "Don't have browser to show tab in."; |
| 119 return; |
| 120 } |
| 121 error_->browser()->ShowSingletonTab(url); |
| 122 } |
| 123 |
| 124 TemplateURLService* Protector::GetTemplateURLService() { |
| 125 if (!error_ || !error_->profile()) { |
| 126 LOG(WARNING) << "Don't have profile to get to search engines."; |
| 127 return NULL; |
| 128 } |
| 129 return TemplateURLServiceFactory::GetForProfile(error_->profile()); |
| 130 } |
| 131 |
| 132 |
| 133 void NotifyUserOfChange(scoped_refptr<SettingChange> change) { |
| 134 scoped_refptr<Protector> protector(new Protector()); |
| 135 protector->AddChange(change); |
| 136 protector->NotifyUser(); |
| 137 } |
| 138 |
| 139 std::string Sign(const std::string& value) { |
| 140 crypto::HMAC hmac(crypto::HMAC::SHA256); |
| 141 DCHECK(hmac.Init(kProtectorSigningKey)); |
| 142 |
| 143 std::vector<unsigned char> digest(hmac.DigestLength()); |
| 144 DCHECK(hmac.Sign(value, &digest[0], digest.size())); |
| 145 |
| 146 return std::string(&digest[0], &digest[0] + digest.size()); |
| 147 } |
| 148 |
| 149 } // namespace protector |
| OLD | NEW |