Chromium Code Reviews| 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/tab_helper.h" | 10 #include "chrome/browser/extensions/tab_helper.h" |
| 11 #include "chrome/browser/extensions/extension_tab_util.h" | 11 #include "chrome/browser/extensions/extension_tab_util.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/ui/browser.h" | 14 #include "chrome/browser/ui/browser.h" |
| 15 #include "chrome/browser/ui/browser_tabstrip.h" | 15 #include "chrome/browser/ui/browser_tabstrip.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 17 #include "chrome/common/chrome_notification_types.h" | 17 #include "chrome/common/chrome_notification_types.h" |
| 18 #include "chrome/common/extensions/extension.h" | 18 #include "chrome/common/extensions/extension.h" |
| 19 #include "chrome/common/extensions/extension_switch_utils.h" | |
| 19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
| 20 #include "content/public/browser/notification_details.h" | 21 #include "content/public/browser/notification_details.h" |
| 21 #include "content/public/browser/notification_source.h" | 22 #include "content/public/browser/notification_source.h" |
| 22 #include "content/public/browser/web_contents.h" | 23 #include "content/public/browser/web_contents.h" |
| 23 | 24 |
| 24 using extensions::Extension; | 25 using extensions::Extension; |
| 25 using extensions::ExtensionList; | 26 using extensions::ExtensionList; |
| 26 | 27 |
| 28 namespace { | |
| 29 | |
| 30 // Returns index of |extension| in |extension_list| | |
| 31 // or -1 if not found. | |
|
msw
2012/07/25 23:02:03
nit: comment fits on one line.
yefimt
2012/07/31 00:10:11
Done.
| |
| 32 int FindInExtensionList(const Extension* extension, | |
|
msw
2012/07/25 23:02:03
Why return an index if the two callers just conver
Peter Kasting
2012/07/26 20:37:17
Yeah, this is weird.
I think I agree with Mike th
yefimt
2012/07/31 00:10:11
Done.
yefimt
2012/07/31 00:10:11
Done.
| |
| 33 const extensions::ExtensionList& extension_list) { | |
| 34 for (size_t i = 0; i < extension_list.size(); i++) { | |
| 35 if (extension_list[i].get() == extension) | |
| 36 return i; | |
| 37 } | |
| 38 return -1; | |
| 39 } | |
| 40 | |
| 41 } // namespace | |
| 42 | |
| 27 ExtensionToolbarModel::ExtensionToolbarModel(ExtensionService* service) | 43 ExtensionToolbarModel::ExtensionToolbarModel(ExtensionService* service) |
| 28 : service_(service), | 44 : service_(service), |
| 29 prefs_(service->profile()->GetPrefs()), | 45 prefs_(service->profile()->GetPrefs()), |
| 30 extensions_initialized_(false) { | 46 extensions_initialized_(false) { |
| 31 DCHECK(service_); | 47 DCHECK(service_); |
| 32 | 48 |
| 33 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, | 49 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_LOADED, |
| 34 content::Source<Profile>(service_->profile())); | 50 content::Source<Profile>(service_->profile())); |
| 35 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | 51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, |
| 36 content::Source<Profile>(service_->profile())); | 52 content::Source<Profile>(service_->profile())); |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 54 observers_.RemoveObserver(observer); | 70 observers_.RemoveObserver(observer); |
| 55 } | 71 } |
| 56 | 72 |
| 57 void ExtensionToolbarModel::MoveBrowserAction(const Extension* extension, | 73 void ExtensionToolbarModel::MoveBrowserAction(const Extension* extension, |
| 58 int index) { | 74 int index) { |
| 59 ExtensionList::iterator pos = std::find(begin(), end(), extension); | 75 ExtensionList::iterator pos = std::find(begin(), end(), extension); |
| 60 if (pos == end()) { | 76 if (pos == end()) { |
| 61 NOTREACHED(); | 77 NOTREACHED(); |
| 62 return; | 78 return; |
| 63 } | 79 } |
| 64 toolitems_.erase(pos); | 80 toolbar_items_.erase(pos); |
| 65 | 81 |
| 66 int i = 0; | 82 int i = 0; |
| 67 bool inserted = false; | 83 bool inserted = false; |
| 68 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter, ++i) { | 84 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter, ++i) { |
| 69 if (i == index) { | 85 if (i == index) { |
| 70 toolitems_.insert(iter, make_scoped_refptr(extension)); | 86 toolbar_items_.insert(iter, make_scoped_refptr(extension)); |
| 71 inserted = true; | 87 inserted = true; |
| 72 break; | 88 break; |
| 73 } | 89 } |
| 74 } | 90 } |
| 75 | 91 |
| 76 if (!inserted) { | 92 if (!inserted) { |
| 77 DCHECK_EQ(index, static_cast<int>(toolitems_.size())); | 93 DCHECK_EQ(index, static_cast<int>(toolbar_items_.size())); |
| 78 index = toolitems_.size(); | 94 index = toolbar_items_.size(); |
| 79 | 95 |
| 80 toolitems_.push_back(make_scoped_refptr(extension)); | 96 toolbar_items_.push_back(make_scoped_refptr(extension)); |
| 81 } | 97 } |
| 82 | 98 |
| 83 FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index)); | 99 FOR_EACH_OBSERVER(Observer, observers_, BrowserActionMoved(extension, index)); |
| 84 | 100 |
| 85 UpdatePrefs(); | 101 UpdatePrefs(); |
| 86 } | 102 } |
| 87 | 103 |
| 88 ExtensionToolbarModel::Action ExtensionToolbarModel::ExecuteBrowserAction( | 104 ExtensionToolbarModel::Action ExtensionToolbarModel::ExecuteBrowserAction( |
| 89 const Extension* extension, | 105 const Extension* extension, |
| 90 Browser* browser, | 106 Browser* browser, |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 120 void ExtensionToolbarModel::SetVisibleIconCount(int count) { | 136 void ExtensionToolbarModel::SetVisibleIconCount(int count) { |
| 121 visible_icon_count_ = count == static_cast<int>(size()) ? -1 : count; | 137 visible_icon_count_ = count == static_cast<int>(size()) ? -1 : count; |
| 122 prefs_->SetInteger(prefs::kExtensionToolbarSize, visible_icon_count_); | 138 prefs_->SetInteger(prefs::kExtensionToolbarSize, visible_icon_count_); |
| 123 } | 139 } |
| 124 | 140 |
| 125 void ExtensionToolbarModel::Observe( | 141 void ExtensionToolbarModel::Observe( |
| 126 int type, | 142 int type, |
| 127 const content::NotificationSource& source, | 143 const content::NotificationSource& source, |
| 128 const content::NotificationDetails& details) { | 144 const content::NotificationDetails& details) { |
| 129 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) { | 145 if (type == chrome::NOTIFICATION_EXTENSIONS_READY) { |
| 130 InitializeExtensionList(); | 146 InitializeExtensionLists(); |
| 131 return; | 147 return; |
| 132 } | 148 } |
| 133 | 149 |
| 134 if (!service_->is_ready()) | 150 if (!service_->is_ready()) |
| 135 return; | 151 return; |
| 136 | 152 |
| 137 const Extension* extension = NULL; | 153 const Extension* extension = NULL; |
| 138 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | 154 if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
| 139 extension = content::Details<extensions::UnloadedExtensionInfo>( | 155 extension = content::Details<extensions::UnloadedExtensionInfo>( |
| 140 details)->extension; | 156 details)->extension; |
| 141 } else { | 157 } else { |
| 142 extension = content::Details<const Extension>(details).ptr(); | 158 extension = content::Details<const Extension>(details).ptr(); |
| 143 } | 159 } |
| 160 bool found_in_toolbar = FindInExtensionList(extension, toolbar_items_) >= 0; | |
| 161 bool found_in_action_box_menu = | |
| 162 FindInExtensionList(extension, action_box_menu_items_) >= 0; | |
| 144 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { | 163 if (type == chrome::NOTIFICATION_EXTENSION_LOADED) { |
| 145 // We don't want to add the same extension twice. It may have already been | 164 // We don't want to add the same extension twice. It may have already been |
| 146 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user | 165 // added by EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED below, if the user |
| 147 // hides the browser action and then disables and enables the extension. | 166 // hides the browser action and then disables and enables the extension. |
| 148 for (size_t i = 0; i < toolitems_.size(); i++) { | 167 if (found_in_toolbar || found_in_action_box_menu) |
| 149 if (toolitems_[i].get() == extension) | 168 return; |
| 150 return; // Already exists. | 169 if (extensions::switch_utils::IsActionBoxEnabled()) |
| 151 } | 170 AddExtension(extension, &action_box_menu_items_); |
| 152 if (service_->extension_prefs()->GetBrowserActionVisibility(extension)) | 171 else if (service_->extension_prefs()->GetBrowserActionVisibility(extension)) |
| 153 AddExtension(extension); | 172 AddExtension(extension, &toolbar_items_); |
| 154 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | 173 } else if (type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { |
| 155 RemoveExtension(extension); | 174 if (found_in_toolbar) |
| 175 RemoveExtension(extension, &toolbar_items_); | |
| 176 if (found_in_action_box_menu) | |
| 177 RemoveExtension(extension, &action_box_menu_items_); | |
| 156 } else if ( | 178 } else if ( |
| 157 type == | 179 type == |
|
Peter Kasting
2012/07/26 20:37:17
Nit: move this up onto prior line
yefimt
2012/07/31 00:10:11
Done.
| |
| 158 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED) { | 180 chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED) { |
|
Peter Kasting
2012/07/26 20:37:17
Nit: Indent 4, not 6
yefimt
2012/07/31 00:10:11
Done.
| |
| 159 if (service_->extension_prefs()->GetBrowserActionVisibility(extension)) | 181 if (extensions::switch_utils::IsActionBoxEnabled()) { |
| 160 AddExtension(extension); | 182 // TODO(yefim): Implement this when implementing drag & drop |
| 161 else | 183 // for action box menu. |
| 162 RemoveExtension(extension); | 184 } else { |
| 185 if (service_->extension_prefs()->GetBrowserActionVisibility(extension)) | |
|
Peter Kasting
2012/07/26 20:37:17
Nit: Combine with "} else {" above so you have "if
yefimt
2012/07/31 00:10:11
I think current formatting is better because if st
| |
| 186 AddExtension(extension, &toolbar_items_); | |
| 187 else | |
| 188 RemoveExtension(extension, &toolbar_items_); | |
| 189 } | |
| 163 } else { | 190 } else { |
| 164 NOTREACHED() << "Received unexpected notification"; | 191 NOTREACHED() << "Received unexpected notification"; |
| 165 } | 192 } |
| 166 } | 193 } |
| 167 | 194 |
| 168 void ExtensionToolbarModel::AddExtension(const Extension* extension) { | 195 void ExtensionToolbarModel::AddExtension(const Extension* extension, |
| 196 ExtensionList* list) { | |
| 169 // We only care about extensions with browser actions. | 197 // We only care about extensions with browser actions. |
| 170 if (!extension->browser_action()) | 198 if (!extension->browser_action()) |
| 171 return; | 199 return; |
| 172 | 200 |
| 173 if (extension->id() == last_extension_removed_ && | 201 if (extension->id() == last_extension_removed_ && |
| 174 last_extension_removed_index_ < toolitems_.size()) { | 202 last_extension_removed_index_ < list->size()) { |
| 175 toolitems_.insert(begin() + last_extension_removed_index_, | 203 list->insert(list->begin() + last_extension_removed_index_, |
| 176 make_scoped_refptr(extension)); | 204 make_scoped_refptr(extension)); |
| 177 FOR_EACH_OBSERVER(Observer, observers_, | 205 FOR_EACH_OBSERVER(Observer, observers_, |
| 178 BrowserActionAdded(extension, last_extension_removed_index_)); | 206 BrowserActionAdded(extension, last_extension_removed_index_)); |
| 179 } else { | 207 } else { |
| 180 toolitems_.push_back(make_scoped_refptr(extension)); | 208 list->push_back(make_scoped_refptr(extension)); |
| 181 FOR_EACH_OBSERVER(Observer, observers_, | 209 FOR_EACH_OBSERVER(Observer, observers_, |
| 182 BrowserActionAdded(extension, toolitems_.size() - 1)); | 210 BrowserActionAdded(extension, list->size() - 1)); |
| 183 } | 211 } |
| 184 | 212 |
| 185 last_extension_removed_ = ""; | 213 last_extension_removed_ = ""; |
| 186 last_extension_removed_index_ = -1; | 214 last_extension_removed_index_ = -1; |
| 187 | 215 |
| 188 UpdatePrefs(); | 216 UpdatePrefs(); |
| 189 } | 217 } |
| 190 | 218 |
| 191 void ExtensionToolbarModel::RemoveExtension(const Extension* extension) { | 219 void ExtensionToolbarModel::RemoveExtension(const Extension* extension, |
| 192 ExtensionList::iterator pos = std::find(begin(), end(), extension); | 220 ExtensionList* list) { |
| 221 ExtensionList::iterator pos = | |
| 222 std::find(list->begin(), list->end(), extension); | |
| 193 if (pos == end()) { | 223 if (pos == end()) { |
|
Peter Kasting
2012/07/26 20:37:17
Nit: No {}
yefimt
2012/07/31 00:10:11
Done.
| |
| 194 return; | 224 return; |
| 195 } | 225 } |
| 196 | 226 |
| 197 last_extension_removed_ = extension->id(); | 227 last_extension_removed_ = extension->id(); |
| 198 last_extension_removed_index_ = pos - begin(); | 228 last_extension_removed_index_ = pos - list->begin(); |
| 199 | 229 |
| 200 toolitems_.erase(pos); | 230 list->erase(pos); |
| 201 FOR_EACH_OBSERVER(Observer, observers_, | 231 FOR_EACH_OBSERVER(Observer, observers_, |
| 202 BrowserActionRemoved(extension)); | 232 BrowserActionRemoved(extension)); |
| 203 | 233 |
| 204 UpdatePrefs(); | 234 UpdatePrefs(); |
| 205 } | 235 } |
| 206 | 236 |
| 207 // Combine the currently enabled extensions that have browser actions (which | 237 // Combine the currently enabled extensions that have browser actions (which |
| 208 // we get from the ExtensionService) with the ordering we get from the | 238 // we get from the ExtensionService) with the ordering we get from the |
| 209 // pref service. For robustness we use a somewhat inefficient process: | 239 // pref service. For robustness we use a somewhat inefficient process: |
| 210 // 1. Create a vector of extensions sorted by their pref values. This vector may | 240 // 1. Create a vector of extensions sorted by their pref values. This vector may |
| 211 // have holes. | 241 // have holes. |
| 212 // 2. Create a vector of extensions that did not have a pref value. | 242 // 2. Create a vector of extensions that did not have a pref value. |
| 213 // 3. Remove holes from the sorted vector and append the unsorted vector. | 243 // 3. Remove holes from the sorted vector and append the unsorted vector. |
| 214 void ExtensionToolbarModel::InitializeExtensionList() { | 244 void ExtensionToolbarModel::InitializeExtensionLists() { |
| 215 DCHECK(service_->is_ready()); | 245 DCHECK(service_->is_ready()); |
| 216 | 246 |
| 217 std::vector<std::string> pref_order = service_->extension_prefs()-> | 247 if (extensions::switch_utils::IsActionBoxEnabled()) |
| 218 GetToolbarOrder(); | 248 PopulateForActionBoxMode(); |
| 249 else | |
| 250 PopulateForNonActionBoxMode(); | |
| 251 | |
| 252 UpdatePrefs(); | |
| 253 | |
| 254 extensions_initialized_ = true; | |
| 255 FOR_EACH_OBSERVER(Observer, observers_, ModelLoaded()); | |
| 256 } | |
| 257 | |
| 258 void ExtensionToolbarModel::PopulateForActionBoxMode() { | |
| 259 const extensions::ExtensionPrefs::ExtensionIdSet toolbar_order = | |
| 260 service_->extension_prefs()->GetToolbarOrder(); | |
| 261 extensions::ExtensionPrefs::ExtensionIdSet action_box_order = | |
| 262 service_->extension_prefs()->GetActionBoxOrder(); | |
| 263 | |
| 264 // Add all browser actions not already in the toolbar or action box | |
| 265 // to the action box. | |
| 266 for (ExtensionSet::const_iterator iter = service_->extensions()->begin(); | |
| 267 iter != service_->extensions()->end(); ++iter) { | |
| 268 const Extension* extension = *iter; | |
| 269 if (!extension->browser_action()) | |
|
Peter Kasting
2012/07/26 20:37:17
Nit: Much shorter:
if (extension->browser_actio
yefimt
2012/07/31 00:10:11
Wow, it is shorter, but would it be shorter to col
Peter Kasting
2012/07/31 02:30:42
Can you at least get rid of your temporaries? The
yefimt
2012/07/31 17:19:35
Done.
| |
| 270 continue; | |
| 271 | |
| 272 extensions::ExtensionPrefs::ExtensionIdSet::const_iterator toolbar_pos = | |
| 273 std::find(toolbar_order.begin(), toolbar_order.end(), extension->id()); | |
| 274 if (toolbar_pos != toolbar_order.end()) | |
| 275 continue; | |
| 276 | |
| 277 extensions::ExtensionPrefs::ExtensionIdSet::const_iterator action_box_pos = | |
| 278 std::find(action_box_order.begin(), action_box_order.end(), | |
| 279 extension->id()); | |
| 280 if (action_box_pos != action_box_order.end()) | |
| 281 continue; | |
| 282 | |
| 283 action_box_order.push_back(extension->id()); | |
| 284 } | |
| 285 | |
| 286 FillExtensionList(action_box_order, &action_box_menu_items_); | |
| 287 FillExtensionList(toolbar_order, &toolbar_items_); | |
| 288 } | |
| 289 | |
| 290 void ExtensionToolbarModel::PopulateForNonActionBoxMode() { | |
| 291 const extensions::ExtensionPrefs::ExtensionIdSet pref_order = | |
| 292 service_->extension_prefs()->GetToolbarOrder(); | |
| 219 // Items that have a pref for their position. | 293 // Items that have a pref for their position. |
| 220 ExtensionList sorted; | 294 ExtensionList sorted; |
| 221 sorted.resize(pref_order.size(), NULL); | 295 sorted.resize(pref_order.size(), NULL); |
| 222 // The items that don't have a pref for their position. | 296 // The items that don't have a pref for their position. |
| 223 ExtensionList unsorted; | 297 ExtensionList unsorted; |
| 224 | 298 |
| 225 // Create the lists. | 299 // Create the lists. |
| 226 for (ExtensionSet::const_iterator it = service_->extensions()->begin(); | 300 for (ExtensionSet::const_iterator it = service_->extensions()->begin(); |
| 227 it != service_->extensions()->end(); ++it) { | 301 it != service_->extensions()->end(); ++it) { |
| 228 const Extension* extension = *it; | 302 const Extension* extension = *it; |
| 229 if (!extension->browser_action()) | 303 if (!extension->browser_action()) |
| 230 continue; | 304 continue; |
| 231 if (!service_->extension_prefs()->GetBrowserActionVisibility(extension)) | 305 if (!service_->extension_prefs()->GetBrowserActionVisibility(extension)) |
| 232 continue; | 306 continue; |
| 233 | 307 |
| 234 std::vector<std::string>::iterator pos = | 308 extensions::ExtensionPrefs::ExtensionIdSet::const_iterator pos = |
| 235 std::find(pref_order.begin(), pref_order.end(), extension->id()); | 309 std::find(pref_order.begin(), pref_order.end(), extension->id()); |
| 236 if (pos != pref_order.end()) { | 310 if (pos != pref_order.end()) |
| 237 int index = std::distance(pref_order.begin(), pos); | 311 sorted[pos - pref_order.begin()] = extension; |
| 238 sorted[index] = extension; | 312 else |
| 239 } else { | |
| 240 unsorted.push_back(make_scoped_refptr(extension)); | 313 unsorted.push_back(make_scoped_refptr(extension)); |
| 241 } | |
| 242 } | 314 } |
| 243 | 315 |
| 244 // Merge the lists. | 316 // Merge the lists. |
| 245 toolitems_.reserve(sorted.size() + unsorted.size()); | 317 toolbar_items_.clear(); |
| 246 for (ExtensionList::iterator iter = sorted.begin(); | 318 toolbar_items_.reserve(sorted.size() + unsorted.size()); |
| 319 for (ExtensionList::const_iterator iter = sorted.begin(); | |
| 247 iter != sorted.end(); ++iter) { | 320 iter != sorted.end(); ++iter) { |
| 321 // This can happen due to sync. The order is synced across machines, but | |
| 322 // sometimes a machine won't necessarily have the extensions loaded | |
| 323 // that are specified here. | |
|
Peter Kasting
2012/07/26 20:37:17
Nit: Even clearer:
"It's possible for the extensi
yefimt
2012/07/31 00:10:11
Done.
| |
| 248 if (*iter != NULL) | 324 if (*iter != NULL) |
| 249 toolitems_.push_back(*iter); | 325 toolbar_items_.push_back(*iter); |
| 250 } | 326 } |
| 251 toolitems_.insert(toolitems_.end(), unsorted.begin(), unsorted.end()); | 327 toolbar_items_.insert(toolbar_items_.end(), unsorted.begin(), |
| 328 unsorted.end()); | |
| 252 | 329 |
| 253 // Inform observers. | 330 // Inform observers. |
| 254 for (size_t i = 0; i < toolitems_.size(); i++) { | 331 for (size_t i = 0; i < toolbar_items_.size(); i++) { |
| 255 FOR_EACH_OBSERVER(Observer, observers_, | 332 FOR_EACH_OBSERVER(Observer, observers_, |
| 256 BrowserActionAdded(toolitems_[i], i)); | 333 BrowserActionAdded(toolbar_items_[i], i)); |
| 257 } | 334 } |
| 335 } | |
| 258 | 336 |
| 259 UpdatePrefs(); | 337 void ExtensionToolbarModel::FillExtensionList( |
| 260 | 338 const extensions::ExtensionPrefs::ExtensionIdSet& order, |
| 261 extensions_initialized_ = true; | 339 ExtensionList* list) { |
| 262 FOR_EACH_OBSERVER(Observer, observers_, ModelLoaded()); | 340 list->clear(); |
| 341 list->reserve(order.size()); | |
| 342 for (size_t i = 0; i < order.size(); ++i) { | |
| 343 const extensions::Extension* extension = | |
| 344 service_->GetExtensionById(order[i], false); | |
| 345 if (extension) | |
| 346 AddExtension(extension, list); | |
| 347 } | |
| 263 } | 348 } |
| 264 | 349 |
| 265 void ExtensionToolbarModel::UpdatePrefs() { | 350 void ExtensionToolbarModel::UpdatePrefs() { |
| 266 if (!service_->extension_prefs()) | 351 if (!service_->extension_prefs()) |
| 267 return; | 352 return; |
| 268 | 353 |
| 269 std::vector<std::string> ids; | 354 extensions::ExtensionPrefs::ExtensionIdSet toolbar_ids; |
| 270 ids.reserve(toolitems_.size()); | 355 toolbar_ids.reserve(toolbar_items_.size()); |
| 271 for (ExtensionList::iterator iter = begin(); iter != end(); ++iter) | 356 for (size_t i = 0; i < toolbar_items_.size(); ++i) |
| 272 ids.push_back((*iter)->id()); | 357 toolbar_ids.push_back(toolbar_items_[i]->id()); |
| 273 service_->extension_prefs()->SetToolbarOrder(ids); | 358 service_->extension_prefs()->SetToolbarOrder(toolbar_ids); |
| 274 } | |
| 275 | 359 |
| 276 const Extension* ExtensionToolbarModel::GetExtensionByIndex(int index) const { | 360 extensions::ExtensionPrefs::ExtensionIdSet action_box_ids; |
| 277 return toolitems_[index]; | 361 action_box_ids.reserve(action_box_menu_items_.size()); |
| 362 for (size_t i = 0; i < action_box_menu_items_.size(); ++i) | |
| 363 action_box_ids.push_back(action_box_menu_items_[i]->id()); | |
| 364 service_->extension_prefs()->SetActionBoxOrder(action_box_ids); | |
| 278 } | 365 } |
| 279 | 366 |
| 280 int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) { | 367 int ExtensionToolbarModel::IncognitoIndexToOriginal(int incognito_index) { |
| 281 int original_index = 0, i = 0; | 368 int original_index = 0, i = 0; |
| 282 for (ExtensionList::iterator iter = begin(); iter != end(); | 369 for (ExtensionList::iterator iter = begin(); iter != end(); |
| 283 ++iter, ++original_index) { | 370 ++iter, ++original_index) { |
| 284 if (service_->IsIncognitoEnabled((*iter)->id())) { | 371 if (service_->IsIncognitoEnabled((*iter)->id())) { |
| 285 if (incognito_index == i) | 372 if (incognito_index == i) |
| 286 break; | 373 break; |
| 287 ++i; | 374 ++i; |
| 288 } | 375 } |
| 289 } | 376 } |
| 290 return original_index; | 377 return original_index; |
| 291 } | 378 } |
| 292 | 379 |
| 293 int ExtensionToolbarModel::OriginalIndexToIncognito(int original_index) { | 380 int ExtensionToolbarModel::OriginalIndexToIncognito(int original_index) { |
| 294 int incognito_index = 0, i = 0; | 381 int incognito_index = 0, i = 0; |
| 295 for (ExtensionList::iterator iter = begin(); iter != end(); | 382 for (ExtensionList::iterator iter = begin(); iter != end(); |
| 296 ++iter, ++i) { | 383 ++iter, ++i) { |
| 297 if (original_index == i) | 384 if (original_index == i) |
| 298 break; | 385 break; |
| 299 if (service_->IsIncognitoEnabled((*iter)->id())) | 386 if (service_->IsIncognitoEnabled((*iter)->id())) |
| 300 ++incognito_index; | 387 ++incognito_index; |
| 301 } | 388 } |
| 302 return incognito_index; | 389 return incognito_index; |
| 303 } | 390 } |
| OLD | NEW |