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

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: windows, ownership Created 4 years, 2 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
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::AddOwnedGlobalError(
23 std::unique_ptr<GlobalError> error) {
24 DCHECK(error);
25 GlobalError* error_ptr = error.get();
26 owned_errors_[error_ptr] = std::move(error);
27 AddUnownedGlobalError(error_ptr);
23 } 28 }
24 29
25 void GlobalErrorService::AddGlobalError(GlobalError* error) { 30 void GlobalErrorService::AddUnownedGlobalError(GlobalError* error) {
26 DCHECK(error); 31 DCHECK(error);
27 errors_.push_back(error); 32 all_errors_.push_back(error);
28 NotifyErrorsChanged(error); 33 NotifyErrorsChanged(error);
29 } 34 }
30 35
31 void GlobalErrorService::RemoveGlobalError(GlobalError* error) { 36 std::unique_ptr<GlobalError> GlobalErrorService::RemoveOwnedGlobalError(
32 errors_.erase(std::find(errors_.begin(), errors_.end(), error)); 37 GlobalError* error_ptr) {
38 std::unique_ptr<GlobalError> ptr = std::move(owned_errors_[error_ptr]);
39 owned_errors_.erase(error_ptr);
40 RemoveUnownedGlobalError(error_ptr);
41 return ptr;
42 }
43
44 void GlobalErrorService::RemoveUnownedGlobalError(GlobalError* error) {
45 DCHECK(owned_errors_.find(error) == owned_errors_.end());
46 all_errors_.erase(std::find(all_errors_.begin(), all_errors_.end(), error));
33 GlobalErrorBubbleViewBase* bubble = error->GetBubbleView(); 47 GlobalErrorBubbleViewBase* bubble = error->GetBubbleView();
34 if (bubble) 48 if (bubble)
35 bubble->CloseBubbleView(); 49 bubble->CloseBubbleView();
36 NotifyErrorsChanged(error); 50 NotifyErrorsChanged(error);
37 } 51 }
38 52
39 GlobalError* GlobalErrorService::GetGlobalErrorByMenuItemCommandID( 53 GlobalError* GlobalErrorService::GetGlobalErrorByMenuItemCommandID(
40 int command_id) const { 54 int command_id) const {
41 for (GlobalErrorList::const_iterator 55 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()) 56 if (error->HasMenuItem() && command_id == error->MenuItemCommandID())
45 return error; 57 return error;
46 } 58
47 return NULL; 59 return nullptr;
48 } 60 }
49 61
50 GlobalError* 62 GlobalError*
51 GlobalErrorService::GetHighestSeverityGlobalErrorWithAppMenuItem() const { 63 GlobalErrorService::GetHighestSeverityGlobalErrorWithAppMenuItem() const {
52 GlobalError::Severity highest_severity = GlobalError::SEVERITY_LOW; 64 GlobalError::Severity highest_severity = GlobalError::SEVERITY_LOW;
53 GlobalError* highest_severity_error = NULL; 65 GlobalError* highest_severity_error = nullptr;
54 66
55 for (GlobalErrorList::const_iterator 67 for (const auto& error : all_errors_) {
56 it = errors_.begin(); it != errors_.end(); ++it) {
57 GlobalError* error = *it;
58 if (error->HasMenuItem()) { 68 if (error->HasMenuItem()) {
59 if (!highest_severity_error || error->GetSeverity() > highest_severity) { 69 if (!highest_severity_error || error->GetSeverity() > highest_severity) {
60 highest_severity = error->GetSeverity(); 70 highest_severity = error->GetSeverity();
61 highest_severity_error = error; 71 highest_severity_error = error;
62 } 72 }
63 } 73 }
64 } 74 }
65 75
66 return highest_severity_error; 76 return highest_severity_error;
67 } 77 }
68 78
69 GlobalError* GlobalErrorService::GetFirstGlobalErrorWithBubbleView() const { 79 GlobalError* GlobalErrorService::GetFirstGlobalErrorWithBubbleView() const {
70 for (GlobalErrorList::const_iterator 80 for (const auto& error : all_errors_) {
71 it = errors_.begin(); it != errors_.end(); ++it) {
72 GlobalError* error = *it;
73 if (error->HasBubbleView() && !error->HasShownBubbleView()) 81 if (error->HasBubbleView() && !error->HasShownBubbleView())
74 return error; 82 return error;
75 } 83 }
76 return NULL; 84 return nullptr;
77 } 85 }
78 86
79 void GlobalErrorService::NotifyErrorsChanged(GlobalError* error) { 87 void GlobalErrorService::NotifyErrorsChanged(GlobalError* error) {
80 // GlobalErrorService is bound only to original profile so we need to send 88 // 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 89 // notifications to both it and its off-the-record profile to update
82 // incognito windows as well. 90 // incognito windows as well.
83 std::vector<Profile*> profiles_to_notify; 91 std::vector<Profile*> profiles_to_notify;
84 if (profile_ != NULL) { 92 if (profile_) {
85 profiles_to_notify.push_back(profile_); 93 profiles_to_notify.push_back(profile_);
86 if (profile_->IsOffTheRecord()) 94 if (profile_->IsOffTheRecord())
87 profiles_to_notify.push_back(profile_->GetOriginalProfile()); 95 profiles_to_notify.push_back(profile_->GetOriginalProfile());
88 else if (profile_->HasOffTheRecordProfile()) 96 else if (profile_->HasOffTheRecordProfile())
89 profiles_to_notify.push_back(profile_->GetOffTheRecordProfile()); 97 profiles_to_notify.push_back(profile_->GetOffTheRecordProfile());
90 for (size_t i = 0; i < profiles_to_notify.size(); ++i) { 98 for (size_t i = 0; i < profiles_to_notify.size(); ++i) {
91 content::NotificationService::current()->Notify( 99 content::NotificationService::current()->Notify(
92 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, 100 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED,
93 content::Source<Profile>(profiles_to_notify[i]), 101 content::Source<Profile>(profiles_to_notify[i]),
94 content::Details<GlobalError>(error)); 102 content::Details<GlobalError>(error));
95 } 103 }
96 } 104 }
97 } 105 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698