| 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 |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 101 AddChildView(image_view_); | 101 AddChildView(image_view_); |
| 102 } | 102 } |
| 103 | 103 |
| 104 void LocationBarView::PageActionWithBadgeView::Layout() { | 104 void LocationBarView::PageActionWithBadgeView::Layout() { |
| 105 image_view_->SetBounds(0, 0, width(), height()); | 105 image_view_->SetBounds(0, 0, width(), height()); |
| 106 } | 106 } |
| 107 | 107 |
| 108 void LocationBarView::PageActionWithBadgeView::PaintChildren( | 108 void LocationBarView::PageActionWithBadgeView::PaintChildren( |
| 109 gfx::Canvas* canvas) { | 109 gfx::Canvas* canvas) { |
| 110 View::PaintChildren(canvas); | 110 View::PaintChildren(canvas); |
| 111 const ExtensionActionState* state = image_view_->GetPageActionState(); | 111 |
| 112 if (state) { | 112 ExtensionAction2* action = image_view_->page_action(); |
| 113 ExtensionActionState::PaintBadge(canvas, gfx::Rect(width(), height()), | 113 int tab_id = image_view_->current_tab_id(); |
| 114 state->badge_text(), | 114 if (tab_id < 0) |
| 115 state->badge_text_color(), | 115 return; |
| 116 state->badge_background_color()); | 116 |
| 117 } | 117 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
| 118 } | 118 } |
| 119 | 119 |
| 120 void LocationBarView::PageActionWithBadgeView::UpdateVisibility( | 120 void LocationBarView::PageActionWithBadgeView::UpdateVisibility( |
| 121 TabContents* contents, const GURL& url) { | 121 TabContents* contents, const GURL& url) { |
| 122 image_view_->UpdateVisibility(contents, url); | 122 image_view_->UpdateVisibility(contents, url); |
| 123 SetVisible(image_view_->IsVisible()); | 123 SetVisible(image_view_->IsVisible()); |
| 124 } | 124 } |
| 125 | 125 |
| 126 | 126 |
| 127 // LocationBarView ----------------------------------------------------------- | 127 // LocationBarView ----------------------------------------------------------- |
| (...skipping 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 677 if (!page_action_views_.empty()) { | 677 if (!page_action_views_.empty()) { |
| 678 for (size_t i = 0; i < page_action_views_.size(); ++i) | 678 for (size_t i = 0; i < page_action_views_.size(); ++i) |
| 679 RemoveChildView(page_action_views_[i]); | 679 RemoveChildView(page_action_views_[i]); |
| 680 STLDeleteContainerPointers(page_action_views_.begin(), | 680 STLDeleteContainerPointers(page_action_views_.begin(), |
| 681 page_action_views_.end()); | 681 page_action_views_.end()); |
| 682 page_action_views_.clear(); | 682 page_action_views_.clear(); |
| 683 } | 683 } |
| 684 } | 684 } |
| 685 | 685 |
| 686 void LocationBarView::RefreshPageActionViews() { | 686 void LocationBarView::RefreshPageActionViews() { |
| 687 std::vector<ExtensionAction*> page_actions; | 687 std::vector<ExtensionAction2*> page_actions; |
| 688 if (profile_->GetExtensionsService()) | 688 ExtensionsService* service = profile_->GetExtensionsService(); |
| 689 page_actions = profile_->GetExtensionsService()->GetPageActions(); | 689 if (!service) |
| 690 return; |
| 690 | 691 |
| 691 // Page actions can be created without an icon, so make sure we count only | 692 for (size_t i = 0; i < service->extensions()->size(); ++i) { |
| 692 // those that have been given an icon. | 693 if (service->extensions()->at(i)->page_action()) |
| 693 for (size_t i = 0; i < page_actions.size();) { | 694 page_actions.push_back(service->extensions()->at(i)->page_action()); |
| 694 if (page_actions[i]->icon_paths().empty()) | |
| 695 page_actions.erase(page_actions.begin() + i); | |
| 696 else | |
| 697 ++i; | |
| 698 } | 695 } |
| 699 | 696 |
| 700 // On startup we sometimes haven't loaded any extensions. This makes sure | 697 // On startup we sometimes haven't loaded any extensions. This makes sure |
| 701 // we catch up when the extensions (and any page actions) load. | 698 // we catch up when the extensions (and any page actions) load. |
| 702 if (page_actions.size() != page_action_views_.size()) { | 699 if (page_actions.size() != page_action_views_.size()) { |
| 703 DeletePageActionViews(); // Delete the old views (if any). | 700 DeletePageActionViews(); // Delete the old views (if any). |
| 704 | 701 |
| 705 page_action_views_.resize(page_actions.size()); | 702 page_action_views_.resize(page_actions.size()); |
| 706 | 703 |
| 707 for (size_t i = 0; i < page_actions.size(); ++i) { | 704 for (size_t i = 0; i < page_actions.size(); ++i) { |
| (...skipping 513 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1221 } | 1218 } |
| 1222 | 1219 |
| 1223 void LocationBarView::SecurityImageView::ShowInfoBubble() { | 1220 void LocationBarView::SecurityImageView::ShowInfoBubble() { |
| 1224 std::wstring text; | 1221 std::wstring text; |
| 1225 model_->GetIconHoverText(&text); | 1222 model_->GetIconHoverText(&text); |
| 1226 ShowInfoBubbleImpl(text, GetColor( | 1223 ShowInfoBubbleImpl(text, GetColor( |
| 1227 model_->GetSecurityLevel() == ToolbarModel::SECURE, | 1224 model_->GetSecurityLevel() == ToolbarModel::SECURE, |
| 1228 SECURITY_INFO_BUBBLE_TEXT)); | 1225 SECURITY_INFO_BUBBLE_TEXT)); |
| 1229 } | 1226 } |
| 1230 | 1227 |
| 1231 void LocationBarView::PageActionImageView::Paint(gfx::Canvas* canvas) { | |
| 1232 LocationBarImageView::Paint(canvas); | |
| 1233 | |
| 1234 TabContents* contents = owner_->delegate_->GetTabContents(); | |
| 1235 if (!contents) | |
| 1236 return; | |
| 1237 | |
| 1238 const ExtensionActionState* state = | |
| 1239 contents->GetPageActionState(page_action_); | |
| 1240 if (state) { | |
| 1241 ExtensionActionState::PaintBadge(canvas, gfx::Rect(width(), height()), | |
| 1242 state->badge_text(), | |
| 1243 state->badge_text_color(), | |
| 1244 state->badge_background_color()); | |
| 1245 } | |
| 1246 } | |
| 1247 | |
| 1248 // PageActionImageView---------------------------------------------------------- | 1228 // PageActionImageView---------------------------------------------------------- |
| 1249 | 1229 |
| 1250 LocationBarView::PageActionImageView::PageActionImageView( | 1230 LocationBarView::PageActionImageView::PageActionImageView( |
| 1251 LocationBarView* owner, | 1231 LocationBarView* owner, |
| 1252 Profile* profile, | 1232 Profile* profile, |
| 1253 const ExtensionAction* page_action, | 1233 ExtensionAction2* page_action, |
| 1254 const BubblePositioner* bubble_positioner) | 1234 const BubblePositioner* bubble_positioner) |
| 1255 : LocationBarImageView(bubble_positioner), | 1235 : LocationBarImageView(bubble_positioner), |
| 1256 owner_(owner), | 1236 owner_(owner), |
| 1257 profile_(profile), | 1237 profile_(profile), |
| 1258 page_action_(page_action), | 1238 page_action_(page_action), |
| 1259 current_tab_id_(-1), | 1239 current_tab_id_(-1) { |
| 1260 tooltip_(page_action_->title()) { | |
| 1261 Extension* extension = profile->GetExtensionsService()->GetExtensionById( | 1240 Extension* extension = profile->GetExtensionsService()->GetExtensionById( |
| 1262 page_action->extension_id()); | 1241 page_action->extension_id()); |
| 1263 DCHECK(extension); | 1242 DCHECK(extension); |
| 1264 | 1243 |
| 1265 // Load the images this view needs asynchronously on the file thread. We'll | 1244 // Load all the icons declared in the manifest. This is the contents of the |
| 1266 // get a call back into OnImageLoaded if the image loads successfully. If not, | 1245 // icons array, plus the default_icon property, if any. |
| 1267 // the ImageView will have no image and will not appear in the Omnibox. | 1246 std::vector<std::string> icon_paths(*page_action->icon_paths()); |
| 1268 DCHECK(!page_action->icon_paths().empty()); | 1247 if (!page_action_->default_icon_path().empty()) |
| 1269 const std::vector<std::string>& icon_paths = page_action->icon_paths(); | 1248 icon_paths.push_back(page_action_->default_icon_path()); |
| 1270 page_action_icons_.resize(icon_paths.size()); | 1249 |
| 1271 tracker_ = new ImageLoadingTracker(this, icon_paths.size()); | 1250 tracker_ = new ImageLoadingTracker(this, icon_paths.size()); |
| 1272 for (std::vector<std::string>::const_iterator iter = icon_paths.begin(); | 1251 for (std::vector<std::string>::iterator iter = icon_paths.begin(); |
| 1273 iter != icon_paths.end(); ++iter) { | 1252 iter != icon_paths.end(); ++iter) { |
| 1274 tracker_->PostLoadImageTask( | 1253 tracker_->PostLoadImageTask( |
| 1275 extension->GetResource(*iter), | 1254 extension->GetResource(*iter), |
| 1276 gfx::Size(Extension::kPageActionIconMaxSize, | 1255 gfx::Size(Extension::kPageActionIconMaxSize, |
| 1277 Extension::kPageActionIconMaxSize)); | 1256 Extension::kPageActionIconMaxSize)); |
| 1278 } | 1257 } |
| 1279 } | 1258 } |
| 1280 | 1259 |
| 1281 LocationBarView::PageActionImageView::~PageActionImageView() { | 1260 LocationBarView::PageActionImageView::~PageActionImageView() { |
| 1282 if (tracker_) | 1261 if (tracker_) |
| 1283 tracker_->StopTrackingImageLoad(); | 1262 tracker_->StopTrackingImageLoad(); |
| 1284 } | 1263 } |
| 1285 | 1264 |
| 1286 bool LocationBarView::PageActionImageView::OnMousePressed( | 1265 bool LocationBarView::PageActionImageView::OnMousePressed( |
| 1287 const views::MouseEvent& event) { | 1266 const views::MouseEvent& event) { |
| 1288 int button = -1; | 1267 int button = -1; |
| 1289 if (event.IsLeftMouseButton()) | 1268 if (event.IsLeftMouseButton()) |
| 1290 button = 1; | 1269 button = 1; |
| 1291 else if (event.IsMiddleMouseButton()) | 1270 else if (event.IsMiddleMouseButton()) |
| 1292 button = 2; | 1271 button = 2; |
| 1293 else if (event.IsRightMouseButton()) | 1272 else if (event.IsRightMouseButton()) |
| 1294 button = 3; | 1273 button = 3; |
| 1295 // Our PageAction icon was clicked on, notify proper authorities. | 1274 // Our PageAction icon was clicked on, notify proper authorities. |
| 1296 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( | 1275 ExtensionBrowserEventRouter::GetInstance()->PageActionExecuted( |
| 1297 profile_, page_action_->extension_id(), page_action_->id(), | 1276 profile_, page_action_->extension_id(), page_action_->id(), |
| 1298 current_tab_id_, current_url_.spec(), button); | 1277 current_tab_id_, current_url_.spec(), button); |
| 1299 return true; | 1278 return true; |
| 1300 } | 1279 } |
| 1301 | 1280 |
| 1302 const ExtensionActionState* | |
| 1303 LocationBarView::PageActionImageView::GetPageActionState() { | |
| 1304 TabContents* contents = owner_->delegate_->GetTabContents(); | |
| 1305 if (!contents) | |
| 1306 return NULL; | |
| 1307 | |
| 1308 return contents->GetPageActionState(page_action_); | |
| 1309 } | |
| 1310 | |
| 1311 void LocationBarView::PageActionImageView::ShowInfoBubble() { | 1281 void LocationBarView::PageActionImageView::ShowInfoBubble() { |
| 1312 ShowInfoBubbleImpl(ASCIIToWide(tooltip_), GetColor(false, TEXT)); | 1282 ShowInfoBubbleImpl(ASCIIToWide(tooltip_), GetColor(false, TEXT)); |
| 1313 } | 1283 } |
| 1314 | 1284 |
| 1315 void LocationBarView::PageActionImageView::OnImageLoaded(SkBitmap* image, | 1285 void LocationBarView::PageActionImageView::OnImageLoaded(SkBitmap* image, |
| 1316 size_t index) { | 1286 size_t index) { |
| 1317 DCHECK(index < page_action_icons_.size()); | 1287 // We loaded icons()->size() icons, plus one extra if the page action had |
| 1318 if (index == page_action_icons_.size() - 1) | 1288 // a default icon. |
| 1319 tracker_ = NULL; // The tracker object will delete itself when we return. | 1289 int total_icons = page_action_->icon_paths()->size(); |
| 1320 if (image) | 1290 if (!page_action_->default_icon_path().empty()) |
| 1321 page_action_icons_[index] = *image; | 1291 total_icons++; |
| 1292 DCHECK(static_cast<int>(index) < total_icons); |
| 1293 |
| 1294 // Map the index of the loaded image back to its name. If we ever get an |
| 1295 // index greater than the number of icons, it must be the default icon. |
| 1296 if (image) { |
| 1297 if (index < page_action_->icon_paths()->size()) |
| 1298 page_action_icons_[page_action_->icon_paths()->at(index)] = *image; |
| 1299 else |
| 1300 page_action_icons_[page_action_->default_icon_path()] = *image; |
| 1301 } |
| 1302 |
| 1303 // If we are done, release the tracker. |
| 1304 if (static_cast<int>(index) == (total_icons - 1)) |
| 1305 tracker_ = NULL; |
| 1306 |
| 1322 owner_->UpdatePageActions(); | 1307 owner_->UpdatePageActions(); |
| 1323 } | 1308 } |
| 1324 | 1309 |
| 1325 void LocationBarView::PageActionImageView::UpdateVisibility( | 1310 void LocationBarView::PageActionImageView::UpdateVisibility( |
| 1326 TabContents* contents, const GURL& url) { | 1311 TabContents* contents, const GURL& url) { |
| 1327 // Save this off so we can pass it back to the extension when the action gets | 1312 // Save this off so we can pass it back to the extension when the action gets |
| 1328 // executed. See PageActionImageView::OnMousePressed. | 1313 // executed. See PageActionImageView::OnMousePressed. |
| 1329 current_tab_id_ = ExtensionTabUtil::GetTabId(contents); | 1314 current_tab_id_ = ExtensionTabUtil::GetTabId(contents); |
| 1330 current_url_ = url; | 1315 current_url_ = url; |
| 1331 | 1316 |
| 1332 const ExtensionActionState* state = | 1317 bool visible = page_action_->GetIsVisible(current_tab_id_); |
| 1333 contents->GetPageActionState(page_action_); | |
| 1334 bool visible = state && !state->hidden(); | |
| 1335 if (visible) { | 1318 if (visible) { |
| 1336 // Set the tooltip. | 1319 // Set the tooltip. |
| 1337 if (state->title().empty()) | 1320 tooltip_ = page_action_->GetTitle(current_tab_id_); |
| 1338 tooltip_ = page_action_->title(); | |
| 1339 else | |
| 1340 tooltip_ = state->title(); | |
| 1341 | 1321 |
| 1342 // Set the image. | 1322 // Set the image. |
| 1343 SkBitmap* icon = state->icon(); | 1323 // It can come from three places. In descending order of priority: |
| 1344 if (!icon) { | 1324 // - The developer can set it dynamically by path or bitmap. It will be in |
| 1345 int index = state->icon_index(); | 1325 // page_action_->GetIcon(). |
| 1346 // The image index (if not within bounds) will be set to the first image. | 1326 // - The developer can set it dynamically by index. It will be in |
| 1347 if (index < 0 || index >= static_cast<int>(page_action_icons_.size())) | 1327 // page_action_->GetIconIndex(). |
| 1348 index = 0; | 1328 // - It can be set in the manifest by path. It will be in page_action_-> |
| 1349 icon = &page_action_icons_[index]; | 1329 // default_icon_path(). |
| 1330 |
| 1331 // First look for a dynamically set bitmap. |
| 1332 SkBitmap icon = page_action_->GetIcon(current_tab_id_); |
| 1333 if (icon.isNull()) { |
| 1334 int icon_index = page_action_->GetIconIndex(current_tab_id_); |
| 1335 std::string icon_path; |
| 1336 if (icon_index >= 0) |
| 1337 icon_path = page_action_->icon_paths()->at(icon_index); |
| 1338 else |
| 1339 icon_path = page_action_->default_icon_path(); |
| 1340 |
| 1341 if (!icon_path.empty()) { |
| 1342 PageActionMap::iterator iter = page_action_icons_.find(icon_path); |
| 1343 if (iter != page_action_icons_.end()) |
| 1344 icon = iter->second; |
| 1345 } |
| 1350 } | 1346 } |
| 1351 ImageView::SetImage(icon); | 1347 |
| 1348 if (!icon.isNull()) |
| 1349 ImageView::SetImage(&icon); |
| 1352 } | 1350 } |
| 1353 SetVisible(visible); | 1351 SetVisible(visible); |
| 1354 } | 1352 } |
| 1355 | 1353 |
| 1356 //////////////////////////////////////////////////////////////////////////////// | 1354 //////////////////////////////////////////////////////////////////////////////// |
| 1357 // LocationBarView, LocationBar implementation: | 1355 // LocationBarView, LocationBar implementation: |
| 1358 | 1356 |
| 1359 void LocationBarView::ShowFirstRunBubble(bool use_OEM_bubble) { | 1357 void LocationBarView::ShowFirstRunBubble(bool use_OEM_bubble) { |
| 1360 // We wait 30 milliseconds to open. It allows less flicker. | 1358 // We wait 30 milliseconds to open. It allows less flicker. |
| 1361 Task* task = first_run_bubble_.NewRunnableMethod( | 1359 Task* task = first_run_bubble_.NewRunnableMethod( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1402 } | 1400 } |
| 1403 | 1401 |
| 1404 int LocationBarView::PageActionVisibleCount() { | 1402 int LocationBarView::PageActionVisibleCount() { |
| 1405 int result = 0; | 1403 int result = 0; |
| 1406 for (size_t i = 0; i < page_action_views_.size(); i++) { | 1404 for (size_t i = 0; i < page_action_views_.size(); i++) { |
| 1407 if (page_action_views_[i]->IsVisible()) | 1405 if (page_action_views_[i]->IsVisible()) |
| 1408 ++result; | 1406 ++result; |
| 1409 } | 1407 } |
| 1410 return result; | 1408 return result; |
| 1411 } | 1409 } |
| OLD | NEW |