Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(723)

Side by Side Diff: chrome/browser/protector/protector_service.cc

Issue 9968007: [protector] Support for collapsing multiple changes into a single one. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Tried to compile after merge. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/logging.h" 7 #include "base/logging.h"
8 #include "chrome/browser/prefs/pref_service.h" 8 #include "chrome/browser/prefs/pref_service.h"
9 #include "chrome/browser/protector/composite_settings_change.h"
10 #include "chrome/browser/protector/keys.h"
11 #include "chrome/browser/protector/protected_prefs_watcher.h"
9 #include "chrome/browser/protector/settings_change_global_error.h" 12 #include "chrome/browser/protector/settings_change_global_error.h"
10 #include "chrome/browser/protector/protected_prefs_watcher.h"
11 #include "chrome/browser/ui/browser.h" 13 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/chrome_notification_types.h" 14 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/pref_names.h" 15 #include "chrome/common/pref_names.h"
14 #include "content/public/browser/notification_source.h" 16 #include "content/public/browser/notification_source.h"
15 17
16 namespace protector { 18 namespace protector {
17 19
18 ProtectorService::ProtectorService(Profile* profile) 20 ProtectorService::ProtectorService(Profile* profile)
19 : profile_(profile), 21 : profile_(profile),
20 has_active_change_(false) { 22 has_active_change_(false) {
21 // Start observing pref changes. 23 // Start observing pref changes.
22 prefs_watcher_.reset(new ProtectedPrefsWatcher(profile)); 24 prefs_watcher_.reset(new ProtectedPrefsWatcher(profile));
23 } 25 }
24 26
25 ProtectorService::~ProtectorService() { 27 ProtectorService::~ProtectorService() {
26 DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown. 28 DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown.
27 } 29 }
28 30
29 void ProtectorService::ShowChange(BaseSettingChange* change) { 31 void ProtectorService::ShowChange(BaseSettingChange* change) {
30 DCHECK(change); 32 DCHECK(change);
33
31 Item new_item; 34 Item new_item;
32 new_item.change.reset(change); 35 new_item.change.reset(change);
whywhat 2012/04/06 16:28:32 Maybe not do it here since we call release later i
Ivan Korotkov 2012/04/09 14:46:47 Done.
33 DVLOG(1) << "Init change"; 36 DVLOG(1) << "Init change";
34 if (!change->Init(profile_)) { 37 if (!change->Init(profile_)) {
35 LOG(WARNING) << "Error while initializing, dismissing change"; 38 LOG(WARNING) << "Error while initializing, dismissing change";
36 return; 39 return;
37 } 40 }
38 SettingsChangeGlobalError* error = 41
39 new SettingsChangeGlobalError(change, this); 42 Item* merger_item = FindItemToMergeWith(change);
whywhat 2012/04/06 16:28:32 merger_item -> item_to_merge_with?
Ivan Korotkov 2012/04/09 14:46:47 Done.
40 new_item.error.reset(error); 43 if (merger_item) {
41 items_.push_back(new_item); 44 CompositeSettingsChange* merged_change =
42 // Do not show the bubble immediately if another one is active. 45 merger_item->change.release()->MergeWith(new_item.change.release());
whywhat 2012/04/06 16:28:32 Would split this into two statements with a commen
Ivan Korotkov 2012/04/09 14:46:47 Done.
43 error->AddToProfile(profile_, !has_active_change_); 46 merger_item->change.reset(merged_change);
44 has_active_change_ = true; 47 merger_item->is_merging = true;
48 if (merger_item->error->GetBubbleView())
49 merger_item->show_when_merged = true;
50 // Remove old GlobalError instance. Later in OnRemovedFromProfile() a new
51 // GlobalError instance will be created for the composite change.
52 merger_item->error->RemoveFromProfile();
53 } else {
54 SettingsChangeGlobalError* error =
55 new SettingsChangeGlobalError(change, this);
56 new_item.error.reset(error);
57 items_.push_back(new_item);
58 // Do not show the bubble immediately if another one is active.
59 error->AddToProfile(profile_, !has_active_change_);
60 has_active_change_ = true;
61 }
45 } 62 }
46 63
47 bool ProtectorService::IsShowingChange() const { 64 bool ProtectorService::IsShowingChange() const {
48 return !items_.empty(); 65 return !items_.empty();
49 } 66 }
50 67
51 void ProtectorService::ApplyChange(BaseSettingChange* change, 68 void ProtectorService::ApplyChange(BaseSettingChange* change,
52 Browser* browser) { 69 Browser* browser) {
53 change->Apply(browser); 70 change->Apply(browser);
54 DismissChange(change); 71 DismissChange(change);
55 } 72 }
56 73
57 void ProtectorService::DiscardChange(BaseSettingChange* change, 74 void ProtectorService::DiscardChange(BaseSettingChange* change,
58 Browser* browser) { 75 Browser* browser) {
59 change->Discard(browser); 76 change->Discard(browser);
60 DismissChange(change); 77 DismissChange(change);
61 } 78 }
62 79
63 void ProtectorService::DismissChange(BaseSettingChange* change) { 80 void ProtectorService::DismissChange(BaseSettingChange* change) {
64 std::vector<Item>::iterator item = 81 Items::iterator item = std::find_if(items_.begin(), items_.end(),
65 std::find_if(items_.begin(), items_.end(), MatchItemByChange(change)); 82 MatchItemByChange(change));
66 DCHECK(item != items_.end()); 83 DCHECK(item != items_.end());
67 item->error->RemoveFromProfile(); 84 item->error->RemoveFromProfile();
68 } 85 }
69 86
70 void ProtectorService::OpenTab(const GURL& url, Browser* browser) { 87 void ProtectorService::OpenTab(const GURL& url, Browser* browser) {
71 DCHECK(browser); 88 DCHECK(browser);
72 browser->ShowSingletonTab(url); 89 browser->ShowSingletonTab(url);
73 } 90 }
74 91
75 ProtectedPrefsWatcher* ProtectorService::GetPrefsWatcher() { 92 ProtectedPrefsWatcher* ProtectorService::GetPrefsWatcher() {
76 return prefs_watcher_.get(); 93 return prefs_watcher_.get();
77 } 94 }
78 95
96 ProtectorService::Item* ProtectorService::FindItemToMergeWith(
97 const BaseSettingChange* change) {
98 std::string keyword = change->GetApplyKeyword();
99 if (keyword.empty())
100 return NULL;
101 for (Items::iterator item = items_.begin(); item != items_.end(); item++) {
102 if (item->change->GetApplyKeyword() == keyword)
103 return &*item;
104 }
105 return NULL;
106 }
107
79 void ProtectorService::Shutdown() { 108 void ProtectorService::Shutdown() {
80 while (IsShowingChange()) 109 while (IsShowingChange())
81 items_[0].error->RemoveFromProfile(); 110 items_[0].error->RemoveFromProfile();
82 } 111 }
83 112
84 void ProtectorService::OnApplyChange(SettingsChangeGlobalError* error, 113 void ProtectorService::OnApplyChange(SettingsChangeGlobalError* error,
85 Browser* browser) { 114 Browser* browser) {
86 DVLOG(1) << "Apply change"; 115 DVLOG(1) << "Apply change";
87 error->change()->Apply(browser); 116 error->change()->Apply(browser);
88 has_active_change_ = false; 117 has_active_change_ = false;
89 } 118 }
90 119
91 void ProtectorService::OnDiscardChange(SettingsChangeGlobalError* error, 120 void ProtectorService::OnDiscardChange(SettingsChangeGlobalError* error,
92 Browser* browser) { 121 Browser* browser) {
93 DVLOG(1) << "Discard change"; 122 DVLOG(1) << "Discard change";
94 error->change()->Discard(browser); 123 error->change()->Discard(browser);
95 has_active_change_ = false; 124 has_active_change_ = false;
96 } 125 }
97 126
98 void ProtectorService::OnDecisionTimeout(SettingsChangeGlobalError* error) { 127 void ProtectorService::OnDecisionTimeout(SettingsChangeGlobalError* error) {
99 DVLOG(1) << "Timeout"; 128 DVLOG(1) << "Timeout";
100 error->change()->Timeout(); 129 error->change()->Timeout();
101 } 130 }
102 131
103 void ProtectorService::OnRemovedFromProfile(SettingsChangeGlobalError* error) { 132 void ProtectorService::OnRemovedFromProfile(SettingsChangeGlobalError* error) {
104 std::vector<Item>::iterator item = 133 Items::iterator item = std::find_if(items_.begin(), items_.end(),
105 std::find_if(items_.begin(), items_.end(), MatchItemByError(error)); 134 MatchItemByError(error));
106 DCHECK(item != items_.end()); 135 DCHECK(item != items_.end());
136
137 if (item->is_merging) {
138 bool show_new_error = !has_active_change_ || item->show_when_merged;
139 item->is_merging = false;
whywhat 2012/04/06 16:28:32 why do we have to false the fields here?
Ivan Korotkov 2012/04/09 14:46:47 So that when this error is really removed from pro
140 item->show_when_merged = false;
141 // Item was merged with another change instance and error has been removed,
142 // create a new one for the composite change.
143 item->error.reset(new SettingsChangeGlobalError(item->change.get(), this));
144 item->error->AddToProfile(profile_, show_new_error);
145 has_active_change_ = true;
146 return;
147 }
148
107 items_.erase(item); 149 items_.erase(item);
108 150
151 // If no other change is shown and there are changes that haven't been shown
152 // yet, show the first one.
109 if (!has_active_change_) { 153 if (!has_active_change_) {
110 // If there are changes that haven't been shown yet, show the first one.
111 for (item = items_.begin(); item != items_.end(); ++item) { 154 for (item = items_.begin(); item != items_.end(); ++item) {
112 if (!item->error->HasShownBubbleView()) { 155 if (!item->error->HasShownBubbleView()) {
113 item->error->ShowBubble(); 156 item->error->ShowBubble();
114 has_active_change_ = true; 157 has_active_change_ = true;
115 return; 158 return;
116 } 159 }
117 } 160 }
118 } 161 }
119 } 162 }
120 163
121 BaseSettingChange* ProtectorService::GetLastChange() { 164 BaseSettingChange* ProtectorService::GetLastChange() {
122 return items_.empty() ? NULL : items_.back().change.get(); 165 return items_.empty() ? NULL : items_.back().change.get();
123 } 166 }
124 167
125 ProtectorService::Item::Item() { 168 ProtectorService::Item::Item()
169 : is_merging(false), show_when_merged(false) {
whywhat 2012/04/06 16:28:32 each field on a separate line, please.
Ivan Korotkov 2012/04/09 14:46:47 Done.
126 } 170 }
127 171
128 ProtectorService::Item::~Item() { 172 ProtectorService::Item::~Item() {
129 } 173 }
130 174
131 ProtectorService::MatchItemByChange::MatchItemByChange( 175 ProtectorService::MatchItemByChange::MatchItemByChange(
132 const BaseSettingChange* other) : other_(other) { 176 const BaseSettingChange* other) : other_(other) {
133 } 177 }
134 178
135 bool ProtectorService::MatchItemByChange::operator()( 179 bool ProtectorService::MatchItemByChange::operator()(
136 const ProtectorService::Item& item) { 180 const ProtectorService::Item& item) {
137 return other_ == item.change.get(); 181 return item.change->Contains(other_);
182 // return item.change.get() == other_;
whywhat 2012/04/06 16:28:32 remove the commented line?
Ivan Korotkov 2012/04/09 14:46:47 Done.
138 } 183 }
139 184
140 ProtectorService::MatchItemByError::MatchItemByError( 185 ProtectorService::MatchItemByError::MatchItemByError(
141 const SettingsChangeGlobalError* other) : other_(other) { 186 const SettingsChangeGlobalError* other) : other_(other) {
142 } 187 }
143 188
144 bool ProtectorService::MatchItemByError::operator()( 189 bool ProtectorService::MatchItemByError::operator()(
145 const ProtectorService::Item& item) { 190 const ProtectorService::Item& item) {
146 return other_ == item.error.get(); 191 return other_ == item.error.get();
147 } 192 }
148 193
149 } // namespace protector 194 } // namespace protector
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698