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

Side by Side Diff: chrome/browser/ui/global_error/global_error_service.cc

Issue 2409443002: Make GlobalErrorService's ownership model slightly less insane. (Closed)
Patch Set: commentary Created 4 years 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
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/ui/global_error/global_error_service.h" 5 #include "chrome/browser/ui/global_error/global_error_service.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/stl_util.h"
12 #include "chrome/browser/chrome_notification_types.h" 11 #include "chrome/browser/chrome_notification_types.h"
13 #include "chrome/browser/profiles/profile.h" 12 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/browser/ui/global_error/global_error.h" 13 #include "chrome/browser/ui/global_error/global_error.h"
15 #include "chrome/browser/ui/global_error/global_error_bubble_view_base.h" 14 #include "chrome/browser/ui/global_error/global_error_bubble_view_base.h"
16 #include "content/public/browser/notification_service.h" 15 #include "content/public/browser/notification_service.h"
17 16
18 GlobalErrorService::GlobalErrorService(Profile* profile) : profile_(profile) { 17 GlobalErrorService::GlobalErrorService(Profile* profile) : profile_(profile) {
19 } 18 }
20 19
21 GlobalErrorService::~GlobalErrorService() { 20 GlobalErrorService::~GlobalErrorService() {}
22 base::STLDeleteElements(&errors_); 21
22 void GlobalErrorService::AddGlobalError(std::unique_ptr<GlobalError> error) {
23 DCHECK(error);
24 GlobalError* error_ptr = error.get();
25 owned_errors_[error_ptr] = std::move(error);
26 AddUnownedGlobalError(error_ptr);
23 } 27 }
24 28
25 void GlobalErrorService::AddGlobalError(GlobalError* error) { 29 void GlobalErrorService::AddUnownedGlobalError(GlobalError* error) {
26 DCHECK(error); 30 DCHECK(error);
27 errors_.push_back(error); 31 all_errors_.push_back(error);
28 NotifyErrorsChanged(error); 32 NotifyErrorsChanged(error);
29 } 33 }
30 34
31 void GlobalErrorService::RemoveGlobalError(GlobalError* error) { 35 std::unique_ptr<GlobalError> GlobalErrorService::RemoveGlobalError(
32 errors_.erase(std::find(errors_.begin(), errors_.end(), error)); 36 GlobalError* error_ptr) {
37 std::unique_ptr<GlobalError> ptr = std::move(owned_errors_[error_ptr]);
38 owned_errors_.erase(error_ptr);
39 RemoveUnownedGlobalError(error_ptr);
40 return ptr;
41 }
42
43 void GlobalErrorService::RemoveUnownedGlobalError(GlobalError* error) {
44 DCHECK(owned_errors_.find(error) == owned_errors_.end());
45 all_errors_.erase(std::find(all_errors_.begin(), all_errors_.end(), error));
33 GlobalErrorBubbleViewBase* bubble = error->GetBubbleView(); 46 GlobalErrorBubbleViewBase* bubble = error->GetBubbleView();
34 if (bubble) 47 if (bubble)
35 bubble->CloseBubbleView(); 48 bubble->CloseBubbleView();
36 NotifyErrorsChanged(error); 49 NotifyErrorsChanged(error);
37 } 50 }
38 51
39 GlobalError* GlobalErrorService::GetGlobalErrorByMenuItemCommandID( 52 GlobalError* GlobalErrorService::GetGlobalErrorByMenuItemCommandID(
40 int command_id) const { 53 int command_id) const {
41 for (GlobalErrorList::const_iterator 54 for (const auto& error : all_errors_)
42 it = errors_.begin(); it != errors_.end(); ++it) {
43 GlobalError* error = *it;
44 if (error->HasMenuItem() && command_id == error->MenuItemCommandID()) 55 if (error->HasMenuItem() && command_id == error->MenuItemCommandID())
45 return error; 56 return error;
46 } 57
47 return NULL; 58 return nullptr;
48 } 59 }
49 60
50 GlobalError* 61 GlobalError*
51 GlobalErrorService::GetHighestSeverityGlobalErrorWithAppMenuItem() const { 62 GlobalErrorService::GetHighestSeverityGlobalErrorWithAppMenuItem() const {
52 GlobalError::Severity highest_severity = GlobalError::SEVERITY_LOW; 63 GlobalError::Severity highest_severity = GlobalError::SEVERITY_LOW;
53 GlobalError* highest_severity_error = NULL; 64 GlobalError* highest_severity_error = nullptr;
54 65
55 for (GlobalErrorList::const_iterator 66 for (const auto& error : all_errors_) {
56 it = errors_.begin(); it != errors_.end(); ++it) {
57 GlobalError* error = *it;
58 if (error->HasMenuItem()) { 67 if (error->HasMenuItem()) {
59 if (!highest_severity_error || error->GetSeverity() > highest_severity) { 68 if (!highest_severity_error || error->GetSeverity() > highest_severity) {
60 highest_severity = error->GetSeverity(); 69 highest_severity = error->GetSeverity();
61 highest_severity_error = error; 70 highest_severity_error = error;
62 } 71 }
63 } 72 }
64 } 73 }
65 74
66 return highest_severity_error; 75 return highest_severity_error;
67 } 76 }
68 77
69 GlobalError* GlobalErrorService::GetFirstGlobalErrorWithBubbleView() const { 78 GlobalError* GlobalErrorService::GetFirstGlobalErrorWithBubbleView() const {
70 for (GlobalErrorList::const_iterator 79 for (const auto& error : all_errors_) {
71 it = errors_.begin(); it != errors_.end(); ++it) {
72 GlobalError* error = *it;
73 if (error->HasBubbleView() && !error->HasShownBubbleView()) 80 if (error->HasBubbleView() && !error->HasShownBubbleView())
74 return error; 81 return error;
75 } 82 }
76 return NULL; 83 return nullptr;
77 } 84 }
78 85
79 void GlobalErrorService::NotifyErrorsChanged(GlobalError* error) { 86 void GlobalErrorService::NotifyErrorsChanged(GlobalError* error) {
80 // GlobalErrorService is bound only to original profile so we need to send 87 // GlobalErrorService is bound only to original profile so we need to send
81 // notifications to both it and its off-the-record profile to update 88 // notifications to both it and its off-the-record profile to update
82 // incognito windows as well. 89 // incognito windows as well.
83 std::vector<Profile*> profiles_to_notify; 90 std::vector<Profile*> profiles_to_notify;
84 if (profile_ != NULL) { 91 if (profile_) {
85 profiles_to_notify.push_back(profile_); 92 profiles_to_notify.push_back(profile_);
86 if (profile_->IsOffTheRecord()) 93 if (profile_->IsOffTheRecord())
87 profiles_to_notify.push_back(profile_->GetOriginalProfile()); 94 profiles_to_notify.push_back(profile_->GetOriginalProfile());
88 else if (profile_->HasOffTheRecordProfile()) 95 else if (profile_->HasOffTheRecordProfile())
89 profiles_to_notify.push_back(profile_->GetOffTheRecordProfile()); 96 profiles_to_notify.push_back(profile_->GetOffTheRecordProfile());
90 for (size_t i = 0; i < profiles_to_notify.size(); ++i) { 97 for (size_t i = 0; i < profiles_to_notify.size(); ++i) {
91 content::NotificationService::current()->Notify( 98 content::NotificationService::current()->Notify(
92 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, 99 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
93 content::Source<Profile>(profiles_to_notify[i]), 100 content::Source<Profile>(profiles_to_notify[i]),
94 content::Details<GlobalError>(error)); 101 content::Details<GlobalError>(error));
95 } 102 }
96 } 103 }
97 } 104 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698