OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_service.h" | 5 #include "chrome/browser/protector/protector_service.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "chrome/browser/browser_process.h" | 9 #include "chrome/browser/browser_process.h" |
10 #include "chrome/browser/prefs/pref_service.h" | 10 #include "chrome/browser/prefs/pref_service.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 ProtectorService::ProtectorService(Profile* profile) | 22 ProtectorService::ProtectorService(Profile* profile) |
23 : profile_(profile) { | 23 : profile_(profile) { |
24 } | 24 } |
25 | 25 |
26 ProtectorService::~ProtectorService() { | 26 ProtectorService::~ProtectorService() { |
27 DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown. | 27 DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown. |
28 } | 28 } |
29 | 29 |
30 void ProtectorService::ShowChange(BaseSettingChange* change) { | 30 void ProtectorService::ShowChange(BaseSettingChange* change) { |
31 DCHECK(change); | 31 DCHECK(change); |
32 change_.reset(change); | 32 Item new_item; |
| 33 new_item.change.reset(change); |
33 DVLOG(1) << "Init change"; | 34 DVLOG(1) << "Init change"; |
34 if (!change->Init(profile_)) { | 35 if (!change->Init(profile_)) { |
35 LOG(WARNING) << "Error while initializing, dismissing change"; | 36 LOG(WARNING) << "Error while initializing, dismissing change"; |
36 change_.reset(); | |
37 return; | 37 return; |
38 } | 38 } |
39 error_.reset(new SettingsChangeGlobalError(change, this)); | 39 SettingsChangeGlobalError* error = |
40 error_->ShowForProfile(profile_); | 40 new SettingsChangeGlobalError(change, this); |
| 41 new_item.error.reset(error); |
| 42 items_.push_back(new_item); |
| 43 error->ShowForProfile(profile_); |
41 } | 44 } |
42 | 45 |
43 bool ProtectorService::IsShowingChange() const { | 46 bool ProtectorService::IsShowingChange() const { |
44 return change_.get() != NULL; | 47 return !items_.empty(); |
45 } | 48 } |
46 | 49 |
47 void ProtectorService::ApplyChange(Browser* browser) { | 50 void ProtectorService::ApplyChange(BaseSettingChange* change, |
48 DCHECK(IsShowingChange()); | 51 Browser* browser) { |
49 change_->Apply(browser); | 52 change->Apply(browser); |
50 DismissChange(); | 53 DismissChange(change); |
51 } | 54 } |
52 | 55 |
53 void ProtectorService::DiscardChange(Browser* browser) { | 56 void ProtectorService::DiscardChange(BaseSettingChange* change, |
54 DCHECK(IsShowingChange()); | 57 Browser* browser) { |
55 change_->Discard(browser); | 58 change->Discard(browser); |
56 DismissChange(); | 59 DismissChange(change); |
57 } | 60 } |
58 | 61 |
59 void ProtectorService::DismissChange() { | 62 void ProtectorService::DismissChange(BaseSettingChange* change) { |
60 DCHECK(IsShowingChange()); | 63 std::vector<Item>::iterator item = |
61 error_->RemoveFromProfile(); | 64 std::find_if(items_.begin(), items_.end(), MatchItemByChange(change)); |
62 DCHECK(!IsShowingChange()); | 65 DCHECK(item != items_.end()); |
| 66 item->error->RemoveFromProfile(); |
63 } | 67 } |
64 | 68 |
65 void ProtectorService::OpenTab(const GURL& url, Browser* browser) { | 69 void ProtectorService::OpenTab(const GURL& url, Browser* browser) { |
66 DCHECK(browser); | 70 DCHECK(browser); |
67 browser->ShowSingletonTab(url); | 71 browser->ShowSingletonTab(url); |
68 } | 72 } |
69 | 73 |
70 void ProtectorService::Shutdown() { | 74 void ProtectorService::Shutdown() { |
71 if (IsShowingChange()) | 75 while (IsShowingChange()) |
72 DismissChange(); | 76 items_[0].error->RemoveFromProfile(); |
73 } | 77 } |
74 | 78 |
75 void ProtectorService::OnApplyChange(Browser* browser) { | 79 void ProtectorService::OnApplyChange(SettingsChangeGlobalError* error, |
| 80 Browser* browser) { |
76 DVLOG(1) << "Apply change"; | 81 DVLOG(1) << "Apply change"; |
77 DCHECK(IsShowingChange()); | 82 error->change()->Apply(browser); |
78 change_->Apply(browser); | |
79 } | 83 } |
80 | 84 |
81 void ProtectorService::OnDiscardChange(Browser* browser) { | 85 void ProtectorService::OnDiscardChange(SettingsChangeGlobalError* error, |
| 86 Browser* browser) { |
82 DVLOG(1) << "Discard change"; | 87 DVLOG(1) << "Discard change"; |
83 DCHECK(IsShowingChange()); | 88 error->change()->Discard(browser); |
84 change_->Discard(browser); | |
85 } | 89 } |
86 | 90 |
87 void ProtectorService::OnDecisionTimeout() { | 91 void ProtectorService::OnDecisionTimeout(SettingsChangeGlobalError* error) { |
88 DVLOG(1) << "Timeout"; | 92 DVLOG(1) << "Timeout"; |
89 DCHECK(IsShowingChange()); | 93 error->change()->Timeout(); |
90 change_->Timeout(); | |
91 } | 94 } |
92 | 95 |
93 void ProtectorService::OnRemovedFromProfile() { | 96 void ProtectorService::OnRemovedFromProfile(SettingsChangeGlobalError* error) { |
94 DCHECK(IsShowingChange()); | 97 std::vector<Item>::iterator item = |
95 error_.reset(); | 98 std::find_if(items_.begin(), items_.end(), MatchItemByError(error)); |
96 change_.reset(); | 99 DCHECK(item != items_.end()); |
| 100 items_.erase(item); |
| 101 } |
| 102 |
| 103 BaseSettingChange* ProtectorService::GetLastChange() { |
| 104 return items_.empty() ? NULL : items_.back().change.get(); |
| 105 } |
| 106 |
| 107 ProtectorService::Item::Item() { |
| 108 } |
| 109 |
| 110 ProtectorService::Item::~Item() { |
| 111 } |
| 112 |
| 113 ProtectorService::MatchItemByChange::MatchItemByChange( |
| 114 const BaseSettingChange* other) : other_(other) { |
| 115 } |
| 116 |
| 117 bool ProtectorService::MatchItemByChange::operator()( |
| 118 const ProtectorService::Item& item) { |
| 119 return other_ == item.change.get(); |
| 120 } |
| 121 |
| 122 ProtectorService::MatchItemByError::MatchItemByError( |
| 123 const SettingsChangeGlobalError* other) : other_(other) { |
| 124 } |
| 125 |
| 126 bool ProtectorService::MatchItemByError::operator()( |
| 127 const ProtectorService::Item& item) { |
| 128 return other_ == item.error.get(); |
97 } | 129 } |
98 | 130 |
99 | 131 |
100 std::string SignSetting(const std::string& value) { | 132 std::string SignSetting(const std::string& value) { |
101 crypto::HMAC hmac(crypto::HMAC::SHA256); | 133 crypto::HMAC hmac(crypto::HMAC::SHA256); |
102 if (!hmac.Init(kProtectorSigningKey)) { | 134 if (!hmac.Init(kProtectorSigningKey)) { |
103 LOG(WARNING) << "Failed to initialize HMAC algorithm for signing"; | 135 LOG(WARNING) << "Failed to initialize HMAC algorithm for signing"; |
104 return std::string(); | 136 return std::string(); |
105 } | 137 } |
106 | 138 |
(...skipping 13 matching lines...) Expand all Loading... |
120 return false; | 152 return false; |
121 } | 153 } |
122 return hmac.Verify(value, signature); | 154 return hmac.Verify(value, signature); |
123 } | 155 } |
124 | 156 |
125 bool IsEnabled() { | 157 bool IsEnabled() { |
126 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoProtector); | 158 return !CommandLine::ForCurrentProcess()->HasSwitch(switches::kNoProtector); |
127 } | 159 } |
128 | 160 |
129 } // namespace protector | 161 } // namespace protector |
OLD | NEW |