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

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: 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/google/google_util.h"
8 #include "chrome/browser/prefs/pref_service.h" 9 #include "chrome/browser/prefs/pref_service.h"
10 #include "chrome/browser/protector/composite_settings_change.h"
11 #include "chrome/browser/protector/keys.h"
12 #include "chrome/browser/protector/protected_prefs_watcher.h"
9 #include "chrome/browser/protector/settings_change_global_error.h" 13 #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" 14 #include "chrome/browser/ui/browser.h"
12 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
14 #include "content/public/browser/notification_source.h" 17 #include "content/public/browser/notification_source.h"
18 #include "net/base/registry_controlled_domain.h"
15 19
16 namespace protector { 20 namespace protector {
17 21
22 namespace {
23
24 // Returns true if changes with URLs |url1| and |url2| can be merged.
25 bool CanMerge(const GURL& url1, const GURL& url2) {
26 VLOG(1) << "Checking if can merge " << url1.spec() << " with " << url2.spec();
27 // All Google URLs are considered the same one.
28 if (google_util::IsGoogleHostname(url1.host()))
29 return google_util::IsGoogleHostname(url2.host());
30 // Otherwise URLs must have the same domain.
31 return net::RegistryControlledDomainService::SameDomainOrHost(url1, url2);
32 }
33
34 } // namespace
35
18 ProtectorService::ProtectorService(Profile* profile) 36 ProtectorService::ProtectorService(Profile* profile)
19 : profile_(profile), 37 : profile_(profile),
20 has_active_change_(false) { 38 has_active_change_(false) {
21 // Start observing pref changes. 39 // Start observing pref changes.
22 prefs_watcher_.reset(new ProtectedPrefsWatcher(profile)); 40 prefs_watcher_.reset(new ProtectedPrefsWatcher(profile));
23 } 41 }
24 42
25 ProtectorService::~ProtectorService() { 43 ProtectorService::~ProtectorService() {
26 DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown. 44 DCHECK(!IsShowingChange()); // Should have been dismissed by Shutdown.
27 } 45 }
28 46
29 void ProtectorService::ShowChange(BaseSettingChange* change) { 47 void ProtectorService::ShowChange(BaseSettingChange* change) {
30 DCHECK(change); 48 DCHECK(change);
31 Item new_item; 49
32 new_item.change.reset(change);
33 DVLOG(1) << "Init change"; 50 DVLOG(1) << "Init change";
34 if (!change->Init(profile_)) { 51 if (!change->Init(profile_)) {
35 LOG(WARNING) << "Error while initializing, dismissing change"; 52 LOG(WARNING) << "Error while initializing, dismissing change";
53 delete change;
36 return; 54 return;
37 } 55 }
38 SettingsChangeGlobalError* error = 56
39 new SettingsChangeGlobalError(change, this); 57 Item* item_to_merge_with = FindItemToMergeWith(change);
40 new_item.error.reset(error); 58 if (item_to_merge_with) {
41 items_.push_back(new_item); 59 // CompositeSettingsChange takes ownership of merged changes.
42 // Do not show the bubble immediately if another one is active. 60 BaseSettingChange* existing_change = item_to_merge_with->change.release();
43 error->AddToProfile(profile_, !has_active_change_); 61 CompositeSettingsChange* merged_change = existing_change->MergeWith(change);
44 has_active_change_ = true; 62 item_to_merge_with->change.reset(merged_change);
63 item_to_merge_with->was_merged = true;
64 if (item_to_merge_with->error->GetBubbleView())
65 item_to_merge_with->show_when_merged = true;
66 // Remove old GlobalError instance. Later in OnRemovedFromProfile() a new
67 // GlobalError instance will be created for the composite change.
68 item_to_merge_with->error->RemoveFromProfile();
69 } else {
70 Item new_item;
71 SettingsChangeGlobalError* error =
72 new SettingsChangeGlobalError(change, this);
73 new_item.error.reset(error);
74 new_item.change.reset(change);
75 items_.push_back(new_item);
76 // Do not show the bubble immediately if another one is active.
77 error->AddToProfile(profile_, !has_active_change_);
78 has_active_change_ = true;
79 }
45 } 80 }
46 81
47 bool ProtectorService::IsShowingChange() const { 82 bool ProtectorService::IsShowingChange() const {
48 return !items_.empty(); 83 return !items_.empty();
49 } 84 }
50 85
51 void ProtectorService::ApplyChange(BaseSettingChange* change, 86 void ProtectorService::ApplyChange(BaseSettingChange* change,
52 Browser* browser) { 87 Browser* browser) {
53 change->Apply(browser); 88 change->Apply(browser);
54 DismissChange(change); 89 DismissChange(change);
55 } 90 }
56 91
57 void ProtectorService::DiscardChange(BaseSettingChange* change, 92 void ProtectorService::DiscardChange(BaseSettingChange* change,
58 Browser* browser) { 93 Browser* browser) {
59 change->Discard(browser); 94 change->Discard(browser);
60 DismissChange(change); 95 DismissChange(change);
61 } 96 }
62 97
63 void ProtectorService::DismissChange(BaseSettingChange* change) { 98 void ProtectorService::DismissChange(BaseSettingChange* change) {
64 std::vector<Item>::iterator item = 99 Items::iterator item = std::find_if(items_.begin(), items_.end(),
65 std::find_if(items_.begin(), items_.end(), MatchItemByChange(change)); 100 MatchItemByChange(change));
66 DCHECK(item != items_.end()); 101 DCHECK(item != items_.end());
67 item->error->RemoveFromProfile(); 102 item->error->RemoveFromProfile();
68 } 103 }
69 104
70 void ProtectorService::OpenTab(const GURL& url, Browser* browser) { 105 void ProtectorService::OpenTab(const GURL& url, Browser* browser) {
71 DCHECK(browser); 106 DCHECK(browser);
72 browser->ShowSingletonTab(url); 107 browser->ShowSingletonTab(url);
73 } 108 }
74 109
75 ProtectedPrefsWatcher* ProtectorService::GetPrefsWatcher() { 110 ProtectedPrefsWatcher* ProtectorService::GetPrefsWatcher() {
76 return prefs_watcher_.get(); 111 return prefs_watcher_.get();
77 } 112 }
78 113
114 ProtectorService::Item* ProtectorService::FindItemToMergeWith(
115 const BaseSettingChange* change) {
116 if (!change->CanBeMerged())
117 return NULL;
118 GURL url = change->GetNewSettingURL();
119 for (Items::iterator item = items_.begin(); item != items_.end(); item++) {
120 if (item->change->CanBeMerged() &&
121 CanMerge(url, item->change->GetNewSettingURL()))
122 return &*item;
123 }
124 return NULL;
125 }
126
79 void ProtectorService::Shutdown() { 127 void ProtectorService::Shutdown() {
80 while (IsShowingChange()) 128 while (IsShowingChange())
81 items_[0].error->RemoveFromProfile(); 129 items_[0].error->RemoveFromProfile();
82 } 130 }
83 131
84 void ProtectorService::OnApplyChange(SettingsChangeGlobalError* error, 132 void ProtectorService::OnApplyChange(SettingsChangeGlobalError* error,
85 Browser* browser) { 133 Browser* browser) {
86 DVLOG(1) << "Apply change"; 134 DVLOG(1) << "Apply change";
87 error->change()->Apply(browser); 135 error->change()->Apply(browser);
88 has_active_change_ = false; 136 has_active_change_ = false;
89 } 137 }
90 138
91 void ProtectorService::OnDiscardChange(SettingsChangeGlobalError* error, 139 void ProtectorService::OnDiscardChange(SettingsChangeGlobalError* error,
92 Browser* browser) { 140 Browser* browser) {
93 DVLOG(1) << "Discard change"; 141 DVLOG(1) << "Discard change";
94 error->change()->Discard(browser); 142 error->change()->Discard(browser);
95 has_active_change_ = false; 143 has_active_change_ = false;
96 } 144 }
97 145
98 void ProtectorService::OnDecisionTimeout(SettingsChangeGlobalError* error) { 146 void ProtectorService::OnDecisionTimeout(SettingsChangeGlobalError* error) {
99 DVLOG(1) << "Timeout"; 147 DVLOG(1) << "Timeout";
100 error->change()->Timeout(); 148 error->change()->Timeout();
101 } 149 }
102 150
103 void ProtectorService::OnRemovedFromProfile(SettingsChangeGlobalError* error) { 151 void ProtectorService::OnRemovedFromProfile(SettingsChangeGlobalError* error) {
104 std::vector<Item>::iterator item = 152 Items::iterator item = std::find_if(items_.begin(), items_.end(),
105 std::find_if(items_.begin(), items_.end(), MatchItemByError(error)); 153 MatchItemByError(error));
106 DCHECK(item != items_.end()); 154 DCHECK(item != items_.end());
155
156 if (item->was_merged) {
157 bool show_new_error = !has_active_change_ || item->show_when_merged;
158 item->was_merged = false;
159 item->show_when_merged = false;
160 // Item was merged with another change instance and error has been removed,
161 // create a new one for the composite change.
162 item->error.reset(new SettingsChangeGlobalError(item->change.get(), this));
163 item->error->AddToProfile(profile_, show_new_error);
164 has_active_change_ = true;
165 return;
166 }
167
107 items_.erase(item); 168 items_.erase(item);
108 169
170 // If no other change is shown and there are changes that haven't been shown
171 // yet, show the first one.
109 if (!has_active_change_) { 172 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) { 173 for (item = items_.begin(); item != items_.end(); ++item) {
112 if (!item->error->HasShownBubbleView()) { 174 if (!item->error->HasShownBubbleView()) {
113 item->error->ShowBubble(); 175 item->error->ShowBubble();
114 has_active_change_ = true; 176 has_active_change_ = true;
115 return; 177 return;
116 } 178 }
117 } 179 }
118 } 180 }
119 } 181 }
120 182
121 BaseSettingChange* ProtectorService::GetLastChange() { 183 BaseSettingChange* ProtectorService::GetLastChange() {
122 return items_.empty() ? NULL : items_.back().change.get(); 184 return items_.empty() ? NULL : items_.back().change.get();
123 } 185 }
124 186
125 ProtectorService::Item::Item() { 187 ProtectorService::Item::Item()
188 : was_merged(false),
189 show_when_merged(false) {
126 } 190 }
127 191
128 ProtectorService::Item::~Item() { 192 ProtectorService::Item::~Item() {
129 } 193 }
130 194
131 ProtectorService::MatchItemByChange::MatchItemByChange( 195 ProtectorService::MatchItemByChange::MatchItemByChange(
132 const BaseSettingChange* other) : other_(other) { 196 const BaseSettingChange* other) : other_(other) {
133 } 197 }
134 198
135 bool ProtectorService::MatchItemByChange::operator()( 199 bool ProtectorService::MatchItemByChange::operator()(
136 const ProtectorService::Item& item) { 200 const ProtectorService::Item& item) {
137 return other_ == item.change.get(); 201 return item.change->Contains(other_);
138 } 202 }
139 203
140 ProtectorService::MatchItemByError::MatchItemByError( 204 ProtectorService::MatchItemByError::MatchItemByError(
141 const SettingsChangeGlobalError* other) : other_(other) { 205 const SettingsChangeGlobalError* other) : other_(other) {
142 } 206 }
143 207
144 bool ProtectorService::MatchItemByError::operator()( 208 bool ProtectorService::MatchItemByError::operator()(
145 const ProtectorService::Item& item) { 209 const ProtectorService::Item& item) {
146 return other_ == item.error.get(); 210 return other_ == item.error.get();
147 } 211 }
148 212
149 } // namespace protector 213 } // namespace protector
OLDNEW
« no previous file with comments | « chrome/browser/protector/protector_service.h ('k') | chrome/browser/protector/protector_service_browsertest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698