Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/protector/protector.h" | 5 #include "chrome/browser/protector/protector.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
| 10 #include "chrome/browser/protector/settings_change_global_error.h" | 10 #include "chrome/browser/protector/settings_change_global_error.h" |
| 11 #include "chrome/browser/protector/keys.h" | 11 #include "chrome/browser/protector/keys.h" |
| 12 #include "chrome/browser/search_engines/template_url_service.h" | 12 #include "chrome/browser/search_engines/template_url_service.h" |
| 13 #include "chrome/browser/search_engines/template_url_service_factory.h" | 13 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
| 16 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/notification_source.h" | 17 #include "content/public/browser/notification_source.h" |
| 18 #include "crypto/hmac.h" | 18 #include "crypto/hmac.h" |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace protector { | 22 namespace protector { |
| 23 | 23 |
| 24 Protector::Protector(Profile* profile) | 24 Protector::Protector(Profile* profile) |
| 25 : profile_(profile) { | 25 : profile_(profile) { |
| 26 } | 26 } |
| 27 | 27 |
| 28 Protector::~Protector() { | 28 Protector::~Protector() { |
| 29 if (change_.get()) | |
| 30 change_->OnBeforeRemoved(); | |
| 29 } | 31 } |
| 30 | 32 |
| 31 void Protector::OpenTab(const GURL& url) { | 33 void Protector::OpenTab(const GURL& url) { |
| 32 if (!error_.get() || !error_->browser()) { | 34 if (!error_.get() || !error_->browser()) { |
| 33 LOG(WARNING) << "Don't have browser to show tab in."; | 35 LOG(WARNING) << "Don't have browser to show tab in."; |
| 34 return; | 36 return; |
| 35 } | 37 } |
| 36 error_->browser()->ShowSingletonTab(url); | 38 error_->browser()->ShowSingletonTab(url); |
| 37 } | 39 } |
| 38 | 40 |
| 39 TemplateURLService* Protector::GetTemplateURLService() { | 41 TemplateURLService* Protector::GetTemplateURLService() { |
| 40 return TemplateURLServiceFactory::GetForProfile(profile_); | 42 return TemplateURLServiceFactory::GetForProfile(profile_); |
| 41 } | 43 } |
| 42 | 44 |
| 43 void Protector::ShowChange(BaseSettingChange* change) { | 45 void Protector::ShowChange(BaseSettingChange* change) { |
| 44 DCHECK(change); | 46 DCHECK(change); |
| 45 BrowserThread::PostTask( | 47 BrowserThread::PostTask( |
| 46 BrowserThread::UI, FROM_HERE, | 48 BrowserThread::UI, FROM_HERE, |
| 47 base::Bind(&Protector::InitAndShowChange, | 49 base::Bind(&Protector::InitAndShowChange, |
| 48 base::Unretained(this), change)); | 50 base::Unretained(this), change)); |
| 49 } | 51 } |
| 50 | 52 |
| 51 void Protector::InitAndShowChange(BaseSettingChange* change) { | 53 void Protector::DismissChange() { |
| 52 VLOG(1) << "Init change"; | 54 DCHECK(error_.get()); |
| 53 if (!change->Init(this)) { | 55 error_->RemoveFromProfile(); |
| 54 VLOG(1) << "Error while initializing, removing ourselves"; | |
| 55 delete change; | |
| 56 OnRemovedFromProfile(); | |
| 57 return; | |
| 58 } | |
| 59 error_.reset(new SettingsChangeGlobalError(change, this)); | |
| 60 error_->ShowForProfile(profile_); | |
| 61 } | 56 } |
| 62 | 57 |
| 63 void Protector::OnApplyChange() { | 58 void Protector::OnApplyChange() { |
| 64 VLOG(1) << "Apply change"; | 59 DVLOG(1) << "Apply change"; |
| 65 error_->mutable_change()->Apply(this); | 60 change_->Apply(); |
| 66 } | 61 } |
| 67 | 62 |
| 68 void Protector::OnDiscardChange() { | 63 void Protector::OnDiscardChange() { |
| 69 VLOG(1) << "Discard change"; | 64 DVLOG(1) << "Discard change"; |
| 70 error_->mutable_change()->Discard(this); | 65 change_->Discard(); |
| 71 } | 66 } |
| 72 | 67 |
| 73 void Protector::OnDecisionTimeout() { | 68 void Protector::OnDecisionTimeout() { |
| 74 // TODO(ivankr): Add histogram. | 69 // TODO(ivankr): Add histogram. |
| 75 VLOG(1) << "Timeout"; | 70 DVLOG(1) << "Timeout"; |
| 76 } | 71 } |
| 77 | 72 |
| 78 void Protector::OnRemovedFromProfile() { | 73 void Protector::OnRemovedFromProfile() { |
| 79 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); | 74 BrowserThread::DeleteSoon(BrowserThread::UI, FROM_HERE, this); |
| 80 } | 75 } |
| 81 | 76 |
| 77 void Protector::InitAndShowChange(BaseSettingChange* change) { | |
| 78 DVLOG(1) << "Init change"; | |
| 79 if (!change->Init(this)) { | |
| 80 LOG(WARNING) << "Error while initializing, removing ourselves"; | |
| 81 delete change; | |
| 82 delete this; | |
|
whywhat
2011/11/21 14:51:21
Do we have to delete this?
Ivan Korotkov
2011/11/21 15:27:38
This is called asynchronously at the moment when n
| |
| 83 return; | |
| 84 } | |
| 85 // |change_| should not be set until a successful |Init| call. | |
| 86 change_.reset(change); | |
| 87 error_.reset(new SettingsChangeGlobalError(change, this)); | |
| 88 error_->ShowForProfile(profile_); | |
| 89 } | |
| 90 | |
| 82 | 91 |
| 83 std::string SignSetting(const std::string& value) { | 92 std::string SignSetting(const std::string& value) { |
| 84 crypto::HMAC hmac(crypto::HMAC::SHA256); | 93 crypto::HMAC hmac(crypto::HMAC::SHA256); |
| 85 if (!hmac.Init(kProtectorSigningKey)) { | 94 if (!hmac.Init(kProtectorSigningKey)) { |
| 86 LOG(WARNING) << "Failed to initialize HMAC algorithm for signing"; | 95 LOG(WARNING) << "Failed to initialize HMAC algorithm for signing"; |
| 87 return std::string(); | 96 return std::string(); |
| 88 } | 97 } |
| 89 | 98 |
| 90 std::vector<unsigned char> digest(hmac.DigestLength()); | 99 std::vector<unsigned char> digest(hmac.DigestLength()); |
| 91 if (!hmac.Sign(value, &digest[0], digest.size())) { | 100 if (!hmac.Sign(value, &digest[0], digest.size())) { |
| 92 LOG(WARNING) << "Failed to sign setting"; | 101 LOG(WARNING) << "Failed to sign setting"; |
| 93 return std::string(); | 102 return std::string(); |
| 94 } | 103 } |
| 95 | 104 |
| 96 return std::string(&digest[0], &digest[0] + digest.size()); | 105 return std::string(&digest[0], &digest[0] + digest.size()); |
| 97 } | 106 } |
| 98 | 107 |
| 99 bool IsSettingValid(const std::string& value, const std::string& signature) { | 108 bool IsSettingValid(const std::string& value, const std::string& signature) { |
| 100 crypto::HMAC hmac(crypto::HMAC::SHA256); | 109 crypto::HMAC hmac(crypto::HMAC::SHA256); |
| 101 if (!hmac.Init(kProtectorSigningKey)) { | 110 if (!hmac.Init(kProtectorSigningKey)) { |
| 102 LOG(WARNING) << "Failed to initialize HMAC algorithm for verification."; | 111 LOG(WARNING) << "Failed to initialize HMAC algorithm for verification."; |
| 103 return false; | 112 return false; |
| 104 } | 113 } |
| 105 return hmac.Verify(value, signature); | 114 return hmac.Verify(value, signature); |
| 106 } | 115 } |
| 107 | 116 |
| 108 } // namespace protector | 117 } // namespace protector |
| OLD | NEW |