| OLD | NEW |
| 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/background/background_application_list_model.h" | 5 #include "chrome/browser/background/background_application_list_model.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <set> | 8 #include <set> |
| 9 | 9 |
| 10 #include "base/stl_util.h" | 10 #include "base/memory/ptr_util.h" |
| 11 #include "base/strings/string_number_conversions.h" | 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "chrome/app/chrome_command_ids.h" | 13 #include "chrome/app/chrome_command_ids.h" |
| 14 #include "chrome/browser/background/background_contents_service.h" | 14 #include "chrome/browser/background/background_contents_service.h" |
| 15 #include "chrome/browser/background/background_contents_service_factory.h" | 15 #include "chrome/browser/background/background_contents_service_factory.h" |
| 16 #include "chrome/browser/background/background_mode_manager.h" | 16 #include "chrome/browser/background/background_mode_manager.h" |
| 17 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | 18 #include "chrome/browser/chrome_notification_types.h" |
| 19 #include "chrome/browser/extensions/extension_service.h" | 19 #include "chrome/browser/extensions/extension_service.h" |
| 20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
| (...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 extension_misc::ExtensionIcons size) { | 140 extension_misc::ExtensionIcons size) { |
| 141 extensions::ExtensionResource resource = | 141 extensions::ExtensionResource resource = |
| 142 extensions::IconsInfo::GetIconResource( | 142 extensions::IconsInfo::GetIconResource( |
| 143 extension_, size, ExtensionIconSet::MATCH_BIGGER); | 143 extension_, size, ExtensionIconSet::MATCH_BIGGER); |
| 144 extensions::ImageLoader::Get(model_->profile_)->LoadImageAsync( | 144 extensions::ImageLoader::Get(model_->profile_)->LoadImageAsync( |
| 145 extension_, resource, gfx::Size(size, size), | 145 extension_, resource, gfx::Size(size, size), |
| 146 base::Bind(&Application::OnImageLoaded, AsWeakPtr())); | 146 base::Bind(&Application::OnImageLoaded, AsWeakPtr())); |
| 147 } | 147 } |
| 148 | 148 |
| 149 BackgroundApplicationListModel::~BackgroundApplicationListModel() { | 149 BackgroundApplicationListModel::~BackgroundApplicationListModel() { |
| 150 base::STLDeleteContainerPairSecondPointers(applications_.begin(), | |
| 151 applications_.end()); | |
| 152 } | 150 } |
| 153 | 151 |
| 154 BackgroundApplicationListModel::BackgroundApplicationListModel(Profile* profile) | 152 BackgroundApplicationListModel::BackgroundApplicationListModel(Profile* profile) |
| 155 : profile_(profile), | 153 : profile_(profile), |
| 156 ready_(false) { | 154 ready_(false) { |
| 157 DCHECK(profile_); | 155 DCHECK(profile_); |
| 158 registrar_.Add(this, | 156 registrar_.Add(this, |
| 159 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 157 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, |
| 160 content::Source<Profile>(profile)); | 158 content::Source<Profile>(profile)); |
| 161 registrar_.Add(this, | 159 registrar_.Add(this, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 187 DCHECK(IsBackgroundApp(*extension, profile_)); | 185 DCHECK(IsBackgroundApp(*extension, profile_)); |
| 188 Application* application = FindApplication(extension); | 186 Application* application = FindApplication(extension); |
| 189 if (!application) { | 187 if (!application) { |
| 190 // App position is used as a dynamic command and so must be less than any | 188 // App position is used as a dynamic command and so must be less than any |
| 191 // predefined command id. | 189 // predefined command id. |
| 192 if (applications_.size() >= IDC_MinimumLabelValue) { | 190 if (applications_.size() >= IDC_MinimumLabelValue) { |
| 193 LOG(ERROR) << "Background application limit of " << IDC_MinimumLabelValue | 191 LOG(ERROR) << "Background application limit of " << IDC_MinimumLabelValue |
| 194 << " exceeded. Ignoring."; | 192 << " exceeded. Ignoring."; |
| 195 return; | 193 return; |
| 196 } | 194 } |
| 197 application = new Application(this, extension); | 195 std::unique_ptr<Application> application_ptr = |
| 198 applications_[extension->id()] = application; | 196 base::MakeUnique<Application>(this, extension); |
| 197 application = application_ptr.get(); |
| 198 applications_[extension->id()] = std::move(application_ptr); |
| 199 Update(); | 199 Update(); |
| 200 application->RequestIcon(extension_misc::EXTENSION_ICON_BITTY); | 200 application->RequestIcon(extension_misc::EXTENSION_ICON_BITTY); |
| 201 } | 201 } |
| 202 } | 202 } |
| 203 | 203 |
| 204 void BackgroundApplicationListModel::DissociateApplicationData( | 204 void BackgroundApplicationListModel::DissociateApplicationData( |
| 205 const Extension* extension) { | 205 const Extension* extension) { |
| 206 ApplicationMap::iterator found = applications_.find(extension->id()); | 206 applications_.erase(extension->id()); |
| 207 if (found != applications_.end()) { | |
| 208 delete found->second; | |
| 209 applications_.erase(found); | |
| 210 } | |
| 211 } | 207 } |
| 212 | 208 |
| 213 const Extension* BackgroundApplicationListModel::GetExtension( | 209 const Extension* BackgroundApplicationListModel::GetExtension( |
| 214 int position) const { | 210 int position) const { |
| 215 DCHECK(position >= 0 && static_cast<size_t>(position) < extensions_.size()); | 211 DCHECK(position >= 0 && static_cast<size_t>(position) < extensions_.size()); |
| 216 return extensions_[position].get(); | 212 return extensions_[position].get(); |
| 217 } | 213 } |
| 218 | 214 |
| 219 const BackgroundApplicationListModel::Application* | 215 const BackgroundApplicationListModel::Application* |
| 220 BackgroundApplicationListModel::FindApplication( | 216 BackgroundApplicationListModel::FindApplication( |
| 221 const Extension* extension) const { | 217 const Extension* extension) const { |
| 222 const std::string& id = extension->id(); | 218 const std::string& id = extension->id(); |
| 223 ApplicationMap::const_iterator found = applications_.find(id); | 219 auto found = applications_.find(id); |
| 224 return (found == applications_.end()) ? NULL : found->second; | 220 return (found == applications_.end()) ? nullptr : found->second.get(); |
| 225 } | 221 } |
| 226 | 222 |
| 227 BackgroundApplicationListModel::Application* | 223 BackgroundApplicationListModel::Application* |
| 228 BackgroundApplicationListModel::FindApplication( | 224 BackgroundApplicationListModel::FindApplication( |
| 229 const Extension* extension) { | 225 const Extension* extension) { |
| 230 const std::string& id = extension->id(); | 226 const std::string& id = extension->id(); |
| 231 ApplicationMap::iterator found = applications_.find(id); | 227 auto found = applications_.find(id); |
| 232 return (found == applications_.end()) ? NULL : found->second; | 228 return (found == applications_.end()) ? nullptr : found->second.get(); |
| 233 } | 229 } |
| 234 | 230 |
| 235 const gfx::ImageSkia* BackgroundApplicationListModel::GetIcon( | 231 const gfx::ImageSkia* BackgroundApplicationListModel::GetIcon( |
| 236 const Extension* extension) { | 232 const Extension* extension) { |
| 237 const Application* application = FindApplication(extension); | 233 const Application* application = FindApplication(extension); |
| 238 if (application) | 234 if (application) |
| 239 return application->icon_.get(); | 235 return application->icon_.get(); |
| 240 AssociateApplicationData(extension); | 236 AssociateApplicationData(extension); |
| 241 return NULL; | 237 return nullptr; |
| 242 } | 238 } |
| 243 | 239 |
| 244 int BackgroundApplicationListModel::GetPosition( | 240 int BackgroundApplicationListModel::GetPosition( |
| 245 const Extension* extension) const { | 241 const Extension* extension) const { |
| 246 int position = 0; | 242 int position = 0; |
| 247 const std::string& id = extension->id(); | 243 const std::string& id = extension->id(); |
| 248 for (const auto& it : extensions_) { | 244 for (const auto& it : extensions_) { |
| 249 if (id == it->id()) | 245 if (id == it->id()) |
| 250 return position; | 246 return position; |
| 251 ++position; | 247 ++position; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 398 (*old_cursor)->name() == (*new_cursor)->name() && | 394 (*old_cursor)->name() == (*new_cursor)->name() && |
| 399 (*old_cursor)->id() == (*new_cursor)->id()) { | 395 (*old_cursor)->id() == (*new_cursor)->id()) { |
| 400 ++old_cursor; | 396 ++old_cursor; |
| 401 ++new_cursor; | 397 ++new_cursor; |
| 402 } | 398 } |
| 403 if (old_cursor != extensions_.end() || new_cursor != extensions.end()) { | 399 if (old_cursor != extensions_.end() || new_cursor != extensions.end()) { |
| 404 extensions_ = extensions; | 400 extensions_ = extensions; |
| 405 FOR_EACH_OBSERVER(Observer, observers_, OnApplicationListChanged(profile_)); | 401 FOR_EACH_OBSERVER(Observer, observers_, OnApplicationListChanged(profile_)); |
| 406 } | 402 } |
| 407 } | 403 } |
| OLD | NEW |