| 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/extensions/extension_warning_service.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/browser_process.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/extensions/extension_system.h" |
| 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/profiles/profile_manager.h" |
| 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "chrome/common/extensions/extension.h" |
| 15 |
| 16 #include "content/public/browser/browser_thread.h" |
| 17 #include "content/public/browser/notification_service.h" |
| 18 |
| 19 using content::BrowserThread; |
| 20 |
| 21 namespace extensions { |
| 22 |
| 23 ExtensionWarningService::ExtensionWarningService(Profile* profile) |
| 24 : profile_(profile) { |
| 25 DCHECK(CalledOnValidThread()); |
| 26 if (profile_) { |
| 27 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 28 content::Source<Profile>(profile_->GetOriginalProfile())); |
| 29 } |
| 30 } |
| 31 |
| 32 ExtensionWarningService::~ExtensionWarningService() {} |
| 33 |
| 34 void ExtensionWarningService::ClearWarnings( |
| 35 const std::set<ExtensionWarning::WarningType>& types) { |
| 36 DCHECK(CalledOnValidThread()); |
| 37 bool deleted_anything = false; |
| 38 for (ExtensionWarningSet::iterator i = warnings_.begin(); |
| 39 i != warnings_.end();) { |
| 40 if (types.find(i->warning_type()) != types.end()) { |
| 41 deleted_anything = true; |
| 42 warnings_.erase(i++); |
| 43 } else { |
| 44 ++i; |
| 45 } |
| 46 } |
| 47 |
| 48 if (deleted_anything) |
| 49 NotifyWarningsChanged(); |
| 50 } |
| 51 |
| 52 void ExtensionWarningService::GetWarningTypesAffectingExtension( |
| 53 const std::string& extension_id, |
| 54 std::set<ExtensionWarning::WarningType>* result) const { |
| 55 DCHECK(CalledOnValidThread()); |
| 56 result->clear(); |
| 57 for (ExtensionWarningSet::const_iterator i = warnings_.begin(); |
| 58 i != warnings_.end(); ++i) { |
| 59 if (i->extension_id() == extension_id) |
| 60 result->insert(i->warning_type()); |
| 61 } |
| 62 } |
| 63 |
| 64 void ExtensionWarningService::GetWarningMessagesForExtension( |
| 65 const std::string& extension_id, |
| 66 std::vector<std::string>* result) const { |
| 67 DCHECK(CalledOnValidThread()); |
| 68 result->clear(); |
| 69 |
| 70 const ExtensionService* extension_service = |
| 71 ExtensionSystem::Get(profile_)->extension_service(); |
| 72 |
| 73 for (ExtensionWarningSet::const_iterator i = warnings_.begin(); |
| 74 i != warnings_.end(); ++i) { |
| 75 if (i->extension_id() == extension_id) |
| 76 result->push_back(i->GetMessage(extension_service->extensions())); |
| 77 } |
| 78 } |
| 79 |
| 80 void ExtensionWarningService::AddWarnings( |
| 81 const ExtensionWarningSet& warnings) { |
| 82 DCHECK(CalledOnValidThread()); |
| 83 size_t old_size = warnings_.size(); |
| 84 |
| 85 warnings_.insert(warnings.begin(), warnings.end()); |
| 86 |
| 87 if (old_size != warnings_.size()) |
| 88 NotifyWarningsChanged(); |
| 89 } |
| 90 |
| 91 // static |
| 92 void ExtensionWarningService::NotifyWarningsOnUI( |
| 93 void* profile_id, |
| 94 const ExtensionWarningSet& warnings) { |
| 95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 96 Profile* profile = reinterpret_cast<Profile*>(profile_id); |
| 97 if (!profile || |
| 98 !g_browser_process->profile_manager() || |
| 99 !g_browser_process->profile_manager()->IsValidProfile(profile)) { |
| 100 return; |
| 101 } |
| 102 |
| 103 extensions::ExtensionWarningService* warning_service = |
| 104 extensions::ExtensionSystem::Get(profile)->warning_service(); |
| 105 |
| 106 warning_service->AddWarnings(warnings); |
| 107 } |
| 108 |
| 109 void ExtensionWarningService::AddObserver(Observer* observer) { |
| 110 observer_list_.AddObserver(observer); |
| 111 } |
| 112 |
| 113 void ExtensionWarningService::RemoveObserver(Observer* observer) { |
| 114 observer_list_.RemoveObserver(observer); |
| 115 } |
| 116 |
| 117 void ExtensionWarningService::NotifyWarningsChanged() { |
| 118 FOR_EACH_OBSERVER(Observer, observer_list_, ExtensionWarningsChanged()); |
| 119 } |
| 120 |
| 121 void ExtensionWarningService::Observe( |
| 122 int type, |
| 123 const content::NotificationSource& source, |
| 124 const content::NotificationDetails& details) { |
| 125 switch (type) { |
| 126 case chrome::NOTIFICATION_EXTENSION_UNLOADED: { |
| 127 const Extension* extension = |
| 128 content::Details<extensions::UnloadedExtensionInfo>(details)-> |
| 129 extension; |
| 130 // Unloading one extension might have solved the problems of others. |
| 131 // Therefore, we clear warnings of this type for all extensions. |
| 132 std::set<ExtensionWarning::WarningType> warning_types; |
| 133 GetWarningTypesAffectingExtension(extension->id(), &warning_types); |
| 134 ClearWarnings(warning_types); |
| 135 break; |
| 136 } |
| 137 default: |
| 138 NOTREACHED(); |
| 139 break; |
| 140 } |
| 141 } |
| 142 |
| 143 } // namespace extensions |
| OLD | NEW |