| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/global_error_service.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/stl_util.h" | |
| 10 #include "chrome/browser/profiles/profile.h" | |
| 11 #include "chrome/browser/ui/global_error.h" | |
| 12 #include "chrome/browser/ui/global_error_bubble_view_base.h" | |
| 13 #include "chrome/common/chrome_notification_types.h" | |
| 14 #include "content/public/browser/notification_service.h" | |
| 15 | |
| 16 GlobalErrorService::GlobalErrorService(Profile* profile) : profile_(profile) { | |
| 17 } | |
| 18 | |
| 19 GlobalErrorService::~GlobalErrorService() { | |
| 20 STLDeleteElements(&errors_); | |
| 21 } | |
| 22 | |
| 23 void GlobalErrorService::AddGlobalError(GlobalError* error) { | |
| 24 errors_.push_back(error); | |
| 25 NotifyErrorsChanged(error); | |
| 26 } | |
| 27 | |
| 28 void GlobalErrorService::RemoveGlobalError(GlobalError* error) { | |
| 29 errors_.erase(std::find(errors_.begin(), errors_.end(), error)); | |
| 30 GlobalErrorBubbleViewBase* bubble = error->GetBubbleView(); | |
| 31 if (bubble) | |
| 32 bubble->CloseBubbleView(); | |
| 33 NotifyErrorsChanged(error); | |
| 34 } | |
| 35 | |
| 36 GlobalError* GlobalErrorService::GetGlobalErrorByMenuItemCommandID( | |
| 37 int command_id) const { | |
| 38 for (GlobalErrorList::const_iterator | |
| 39 it = errors_.begin(); it != errors_.end(); ++it) { | |
| 40 GlobalError* error = *it; | |
| 41 if (error->HasMenuItem() && command_id == error->MenuItemCommandID()) | |
| 42 return error; | |
| 43 } | |
| 44 return NULL; | |
| 45 } | |
| 46 | |
| 47 int GlobalErrorService::GetFirstBadgeResourceID() const { | |
| 48 for (GlobalErrorList::const_iterator | |
| 49 it = errors_.begin(); it != errors_.end(); ++it) { | |
| 50 GlobalError* error = *it; | |
| 51 if (error->HasBadge()) | |
| 52 return error->GetBadgeResourceID(); | |
| 53 } | |
| 54 return 0; | |
| 55 } | |
| 56 | |
| 57 GlobalError* GlobalErrorService::GetFirstGlobalErrorWithBubbleView() const { | |
| 58 for (GlobalErrorList::const_iterator | |
| 59 it = errors_.begin(); it != errors_.end(); ++it) { | |
| 60 GlobalError* error = *it; | |
| 61 if (error->HasBubbleView() && !error->HasShownBubbleView()) | |
| 62 return error; | |
| 63 } | |
| 64 return NULL; | |
| 65 } | |
| 66 | |
| 67 void GlobalErrorService::NotifyErrorsChanged(GlobalError* error) { | |
| 68 // GlobalErrorService is bound only to original profile so we need to send | |
| 69 // notifications to both it and its off-the-record profile to update | |
| 70 // incognito windows as well. | |
| 71 std::vector<Profile*> profiles_to_notify; | |
| 72 if (profile_ != NULL) { | |
| 73 profiles_to_notify.push_back(profile_); | |
| 74 if (profile_->IsOffTheRecord()) | |
| 75 profiles_to_notify.push_back(profile_->GetOriginalProfile()); | |
| 76 else if (profile_->HasOffTheRecordProfile()) | |
| 77 profiles_to_notify.push_back(profile_->GetOffTheRecordProfile()); | |
| 78 for (size_t i = 0; i < profiles_to_notify.size(); ++i) { | |
| 79 content::NotificationService::current()->Notify( | |
| 80 chrome::NOTIFICATION_GLOBAL_ERRORS_CHANGED, | |
| 81 content::Source<Profile>(profiles_to_notify[i]), | |
| 82 content::Details<GlobalError>(error)); | |
| 83 } | |
| 84 } | |
| 85 } | |
| OLD | NEW |