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/extensions/extension_toolbar_model.h" | 5 #include "chrome/browser/extensions/extension_toolbar_model.h" |
6 | 6 |
7 #include "chrome/browser/extensions/extension_browser_event_router.h" | 7 #include "chrome/browser/extensions/extension_browser_event_router.h" |
8 #include "chrome/browser/extensions/extension_prefs.h" | 8 #include "chrome/browser/extensions/extension_prefs.h" |
9 #include "chrome/browser/extensions/extension_service.h" | 9 #include "chrome/browser/extensions/extension_service.h" |
10 #include "chrome/browser/extensions/extension_tab_util.h" | 10 #include "chrome/browser/extensions/extension_tab_util.h" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 void ExtensionToolbarModel::SetVisibleIconCount(int count) { | 110 void ExtensionToolbarModel::SetVisibleIconCount(int count) { |
111 visible_icon_count_ = count == static_cast<int>(size()) ? -1 : count; | 111 visible_icon_count_ = count == static_cast<int>(size()) ? -1 : count; |
112 prefs_->SetInteger(prefs::kExtensionToolbarSize, visible_icon_count_); | 112 prefs_->SetInteger(prefs::kExtensionToolbarSize, visible_icon_count_); |
113 } | 113 } |
114 | 114 |
115 void ExtensionToolbarModel::Observe( | 115 void ExtensionToolbarModel::Observe( |
116 int type, | 116 int type, |
117 const content::NotificationSource& source, | 117 const content::NotificationSource& source, |
118 const content::NotificationDetails& details) { | 118 const content::NotificationDetails& details) { |
119 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) { | 119 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) { |
120 InitializeExtensionList(); | 120 InitializeExtensionLists(); |
121 return; | 121 return; |
122 } | 122 } |
123 | 123 |
124 if (!service_->is_ready()) | 124 if (!service_->is_ready()) |
125 return; | 125 return; |
126 | 126 |
127 const Extension* extension = NULL; | 127 const Extension* extension = NULL; |
128 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | 128 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
129 extension = content::Details<extensions::UnloadedExtensionInfo>( | 129 extension = content::Details<extensions::UnloadedExtensionInfo>( |
130 details)->extension; | 130 details)->extension; |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
194 UpdatePrefs(); | 194 UpdatePrefs(); |
195 } | 195 } |
196 | 196 |
197 // Combine the currently enabled extensions that have browser actions (which | 197 // Combine the currently enabled extensions that have browser actions (which |
198 // we get from the ExtensionService) with the ordering we get from the | 198 // we get from the ExtensionService) with the ordering we get from the |
199 // pref service. For robustness we use a somewhat inefficient process: | 199 // pref service. For robustness we use a somewhat inefficient process: |
200 // 1. Create a vector of extensions sorted by their pref values. This vector may | 200 // 1. Create a vector of extensions sorted by their pref values. This vector may |
201 // have holes. | 201 // have holes. |
202 // 2. Create a vector of extensions that did not have a pref value. | 202 // 2. Create a vector of extensions that did not have a pref value. |
203 // 3. Remove holes from the sorted vector and append the unsorted vector. | 203 // 3. Remove holes from the sorted vector and append the unsorted vector. |
204 void ExtensionToolbarModel::InitializeExtensionList() { | 204 void ExtensionToolbarModel::InitializeExtensionLists() { |
Aaron Boodman
2012/06/12 05:53:44
This is pretty hard to follow. How about something
yefimt
2012/06/13 01:24:21
Done.
| |
205 DCHECK(service_->is_ready()); | 205 DCHECK(service_->is_ready()); |
206 | 206 |
207 std::vector<std::string> pref_order = service_->extension_prefs()-> | 207 std::vector<std::string> pref_order = service_->extension_prefs()-> |
208 GetToolbarOrder(); | 208 GetToolbarOrder(); |
209 InitializeExtensionList(pref_order, &toolitems_, true); | |
210 | |
211 pref_order = service_->extension_prefs()->GetActionboxOrder(); | |
212 InitializeExtensionList(pref_order, &action_box_menu_items_, false); | |
213 | |
214 UpdatePrefs(); | |
215 | |
216 extensions_initialized_ = true; | |
217 FOR_EACH_OBSERVER(Observer, observers_, ModelLoaded()); | |
218 } | |
219 | |
220 void ExtensionToolbarModel::InitializeExtensionList( | |
221 const std::vector<std::string>& pref_order, | |
222 extensions::ExtensionList* extension_list, | |
223 bool bar) { | |
209 // Items that have a pref for their position. | 224 // Items that have a pref for their position. |
210 ExtensionList sorted; | 225 ExtensionList sorted; |
211 sorted.resize(pref_order.size(), NULL); | 226 sorted.resize(pref_order.size(), NULL); |
212 // The items that don't have a pref for their position. | 227 // The items that don't have a pref for their position. |
213 ExtensionList unsorted; | 228 ExtensionList unsorted; |
214 | 229 |
215 // Create the lists. | 230 // Create the lists. |
216 for (ExtensionSet::const_iterator it = service_->extensions()->begin(); | 231 for (ExtensionSet::const_iterator it = service_->extensions()->begin(); |
217 it != service_->extensions()->end(); ++it) { | 232 it != service_->extensions()->end(); ++it) { |
218 const Extension* extension = *it; | 233 const Extension* extension = *it; |
219 if (!extension->browser_action()) | 234 if (!extension->browser_action()) |
220 continue; | 235 continue; |
221 if (!service_->extension_prefs()->GetBrowserActionVisibility(extension)) | 236 bool action_visibility = service_->extension_prefs()-> |
222 continue; | 237 GetBrowserActionVisibility(extension); |
238 if (bar ^ action_visibility) | |
239 continue; | |
223 | 240 |
224 std::vector<std::string>::iterator pos = | 241 std::vector<std::string>::const_iterator pos = |
225 std::find(pref_order.begin(), pref_order.end(), extension->id()); | 242 std::find(pref_order.begin(), pref_order.end(), extension->id()); |
226 if (pos != pref_order.end()) { | 243 if (pos != pref_order.end()) { |
227 int index = std::distance(pref_order.begin(), pos); | 244 int index = std::distance(pref_order.begin(), pos); |
228 sorted[index] = extension; | 245 sorted[index] = extension; |
229 } else { | 246 } else { |
230 unsorted.push_back(make_scoped_refptr(extension)); | 247 unsorted.push_back(make_scoped_refptr(extension)); |
231 } | 248 } |
232 } | 249 } |
233 | 250 |
234 // Merge the lists. | 251 // Merge the lists. |
235 toolitems_.reserve(sorted.size() + unsorted.size()); | 252 extension_list->reserve(sorted.size() + unsorted.size()); |
236 for (ExtensionList::iterator iter = sorted.begin(); | 253 for (ExtensionList::iterator iter = sorted.begin(); |
237 iter != sorted.end(); ++iter) { | 254 iter != sorted.end(); ++iter) { |
238 if (*iter != NULL) | 255 if (*iter != NULL) |
239 toolitems_.push_back(*iter); | 256 extension_list->push_back(*iter); |
240 } | 257 } |
241 toolitems_.insert(toolitems_.end(), unsorted.begin(), unsorted.end()); | 258 extension_list->insert(extension_list->end(), unsorted.begin(), |
259 unsorted.end()); | |
242 | 260 |
243 // Inform observers. | 261 // Inform observers. |
244 for (size_t i = 0; i < toolitems_.size(); i++) { | 262 for (size_t i = 0; i < extension_list->size(); i++) { |
245 FOR_EACH_OBSERVER(Observer, observers_, | 263 FOR_EACH_OBSERVER(Observer, observers_, |
246 BrowserActionAdded(toolitems_[i], i)); | 264 BrowserActionAdded(extension_list->at(i), i)); |
247 } | 265 } |
248 | |
249 UpdatePrefs(); | |
250 | |
251 extensions_initialized_ = true; | |
252 FOR_EACH_OBSERVER(Observer, observers_, ModelLoaded()); | |
253 } | 266 } |
254 | 267 |
255 void ExtensionToolbarModel::UpdatePrefs() { | 268 void ExtensionToolbarModel::UpdatePrefs() { |
256 if (!service_->extension_prefs()) | 269 if (!service_->extension_prefs()) |
257 return; | 270 return; |
258 | 271 |
259 std::vector<std::string> ids; | 272 std::vector<std::string> ids; |
260 ids.reserve(toolitems_.size()); | 273 ids.reserve(toolitems_.size()); |
261 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter) | 274 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter) |
262 ids.push_back((*iter)->id()); | 275 ids.push_back((*iter)->id()); |
263 service_->extension_prefs()->SetToolbarOrder(ids); | 276 service_->extension_prefs()->SetToolbarOrder(ids); |
277 | |
278 ids.clear(); | |
279 ids.reserve(action_box_menu_items_.size()); | |
280 for (size_t i = 0; i < action_box_menu_items_.size(); ++i) | |
281 ids.push_back(action_box_menu_items_[i]->id()); | |
282 service_->extension_prefs()->SetActionboxOrder(ids); | |
264 } | 283 } |
265 | 284 |
266 const Extension* ExtensionToolbarModel::GetExtensionByIndex(int index) const { | 285 const Extension* ExtensionToolbarModel::GetExtensionByIndex(int index) const { |
267 return toolitems_[index]; | 286 return toolitems_[index]; |
268 } | 287 } |
269 | 288 |
270 int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) { | 289 int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) { |
271 int original_index = 0, i = 0; | 290 int original_index = 0, i = 0; |
272 for (ExtensionList::iterator iter = begin(); iter != end(); | 291 for (ExtensionList::iterator iter = begin(); iter != end(); |
273 ++iter, ++original_index) { | 292 ++iter, ++original_index) { |
(...skipping 10 matching lines...) Expand all Loading... | |
284 int incognito_index = 0, i = 0; | 303 int incognito_index = 0, i = 0; |
285 for (ExtensionList::iterator iter = begin(); iter != end(); | 304 for (ExtensionList::iterator iter = begin(); iter != end(); |
286 ++iter, ++i) { | 305 ++iter, ++i) { |
287 if (original_index == i) | 306 if (original_index == i) |
288 break; | 307 break; |
289 if (service_->IsIncognitoEnabled((*iter)->id())) | 308 if (service_->IsIncognitoEnabled((*iter)->id())) |
290 ++incognito_index; | 309 ++incognito_index; |
291 } | 310 } |
292 return incognito_index; | 311 return incognito_index; |
293 } | 312 } |
313 | |
314 const Extension* ExtensionToolbarModel::GetActionBoxExtensionByIndex( | |
315 int index) const { | |
316 return action_box_menu_items_[index]; | |
317 } | |
OLD | NEW |