Chromium Code Reviews| Index: chrome/browser/protector/protector.cc |
| diff --git a/chrome/browser/protector/protector.cc b/chrome/browser/protector/protector.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..789cfd41b2a0186534cc80e1fb82360eb9448d7c |
| --- /dev/null |
| +++ b/chrome/browser/protector/protector.cc |
| @@ -0,0 +1,144 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/protector/protector.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/logging.h" |
| +#include "chrome/browser/protector/settings_change_global_error.h" |
| +#include "chrome/browser/protector/keys.h" |
| +#include "chrome/browser/search_engines/template_url.h" |
| +#include "chrome/browser/search_engines/template_url_service.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/webdata/keyword_table.h" |
| +#include "chrome/common/url_constants.h" |
| +#include "crypto/hmac.h" |
| +#include "googleurl/src/gurl.h" |
| + |
| +namespace protector { |
| + |
| +DefaultSearchProviderChange::DefaultSearchProviderChange( |
|
Ivan Korotkov
2011/10/19 20:24:37
Please move to a separate .cc file.
whywhat
2011/10/20 20:50:10
Done.
|
| + KeywordTable* table, |
| + int64 old_id, |
| + int64 new_id) |
| + : SettingChange(kSearchEngineChanged), |
| + new_id_(new_id), |
| + old_name_(table->GetKeywordShortName(old_id)), |
| + new_name_(table->GetKeywordShortName(new_id)) { |
| +} |
| + |
| +DefaultSearchProviderChange::~DefaultSearchProviderChange() { |
| +} |
| + |
| +const string16& DefaultSearchProviderChange::GetOldSetting() const { |
| + return old_name_; |
| +} |
| + |
| +const string16& DefaultSearchProviderChange::GetNewSetting() const { |
| + return new_name_; |
| +} |
| + |
| +void DefaultSearchProviderChange::Accept(Protector* protector) { |
| + DCHECK(protector); |
| + TemplateURLService* url_service = protector->GetTemplateURLService(); |
| + if (!url_service) { |
| + LOG(WARNING) << "Can't get TemplateURLService object."; |
| + return; |
| + } |
| + const TemplateURLService::TemplateURLVector& urls = |
| + url_service->GetTemplateURLs(); |
| + for (size_t i = 0; i < urls.size(); ++i) { |
| + if (urls[i]->id() == new_id_) { |
| + url_service->SetDefaultSearchProvider(urls[i]); |
| + return; |
| + } |
| + } |
| + LOG(WARNING) << "Didn't find search provider with id " << new_id_; |
| + // TODO(avayvod): Add histrogram. |
| +} |
| + |
| +void DefaultSearchProviderChange::Revert(Protector* protector) { |
| + DCHECK(protector); |
| + if (old_name_.empty()) { |
| + // Open settings page in case the original setting was lost. |
| + protector->OpenTab( |
| + GURL(std::string(chrome::kChromeUISettingsURL) + |
| + chrome::kSearchEnginesSubPage)); |
| + } |
| + // TODO(avayvod): Add histrogram. |
| +} |
| + |
| + |
| +Protector::Protector() : error_(NULL) { |
| +} |
| + |
| +Protector::~Protector() { |
| +} |
| + |
| +void Protector::AddChange(scoped_refptr<SettingChange> change) { |
|
Ivan Korotkov
2011/10/19 20:24:37
I don't think there is any need to store refptr's
whywhat
2011/10/20 20:50:10
Done.
|
| + if (error_) { |
| + NOTREACHED() << "Adding setting change after showing the error."; |
| + return; |
| + } |
| + changes_.push_back(change); |
| +} |
| + |
| +void Protector::NotifyUser() { |
| + if (changes_.empty()) |
|
Ivan Korotkov
2011/10/19 20:24:37
DCHECK?
whywhat
2011/10/20 20:50:10
Done.
|
| + return; |
| + |
| + error_ = new SettingsChangeGlobalError( |
| + changes_, |
| + base::Bind(&Protector::OnChangesAccepted, this), |
| + base::Bind(&Protector::OnChangesDeclined, this)); |
| + error_->ShowForDefaultProfile(); |
| +} |
| + |
| +void Protector::OnChangesAccepted() { |
| + for (size_t i = 0; i < changes_.size(); ++i) |
| + changes_[i]->Accept(this); |
| + error_ = NULL; |
| +} |
| + |
| +void Protector::OnChangesDeclined() { |
| + for (size_t i = 0; i < changes_.size(); ++i) |
| + changes_[i]->Revert(this); |
| + error_ = NULL; |
| +} |
| + |
| +void Protector::OpenTab(const GURL& url) { |
| + if (!error_ || !error_->browser()) { |
| + LOG(WARNING) << "Don't have browser to show tab in."; |
| + return; |
| + } |
| + error_->browser()->ShowSingletonTab(url); |
| +} |
| + |
| +TemplateURLService* Protector::GetTemplateURLService() { |
| + if (!error_ || !error_->profile()) { |
| + LOG(WARNING) << "Don't have profile to get to search engines."; |
| + return NULL; |
| + } |
| + return TemplateURLServiceFactory::GetForProfile(error_->profile()); |
| +} |
| + |
| + |
| +void NotifyUserOfChange(scoped_refptr<SettingChange> change) { |
| + scoped_refptr<Protector> protector(new Protector()); |
| + protector->AddChange(change); |
|
Ivan Korotkov
2011/10/19 20:24:37
Please explicitly AddRef here and Release when we
whywhat
2011/10/20 20:50:10
Discussed offline how to better manage Protector's
|
| + protector->NotifyUser(); |
| +} |
| + |
| +std::string Sign(const std::string& value) { |
| + crypto::HMAC hmac(crypto::HMAC::SHA256); |
| + DCHECK(hmac.Init(kProtectorSigningKey)); |
| + |
| + std::vector<unsigned char> digest(hmac.DigestLength()); |
| + DCHECK(hmac.Sign(value, &digest[0], digest.size())); |
| + |
| + return std::string(&digest[0], &digest[0] + digest.size()); |
| +} |
| + |
| +} // namespace protector |