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 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" | 5 #import "chrome/browser/ui/cocoa/bookmarks/bookmark_bar_controller.h" |
6 | 6 |
7 #include "base/mac/bundle_locations.h" | 7 #include "base/mac/bundle_locations.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 11 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
12 #include "chrome/browser/bookmarks/bookmark_stats.h" | 12 #include "chrome/browser/bookmarks/bookmark_stats.h" |
13 #include "chrome/browser/bookmarks/chrome_bookmark_client.h" | |
14 #include "chrome/browser/bookmarks/chrome_bookmark_client_factory.h" | |
15 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 13 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
16 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/themes/theme_properties.h" | 15 #include "chrome/browser/themes/theme_properties.h" |
18 #include "chrome/browser/themes/theme_service.h" | 16 #include "chrome/browser/themes/theme_service.h" |
19 #import "chrome/browser/themes/theme_service_factory.h" | 17 #import "chrome/browser/themes/theme_service_factory.h" |
20 #include "chrome/browser/ui/bookmarks/bookmark_editor.h" | 18 #include "chrome/browser/ui/bookmarks/bookmark_editor.h" |
21 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" | 19 #include "chrome/browser/ui/bookmarks/bookmark_utils.h" |
22 #include "chrome/browser/ui/browser.h" | 20 #include "chrome/browser/ui/browser.h" |
23 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
24 #include "chrome/browser/ui/chrome_pages.h" | 22 #include "chrome/browser/ui/chrome_pages.h" |
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 delegate:(id<BookmarkBarControllerDelegate>)delegate | 250 delegate:(id<BookmarkBarControllerDelegate>)delegate |
253 resizeDelegate:(id<ViewResizer>)resizeDelegate { | 251 resizeDelegate:(id<ViewResizer>)resizeDelegate { |
254 if ((self = [super initWithNibName:@"BookmarkBar" | 252 if ((self = [super initWithNibName:@"BookmarkBar" |
255 bundle:base::mac::FrameworkBundle()])) { | 253 bundle:base::mac::FrameworkBundle()])) { |
256 currentState_ = BookmarkBar::HIDDEN; | 254 currentState_ = BookmarkBar::HIDDEN; |
257 lastState_ = BookmarkBar::HIDDEN; | 255 lastState_ = BookmarkBar::HIDDEN; |
258 | 256 |
259 browser_ = browser; | 257 browser_ = browser; |
260 initialWidth_ = initialWidth; | 258 initialWidth_ = initialWidth; |
261 bookmarkModel_ = BookmarkModelFactory::GetForProfile(browser_->profile()); | 259 bookmarkModel_ = BookmarkModelFactory::GetForProfile(browser_->profile()); |
262 bookmarkClient_ = | |
263 ChromeBookmarkClientFactory::GetForProfile(browser_->profile()); | |
264 buttons_.reset([[NSMutableArray alloc] init]); | 260 buttons_.reset([[NSMutableArray alloc] init]); |
265 delegate_ = delegate; | 261 delegate_ = delegate; |
266 resizeDelegate_ = resizeDelegate; | 262 resizeDelegate_ = resizeDelegate; |
267 folderTarget_.reset( | 263 folderTarget_.reset( |
268 [[BookmarkFolderTarget alloc] initWithController:self | 264 [[BookmarkFolderTarget alloc] initWithController:self |
269 profile:browser_->profile()]); | 265 profile:browser_->profile()]); |
270 | 266 |
271 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 267 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
272 folderImage_.reset( | 268 folderImage_.reset( |
273 rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER).CopyNSImage()); | 269 rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER).CopyNSImage()); |
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
588 } | 584 } |
589 | 585 |
590 // In general, only show the divider when it's in the normal showing state. | 586 // In general, only show the divider when it's in the normal showing state. |
591 return [self isInState:BookmarkBar::SHOW] ? 0 : 1; | 587 return [self isInState:BookmarkBar::SHOW] ? 0 : 1; |
592 } | 588 } |
593 | 589 |
594 - (NSImage*)faviconForNode:(const BookmarkNode*)node { | 590 - (NSImage*)faviconForNode:(const BookmarkNode*)node { |
595 if (!node) | 591 if (!node) |
596 return defaultImage_; | 592 return defaultImage_; |
597 | 593 |
598 if (node == bookmarkClient_->managed_node()) { | 594 if (node == bookmarkModel_->managed_node()) { |
599 // Most users never see this node, so the image is only loaded if needed. | 595 // Most users never see this node, so the image is only loaded if needed. |
600 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 596 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
601 return rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER_MANAGED).ToNSImage(); | 597 return rb.GetNativeImageNamed(IDR_BOOKMARK_BAR_FOLDER_MANAGED).ToNSImage(); |
602 } | 598 } |
603 | 599 |
604 if (node == bookmarkClient_->supervised_node()) { | 600 if (node == bookmarkModel_->supervised_node()) { |
605 // Most users never see this node, so the image is only loaded if needed. | 601 // Most users never see this node, so the image is only loaded if needed. |
606 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); | 602 ResourceBundle& rb = ResourceBundle::GetSharedInstance(); |
607 return rb.GetNativeImageNamed( | 603 return rb.GetNativeImageNamed( |
608 IDR_BOOKMARK_BAR_FOLDER_SUPERVISED).ToNSImage(); | 604 IDR_BOOKMARK_BAR_FOLDER_SUPERVISED).ToNSImage(); |
609 } | 605 } |
610 | 606 |
611 if (node->is_folder()) | 607 if (node->is_folder()) |
612 return folderImage_; | 608 return folderImage_; |
613 | 609 |
614 const gfx::Image& favicon = bookmarkModel_->GetFavicon(node); | 610 const gfx::Image& favicon = bookmarkModel_->GetFavicon(node); |
615 if (!favicon.IsEmpty()) | 611 if (!favicon.IsEmpty()) |
616 return favicon.ToNSImage(); | 612 return favicon.ToNSImage(); |
617 | 613 |
618 return defaultImage_; | 614 return defaultImage_; |
619 } | 615 } |
620 | 616 |
621 - (void)closeFolderAndStopTrackingMenus { | 617 - (void)closeFolderAndStopTrackingMenus { |
622 showFolderMenus_ = NO; | 618 showFolderMenus_ = NO; |
623 [self closeAllBookmarkFolders]; | 619 [self closeAllBookmarkFolders]; |
624 } | 620 } |
625 | 621 |
626 - (BOOL)canEditBookmarks { | 622 - (BOOL)canEditBookmarks { |
627 PrefService* prefs = browser_->profile()->GetPrefs(); | 623 PrefService* prefs = browser_->profile()->GetPrefs(); |
628 return prefs->GetBoolean(bookmarks::prefs::kEditBookmarksEnabled); | 624 return prefs->GetBoolean(bookmarks::prefs::kEditBookmarksEnabled); |
629 } | 625 } |
630 | 626 |
631 - (BOOL)canEditBookmark:(const BookmarkNode*)node { | 627 - (BOOL)canEditBookmark:(const BookmarkNode*)node { |
632 // Don't allow edit/delete of the permanent nodes. | 628 // Don't allow edit/delete of the permanent nodes. |
633 if (node == nil || bookmarkModel_->is_permanent_node(node) || | 629 if (node == nil || bookmarkModel_->is_permanent_node(node) || |
634 !bookmarkClient_->CanBeEditedByUser(node)) { | 630 !bookmarkModel_->CanBeEditedByUser(node)) { |
635 return NO; | 631 return NO; |
636 } | 632 } |
637 return YES; | 633 return YES; |
638 } | 634 } |
639 | 635 |
640 #pragma mark Actions | 636 #pragma mark Actions |
641 | 637 |
642 // Helper methods called on the main thread by runMenuFlashThread. | 638 // Helper methods called on the main thread by runMenuFlashThread. |
643 | 639 |
644 - (void)setButtonFlashStateOn:(id)sender { | 640 - (void)setButtonFlashStateOn:(id)sender { |
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1285 // until the end of the current event loop. | 1281 // until the end of the current event loop. |
1286 [[button retain] autorelease]; | 1282 [[button retain] autorelease]; |
1287 return button; | 1283 return button; |
1288 } | 1284 } |
1289 | 1285 |
1290 // Creates the button for "Managed Bookmarks", but does not position it. | 1286 // Creates the button for "Managed Bookmarks", but does not position it. |
1291 - (void)createManagedBookmarksButton { | 1287 - (void)createManagedBookmarksButton { |
1292 if (managedBookmarksButton_.get()) { | 1288 if (managedBookmarksButton_.get()) { |
1293 // The node's title might have changed if the user signed in or out. | 1289 // The node's title might have changed if the user signed in or out. |
1294 // Make sure it's up to date now. | 1290 // Make sure it's up to date now. |
1295 const BookmarkNode* node = bookmarkClient_->managed_node(); | 1291 const BookmarkNode* node = bookmarkModel_->managed_node(); |
1296 NSString* title = base::SysUTF16ToNSString(node->GetTitle()); | 1292 NSString* title = base::SysUTF16ToNSString(node->GetTitle()); |
1297 NSCell* cell = [managedBookmarksButton_ cell]; | 1293 NSCell* cell = [managedBookmarksButton_ cell]; |
1298 [cell setTitle:title]; | 1294 [cell setTitle:title]; |
1299 | 1295 |
1300 // Its visibility may have changed too. | 1296 // Its visibility may have changed too. |
1301 [self setManagedBookmarksButtonVisibility]; | 1297 [self setManagedBookmarksButtonVisibility]; |
1302 | 1298 |
1303 return; | 1299 return; |
1304 } | 1300 } |
1305 | 1301 |
1306 NSCell* cell = [self cellForBookmarkNode:bookmarkClient_->managed_node()]; | 1302 NSCell* cell = [self cellForBookmarkNode:bookmarkModel_->managed_node()]; |
1307 managedBookmarksButton_.reset([self createCustomBookmarkButtonForCell:cell]); | 1303 managedBookmarksButton_.reset([self createCustomBookmarkButtonForCell:cell]); |
1308 [managedBookmarksButton_ setAction:@selector(openBookmarkFolderFromButton:)]; | 1304 [managedBookmarksButton_ setAction:@selector(openBookmarkFolderFromButton:)]; |
1309 view_id_util::SetID(managedBookmarksButton_.get(), VIEW_ID_MANAGED_BOOKMARKS); | 1305 view_id_util::SetID(managedBookmarksButton_.get(), VIEW_ID_MANAGED_BOOKMARKS); |
1310 [buttonView_ addSubview:managedBookmarksButton_.get()]; | 1306 [buttonView_ addSubview:managedBookmarksButton_.get()]; |
1311 | 1307 |
1312 [self setManagedBookmarksButtonVisibility]; | 1308 [self setManagedBookmarksButtonVisibility]; |
1313 } | 1309 } |
1314 | 1310 |
1315 // Creates the button for "Supervised Bookmarks", but does not position it. | 1311 // Creates the button for "Supervised Bookmarks", but does not position it. |
1316 - (void)createSupervisedBookmarksButton { | 1312 - (void)createSupervisedBookmarksButton { |
1317 if (supervisedBookmarksButton_.get()) { | 1313 if (supervisedBookmarksButton_.get()) { |
1318 // The button's already there, but its visibility may have changed. | 1314 // The button's already there, but its visibility may have changed. |
1319 [self setSupervisedBookmarksButtonVisibility]; | 1315 [self setSupervisedBookmarksButtonVisibility]; |
1320 return; | 1316 return; |
1321 } | 1317 } |
1322 | 1318 |
1323 NSCell* cell = [self cellForBookmarkNode:bookmarkClient_->supervised_node()]; | 1319 NSCell* cell = [self cellForBookmarkNode:bookmarkModel_->supervised_node()]; |
1324 supervisedBookmarksButton_.reset( | 1320 supervisedBookmarksButton_.reset( |
1325 [self createCustomBookmarkButtonForCell:cell]); | 1321 [self createCustomBookmarkButtonForCell:cell]); |
1326 [supervisedBookmarksButton_ | 1322 [supervisedBookmarksButton_ |
1327 setAction:@selector(openBookmarkFolderFromButton:)]; | 1323 setAction:@selector(openBookmarkFolderFromButton:)]; |
1328 view_id_util::SetID(supervisedBookmarksButton_.get(), | 1324 view_id_util::SetID(supervisedBookmarksButton_.get(), |
1329 VIEW_ID_SUPERVISED_BOOKMARKS); | 1325 VIEW_ID_SUPERVISED_BOOKMARKS); |
1330 [buttonView_ addSubview:supervisedBookmarksButton_.get()]; | 1326 [buttonView_ addSubview:supervisedBookmarksButton_.get()]; |
1331 | 1327 |
1332 [self setSupervisedBookmarksButtonVisibility]; | 1328 [self setSupervisedBookmarksButtonVisibility]; |
1333 } | 1329 } |
(...skipping 743 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2077 if ([button isFolder]) { | 2073 if ([button isFolder]) { |
2078 destParent = [button bookmarkNode]; | 2074 destParent = [button bookmarkNode]; |
2079 // Drop it at the end. | 2075 // Drop it at the end. |
2080 destIndex = [button bookmarkNode]->child_count(); | 2076 destIndex = [button bookmarkNode]->child_count(); |
2081 } else { | 2077 } else { |
2082 // Else we're dropping somewhere on the bar, so find the right spot. | 2078 // Else we're dropping somewhere on the bar, so find the right spot. |
2083 destParent = bookmarkModel_->bookmark_bar_node(); | 2079 destParent = bookmarkModel_->bookmark_bar_node(); |
2084 destIndex = [self indexForDragToPoint:point]; | 2080 destIndex = [self indexForDragToPoint:point]; |
2085 } | 2081 } |
2086 | 2082 |
2087 if (!bookmarkClient_->CanBeEditedByUser(destParent)) | 2083 if (!bookmarkModel_->CanBeEditedByUser(destParent)) |
2088 return NO; | 2084 return NO; |
2089 if (!bookmarkClient_->CanBeEditedByUser(sourceNode)) | 2085 if (!bookmarkModel_->CanBeEditedByUser(sourceNode)) |
2090 copy = YES; | 2086 copy = YES; |
2091 | 2087 |
2092 // Be sure we don't try and drop a folder into itself. | 2088 // Be sure we don't try and drop a folder into itself. |
2093 if (sourceNode != destParent) { | 2089 if (sourceNode != destParent) { |
2094 if (copy) | 2090 if (copy) |
2095 bookmarkModel_->Copy(sourceNode, destParent, destIndex); | 2091 bookmarkModel_->Copy(sourceNode, destParent, destIndex); |
2096 else | 2092 else |
2097 bookmarkModel_->Move(sourceNode, destParent, destIndex); | 2093 bookmarkModel_->Move(sourceNode, destParent, destIndex); |
2098 } | 2094 } |
2099 | 2095 |
(...skipping 696 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2796 if ([button isFolder]) { | 2792 if ([button isFolder]) { |
2797 destParent = [button bookmarkNode]; | 2793 destParent = [button bookmarkNode]; |
2798 // Drop it at the end. | 2794 // Drop it at the end. |
2799 destIndex = [button bookmarkNode]->child_count(); | 2795 destIndex = [button bookmarkNode]->child_count(); |
2800 } else { | 2796 } else { |
2801 // Else we're dropping somewhere on the bar, so find the right spot. | 2797 // Else we're dropping somewhere on the bar, so find the right spot. |
2802 destParent = bookmarkModel_->bookmark_bar_node(); | 2798 destParent = bookmarkModel_->bookmark_bar_node(); |
2803 destIndex = [self indexForDragToPoint:point]; | 2799 destIndex = [self indexForDragToPoint:point]; |
2804 } | 2800 } |
2805 | 2801 |
2806 if (!bookmarkClient_->CanBeEditedByUser(destParent)) | 2802 if (!bookmarkModel_->CanBeEditedByUser(destParent)) |
2807 return NO; | 2803 return NO; |
2808 | 2804 |
2809 // Don't add the bookmarks if the destination index shows an error. | 2805 // Don't add the bookmarks if the destination index shows an error. |
2810 if (destIndex >= 0) { | 2806 if (destIndex >= 0) { |
2811 // Create and add the new bookmark nodes. | 2807 // Create and add the new bookmark nodes. |
2812 size_t urlCount = [urls count]; | 2808 size_t urlCount = [urls count]; |
2813 for (size_t i = 0; i < urlCount; ++i) { | 2809 for (size_t i = 0; i < urlCount; ++i) { |
2814 GURL gurl; | 2810 GURL gurl; |
2815 const char* string = [[urls objectAtIndex:i] UTF8String]; | 2811 const char* string = [[urls objectAtIndex:i] UTF8String]; |
2816 if (string) | 2812 if (string) |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2902 - (id<BookmarkButtonControllerProtocol>)controllerForNode: | 2898 - (id<BookmarkButtonControllerProtocol>)controllerForNode: |
2903 (const BookmarkNode*)node { | 2899 (const BookmarkNode*)node { |
2904 // See if it's in the bar, then if it is in the hierarchy of visible | 2900 // See if it's in the bar, then if it is in the hierarchy of visible |
2905 // folder menus. | 2901 // folder menus. |
2906 if (bookmarkModel_->bookmark_bar_node() == node) | 2902 if (bookmarkModel_->bookmark_bar_node() == node) |
2907 return self; | 2903 return self; |
2908 return [folderController_ controllerForNode:node]; | 2904 return [folderController_ controllerForNode:node]; |
2909 } | 2905 } |
2910 | 2906 |
2911 @end | 2907 @end |
OLD | NEW |