| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 #import "chrome/browser/cocoa/location_bar_view_mac.h" | 5 #import "chrome/browser/cocoa/location_bar_view_mac.h" |
| 6 | 6 |
| 7 #include "app/l10n_util_mac.h" | 7 #include "app/l10n_util_mac.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/stl_util-inl.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 11 #include "chrome/app/chrome_dll_resource.h" | 12 #include "chrome/app/chrome_dll_resource.h" |
| 12 #include "chrome/browser/alternate_nav_url_fetcher.h" | 13 #include "chrome/browser/alternate_nav_url_fetcher.h" |
| 13 #import "chrome/browser/app_controller_mac.h" | 14 #import "chrome/browser/app_controller_mac.h" |
| 14 #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" | 15 #import "chrome/browser/autocomplete/autocomplete_edit_view_mac.h" |
| 15 #include "chrome/browser/browser_list.h" | 16 #include "chrome/browser/browser_list.h" |
| 16 #import "chrome/browser/cocoa/autocomplete_text_field.h" | 17 #import "chrome/browser/cocoa/autocomplete_text_field.h" |
| 17 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" | 18 #import "chrome/browser/cocoa/autocomplete_text_field_cell.h" |
| 18 #include "chrome/browser/cocoa/event_utils.h" | 19 #include "chrome/browser/cocoa/event_utils.h" |
| 19 #include "chrome/browser/command_updater.h" | 20 #include "chrome/browser/command_updater.h" |
| 21 #include "chrome/browser/extensions/extension_browser_event_router.h" |
| 22 #include "chrome/browser/extensions/extensions_service.h" |
| 23 #include "chrome/browser/extensions/extension_tabs_module.h" |
| 20 #include "chrome/browser/profile.h" | 24 #include "chrome/browser/profile.h" |
| 21 #include "chrome/browser/search_engines/template_url.h" | 25 #include "chrome/browser/search_engines/template_url.h" |
| 22 #include "chrome/browser/search_engines/template_url_model.h" | 26 #include "chrome/browser/search_engines/template_url_model.h" |
| 23 #include "chrome/browser/tab_contents/navigation_entry.h" | 27 #include "chrome/browser/tab_contents/navigation_entry.h" |
| 24 #include "chrome/browser/tab_contents/tab_contents.h" | 28 #include "chrome/browser/tab_contents/tab_contents.h" |
| 29 #include "chrome/common/extensions/extension.h" |
| 30 #include "chrome/common/extensions/extension_action.h" |
| 31 #include "chrome/common/notification_service.h" |
| 25 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
| 26 #include "grit/theme_resources.h" | 33 #include "grit/theme_resources.h" |
| 27 #include "skia/ext/skia_utils_mac.h" | 34 #include "skia/ext/skia_utils_mac.h" |
| 28 #include "third_party/skia/include/core/SkBitmap.h" | 35 #include "third_party/skia/include/core/SkBitmap.h" |
| 29 | 36 |
| 30 // TODO(shess): This code is mostly copied from the gtk | 37 // TODO(shess): This code is mostly copied from the gtk |
| 31 // implementation. Make sure it's all appropriate and flesh it out. | 38 // implementation. Make sure it's all appropriate and flesh it out. |
| 32 | 39 |
| 33 namespace { | 40 namespace { |
| 34 | 41 |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 const BubblePositioner* bubble_positioner, | 85 const BubblePositioner* bubble_positioner, |
| 79 CommandUpdater* command_updater, | 86 CommandUpdater* command_updater, |
| 80 ToolbarModel* toolbar_model, | 87 ToolbarModel* toolbar_model, |
| 81 Profile* profile) | 88 Profile* profile) |
| 82 : edit_view_(new AutocompleteEditViewMac(this, bubble_positioner, | 89 : edit_view_(new AutocompleteEditViewMac(this, bubble_positioner, |
| 83 toolbar_model, profile, command_updater, field)), | 90 toolbar_model, profile, command_updater, field)), |
| 84 command_updater_(command_updater), | 91 command_updater_(command_updater), |
| 85 field_(field), | 92 field_(field), |
| 86 disposition_(CURRENT_TAB), | 93 disposition_(CURRENT_TAB), |
| 87 security_image_view_(profile, toolbar_model), | 94 security_image_view_(profile, toolbar_model), |
| 95 page_action_views_(new PageActionViewList(this, profile, toolbar_model)), |
| 88 profile_(profile), | 96 profile_(profile), |
| 89 toolbar_model_(toolbar_model), | 97 toolbar_model_(toolbar_model), |
| 90 transition_(PageTransition::TYPED) { | 98 transition_(PageTransition::TYPED) { |
| 91 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; | 99 AutocompleteTextFieldCell* cell = [field_ autocompleteTextFieldCell]; |
| 92 [cell setSecurityImageView:&security_image_view_]; | 100 [cell setSecurityImageView:&security_image_view_]; |
| 101 [cell setPageActionViewList:page_action_views_]; |
| 93 } | 102 } |
| 94 | 103 |
| 95 LocationBarViewMac::~LocationBarViewMac() { | 104 LocationBarViewMac::~LocationBarViewMac() { |
| 96 // TODO(shess): Placeholder for omnibox changes. | 105 // TODO(shess): Placeholder for omnibox changes. |
| 97 } | 106 } |
| 98 | 107 |
| 99 std::wstring LocationBarViewMac::GetInputString() const { | 108 std::wstring LocationBarViewMac::GetInputString() const { |
| 100 return location_input_; | 109 return location_input_; |
| 101 } | 110 } |
| 102 | 111 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 126 | 135 |
| 127 void LocationBarViewMac::FocusLocation() { | 136 void LocationBarViewMac::FocusLocation() { |
| 128 edit_view_->FocusLocation(); | 137 edit_view_->FocusLocation(); |
| 129 } | 138 } |
| 130 | 139 |
| 131 void LocationBarViewMac::FocusSearch() { | 140 void LocationBarViewMac::FocusSearch() { |
| 132 edit_view_->SetForcedQuery(); | 141 edit_view_->SetForcedQuery(); |
| 133 // TODO(pkasting): Focus the edit a la Linux/Win | 142 // TODO(pkasting): Focus the edit a la Linux/Win |
| 134 } | 143 } |
| 135 | 144 |
| 145 void LocationBarViewMac::UpdatePageActions() { |
| 146 page_action_views_->RefreshViews(); |
| 147 [field_ resetFieldEditorFrameIfNeeded]; |
| 148 } |
| 149 |
| 150 void LocationBarViewMac::InvalidatePageActions() { |
| 151 page_action_views_->DeleteAll(); |
| 152 } |
| 153 |
| 136 void LocationBarViewMac::SaveStateToContents(TabContents* contents) { | 154 void LocationBarViewMac::SaveStateToContents(TabContents* contents) { |
| 137 // TODO(shess): Why SaveStateToContents vs SaveStateToTab? | 155 // TODO(shess): Why SaveStateToContents vs SaveStateToTab? |
| 138 edit_view_->SaveStateToTab(contents); | 156 edit_view_->SaveStateToTab(contents); |
| 139 } | 157 } |
| 140 | 158 |
| 141 void LocationBarViewMac::Update(const TabContents* contents, | 159 void LocationBarViewMac::Update(const TabContents* contents, |
| 142 bool should_restore_state) { | 160 bool should_restore_state) { |
| 143 SetSecurityIcon(toolbar_model_->GetIcon()); | 161 SetSecurityIcon(toolbar_model_->GetIcon()); |
| 162 page_action_views_->RefreshViews(); |
| 144 // AutocompleteEditView restores state if the tab is non-NULL. | 163 // AutocompleteEditView restores state if the tab is non-NULL. |
| 145 edit_view_->Update(should_restore_state ? contents : NULL); | 164 edit_view_->Update(should_restore_state ? contents : NULL); |
| 146 } | 165 } |
| 147 | 166 |
| 148 void LocationBarViewMac::OnAutocompleteAccept(const GURL& url, | 167 void LocationBarViewMac::OnAutocompleteAccept(const GURL& url, |
| 149 WindowOpenDisposition disposition, | 168 WindowOpenDisposition disposition, |
| 150 PageTransition::Type transition, | 169 PageTransition::Type transition, |
| 151 const GURL& alternate_nav_url) { | 170 const GURL& alternate_nav_url) { |
| 152 if (!url.is_valid()) | 171 if (!url.is_valid()) |
| 153 return; | 172 return; |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 | 303 |
| 285 std::wstring LocationBarViewMac::GetTitle() const { | 304 std::wstring LocationBarViewMac::GetTitle() const { |
| 286 NOTIMPLEMENTED(); | 305 NOTIMPLEMENTED(); |
| 287 return std::wstring(); | 306 return std::wstring(); |
| 288 } | 307 } |
| 289 | 308 |
| 290 void LocationBarViewMac::Revert() { | 309 void LocationBarViewMac::Revert() { |
| 291 edit_view_->RevertAll(); | 310 edit_view_->RevertAll(); |
| 292 } | 311 } |
| 293 | 312 |
| 313 // TODO(pamg): Change all these, here and for other platforms, to size_t. |
| 294 int LocationBarViewMac::PageActionCount() { | 314 int LocationBarViewMac::PageActionCount() { |
| 295 NOTIMPLEMENTED(); | 315 return static_cast<int>(page_action_views_->Count()); |
| 296 return -1; | |
| 297 } | 316 } |
| 298 | 317 |
| 299 int LocationBarViewMac::PageActionVisibleCount() { | 318 int LocationBarViewMac::PageActionVisibleCount() { |
| 300 NOTIMPLEMENTED(); | 319 return static_cast<int>(page_action_views_->VisibleCount()); |
| 301 return -1; | |
| 302 } | 320 } |
| 303 | 321 |
| 304 ExtensionAction* LocationBarViewMac::GetPageAction(size_t index) { | 322 ExtensionAction* LocationBarViewMac::GetPageAction(size_t index) { |
| 305 NOTIMPLEMENTED(); | 323 if (index < page_action_views_->Count()) |
| 324 return page_action_views_->ViewAt(index)->page_action(); |
| 325 NOTREACHED(); |
| 306 return NULL; | 326 return NULL; |
| 307 } | 327 } |
| 308 | 328 |
| 309 ExtensionAction* LocationBarViewMac::GetVisiblePageAction(size_t index) { | 329 ExtensionAction* LocationBarViewMac::GetVisiblePageAction(size_t index) { |
| 310 NOTIMPLEMENTED(); | 330 size_t current = 0; |
| 331 for (size_t i = 0; i < page_action_views_->Count(); ++i) { |
| 332 if (page_action_views_->ViewAt(i)->IsVisible()) { |
| 333 if (current == index) |
| 334 return page_action_views_->ViewAt(i)->page_action(); |
| 335 |
| 336 ++current; |
| 337 } |
| 338 } |
| 339 |
| 340 NOTREACHED(); |
| 311 return NULL; | 341 return NULL; |
| 312 } | 342 } |
| 313 | 343 |
| 314 void LocationBarViewMac::TestPageActionPressed(size_t index) { | 344 void LocationBarViewMac::TestPageActionPressed(size_t index) { |
| 315 NOTIMPLEMENTED(); | 345 NOTIMPLEMENTED(); |
| 316 } | 346 } |
| 317 | 347 |
| 318 NSImage* LocationBarViewMac::GetTabButtonImage() { | 348 NSImage* LocationBarViewMac::GetTabButtonImage() { |
| 319 if (!tab_button_image_) { | 349 if (!tab_button_image_) { |
| 320 SkBitmap* skiaBitmap = ResourceBundle::GetSharedInstance(). | 350 SkBitmap* skiaBitmap = ResourceBundle::GetSharedInstance(). |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 } | 395 } |
| 366 [field_ resetFieldEditorFrameIfNeeded]; | 396 [field_ resetFieldEditorFrameIfNeeded]; |
| 367 } | 397 } |
| 368 | 398 |
| 369 // LocationBarImageView--------------------------------------------------------- | 399 // LocationBarImageView--------------------------------------------------------- |
| 370 | 400 |
| 371 void LocationBarViewMac::LocationBarImageView::SetImage(NSImage* image) { | 401 void LocationBarViewMac::LocationBarImageView::SetImage(NSImage* image) { |
| 372 image_.reset([image retain]); | 402 image_.reset([image retain]); |
| 373 } | 403 } |
| 374 | 404 |
| 405 void LocationBarViewMac::LocationBarImageView::SetImage(SkBitmap* image) { |
| 406 SetImage(gfx::SkBitmapToNSImage(*image)); |
| 407 } |
| 408 |
| 375 void LocationBarViewMac::LocationBarImageView::SetLabel(NSString* text, | 409 void LocationBarViewMac::LocationBarImageView::SetLabel(NSString* text, |
| 376 NSFont* baseFont, | 410 NSFont* baseFont, |
| 377 NSColor* color) { | 411 NSColor* color) { |
| 378 // Create an attributed string for the label, if a label was given. | 412 // Create an attributed string for the label, if a label was given. |
| 379 label_.reset(); | 413 label_.reset(); |
| 380 if (text) { | 414 if (text) { |
| 381 DCHECK(color); | 415 DCHECK(color); |
| 382 DCHECK(baseFont); | 416 DCHECK(baseFont); |
| 383 NSFont* font = [NSFont fontWithDescriptor:[baseFont fontDescriptor] | 417 NSFont* font = [NSFont fontWithDescriptor:[baseFont fontDescriptor] |
| 384 size:[baseFont pointSize] - 2.0]; | 418 size:[baseFont pointSize] - 2.0]; |
| 385 NSDictionary* attributes = | 419 NSDictionary* attributes = |
| 386 [NSDictionary dictionaryWithObjectsAndKeys: | 420 [NSDictionary dictionaryWithObjectsAndKeys: |
| 387 color, NSForegroundColorAttributeName, | 421 color, NSForegroundColorAttributeName, |
| 388 font, NSFontAttributeName, | 422 font, NSFontAttributeName, |
| 389 NULL]; | 423 NULL]; |
| 390 NSAttributedString* attrStr = | 424 NSAttributedString* attrStr = |
| 391 [[NSAttributedString alloc] initWithString:text attributes:attributes]; | 425 [[NSAttributedString alloc] initWithString:text attributes:attributes]; |
| 392 label_.reset(attrStr); | 426 label_.reset(attrStr); |
| 393 } | 427 } |
| 394 } | 428 } |
| 395 | 429 |
| 396 void LocationBarViewMac::LocationBarImageView::SetVisible(bool visible) { | 430 void LocationBarViewMac::LocationBarImageView::SetVisible(bool visible) { |
| 397 DCHECK(!visible || image_); | |
| 398 visible_ = visible; | 431 visible_ = visible; |
| 399 } | 432 } |
| 400 | 433 |
| 401 // SecurityImageView------------------------------------------------------------ | 434 // SecurityImageView------------------------------------------------------------ |
| 402 | 435 |
| 403 LocationBarViewMac::SecurityImageView::SecurityImageView( | 436 LocationBarViewMac::SecurityImageView::SecurityImageView( |
| 404 Profile* profile, | 437 Profile* profile, |
| 405 ToolbarModel* model) | 438 ToolbarModel* model) |
| 406 : lock_icon_(nil), | 439 : lock_icon_(nil), |
| 407 warning_icon_(nil), | 440 warning_icon_(nil), |
| (...skipping 17 matching lines...) Expand all Loading... |
| 425 warning_icon_.reset([rb.GetNSImageNamed(IDR_WARNING) retain]); | 458 warning_icon_.reset([rb.GetNSImageNamed(IDR_WARNING) retain]); |
| 426 } | 459 } |
| 427 SetImage(warning_icon_); | 460 SetImage(warning_icon_); |
| 428 break; | 461 break; |
| 429 default: | 462 default: |
| 430 NOTREACHED(); | 463 NOTREACHED(); |
| 431 break; | 464 break; |
| 432 } | 465 } |
| 433 } | 466 } |
| 434 | 467 |
| 435 | |
| 436 bool LocationBarViewMac::SecurityImageView::OnMousePressed() { | 468 bool LocationBarViewMac::SecurityImageView::OnMousePressed() { |
| 437 TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents(); | 469 TabContents* tab = BrowserList::GetLastActive()->GetSelectedTabContents(); |
| 438 NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); | 470 NavigationEntry* nav_entry = tab->controller().GetActiveEntry(); |
| 439 if (!nav_entry) { | 471 if (!nav_entry) { |
| 440 NOTREACHED(); | 472 NOTREACHED(); |
| 441 return true; | 473 return true; |
| 442 } | 474 } |
| 443 tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); | 475 tab->ShowPageInfo(nav_entry->url(), nav_entry->ssl(), true); |
| 444 return true; | 476 return true; |
| 445 } | 477 } |
| 478 |
| 479 // PageActionImageView---------------------------------------------------------- |
| 480 |
| 481 LocationBarViewMac::PageActionImageView::PageActionImageView( |
| 482 LocationBarViewMac* owner, |
| 483 Profile* profile, |
| 484 ExtensionAction* page_action) |
| 485 : owner_(owner), |
| 486 profile_(profile), |
| 487 page_action_(page_action), |
| 488 current_tab_id_(-1), |
| 489 preview_enabled_(false) { |
| 490 Extension* extension = profile->GetExtensionsService()->GetExtensionById( |
| 491 page_action->extension_id(), false); |
| 492 DCHECK(extension); |
| 493 |
| 494 // Load all the icons declared in the manifest. This is the contents of the |
| 495 // icons array, plus the default_icon property, if any. |
| 496 std::vector<std::string> icon_paths(*page_action->icon_paths()); |
| 497 if (!page_action_->default_icon_path().empty()) |
| 498 icon_paths.push_back(page_action_->default_icon_path()); |
| 499 |
| 500 tracker_ = new ImageLoadingTracker(this, icon_paths.size()); |
| 501 for (std::vector<std::string>::iterator iter = icon_paths.begin(); |
| 502 iter != icon_paths.end(); ++iter) { |
| 503 tracker_->PostLoadImageTask(extension->GetResource(*iter), |
| 504 gfx::Size(Extension::kPageActionIconMaxSize, |
| 505 Extension::kPageActionIconMaxSize)); |
| 506 } |
| 507 |
| 508 registrar_.Add(this, NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE, |
| 509 Source<Profile>(profile_)); |
| 510 } |
| 511 |
| 512 LocationBarViewMac::PageActionImageView::~PageActionImageView() { |
| 513 if (tracker_) |
| 514 tracker_->StopTrackingImageLoad(); |
| 515 } |
| 516 |
| 517 // Overridden from LocationBarImageView. Either notify listeners or show a |
| 518 // popup depending on the Page Action. |
| 519 bool LocationBarViewMac::PageActionImageView::OnMousePressed(NSRect bounds) { |
| 520 if (page_action_->has_popup()) { |
| 521 NOTIMPLEMENTED(); |
| 522 } else { |
| 523 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( |
| 524 profile_, page_action_->extension_id(), page_action_->id(), |
| 525 current_tab_id_, current_url_.spec(), |
| 526 1); // TODO(pamg): Add support for middle and right buttons. |
| 527 } |
| 528 return true; |
| 529 } |
| 530 |
| 531 void LocationBarViewMac::PageActionImageView::OnImageLoaded(SkBitmap* image, |
| 532 size_t index) { |
| 533 // We loaded icons()->size() icons, plus one extra if the Page Action had |
| 534 // a default icon. |
| 535 int total_icons = page_action_->icon_paths()->size(); |
| 536 if (!page_action_->default_icon_path().empty()) |
| 537 total_icons++; |
| 538 DCHECK(static_cast<int>(index) < total_icons); |
| 539 |
| 540 // Map the index of the loaded image back to its name. If we ever get an |
| 541 // index greater than the number of icons, it must be the default icon. |
| 542 if (image) { |
| 543 if (index < page_action_->icon_paths()->size()) |
| 544 page_action_icons_[page_action_->icon_paths()->at(index)] = *image; |
| 545 else |
| 546 page_action_icons_[page_action_->default_icon_path()] = *image; |
| 547 } |
| 548 |
| 549 // If we are done, release the tracker. |
| 550 if (static_cast<int>(index) == (total_icons - 1)) |
| 551 tracker_ = NULL; |
| 552 |
| 553 owner_->UpdatePageActions(); |
| 554 } |
| 555 |
| 556 void LocationBarViewMac::PageActionImageView::UpdateVisibility( |
| 557 TabContents* contents, const GURL& url) { |
| 558 // Save this off so we can pass it back to the extension when the action gets |
| 559 // executed. See PageActionImageView::OnMousePressed. |
| 560 current_tab_id_ = ExtensionTabUtil::GetTabId(contents); |
| 561 current_url_ = url; |
| 562 |
| 563 bool visible = preview_enabled_ || |
| 564 page_action_->GetIsVisible(current_tab_id_); |
| 565 if (visible) { |
| 566 // Set the tooltip. |
| 567 tooltip_ = page_action_->GetTitle(current_tab_id_); |
| 568 // TODO(port): implement tooltips |
| 569 //SetTooltipText(ASCIIToWide(tooltip_)); |
| 570 |
| 571 // Set the image. |
| 572 // It can come from three places. In descending order of priority: |
| 573 // - The developer can set it dynamically by path or bitmap. It will be in |
| 574 // page_action_->GetIcon(). |
| 575 // - The developer can set it dynamically by index. It will be in |
| 576 // page_action_->GetIconIndex(). |
| 577 // - It can be set in the manifest by path. It will be in page_action_-> |
| 578 // default_icon_path(). |
| 579 |
| 580 // First look for a dynamically set bitmap. |
| 581 SkBitmap skia_icon = page_action_->GetIcon(current_tab_id_); |
| 582 if (skia_icon.isNull()) { |
| 583 int icon_index = page_action_->GetIconIndex(current_tab_id_); |
| 584 std::string icon_path; |
| 585 if (icon_index >= 0) |
| 586 icon_path = page_action_->icon_paths()->at(icon_index); |
| 587 else |
| 588 icon_path = page_action_->default_icon_path(); |
| 589 |
| 590 if (!icon_path.empty()) { |
| 591 PageActionMap::iterator iter = page_action_icons_.find(icon_path); |
| 592 if (iter != page_action_icons_.end()) |
| 593 skia_icon = iter->second; |
| 594 } |
| 595 } |
| 596 |
| 597 if (!skia_icon.isNull()) |
| 598 SetImage(gfx::SkBitmapToNSImage(skia_icon)); |
| 599 } |
| 600 SetVisible(visible); |
| 601 } |
| 602 |
| 603 void LocationBarViewMac::PageActionImageView::Observe( |
| 604 NotificationType type, |
| 605 const NotificationSource& source, |
| 606 const NotificationDetails& details) { |
| 607 switch (type.value) { |
| 608 case NotificationType::EXTENSION_HOST_VIEW_SHOULD_CLOSE: |
| 609 // If we aren't the host of the popup, then disregard the notification. |
| 610 //if (!popup_ || Details<ExtensionHost>(popup_->host()) != details) |
| 611 // return; |
| 612 |
| 613 //HidePopup(); |
| 614 NOTIMPLEMENTED(); |
| 615 break; |
| 616 default: |
| 617 NOTREACHED() << "Unexpected notification"; |
| 618 break; |
| 619 } |
| 620 } |
| 621 |
| 622 // PageActionViewList----------------------------------------------------------- |
| 623 |
| 624 void LocationBarViewMac::PageActionViewList::DeleteAll() { |
| 625 if (!views_.empty()) { |
| 626 STLDeleteContainerPointers(views_.begin(), views_.end()); |
| 627 views_.clear(); |
| 628 } |
| 629 } |
| 630 |
| 631 void LocationBarViewMac::PageActionViewList::RefreshViews() { |
| 632 std::vector<ExtensionAction*> page_actions; |
| 633 ExtensionsService* service = profile_->GetExtensionsService(); |
| 634 if (!service) |
| 635 return; |
| 636 |
| 637 // Remember the previous visibility of the Page Actions so that we can |
| 638 // notify when this changes. |
| 639 std::map<ExtensionAction*, bool> old_visibility; |
| 640 for (size_t i = 0; i < views_.size(); ++i) { |
| 641 old_visibility[views_[i]->page_action()] = views_[i]->IsVisible(); |
| 642 } |
| 643 |
| 644 for (size_t i = 0; i < service->extensions()->size(); ++i) { |
| 645 if (service->extensions()->at(i)->page_action()) |
| 646 page_actions.push_back(service->extensions()->at(i)->page_action()); |
| 647 } |
| 648 |
| 649 // On startup we sometimes haven't loaded any extensions. This makes sure |
| 650 // we catch up when the extensions (and any Page Actions) load. |
| 651 if (page_actions.size() != views_.size()) { |
| 652 DeleteAll(); // Delete the old views (if any). |
| 653 |
| 654 views_.resize(page_actions.size()); |
| 655 |
| 656 for (size_t i = 0; i < page_actions.size(); ++i) { |
| 657 views_[i] = new PageActionImageView(owner_, profile_, page_actions[i]); |
| 658 views_[i]->SetVisible(false); |
| 659 } |
| 660 } |
| 661 |
| 662 if (views_.empty()) |
| 663 return; |
| 664 |
| 665 Browser* browser = BrowserList::GetLastActive(); |
| 666 // The last-active browser can be NULL during startup. |
| 667 if (!browser) |
| 668 return; |
| 669 |
| 670 TabContents* contents = browser->GetSelectedTabContents(); |
| 671 if (!contents) |
| 672 return; |
| 673 |
| 674 GURL url = GURL(WideToUTF8(toolbar_model_->GetText())); |
| 675 for (size_t i = 0; i < views_.size(); ++i) { |
| 676 views_[i]->UpdateVisibility(contents, url); |
| 677 // Check if the visibility of the action changed and notify if it did. |
| 678 ExtensionAction* action = views_[i]->page_action(); |
| 679 if (old_visibility.find(action) == old_visibility.end() || |
| 680 old_visibility[action] != views_[i]->IsVisible()) { |
| 681 NotificationService::current()->Notify( |
| 682 NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| 683 Source<ExtensionAction>(action), |
| 684 Details<TabContents>(contents)); |
| 685 } |
| 686 } |
| 687 } |
| 688 |
| 689 LocationBarViewMac::PageActionImageView* |
| 690 LocationBarViewMac::PageActionViewList::ViewAt(size_t index) { |
| 691 CHECK(index < Count()); |
| 692 return views_[index]; |
| 693 } |
| 694 |
| 695 size_t LocationBarViewMac::PageActionViewList::Count() { |
| 696 return views_.size(); |
| 697 } |
| 698 |
| 699 size_t LocationBarViewMac::PageActionViewList::VisibleCount() { |
| 700 size_t result = 0; |
| 701 for (size_t i = 0; i < views_.size(); ++i) { |
| 702 if (views_[i]->IsVisible()) |
| 703 ++result; |
| 704 } |
| 705 return result; |
| 706 } |
| 707 |
| 708 void LocationBarViewMac::PageActionViewList::OnMousePressed(NSRect iconFrame, |
| 709 size_t index) { |
| 710 ViewAt(index)->OnMousePressed(iconFrame); |
| 711 } |
| OLD | NEW |