Chromium Code Reviews| 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( | |
|
Ivan Korotkov
2011/10/19 20:24:37
Please move to a separate .cc file.
whywhat
2011/10/20 20:50:10
Done.
| |
| 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 // Open settings page in case the original setting was lost. | |
| 66 protector->OpenTab( | |
| 67 GURL(std::string(chrome::kChromeUISettingsURL) + | |
| 68 chrome::kSearchEnginesSubPage)); | |
| 69 } | |
| 70 // TODO(avayvod): Add histrogram. | |
| 71 } | |
| 72 | |
| 73 | |
| 74 Protector::Protector() : error_(NULL) { | |
| 75 } | |
| 76 | |
| 77 Protector::~Protector() { | |
| 78 } | |
| 79 | |
| 80 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.
| |
| 81 if (error_) { | |
| 82 NOTREACHED() << "Adding setting change after showing the error."; | |
| 83 return; | |
| 84 } | |
| 85 changes_.push_back(change); | |
| 86 } | |
| 87 | |
| 88 void Protector::NotifyUser() { | |
| 89 if (changes_.empty()) | |
|
Ivan Korotkov
2011/10/19 20:24:37
DCHECK?
whywhat
2011/10/20 20:50:10
Done.
| |
| 90 return; | |
| 91 | |
| 92 error_ = new SettingsChangeGlobalError( | |
| 93 changes_, | |
| 94 base::Bind(&Protector::OnChangesAccepted, this), | |
| 95 base::Bind(&Protector::OnChangesDeclined, this)); | |
| 96 error_->ShowForDefaultProfile(); | |
| 97 } | |
| 98 | |
| 99 void Protector::OnChangesAccepted() { | |
| 100 for (size_t i = 0; i < changes_.size(); ++i) | |
| 101 changes_[i]->Accept(this); | |
| 102 error_ = NULL; | |
| 103 } | |
| 104 | |
| 105 void Protector::OnChangesDeclined() { | |
| 106 for (size_t i = 0; i < changes_.size(); ++i) | |
| 107 changes_[i]->Revert(this); | |
| 108 error_ = NULL; | |
| 109 } | |
| 110 | |
| 111 void Protector::OpenTab(const GURL& url) { | |
| 112 if (!error_ || !error_->browser()) { | |
| 113 LOG(WARNING) << "Don't have browser to show tab in."; | |
| 114 return; | |
| 115 } | |
| 116 error_->browser()->ShowSingletonTab(url); | |
| 117 } | |
| 118 | |
| 119 TemplateURLService* Protector::GetTemplateURLService() { | |
| 120 if (!error_ || !error_->profile()) { | |
| 121 LOG(WARNING) << "Don't have profile to get to search engines."; | |
| 122 return NULL; | |
| 123 } | |
| 124 return TemplateURLServiceFactory::GetForProfile(error_->profile()); | |
| 125 } | |
| 126 | |
| 127 | |
| 128 void NotifyUserOfChange(scoped_refptr<SettingChange> change) { | |
| 129 scoped_refptr<Protector> protector(new Protector()); | |
| 130 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
| |
| 131 protector->NotifyUser(); | |
| 132 } | |
| 133 | |
| 134 std::string Sign(const std::string& value) { | |
| 135 crypto::HMAC hmac(crypto::HMAC::SHA256); | |
| 136 DCHECK(hmac.Init(kProtectorSigningKey)); | |
| 137 | |
| 138 std::vector<unsigned char> digest(hmac.DigestLength()); | |
| 139 DCHECK(hmac.Sign(value, &digest[0], digest.size())); | |
| 140 | |
| 141 return std::string(&digest[0], &digest[0] + digest.size()); | |
| 142 } | |
| 143 | |
| 144 } // namespace protector | |
| OLD | NEW |