| 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 <algorithm> | 7 #include <algorithm> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 last_known_positions_.push_back(id); | 115 last_known_positions_.push_back(id); |
| 116 } | 116 } |
| 117 | 117 |
| 118 FOR_EACH_OBSERVER( | 118 FOR_EACH_OBSERVER( |
| 119 Observer, observers_, ToolbarExtensionMoved(extension.get(), index)); | 119 Observer, observers_, ToolbarExtensionMoved(extension.get(), index)); |
| 120 MaybeUpdateVisibilityPref(extension.get(), index); | 120 MaybeUpdateVisibilityPref(extension.get(), index); |
| 121 UpdatePrefs(); | 121 UpdatePrefs(); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void ExtensionToolbarModel::SetVisibleIconCount(size_t count) { | 124 void ExtensionToolbarModel::SetVisibleIconCount(size_t count) { |
| 125 visible_icon_count_ = (count == toolbar_items_.size()) ? -1 : count; | 125 visible_icon_count_ = (count >= toolbar_items_.size()) ? -1 : count; |
| 126 | 126 |
| 127 // Only set the prefs if we're not in highlight mode and the profile is not | 127 // Only set the prefs if we're not in highlight mode and the profile is not |
| 128 // incognito. Highlight mode is designed to be a transitory state, and should | 128 // incognito. Highlight mode is designed to be a transitory state, and should |
| 129 // not persist across browser restarts (though it may be re-entered), and we | 129 // not persist across browser restarts (though it may be re-entered), and we |
| 130 // don't store anything in incognito. | 130 // don't store anything in incognito. |
| 131 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { | 131 if (!is_highlighting_ && !profile_->IsOffTheRecord()) { |
| 132 // Additionally, if we are using the new toolbar, any icons which are in the | 132 // Additionally, if we are using the new toolbar, any icons which are in the |
| 133 // overflow menu are considered "hidden". But it so happens that the times | 133 // overflow menu are considered "hidden". But it so happens that the times |
| 134 // we are likely to call SetVisibleIconCount() are also those when we are | 134 // we are likely to call SetVisibleIconCount() are also those when we are |
| 135 // in flux. So wait for things to cool down before setting the prefs. | 135 // in flux. So wait for things to cool down before setting the prefs. |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 // It's possible that we haven't added this extension yet, if its | 220 // It's possible that we haven't added this extension yet, if its |
| 221 // visibility was adjusted in the course of its initialization. | 221 // visibility was adjusted in the course of its initialization. |
| 222 if (std::find(toolbar_items_.begin(), toolbar_items_.end(), extension) == | 222 if (std::find(toolbar_items_.begin(), toolbar_items_.end(), extension) == |
| 223 toolbar_items_.end()) | 223 toolbar_items_.end()) |
| 224 return; | 224 return; |
| 225 | 225 |
| 226 int new_size = 0; | 226 int new_size = 0; |
| 227 int new_index = 0; | 227 int new_index = 0; |
| 228 if (visible) { | 228 if (visible) { |
| 229 // If this action used to be hidden, we can't possibly be showing all. | 229 // If this action used to be hidden, we can't possibly be showing all. |
| 230 DCHECK_NE(-1, visible_icon_count_); | 230 DCHECK_LT(visible_icon_count(), toolbar_items_.size()); |
| 231 // Grow the bar by one and move the extension to the end of the visibles. | 231 // Grow the bar by one and move the extension to the end of the visibles. |
| 232 new_size = visible_icon_count_ + 1; | 232 new_size = visible_icon_count() + 1; |
| 233 new_index = new_size - 1; | 233 new_index = new_size - 1; |
| 234 } else { | 234 } else { |
| 235 // If we're hiding one, we must be showing at least one. | 235 // If we're hiding one, we must be showing at least one. |
| 236 DCHECK_NE(visible_icon_count_, 0); | 236 DCHECK_GE(visible_icon_count(), 0u); |
| 237 // Shrink the bar by one and move the extension to the beginning of the | 237 // Shrink the bar by one and move the extension to the beginning of the |
| 238 // overflow menu. | 238 // overflow menu. |
| 239 new_size = visible_icon_count_ == -1 ? | 239 new_size = visible_icon_count() - 1; |
| 240 toolbar_items_.size() - 1 : visible_icon_count_ - 1; | |
| 241 new_index = new_size; | 240 new_index = new_size; |
| 242 } | 241 } |
| 243 SetVisibleIconCount(new_size); | 242 SetVisibleIconCount(new_size); |
| 244 MoveExtensionIcon(extension->id(), new_index); | 243 MoveExtensionIcon(extension->id(), new_index); |
| 245 } else { // Don't include all extensions. | 244 } else { // Don't include all extensions. |
| 246 if (visible) | 245 if (visible) |
| 247 AddExtension(extension); | 246 AddExtension(extension); |
| 248 else | 247 else |
| 249 RemoveExtension(extension); | 248 RemoveExtension(extension); |
| 250 } | 249 } |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 304 // the overflow menu by default. | 303 // the overflow menu by default. |
| 305 return action_manager->GetExtensionAction(*extension) != NULL; | 304 return action_manager->GetExtensionAction(*extension) != NULL; |
| 306 } | 305 } |
| 307 | 306 |
| 308 return action_manager->GetBrowserAction(*extension) && | 307 return action_manager->GetBrowserAction(*extension) && |
| 309 ExtensionActionAPI::GetBrowserActionVisibility( | 308 ExtensionActionAPI::GetBrowserActionVisibility( |
| 310 extension_prefs_, extension->id()); | 309 extension_prefs_, extension->id()); |
| 311 } | 310 } |
| 312 | 311 |
| 313 void ExtensionToolbarModel::AddExtension(const Extension* extension) { | 312 void ExtensionToolbarModel::AddExtension(const Extension* extension) { |
| 313 // We only use AddExtension() once the system is initialized. |
| 314 DCHECK(extensions_initialized_); |
| 314 if (!ShouldAddExtension(extension)) | 315 if (!ShouldAddExtension(extension)) |
| 315 return; | 316 return; |
| 316 | 317 |
| 317 // See if we have a last known good position for this extension. | 318 // See if we have a last known good position for this extension. |
| 318 bool is_new_extension = | 319 bool is_new_extension = |
| 319 std::find(last_known_positions_.begin(), | 320 std::find(last_known_positions_.begin(), |
| 320 last_known_positions_.end(), | 321 last_known_positions_.end(), |
| 321 extension->id()) == last_known_positions_.end(); | 322 extension->id()) == last_known_positions_.end(); |
| 322 size_t new_index = is_new_extension ? toolbar_items_.size() : | 323 size_t new_index = is_new_extension ? toolbar_items_.size() : |
| 323 FindNewPositionFromLastKnownGood(extension); | 324 FindNewPositionFromLastKnownGood(extension); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 338 Observer, observers_, ToolbarExtensionAdded(extension, new_index)); | 339 Observer, observers_, ToolbarExtensionAdded(extension, new_index)); |
| 339 | 340 |
| 340 // If this is an incognito profile, we also have to check to make sure the | 341 // If this is an incognito profile, we also have to check to make sure the |
| 341 // overflow matches the main bar's status. | 342 // overflow matches the main bar's status. |
| 342 if (profile_->IsOffTheRecord()) { | 343 if (profile_->IsOffTheRecord()) { |
| 343 ExtensionToolbarModel* main_model = | 344 ExtensionToolbarModel* main_model = |
| 344 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); | 345 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); |
| 345 // Find what the index will be in the main bar. Because Observer calls are | 346 // Find what the index will be in the main bar. Because Observer calls are |
| 346 // nondeterministic, we can't just assume the main bar will have the | 347 // nondeterministic, we can't just assume the main bar will have the |
| 347 // extension and look it up. | 348 // extension and look it up. |
| 348 int main_index = is_new_extension ? | 349 size_t main_index = is_new_extension ? |
| 349 main_model->toolbar_items_.size() : | 350 main_model->toolbar_items_.size() : |
| 350 main_model->FindNewPositionFromLastKnownGood(extension); | 351 main_model->FindNewPositionFromLastKnownGood(extension); |
| 351 bool visible = main_index < main_model->visible_icon_count_ || | 352 bool visible = main_index < main_model->visible_icon_count(); |
| 352 main_model->visible_icon_count_ == -1; | |
| 353 // We may need to adjust the visible count if the incognito bar isn't | 353 // We may need to adjust the visible count if the incognito bar isn't |
| 354 // showing all icons and this one is visible, or if it is showing all | 354 // showing all icons and this one is visible, or if it is showing all |
| 355 // icons and this is hidden. | 355 // icons and this is hidden. |
| 356 if (visible && visible_icon_count_ != -1) | 356 if (visible && !all_icons_visible()) |
| 357 SetVisibleIconCount(visible_icon_count_ + 1); | 357 SetVisibleIconCount(visible_icon_count() + 1); |
| 358 else if (!visible && visible_icon_count_ == -1) | 358 else if (!visible && all_icons_visible()) |
| 359 SetVisibleIconCount(toolbar_items_.size() - 1); | 359 SetVisibleIconCount(visible_icon_count() - 1); |
| 360 } | 360 } |
| 361 } | 361 } |
| 362 } | 362 } |
| 363 | 363 |
| 364 void ExtensionToolbarModel::RemoveExtension(const Extension* extension) { | 364 void ExtensionToolbarModel::RemoveExtension(const Extension* extension) { |
| 365 ExtensionList::iterator pos = | 365 ExtensionList::iterator pos = |
| 366 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension); | 366 std::find(toolbar_items_.begin(), toolbar_items_.end(), extension); |
| 367 if (pos == toolbar_items_.end()) | 367 if (pos == toolbar_items_.end()) |
| 368 return; | 368 return; |
| 369 | 369 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 405 } | 405 } |
| 406 | 406 |
| 407 // Combine the currently enabled extensions that have browser actions (which | 407 // Combine the currently enabled extensions that have browser actions (which |
| 408 // we get from the ExtensionRegistry) with the ordering we get from the | 408 // we get from the ExtensionRegistry) with the ordering we get from the |
| 409 // pref service. For robustness we use a somewhat inefficient process: | 409 // pref service. For robustness we use a somewhat inefficient process: |
| 410 // 1. Create a vector of extensions sorted by their pref values. This vector may | 410 // 1. Create a vector of extensions sorted by their pref values. This vector may |
| 411 // have holes. | 411 // have holes. |
| 412 // 2. Create a vector of extensions that did not have a pref value. | 412 // 2. Create a vector of extensions that did not have a pref value. |
| 413 // 3. Remove holes from the sorted vector and append the unsorted vector. | 413 // 3. Remove holes from the sorted vector and append the unsorted vector. |
| 414 void ExtensionToolbarModel::InitializeExtensionList() { | 414 void ExtensionToolbarModel::InitializeExtensionList() { |
| 415 DCHECK(toolbar_items_.empty()); // We shouldn't have any items yet. |
| 416 |
| 415 last_known_positions_ = extension_prefs_->GetToolbarOrder(); | 417 last_known_positions_ = extension_prefs_->GetToolbarOrder(); |
| 416 if (profile_->IsOffTheRecord()) | 418 if (profile_->IsOffTheRecord()) |
| 417 IncognitoPopulate(); | 419 IncognitoPopulate(); |
| 418 else | 420 else |
| 419 Populate(last_known_positions_); | 421 Populate(last_known_positions_); |
| 420 | 422 |
| 421 extensions_initialized_ = true; | 423 extensions_initialized_ = true; |
| 422 MaybeUpdateVisibilityPrefs(); | 424 MaybeUpdateVisibilityPrefs(); |
| 423 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); | 425 FOR_EACH_OBSERVER(Observer, observers_, OnToolbarModelInitialized()); |
| 424 } | 426 } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 443 } | 445 } |
| 444 | 446 |
| 445 ExtensionIdList::const_iterator pos = | 447 ExtensionIdList::const_iterator pos = |
| 446 std::find(positions.begin(), positions.end(), extension->id()); | 448 std::find(positions.begin(), positions.end(), extension->id()); |
| 447 if (pos != positions.end()) | 449 if (pos != positions.end()) |
| 448 sorted[pos - positions.begin()] = extension; | 450 sorted[pos - positions.begin()] = extension; |
| 449 else | 451 else |
| 450 unsorted.push_back(extension); | 452 unsorted.push_back(extension); |
| 451 } | 453 } |
| 452 | 454 |
| 453 // Clear the current items, if any. | |
| 454 ClearItems(); | |
| 455 | |
| 456 // Merge the lists. | 455 // Merge the lists. |
| 457 toolbar_items_.reserve(sorted.size() + unsorted.size()); | 456 sorted.insert(sorted.end(), unsorted.begin(), unsorted.end()); |
| 457 toolbar_items_.reserve(sorted.size()); |
| 458 | 458 |
| 459 for (const scoped_refptr<const Extension>& extension : sorted) { | 459 for (const scoped_refptr<const Extension>& extension : sorted) { |
| 460 // It's possible for the extension order to contain items that aren't | 460 // It's possible for the extension order to contain items that aren't |
| 461 // actually loaded on this machine. For example, when extension sync is on, | 461 // actually loaded on this machine. For example, when extension sync is on, |
| 462 // we sync the extension order as-is but double-check with the user before | 462 // we sync the extension order as-is but double-check with the user before |
| 463 // syncing NPAPI-containing extensions, so if one of those is not actually | 463 // syncing NPAPI-containing extensions, so if one of those is not actually |
| 464 // synced, we'll get a NULL in the list. This sort of case can also happen | 464 // synced, we'll get a NULL in the list. This sort of case can also happen |
| 465 // if some error prevents an extension from loading. | 465 // if some error prevents an extension from loading. |
| 466 if (extension.get() != NULL) { | 466 if (extension.get()) { |
| 467 // We don't notify observers of the added extension yet. Rather, observers |
| 468 // should wait for the "OnToolbarModelInitialized" notification, and then |
| 469 // bulk-update. (This saves a lot of bouncing-back-and-forth here, and |
| 470 // allows observers to ensure that the extension system is always |
| 471 // initialized before using the extensions). |
| 467 toolbar_items_.push_back(extension); | 472 toolbar_items_.push_back(extension); |
| 468 FOR_EACH_OBSERVER( | |
| 469 Observer, | |
| 470 observers_, | |
| 471 ToolbarExtensionAdded(extension.get(), toolbar_items_.size() - 1)); | |
| 472 } | |
| 473 } | |
| 474 for (const scoped_refptr<const Extension>& extension : unsorted) { | |
| 475 if (extension.get() != NULL) { | |
| 476 toolbar_items_.push_back(extension); | |
| 477 FOR_EACH_OBSERVER( | |
| 478 Observer, | |
| 479 observers_, | |
| 480 ToolbarExtensionAdded(extension.get(), toolbar_items_.size() - 1)); | |
| 481 } | 473 } |
| 482 } | 474 } |
| 483 | 475 |
| 484 UMA_HISTOGRAM_COUNTS_100( | 476 UMA_HISTOGRAM_COUNTS_100( |
| 485 "ExtensionToolbarModel.BrowserActionsPermanentlyHidden", hidden); | 477 "ExtensionToolbarModel.BrowserActionsPermanentlyHidden", hidden); |
| 486 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsCount", | 478 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsCount", |
| 487 toolbar_items_.size()); | 479 toolbar_items_.size()); |
| 488 | 480 |
| 489 if (!toolbar_items_.empty()) { | 481 if (!toolbar_items_.empty()) { |
| 490 // Visible count can be -1, meaning: 'show all'. Since UMA converts negative | 482 // Visible count can be -1, meaning: 'show all'. Since UMA converts negative |
| 491 // values to 0, this would be counted as 'show none' unless we convert it to | 483 // values to 0, this would be counted as 'show none' unless we convert it to |
| 492 // max. | 484 // max. |
| 493 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsVisible", | 485 UMA_HISTOGRAM_COUNTS_100("ExtensionToolbarModel.BrowserActionsVisible", |
| 494 visible_icon_count_ == -1 ? | 486 visible_icon_count_ == -1 ? |
| 495 base::HistogramBase::kSampleType_MAX : | 487 base::HistogramBase::kSampleType_MAX : |
| 496 visible_icon_count_); | 488 visible_icon_count_); |
| 497 } | 489 } |
| 498 } | 490 } |
| 499 | 491 |
| 500 void ExtensionToolbarModel::IncognitoPopulate() { | 492 void ExtensionToolbarModel::IncognitoPopulate() { |
| 501 DCHECK(profile_->IsOffTheRecord()); | 493 DCHECK(profile_->IsOffTheRecord()); |
| 502 // Clear the current items, if any. | |
| 503 ClearItems(); | |
| 504 | |
| 505 const ExtensionToolbarModel* original_model = | 494 const ExtensionToolbarModel* original_model = |
| 506 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); | 495 ExtensionToolbarModel::Get(profile_->GetOriginalProfile()); |
| 507 | 496 |
| 508 // Find the absolute value of the original model's count. | 497 // Find the absolute value of the original model's count. |
| 509 int original_visible = original_model->visible_icon_count(); | 498 int original_visible = original_model->visible_icon_count(); |
| 510 | 499 |
| 511 // In incognito mode, we show only those extensions that are | 500 // In incognito mode, we show only those extensions that are |
| 512 // incognito-enabled. Further, any actions that were overflowed in regular | 501 // incognito-enabled. Further, any actions that were overflowed in regular |
| 513 // mode are still overflowed. Order is the same as in regular mode. | 502 // mode are still overflowed. Order is the same as in regular mode. |
| 514 visible_icon_count_ = 0; | 503 visible_icon_count_ = 0; |
| 515 for (ExtensionList::const_iterator iter = | 504 for (ExtensionList::const_iterator iter = |
| 516 original_model->toolbar_items_.begin(); | 505 original_model->toolbar_items_.begin(); |
| 517 iter != original_model->toolbar_items_.end(); ++iter) { | 506 iter != original_model->toolbar_items_.end(); ++iter) { |
| 518 if (ShouldAddExtension(iter->get())) { | 507 if (ShouldAddExtension(iter->get())) { |
| 519 toolbar_items_.push_back(*iter); | 508 toolbar_items_.push_back(*iter); |
| 520 if (iter - original_model->toolbar_items_.begin() < original_visible) | 509 if (iter - original_model->toolbar_items_.begin() < original_visible) |
| 521 ++visible_icon_count_; | 510 ++visible_icon_count_; |
| 522 FOR_EACH_OBSERVER( | |
| 523 Observer, | |
| 524 observers_, | |
| 525 ToolbarExtensionAdded(iter->get(), toolbar_items_.size() - 1)); | |
| 526 } | 511 } |
| 527 } | 512 } |
| 528 } | 513 } |
| 529 | 514 |
| 530 void ExtensionToolbarModel::UpdatePrefs() { | 515 void ExtensionToolbarModel::UpdatePrefs() { |
| 531 if (!extension_prefs_ || profile_->IsOffTheRecord()) | 516 if (!extension_prefs_ || profile_->IsOffTheRecord()) |
| 532 return; | 517 return; |
| 533 | 518 |
| 534 // Don't observe change caused by self. | 519 // Don't observe change caused by self. |
| 535 pref_change_registrar_.Remove(pref_names::kToolbar); | 520 pref_change_registrar_.Remove(pref_names::kToolbar); |
| 536 extension_prefs_->SetToolbarOrder(last_known_positions_); | 521 extension_prefs_->SetToolbarOrder(last_known_positions_); |
| 537 pref_change_registrar_.Add(pref_names::kToolbar, pref_change_callback_); | 522 pref_change_registrar_.Add(pref_names::kToolbar, pref_change_callback_); |
| 538 } | 523 } |
| 539 | 524 |
| 540 void ExtensionToolbarModel::MaybeUpdateVisibilityPref( | 525 void ExtensionToolbarModel::MaybeUpdateVisibilityPref( |
| 541 const Extension* extension, int index) { | 526 const Extension* extension, size_t index) { |
| 542 // We only update the visibility pref for hidden/not hidden based on the | 527 // We only update the visibility pref for hidden/not hidden based on the |
| 543 // overflow menu with the new toolbar design. | 528 // overflow menu with the new toolbar design. |
| 544 if (include_all_extensions_ && !profile_->IsOffTheRecord()) { | 529 if (include_all_extensions_ && !profile_->IsOffTheRecord()) { |
| 545 bool visible = index < visible_icon_count_ || visible_icon_count_ == -1; | 530 bool visible = index < visible_icon_count(); |
| 546 if (visible != ExtensionActionAPI::GetBrowserActionVisibility( | 531 if (visible != ExtensionActionAPI::GetBrowserActionVisibility( |
| 547 extension_prefs_, extension->id())) { | 532 extension_prefs_, extension->id())) { |
| 548 // Don't observe changes caused by ourselves. | 533 // Don't observe changes caused by ourselves. |
| 549 bool was_registered = false; | 534 bool was_registered = false; |
| 550 if (registrar_.IsRegistered( | 535 if (registrar_.IsRegistered( |
| 551 this, | 536 this, |
| 552 NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, | 537 NOTIFICATION_EXTENSION_BROWSER_ACTION_VISIBILITY_CHANGED, |
| 553 content::Source<ExtensionPrefs>(extension_prefs_))) { | 538 content::Source<ExtensionPrefs>(extension_prefs_))) { |
| 554 was_registered = true; | 539 was_registered = true; |
| 555 registrar_.Remove( | 540 registrar_.Remove( |
| (...skipping 27 matching lines...) Expand all Loading... |
| 583 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); | 568 ExtensionIdList pref_positions = extension_prefs_->GetToolbarOrder(); |
| 584 size_t pref_position_size = pref_positions.size(); | 569 size_t pref_position_size = pref_positions.size(); |
| 585 for (size_t i = 0; i < last_known_positions_.size(); ++i) { | 570 for (size_t i = 0; i < last_known_positions_.size(); ++i) { |
| 586 if (std::find(pref_positions.begin(), pref_positions.end(), | 571 if (std::find(pref_positions.begin(), pref_positions.end(), |
| 587 last_known_positions_[i]) == pref_positions.end()) { | 572 last_known_positions_[i]) == pref_positions.end()) { |
| 588 pref_positions.push_back(last_known_positions_[i]); | 573 pref_positions.push_back(last_known_positions_[i]); |
| 589 } | 574 } |
| 590 } | 575 } |
| 591 last_known_positions_.swap(pref_positions); | 576 last_known_positions_.swap(pref_positions); |
| 592 | 577 |
| 578 ClearItems(); |
| 579 |
| 593 // Re-populate. | 580 // Re-populate. |
| 594 Populate(last_known_positions_); | 581 Populate(last_known_positions_); |
| 595 | 582 |
| 596 if (last_known_positions_.size() > pref_position_size) { | 583 if (last_known_positions_.size() > pref_position_size) { |
| 597 // Need to update pref because we have extra icons. But can't call | 584 // Need to update pref because we have extra icons. But can't call |
| 598 // UpdatePrefs() directly within observation closure. | 585 // UpdatePrefs() directly within observation closure. |
| 599 base::MessageLoop::current()->PostTask( | 586 base::MessageLoop::current()->PostTask( |
| 600 FROM_HERE, | 587 FROM_HERE, |
| 601 base::Bind(&ExtensionToolbarModel::UpdatePrefs, | 588 base::Bind(&ExtensionToolbarModel::UpdatePrefs, |
| 602 weak_ptr_factory_.GetWeakPtr())); | 589 weak_ptr_factory_.GetWeakPtr())); |
| 603 } | 590 } |
| 604 } | 591 } |
| 605 | 592 |
| 606 size_t ExtensionToolbarModel::GetVisibleIconCountForTab( | 593 size_t ExtensionToolbarModel::GetVisibleIconCountForTab( |
| 607 content::WebContents* web_contents) const { | 594 content::WebContents* web_contents) const { |
| 608 if (all_icons_visible()) | 595 if (all_icons_visible()) |
| 609 return visible_icon_count(); // Already displaying all actions. | 596 return visible_icon_count(); // Already displaying all actions. |
| 610 | 597 |
| 611 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_); | 598 ExtensionActionAPI* extension_action_api = ExtensionActionAPI::Get(profile_); |
| 612 size_t total_icons = visible_icon_count_; | 599 size_t total_icons = visible_icon_count(); |
| 613 for (size_t i = total_icons; i < toolbar_items_.size(); ++i) { | 600 for (size_t i = total_icons; i < toolbar_items_.size(); ++i) { |
| 614 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(), | 601 if (extension_action_api->ExtensionWantsToRun(toolbar_items_[i].get(), |
| 615 web_contents)) | 602 web_contents)) |
| 616 ++total_icons; | 603 ++total_icons; |
| 617 } | 604 } |
| 618 return total_icons; | 605 return total_icons; |
| 619 } | 606 } |
| 620 | 607 |
| 621 ExtensionList ExtensionToolbarModel::GetItemOrderForTab( | 608 ExtensionList ExtensionToolbarModel::GetItemOrderForTab( |
| 622 content::WebContents* web_contents) const { | 609 content::WebContents* web_contents) const { |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 657 // (like we do for LocationBar), but sadly, we don't. | 644 // (like we do for LocationBar), but sadly, we don't. |
| 658 while ((obs = it.GetNext()) != NULL) { | 645 while ((obs = it.GetNext()) != NULL) { |
| 659 if (obs->GetBrowser() == browser) | 646 if (obs->GetBrowser() == browser) |
| 660 return obs->ShowExtensionActionPopup(extension, grant_active_tab); | 647 return obs->ShowExtensionActionPopup(extension, grant_active_tab); |
| 661 } | 648 } |
| 662 return false; | 649 return false; |
| 663 } | 650 } |
| 664 | 651 |
| 665 void ExtensionToolbarModel::EnsureVisibility( | 652 void ExtensionToolbarModel::EnsureVisibility( |
| 666 const ExtensionIdList& extension_ids) { | 653 const ExtensionIdList& extension_ids) { |
| 667 if (visible_icon_count_ == -1) | 654 if (all_icons_visible()) |
| 668 return; // Already showing all. | 655 return; // Already showing all. |
| 669 | 656 |
| 670 // Otherwise, make sure we have enough room to show all the extensions | 657 // Otherwise, make sure we have enough room to show all the extensions |
| 671 // requested. | 658 // requested. |
| 672 if (visible_icon_count_ < static_cast<int>(extension_ids.size())) | 659 if (visible_icon_count() < extension_ids.size()) |
| 673 SetVisibleIconCount(extension_ids.size()); | 660 SetVisibleIconCount(extension_ids.size()); |
| 674 | 661 |
| 675 if (visible_icon_count_ == -1) | 662 if (all_icons_visible()) |
| 676 return; // May have been set to max by SetVisibleIconCount. | 663 return; // May have been set to max by SetVisibleIconCount. |
| 677 | 664 |
| 678 // Guillotine's Delight: Move an orange noble to the front of the line. | 665 // Guillotine's Delight: Move an orange noble to the front of the line. |
| 679 for (ExtensionIdList::const_iterator it = extension_ids.begin(); | 666 for (ExtensionIdList::const_iterator it = extension_ids.begin(); |
| 680 it != extension_ids.end(); ++it) { | 667 it != extension_ids.end(); ++it) { |
| 681 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); | 668 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); |
| 682 extension != toolbar_items_.end(); ++extension) { | 669 extension != toolbar_items_.end(); ++extension) { |
| 683 if ((*extension)->id() == (*it)) { | 670 if ((*extension)->id() == (*it)) { |
| 684 if (extension - toolbar_items_.begin() >= visible_icon_count_) | 671 if (extension - toolbar_items_.begin() >= |
| 672 static_cast<int>(visible_icon_count())) |
| 685 MoveExtensionIcon((*extension)->id(), 0); | 673 MoveExtensionIcon((*extension)->id(), 0); |
| 686 break; | 674 break; |
| 687 } | 675 } |
| 688 } | 676 } |
| 689 } | 677 } |
| 690 } | 678 } |
| 691 | 679 |
| 692 bool ExtensionToolbarModel::HighlightExtensions( | 680 bool ExtensionToolbarModel::HighlightExtensions( |
| 693 const ExtensionIdList& extension_ids) { | 681 const ExtensionIdList& extension_ids) { |
| 694 highlighted_items_.clear(); | 682 highlighted_items_.clear(); |
| 695 | 683 |
| 696 for (ExtensionIdList::const_iterator id = extension_ids.begin(); | 684 for (ExtensionIdList::const_iterator id = extension_ids.begin(); |
| 697 id != extension_ids.end(); | 685 id != extension_ids.end(); |
| 698 ++id) { | 686 ++id) { |
| 699 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); | 687 for (ExtensionList::const_iterator extension = toolbar_items_.begin(); |
| 700 extension != toolbar_items_.end(); | 688 extension != toolbar_items_.end(); |
| 701 ++extension) { | 689 ++extension) { |
| 702 if (*id == (*extension)->id()) | 690 if (*id == (*extension)->id()) |
| 703 highlighted_items_.push_back(*extension); | 691 highlighted_items_.push_back(*extension); |
| 704 } | 692 } |
| 705 } | 693 } |
| 706 | 694 |
| 707 // If we have any items in |highlighted_items_|, then we entered highlighting | 695 // If we have any items in |highlighted_items_|, then we entered highlighting |
| 708 // mode. | 696 // mode. |
| 709 if (highlighted_items_.size()) { | 697 if (highlighted_items_.size()) { |
| 710 old_visible_icon_count_ = visible_icon_count_; | 698 old_visible_icon_count_ = visible_icon_count_; |
| 711 is_highlighting_ = true; | 699 is_highlighting_ = true; |
| 712 if (visible_icon_count_ != -1 && | 700 if (visible_icon_count() < extension_ids.size()) |
| 713 visible_icon_count_ < static_cast<int>(extension_ids.size())) { | |
| 714 SetVisibleIconCount(extension_ids.size()); | 701 SetVisibleIconCount(extension_ids.size()); |
| 715 } | |
| 716 | 702 |
| 717 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(true)); | 703 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(true)); |
| 718 return true; | 704 return true; |
| 719 } | 705 } |
| 720 | 706 |
| 721 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if | 707 // Otherwise, we didn't enter highlighting mode (and, in fact, exited it if |
| 722 // we were otherwise in it). | 708 // we were otherwise in it). |
| 723 if (is_highlighting_) | 709 if (is_highlighting_) |
| 724 StopHighlighting(); | 710 StopHighlighting(); |
| 725 return false; | 711 return false; |
| 726 } | 712 } |
| 727 | 713 |
| 728 void ExtensionToolbarModel::StopHighlighting() { | 714 void ExtensionToolbarModel::StopHighlighting() { |
| 729 if (is_highlighting_) { | 715 if (is_highlighting_) { |
| 730 highlighted_items_.clear(); | 716 highlighted_items_.clear(); |
| 731 is_highlighting_ = false; | 717 is_highlighting_ = false; |
| 732 if (old_visible_icon_count_ != visible_icon_count_) | 718 if (old_visible_icon_count_ != visible_icon_count_) |
| 733 SetVisibleIconCount(old_visible_icon_count_); | 719 SetVisibleIconCount(old_visible_icon_count_); |
| 734 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); | 720 FOR_EACH_OBSERVER(Observer, observers_, ToolbarHighlightModeChanged(false)); |
| 735 } | 721 } |
| 736 } | 722 } |
| 737 | 723 |
| 738 } // namespace extensions | 724 } // namespace extensions |
| OLD | NEW |