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 #include "chrome/browser/views/location_bar_view.h" | 5 #include "chrome/browser/views/location_bar_view.h" |
6 | 6 |
7 #if defined(OS_LINUX) | 7 #if defined(OS_LINUX) |
8 #include <gtk/gtk.h> | 8 #include <gtk/gtk.h> |
9 #endif | 9 #endif |
10 | 10 |
11 #include "app/gfx/canvas.h" | 11 #include "app/gfx/canvas.h" |
12 #include "app/gfx/color_utils.h" | 12 #include "app/gfx/color_utils.h" |
13 #include "app/l10n_util.h" | 13 #include "app/l10n_util.h" |
14 #include "app/resource_bundle.h" | 14 #include "app/resource_bundle.h" |
15 #include "app/theme_provider.h" | 15 #include "app/theme_provider.h" |
16 #include "base/stl_util-inl.h" | 16 #include "base/stl_util-inl.h" |
17 #include "chrome/app/chrome_dll_resource.h" | 17 #include "chrome/app/chrome_dll_resource.h" |
18 #include "chrome/browser/alternate_nav_url_fetcher.h" | 18 #include "chrome/browser/alternate_nav_url_fetcher.h" |
19 #include "chrome/browser/browser_list.h" | 19 #include "chrome/browser/browser_list.h" |
20 #include "chrome/browser/bubble_positioner.h" | 20 #include "chrome/browser/bubble_positioner.h" |
21 #include "chrome/browser/command_updater.h" | 21 #include "chrome/browser/command_updater.h" |
22 #include "chrome/browser/extensions/extension_browser_event_router.h" | 22 #include "chrome/browser/extensions/extension_browser_event_router.h" |
23 #include "chrome/browser/extensions/extensions_service.h" | 23 #include "chrome/browser/extensions/extensions_service.h" |
24 #include "chrome/browser/profile.h" | 24 #include "chrome/browser/profile.h" |
25 #include "chrome/browser/search_engines/template_url_model.h" | 25 #include "chrome/browser/search_engines/template_url_model.h" |
26 #include "chrome/browser/view_ids.h" | 26 #include "chrome/browser/view_ids.h" |
| 27 #include "chrome/browser/views/extensions/extension_popup.h" |
27 #include "grit/generated_resources.h" | 28 #include "grit/generated_resources.h" |
28 #include "grit/theme_resources.h" | 29 #include "grit/theme_resources.h" |
29 | 30 |
30 #if defined(OS_WIN) | 31 #if defined(OS_WIN) |
31 #include "chrome/browser/views/first_run_bubble.h" | 32 #include "chrome/browser/views/first_run_bubble.h" |
32 #endif | 33 #endif |
33 | 34 |
34 using views::View; | 35 using views::View; |
35 | 36 |
36 // static | 37 // static |
(...skipping 651 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
688 page_action_views_.clear(); | 689 page_action_views_.clear(); |
689 } | 690 } |
690 } | 691 } |
691 | 692 |
692 void LocationBarView::RefreshPageActionViews() { | 693 void LocationBarView::RefreshPageActionViews() { |
693 std::vector<ExtensionAction*> page_actions; | 694 std::vector<ExtensionAction*> page_actions; |
694 ExtensionsService* service = profile_->GetExtensionsService(); | 695 ExtensionsService* service = profile_->GetExtensionsService(); |
695 if (!service) | 696 if (!service) |
696 return; | 697 return; |
697 | 698 |
| 699 std::map<ExtensionAction*, bool> old_visibility; |
| 700 for (size_t i = 0; i < page_action_views_.size(); i++) { |
| 701 old_visibility[page_action_views_[i]->image_view()->page_action()] = |
| 702 page_action_views_[i]->IsVisible(); |
| 703 } |
| 704 |
| 705 // Remember the previous visibility of the page actions so that we can |
| 706 // notify when this changes. |
698 for (size_t i = 0; i < service->extensions()->size(); ++i) { | 707 for (size_t i = 0; i < service->extensions()->size(); ++i) { |
699 if (service->extensions()->at(i)->page_action()) | 708 if (service->extensions()->at(i)->page_action()) |
700 page_actions.push_back(service->extensions()->at(i)->page_action()); | 709 page_actions.push_back(service->extensions()->at(i)->page_action()); |
701 } | 710 } |
702 | 711 |
703 // On startup we sometimes haven't loaded any extensions. This makes sure | 712 // On startup we sometimes haven't loaded any extensions. This makes sure |
704 // we catch up when the extensions (and any page actions) load. | 713 // we catch up when the extensions (and any page actions) load. |
705 if (page_actions.size() != page_action_views_.size()) { | 714 if (page_actions.size() != page_action_views_.size()) { |
706 DeletePageActionViews(); // Delete the old views (if any). | 715 DeletePageActionViews(); // Delete the old views (if any). |
707 | 716 |
708 page_action_views_.resize(page_actions.size()); | 717 page_action_views_.resize(page_actions.size()); |
709 | 718 |
710 for (size_t i = 0; i < page_actions.size(); ++i) { | 719 for (size_t i = 0; i < page_actions.size(); ++i) { |
711 page_action_views_[i] = new PageActionWithBadgeView( | 720 page_action_views_[i] = new PageActionWithBadgeView( |
712 new PageActionImageView(this, profile_, | 721 new PageActionImageView(this, profile_, |
713 page_actions[i], bubble_positioner_)); | 722 page_actions[i], bubble_positioner_)); |
714 page_action_views_[i]->SetVisible(false); | 723 page_action_views_[i]->SetVisible(false); |
715 page_action_views_[i]->SetParentOwned(false); | 724 page_action_views_[i]->SetParentOwned(false); |
716 AddChildView(page_action_views_[i]); | 725 AddChildView(page_action_views_[i]); |
717 } | 726 } |
718 } | 727 } |
719 | 728 |
720 TabContents* contents = delegate_->GetTabContents(); | 729 TabContents* contents = delegate_->GetTabContents(); |
721 if (!page_action_views_.empty() && contents) { | 730 if (!page_action_views_.empty() && contents) { |
722 GURL url = GURL(WideToUTF8(model_->GetText())); | 731 GURL url = GURL(WideToUTF8(model_->GetText())); |
723 | 732 |
724 for (size_t i = 0; i < page_action_views_.size(); i++) | 733 for (size_t i = 0; i < page_action_views_.size(); i++) { |
725 page_action_views_[i]->UpdateVisibility(contents, url); | 734 page_action_views_[i]->UpdateVisibility(contents, url); |
| 735 |
| 736 // Check if the visibility of the action changed and notify if it did. |
| 737 ExtensionAction* action = |
| 738 page_action_views_[i]->image_view()->page_action(); |
| 739 if (old_visibility.find(action) == old_visibility.end() || |
| 740 old_visibility[action] != page_action_views_[i]->IsVisible()) { |
| 741 NotificationService::current()->Notify( |
| 742 NotificationType::EXTENSION_PAGE_ACTION_VISIBILITY_CHANGED, |
| 743 Source<ExtensionAction>(action), |
| 744 Details<TabContents>(contents)); |
| 745 } |
| 746 } |
726 } | 747 } |
727 } | 748 } |
728 | 749 |
729 void LocationBarView::SetInfoText(const std::wstring& text, | 750 void LocationBarView::SetInfoText(const std::wstring& text, |
730 ToolbarModel::InfoTextType text_type, | 751 ToolbarModel::InfoTextType text_type, |
731 const std::wstring& tooltip_text) { | 752 const std::wstring& tooltip_text) { |
732 info_label_.SetVisible(!text.empty()); | 753 info_label_.SetVisible(!text.empty()); |
733 info_label_.SetText(text); | 754 info_label_.SetText(text); |
734 if (text_type == ToolbarModel::INFO_EV_TEXT) | 755 if (text_type == ToolbarModel::INFO_EV_TEXT) |
735 info_label_.SetColor(GetColor(true, SECURITY_TEXT)); | 756 info_label_.SetColor(GetColor(true, SECURITY_TEXT)); |
(...skipping 525 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1261 gfx::Size(Extension::kPageActionIconMaxSize, | 1282 gfx::Size(Extension::kPageActionIconMaxSize, |
1262 Extension::kPageActionIconMaxSize)); | 1283 Extension::kPageActionIconMaxSize)); |
1263 } | 1284 } |
1264 } | 1285 } |
1265 | 1286 |
1266 LocationBarView::PageActionImageView::~PageActionImageView() { | 1287 LocationBarView::PageActionImageView::~PageActionImageView() { |
1267 if (tracker_) | 1288 if (tracker_) |
1268 tracker_->StopTrackingImageLoad(); | 1289 tracker_->StopTrackingImageLoad(); |
1269 } | 1290 } |
1270 | 1291 |
| 1292 void LocationBarView::PageActionImageView::ExecuteAction(int button) { |
| 1293 if (page_action_->has_popup()) { |
| 1294 gfx::Point origin; |
| 1295 View::ConvertPointToScreen(this, &origin); |
| 1296 gfx::Rect rect = bounds(); |
| 1297 rect.set_x(origin.x()); |
| 1298 rect.set_y(origin.y()); |
| 1299 |
| 1300 // In tests, GetLastActive could return NULL, so we need to have |
| 1301 // a fallback. |
| 1302 // TODO(erikkay): Find a better way to get the Browser that this |
| 1303 // button is in. |
| 1304 Browser* browser = BrowserList::GetLastActiveWithProfile(profile_); |
| 1305 if (!browser) |
| 1306 browser = BrowserList::FindBrowserWithProfile(profile_); |
| 1307 ExtensionPopup::Show(page_action_->popup_url(), browser, rect); |
| 1308 } else { |
| 1309 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( |
| 1310 profile_, page_action_->extension_id(), page_action_->id(), |
| 1311 current_tab_id_, current_url_.spec(), button); |
| 1312 } |
| 1313 } |
| 1314 |
| 1315 void LocationBarView::PageActionImageView::OnMouseMoved( |
| 1316 const views::MouseEvent& event) { |
| 1317 // PageActionImageView uses normal tooltips rather than the info bubble, |
| 1318 // so just do nothing here rather than letting LocationBarImageView start |
| 1319 // its hover timer. |
| 1320 } |
| 1321 |
1271 bool LocationBarView::PageActionImageView::OnMousePressed( | 1322 bool LocationBarView::PageActionImageView::OnMousePressed( |
1272 const views::MouseEvent& event) { | 1323 const views::MouseEvent& event) { |
1273 int button = -1; | 1324 int button = -1; |
1274 if (event.IsLeftMouseButton()) | 1325 if (event.IsLeftMouseButton()) |
1275 button = 1; | 1326 button = 1; |
1276 else if (event.IsMiddleMouseButton()) | 1327 else if (event.IsMiddleMouseButton()) |
1277 button = 2; | 1328 button = 2; |
1278 else if (event.IsRightMouseButton()) | 1329 else if (event.IsRightMouseButton()) |
1279 button = 3; | 1330 button = 3; |
1280 // Our PageAction icon was clicked on, notify proper authorities. | 1331 |
1281 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( | 1332 ExecuteAction(button); |
1282 profile_, page_action_->extension_id(), page_action_->id(), | |
1283 current_tab_id_, current_url_.spec(), button); | |
1284 return true; | 1333 return true; |
1285 } | 1334 } |
1286 | 1335 |
1287 void LocationBarView::PageActionImageView::ShowInfoBubble() { | 1336 void LocationBarView::PageActionImageView::ShowInfoBubble() { |
1288 ShowInfoBubbleImpl(ASCIIToWide(tooltip_), GetColor(false, TEXT)); | 1337 ShowInfoBubbleImpl(ASCIIToWide(tooltip_), GetColor(false, TEXT)); |
1289 } | 1338 } |
1290 | 1339 |
1291 void LocationBarView::PageActionImageView::OnImageLoaded(SkBitmap* image, | 1340 void LocationBarView::PageActionImageView::OnImageLoaded(SkBitmap* image, |
1292 size_t index) { | 1341 size_t index) { |
1293 // We loaded icons()->size() icons, plus one extra if the page action had | 1342 // We loaded icons()->size() icons, plus one extra if the page action had |
(...skipping 23 matching lines...) Expand all Loading... |
1317 TabContents* contents, const GURL& url) { | 1366 TabContents* contents, const GURL& url) { |
1318 // Save this off so we can pass it back to the extension when the action gets | 1367 // Save this off so we can pass it back to the extension when the action gets |
1319 // executed. See PageActionImageView::OnMousePressed. | 1368 // executed. See PageActionImageView::OnMousePressed. |
1320 current_tab_id_ = ExtensionTabUtil::GetTabId(contents); | 1369 current_tab_id_ = ExtensionTabUtil::GetTabId(contents); |
1321 current_url_ = url; | 1370 current_url_ = url; |
1322 | 1371 |
1323 bool visible = page_action_->GetIsVisible(current_tab_id_); | 1372 bool visible = page_action_->GetIsVisible(current_tab_id_); |
1324 if (visible) { | 1373 if (visible) { |
1325 // Set the tooltip. | 1374 // Set the tooltip. |
1326 tooltip_ = page_action_->GetTitle(current_tab_id_); | 1375 tooltip_ = page_action_->GetTitle(current_tab_id_); |
| 1376 SetTooltipText(ASCIIToWide(tooltip_)); |
1327 | 1377 |
1328 // Set the image. | 1378 // Set the image. |
1329 // It can come from three places. In descending order of priority: | 1379 // It can come from three places. In descending order of priority: |
1330 // - The developer can set it dynamically by path or bitmap. It will be in | 1380 // - The developer can set it dynamically by path or bitmap. It will be in |
1331 // page_action_->GetIcon(). | 1381 // page_action_->GetIcon(). |
1332 // - The developer can set it dynamically by index. It will be in | 1382 // - The developer can set it dynamically by index. It will be in |
1333 // page_action_->GetIconIndex(). | 1383 // page_action_->GetIconIndex(). |
1334 // - It can be set in the manifest by path. It will be in page_action_-> | 1384 // - It can be set in the manifest by path. It will be in page_action_-> |
1335 // default_icon_path(). | 1385 // default_icon_path(). |
1336 | 1386 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1406 } | 1456 } |
1407 | 1457 |
1408 int LocationBarView::PageActionVisibleCount() { | 1458 int LocationBarView::PageActionVisibleCount() { |
1409 int result = 0; | 1459 int result = 0; |
1410 for (size_t i = 0; i < page_action_views_.size(); i++) { | 1460 for (size_t i = 0; i < page_action_views_.size(); i++) { |
1411 if (page_action_views_[i]->IsVisible()) | 1461 if (page_action_views_[i]->IsVisible()) |
1412 ++result; | 1462 ++result; |
1413 } | 1463 } |
1414 return result; | 1464 return result; |
1415 } | 1465 } |
| 1466 |
| 1467 ExtensionAction* LocationBarView::GetPageAction(size_t index) { |
| 1468 if (index < page_action_views_.size()) |
| 1469 return page_action_views_[index]->image_view()->page_action(); |
| 1470 |
| 1471 NOTREACHED(); |
| 1472 return NULL; |
| 1473 } |
| 1474 |
| 1475 ExtensionAction* LocationBarView::GetVisiblePageAction(size_t index) { |
| 1476 if (index >= 0) { |
| 1477 size_t current = 0; |
| 1478 for (size_t i = 0; i < page_action_views_.size(); ++i) { |
| 1479 if (page_action_views_[i]->IsVisible()) { |
| 1480 if (current == index) |
| 1481 return page_action_views_[i]->image_view()->page_action(); |
| 1482 |
| 1483 ++current; |
| 1484 } |
| 1485 } |
| 1486 } |
| 1487 |
| 1488 NOTREACHED(); |
| 1489 return NULL; |
| 1490 } |
| 1491 |
| 1492 void LocationBarView::TestPageActionPressed(size_t index) { |
| 1493 if (index >= 0) { |
| 1494 size_t current = 0; |
| 1495 for (size_t i = 0; i < page_action_views_.size(); ++i) { |
| 1496 if (page_action_views_[i]->IsVisible()) { |
| 1497 if (current == index) { |
| 1498 const int button = 1; // Left mouse button. |
| 1499 page_action_views_[i]->image_view()->ExecuteAction(button); |
| 1500 return; |
| 1501 } |
| 1502 ++current; |
| 1503 } |
| 1504 } |
| 1505 } |
| 1506 NOTREACHED(); |
| 1507 } |
OLD | NEW |