OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_application_list_model.h" | 5 #include "chrome/browser/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-inl.h" | 10 #include "base/stl_util-inl.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
41 UTF8ToUTF16(x->name()), | 41 UTF8ToUTF16(x->name()), |
42 UTF8ToUTF16(y->name())); | 42 UTF8ToUTF16(y->name())); |
43 } | 43 } |
44 | 44 |
45 // Background application representation, private to the | 45 // Background application representation, private to the |
46 // BackgroundApplicationListModel class. | 46 // BackgroundApplicationListModel class. |
47 class BackgroundApplicationListModel::Application | 47 class BackgroundApplicationListModel::Application |
48 : public ImageLoadingTracker::Observer { | 48 : public ImageLoadingTracker::Observer { |
49 public: | 49 public: |
50 Application(BackgroundApplicationListModel* model, | 50 Application(BackgroundApplicationListModel* model, |
51 const Extension* an_extension); | 51 const Extension* an_extension, |
52 Profile* profile_); | |
Miranda Callahan
2011/05/20 15:16:53
nit: remove trailing _
rpetterson
2011/05/23 03:23:19
Done.
| |
52 | 53 |
53 virtual ~Application(); | 54 virtual ~Application(); |
54 | 55 |
55 // Invoked when a request icon is available. | 56 // Invoked when a request icon is available. |
56 virtual void OnImageLoaded(SkBitmap* image, | 57 virtual void OnImageLoaded(SkBitmap* image, |
57 const ExtensionResource& resource, | 58 const ExtensionResource& resource, |
58 int index); | 59 int index); |
59 | 60 |
60 // Uses the FILE thread to request this extension's icon, sized | 61 // Uses the FILE thread to request this extension's icon, sized |
61 // appropriately. | 62 // appropriately. |
62 void RequestIcon(Extension::Icons size); | 63 void RequestIcon(Extension::Icons size); |
63 | 64 |
64 const Extension* extension_; | 65 const Extension* extension_; |
65 scoped_ptr<SkBitmap> icon_; | 66 scoped_ptr<SkBitmap> icon_; |
66 BackgroundApplicationListModel* model_; | 67 BackgroundApplicationListModel* model_; |
67 ImageLoadingTracker tracker_; | 68 ImageLoadingTracker tracker_; |
69 Profile* profile_; | |
68 }; | 70 }; |
69 | 71 |
70 namespace { | 72 namespace { |
71 void GetServiceApplications(ExtensionService* service, | 73 void GetServiceApplications(ExtensionService* service, |
72 ExtensionList* applications_result) { | 74 ExtensionList* applications_result) { |
73 const ExtensionList* extensions = service->extensions(); | 75 const ExtensionList* extensions = service->extensions(); |
74 | 76 |
75 for (ExtensionList::const_iterator cursor = extensions->begin(); | 77 for (ExtensionList::const_iterator cursor = extensions->begin(); |
76 cursor != extensions->end(); | 78 cursor != extensions->end(); |
77 ++cursor) { | 79 ++cursor) { |
(...skipping 11 matching lines...) Expand all Loading... | |
89 | 91 |
90 bool HasBackgroundAppPermission( | 92 bool HasBackgroundAppPermission( |
91 const std::set<std::string>& api_permissions) { | 93 const std::set<std::string>& api_permissions) { |
92 return Extension::HasApiPermission( | 94 return Extension::HasApiPermission( |
93 api_permissions, Extension::kBackgroundPermission); | 95 api_permissions, Extension::kBackgroundPermission); |
94 } | 96 } |
95 } // namespace | 97 } // namespace |
96 | 98 |
97 void | 99 void |
98 BackgroundApplicationListModel::Observer::OnApplicationDataChanged( | 100 BackgroundApplicationListModel::Observer::OnApplicationDataChanged( |
99 const Extension* extension) { | 101 const Extension* extension, Profile* profile) { |
100 } | 102 } |
101 | 103 |
102 void | 104 void |
103 BackgroundApplicationListModel::Observer::OnApplicationListChanged() { | 105 BackgroundApplicationListModel::Observer::OnApplicationListChanged( |
106 Profile* profile) { | |
104 } | 107 } |
105 | 108 |
106 BackgroundApplicationListModel::Observer::~Observer() { | 109 BackgroundApplicationListModel::Observer::~Observer() { |
107 } | 110 } |
108 | 111 |
109 BackgroundApplicationListModel::Application::~Application() { | 112 BackgroundApplicationListModel::Application::~Application() { |
110 } | 113 } |
111 | 114 |
112 BackgroundApplicationListModel::Application::Application( | 115 BackgroundApplicationListModel::Application::Application( |
113 BackgroundApplicationListModel* model, | 116 BackgroundApplicationListModel* model, |
114 const Extension* extension) | 117 const Extension* extension, |
118 Profile* profile) | |
115 : extension_(extension), | 119 : extension_(extension), |
116 icon_(NULL), | 120 icon_(NULL), |
117 model_(model), | 121 model_(model), |
122 profile_(profile), | |
118 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { | 123 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)) { |
119 } | 124 } |
120 | 125 |
121 void BackgroundApplicationListModel::Application::OnImageLoaded( | 126 void BackgroundApplicationListModel::Application::OnImageLoaded( |
122 SkBitmap* image, | 127 SkBitmap* image, |
123 const ExtensionResource& resource, | 128 const ExtensionResource& resource, |
124 int index) { | 129 int index) { |
125 if (!image) | 130 if (!image) |
126 return; | 131 return; |
127 icon_.reset(new SkBitmap(*image)); | 132 icon_.reset(new SkBitmap(*image)); |
128 model_->OnApplicationDataChanged(extension_); | 133 model_->OnApplicationDataChanged(extension_, profile_); |
129 } | 134 } |
130 | 135 |
131 void BackgroundApplicationListModel::Application::RequestIcon( | 136 void BackgroundApplicationListModel::Application::RequestIcon( |
132 Extension::Icons size) { | 137 Extension::Icons size) { |
133 ExtensionResource resource = extension_->GetIconResource( | 138 ExtensionResource resource = extension_->GetIconResource( |
134 size, ExtensionIconSet::MATCH_BIGGER); | 139 size, ExtensionIconSet::MATCH_BIGGER); |
135 tracker_.LoadImage(extension_, resource, gfx::Size(size, size), | 140 tracker_.LoadImage(extension_, resource, gfx::Size(size, size), |
136 ImageLoadingTracker::CACHE); | 141 ImageLoadingTracker::CACHE); |
137 } | 142 } |
138 | 143 |
(...skipping 28 matching lines...) Expand all Loading... | |
167 DCHECK(IsBackgroundApp(*extension)); | 172 DCHECK(IsBackgroundApp(*extension)); |
168 Application* application = FindApplication(extension); | 173 Application* application = FindApplication(extension); |
169 if (!application) { | 174 if (!application) { |
170 // App position is used as a dynamic command and so must be less than any | 175 // App position is used as a dynamic command and so must be less than any |
171 // predefined command id. | 176 // predefined command id. |
172 if (applications_.size() >= IDC_MinimumLabelValue) { | 177 if (applications_.size() >= IDC_MinimumLabelValue) { |
173 LOG(ERROR) << "Background application limit of " << IDC_MinimumLabelValue | 178 LOG(ERROR) << "Background application limit of " << IDC_MinimumLabelValue |
174 << " exceeded. Ignoring."; | 179 << " exceeded. Ignoring."; |
175 return; | 180 return; |
176 } | 181 } |
177 application = new Application(this, extension); | 182 application = new Application(this, extension, profile_); |
178 applications_[extension->id()] = application; | 183 applications_[extension->id()] = application; |
179 application->RequestIcon(Extension::EXTENSION_ICON_BITTY); | 184 application->RequestIcon(Extension::EXTENSION_ICON_BITTY); |
180 } | 185 } |
181 } | 186 } |
182 | 187 |
183 void BackgroundApplicationListModel::DissociateApplicationData( | 188 void BackgroundApplicationListModel::DissociateApplicationData( |
184 const Extension* extension) { | 189 const Extension* extension) { |
185 ApplicationMap::iterator found = applications_.find(extension->id()); | 190 ApplicationMap::iterator found = applications_.find(extension->id()); |
186 if (found != applications_.end()) { | 191 if (found != applications_.end()) { |
187 delete found->second; | 192 delete found->second; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
232 NOTREACHED(); | 237 NOTREACHED(); |
233 return -1; | 238 return -1; |
234 } | 239 } |
235 | 240 |
236 // static | 241 // static |
237 bool BackgroundApplicationListModel::IsBackgroundApp( | 242 bool BackgroundApplicationListModel::IsBackgroundApp( |
238 const Extension& extension) { | 243 const Extension& extension) { |
239 return HasBackgroundAppPermission(extension.api_permissions()); | 244 return HasBackgroundAppPermission(extension.api_permissions()); |
240 } | 245 } |
241 | 246 |
247 bool BackgroundApplicationListModel::HasBackgroundApp() { | |
248 return (applications_.size() > 0); | |
249 } | |
250 | |
242 void BackgroundApplicationListModel::Observe( | 251 void BackgroundApplicationListModel::Observe( |
243 NotificationType type, | 252 NotificationType type, |
244 const NotificationSource& source, | 253 const NotificationSource& source, |
245 const NotificationDetails& details) { | 254 const NotificationDetails& details) { |
246 if (type == NotificationType::EXTENSIONS_READY) { | 255 if (type == NotificationType::EXTENSIONS_READY) { |
247 Update(); | 256 Update(); |
248 return; | 257 return; |
249 } | 258 } |
250 ExtensionService* service = profile_->GetExtensionService(); | 259 ExtensionService* service = profile_->GetExtensionService(); |
251 if (!service || !service->is_ready()) | 260 if (!service || !service->is_ready()) |
252 return; | 261 return; |
253 | 262 |
254 switch (type.value) { | 263 switch (type.value) { |
255 case NotificationType::EXTENSION_LOADED: | 264 case NotificationType::EXTENSION_LOADED: |
256 OnExtensionLoaded(Details<Extension>(details).ptr()); | 265 OnExtensionLoaded(Details<Extension>(details).ptr()); |
257 break; | 266 break; |
258 case NotificationType::EXTENSION_UNLOADED: | 267 case NotificationType::EXTENSION_UNLOADED: |
259 OnExtensionUnloaded(Details<UnloadedExtensionInfo>(details)->extension); | 268 OnExtensionUnloaded(Details<UnloadedExtensionInfo>(details)->extension); |
260 break; | 269 break; |
261 default: | 270 default: |
262 NOTREACHED() << "Received unexpected notification"; | 271 NOTREACHED() << "Received unexpected notification"; |
263 } | 272 } |
264 } | 273 } |
265 | 274 |
266 void BackgroundApplicationListModel::OnApplicationDataChanged( | 275 void BackgroundApplicationListModel::OnApplicationDataChanged( |
267 const Extension* extension) { | 276 const Extension* extension, Profile* profile) { |
268 FOR_EACH_OBSERVER(Observer, observers_, OnApplicationDataChanged(extension)); | 277 DCHECK(profile == profile_); |
Andrew T Wilson (Slow)
2011/05/20 16:52:46
Keep in mind that this is *not* an implementation
rpetterson
2011/05/23 03:23:19
Thanks for clearing up this confusion. I am renami
| |
278 FOR_EACH_OBSERVER(Observer, observers_, OnApplicationDataChanged(extension, | |
279 profile)); | |
269 } | 280 } |
270 | 281 |
271 void BackgroundApplicationListModel::OnExtensionLoaded(Extension* extension) { | 282 void BackgroundApplicationListModel::OnExtensionLoaded(Extension* extension) { |
272 // We only care about extensions that are background applications | 283 // We only care about extensions that are background applications |
273 if (!IsBackgroundApp(*extension)) | 284 if (!IsBackgroundApp(*extension)) |
274 return; | 285 return; |
275 AssociateApplicationData(extension); | 286 AssociateApplicationData(extension); |
276 Update(); | 287 Update(); |
277 } | 288 } |
278 | 289 |
(...skipping 24 matching lines...) Expand all Loading... | |
303 ExtensionList::const_iterator new_cursor = extensions.begin(); | 314 ExtensionList::const_iterator new_cursor = extensions.begin(); |
304 while (old_cursor != extensions_.end() && | 315 while (old_cursor != extensions_.end() && |
305 new_cursor != extensions.end() && | 316 new_cursor != extensions.end() && |
306 (*old_cursor)->name() == (*new_cursor)->name() && | 317 (*old_cursor)->name() == (*new_cursor)->name() && |
307 (*old_cursor)->id() == (*new_cursor)->id()) { | 318 (*old_cursor)->id() == (*new_cursor)->id()) { |
308 ++old_cursor; | 319 ++old_cursor; |
309 ++new_cursor; | 320 ++new_cursor; |
310 } | 321 } |
311 if (old_cursor != extensions_.end() || new_cursor != extensions.end()) { | 322 if (old_cursor != extensions_.end() || new_cursor != extensions.end()) { |
312 extensions_ = extensions; | 323 extensions_ = extensions; |
313 FOR_EACH_OBSERVER(Observer, observers_, OnApplicationListChanged()); | 324 FOR_EACH_OBSERVER(Observer, observers_, OnApplicationListChanged(profile_)); |
314 } | 325 } |
315 } | 326 } |
OLD | NEW |