| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "ios/chrome/browser/ui/bookmarks/bookmark_interaction_controller.h" | 5 #import "ios/chrome/browser/ui/bookmarks/bookmark_interaction_controller.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #import "base/ios/weak_nsobject.h" | |
| 10 #include "base/logging.h" | 9 #include "base/logging.h" |
| 11 #include "base/mac/bind_objc_block.h" | 10 #include "base/mac/bind_objc_block.h" |
| 12 #include "base/mac/objc_property_releaser.h" | |
| 13 #include "base/mac/scoped_nsobject.h" | |
| 14 #include "base/metrics/user_metrics.h" | 11 #include "base/metrics/user_metrics.h" |
| 15 #include "base/metrics/user_metrics_action.h" | 12 #include "base/metrics/user_metrics_action.h" |
| 16 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 17 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
| 18 #include "base/time/time.h" | 15 #include "base/time/time.h" |
| 19 #include "components/bookmarks/browser/bookmark_model.h" | 16 #include "components/bookmarks/browser/bookmark_model.h" |
| 20 #include "components/bookmarks/browser/bookmark_utils.h" | 17 #include "components/bookmarks/browser/bookmark_utils.h" |
| 21 #include "components/pref_registry/pref_registry_syncable.h" | 18 #include "components/pref_registry/pref_registry_syncable.h" |
| 22 #include "components/prefs/pref_service.h" | 19 #include "components/prefs/pref_service.h" |
| 23 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" | 20 #include "ios/chrome/browser/bookmarks/bookmark_model_factory.h" |
| 24 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" | 21 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 25 #import "ios/chrome/browser/metrics/new_tab_page_uma.h" | 22 #import "ios/chrome/browser/metrics/new_tab_page_uma.h" |
| 26 #include "ios/chrome/browser/pref_names.h" | 23 #include "ios/chrome/browser/pref_names.h" |
| 27 #import "ios/chrome/browser/tabs/tab.h" | 24 #import "ios/chrome/browser/tabs/tab.h" |
| 28 #import "ios/chrome/browser/ui/bookmarks/bookmark_controller_factory.h" | 25 #import "ios/chrome/browser/ui/bookmarks/bookmark_controller_factory.h" |
| 29 #import "ios/chrome/browser/ui/bookmarks/bookmark_edit_view_controller.h" | 26 #import "ios/chrome/browser/ui/bookmarks/bookmark_edit_view_controller.h" |
| 30 #import "ios/chrome/browser/ui/bookmarks/bookmark_home_view_controller.h" | 27 #import "ios/chrome/browser/ui/bookmarks/bookmark_home_view_controller.h" |
| 31 #import "ios/chrome/browser/ui/bookmarks/bookmark_navigation_controller.h" | 28 #import "ios/chrome/browser/ui/bookmarks/bookmark_navigation_controller.h" |
| 32 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" | 29 #import "ios/chrome/browser/ui/bookmarks/bookmark_utils_ios.h" |
| 33 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 30 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 34 #include "ios/chrome/browser/ui/url_loader.h" | 31 #include "ios/chrome/browser/ui/url_loader.h" |
| 35 #include "ios/chrome/grit/ios_strings.h" | 32 #include "ios/chrome/grit/ios_strings.h" |
| 36 #import "ios/third_party/material_components_ios/src/components/Snackbar/src/Mat
erialSnackbar.h" | 33 #import "ios/third_party/material_components_ios/src/components/Snackbar/src/Mat
erialSnackbar.h" |
| 37 #include "ios/web/public/referrer.h" | 34 #include "ios/web/public/referrer.h" |
| 38 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
| 39 | 36 |
| 37 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 38 #error "This file requires ARC support." |
| 39 #endif |
| 40 |
| 40 using bookmarks::BookmarkModel; | 41 using bookmarks::BookmarkModel; |
| 41 using bookmarks::BookmarkNode; | 42 using bookmarks::BookmarkNode; |
| 42 | 43 |
| 43 namespace { | 44 namespace { |
| 44 const int64_t kLastUsedFolderNone = -1; | 45 const int64_t kLastUsedFolderNone = -1; |
| 45 } // namespace | 46 } // namespace |
| 46 | 47 |
| 47 @interface BookmarkInteractionController ()< | 48 @interface BookmarkInteractionController ()< |
| 48 BookmarkEditViewControllerDelegate, | 49 BookmarkEditViewControllerDelegate, |
| 49 BookmarkHomeViewControllerDelegate> { | 50 BookmarkHomeViewControllerDelegate> { |
| 50 // The browser state of the current user. | 51 // The browser state of the current user. |
| 51 ios::ChromeBrowserState* _currentBrowserState; // weak | 52 ios::ChromeBrowserState* _currentBrowserState; // weak |
| 52 | 53 |
| 53 // The browser state to use, might be different from _currentBrowserState if | 54 // The browser state to use, might be different from _currentBrowserState if |
| 54 // it is incognito. | 55 // it is incognito. |
| 55 ios::ChromeBrowserState* _browserState; // weak | 56 ios::ChromeBrowserState* _browserState; // weak |
| 56 | 57 |
| 57 // The designated url loader. | 58 // The designated url loader. |
| 58 base::WeakNSProtocol<id<UrlLoader>> _loader; | 59 __weak id<UrlLoader> _loader; |
| 59 | 60 |
| 60 // The parent controller on top of which the UI needs to be presented. | 61 // The parent controller on top of which the UI needs to be presented. |
| 61 base::WeakNSObject<UIViewController> _parentController; | 62 __weak UIViewController* _parentController; |
| 62 | |
| 63 base::mac::ObjCPropertyReleaser | |
| 64 _propertyReleaser_BookmarkInteractionController; | |
| 65 } | 63 } |
| 66 | 64 |
| 67 // The bookmark model in use. | 65 // The bookmark model in use. |
| 68 @property(nonatomic, assign) BookmarkModel* bookmarkModel; | 66 @property(nonatomic, assign) BookmarkModel* bookmarkModel; |
| 69 | 67 |
| 70 // A reference to the potentially presented bookmark browser. | 68 // A reference to the potentially presented bookmark browser. |
| 71 @property(nonatomic, retain) BookmarkHomeViewController* bookmarkBrowser; | 69 @property(nonatomic, strong) BookmarkHomeViewController* bookmarkBrowser; |
| 72 | 70 |
| 73 // A reference to the potentially presented single bookmark editor. | 71 // A reference to the potentially presented single bookmark editor. |
| 74 @property(nonatomic, retain) BookmarkEditViewController* bookmarkEditor; | 72 @property(nonatomic, strong) BookmarkEditViewController* bookmarkEditor; |
| 75 | 73 |
| 76 // The user wants to bookmark the current tab. | 74 // The user wants to bookmark the current tab. |
| 77 - (void)addBookmarkForTab:(Tab*)tab; | 75 - (void)addBookmarkForTab:(Tab*)tab; |
| 78 | 76 |
| 79 // Builds a controller and brings it on screen. | 77 // Builds a controller and brings it on screen. |
| 80 - (void)presentBookmarkForTab:(Tab*)tab; | 78 - (void)presentBookmarkForTab:(Tab*)tab; |
| 81 | 79 |
| 82 // Dismisses the bookmark browser. | 80 // Dismisses the bookmark browser. |
| 83 - (void)dismissBookmarkBrowserAnimated:(BOOL)animated; | 81 - (void)dismissBookmarkBrowserAnimated:(BOOL)animated; |
| 84 | 82 |
| 85 // Dismisses the bookmark editor. | 83 // Dismisses the bookmark editor. |
| 86 - (void)dismissBookmarkEditorAnimated:(BOOL)animated; | 84 - (void)dismissBookmarkEditorAnimated:(BOOL)animated; |
| 87 | 85 |
| 88 @end | 86 @end |
| 89 | 87 |
| 90 @implementation BookmarkInteractionController | 88 @implementation BookmarkInteractionController |
| 91 | |
| 92 @synthesize bookmarkBrowser = _bookmarkBrowser; | 89 @synthesize bookmarkBrowser = _bookmarkBrowser; |
| 93 @synthesize bookmarkEditor = _bookmarkEditor; | 90 @synthesize bookmarkEditor = _bookmarkEditor; |
| 94 @synthesize bookmarkModel = _bookmarkModel; | 91 @synthesize bookmarkModel = _bookmarkModel; |
| 95 | 92 |
| 96 + (void)registerBrowserStatePrefs:(user_prefs::PrefRegistrySyncable*)registry { | 93 + (void)registerBrowserStatePrefs:(user_prefs::PrefRegistrySyncable*)registry { |
| 97 registry->RegisterInt64Pref(prefs::kIosBookmarkFolderDefault, | 94 registry->RegisterInt64Pref(prefs::kIosBookmarkFolderDefault, |
| 98 kLastUsedFolderNone); | 95 kLastUsedFolderNone); |
| 99 } | 96 } |
| 100 | 97 |
| 101 + (const BookmarkNode*)folderForNewBookmarksInBrowserState: | 98 + (const BookmarkNode*)folderForNewBookmarksInBrowserState: |
| (...skipping 20 matching lines...) Expand all Loading... |
| 122 DCHECK(folder && folder->is_folder()); | 119 DCHECK(folder && folder->is_folder()); |
| 123 browserState->GetPrefs()->SetInt64(prefs::kIosBookmarkFolderDefault, | 120 browserState->GetPrefs()->SetInt64(prefs::kIosBookmarkFolderDefault, |
| 124 folder->id()); | 121 folder->id()); |
| 125 } | 122 } |
| 126 | 123 |
| 127 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState | 124 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState |
| 128 loader:(id<UrlLoader>)loader | 125 loader:(id<UrlLoader>)loader |
| 129 parentController:(UIViewController*)parentController { | 126 parentController:(UIViewController*)parentController { |
| 130 self = [super init]; | 127 self = [super init]; |
| 131 if (self) { | 128 if (self) { |
| 132 _propertyReleaser_BookmarkInteractionController.Init( | |
| 133 self, [BookmarkInteractionController class]); | |
| 134 // Bookmarks are always opened with the main browser state, even in | 129 // Bookmarks are always opened with the main browser state, even in |
| 135 // incognito mode. | 130 // incognito mode. |
| 136 _currentBrowserState = browserState; | 131 _currentBrowserState = browserState; |
| 137 _browserState = browserState->GetOriginalChromeBrowserState(); | 132 _browserState = browserState->GetOriginalChromeBrowserState(); |
| 138 _loader.reset(loader); | 133 _loader = loader; |
| 139 _parentController.reset(parentController); | 134 _parentController = parentController; |
| 140 _bookmarkModel = | 135 _bookmarkModel = |
| 141 ios::BookmarkModelFactory::GetForBrowserState(_browserState); | 136 ios::BookmarkModelFactory::GetForBrowserState(_browserState); |
| 142 DCHECK(_bookmarkModel); | 137 DCHECK(_bookmarkModel); |
| 143 DCHECK(_parentController); | 138 DCHECK(_parentController); |
| 144 } | 139 } |
| 145 return self; | 140 return self; |
| 146 } | 141 } |
| 147 | 142 |
| 148 - (void)dealloc { | 143 - (void)dealloc { |
| 149 _bookmarkBrowser.delegate = nil; | 144 _bookmarkBrowser.delegate = nil; |
| 150 _bookmarkEditor.delegate = nil; | 145 _bookmarkEditor.delegate = nil; |
| 151 [super dealloc]; | |
| 152 } | 146 } |
| 153 | 147 |
| 154 - (void)addBookmarkForTab:(Tab*)tab { | 148 - (void)addBookmarkForTab:(Tab*)tab { |
| 155 base::RecordAction(base::UserMetricsAction("BookmarkAdded")); | 149 base::RecordAction(base::UserMetricsAction("BookmarkAdded")); |
| 156 const BookmarkNode* defaultFolder = | 150 const BookmarkNode* defaultFolder = |
| 157 [[self class] folderForNewBookmarksInBrowserState:_browserState]; | 151 [[self class] folderForNewBookmarksInBrowserState:_browserState]; |
| 158 self.bookmarkModel->AddURL(defaultFolder, defaultFolder->child_count(), | 152 self.bookmarkModel->AddURL(defaultFolder, defaultFolder->child_count(), |
| 159 base::SysNSStringToUTF16(tab.title), tab.url); | 153 base::SysNSStringToUTF16(tab.title), tab.url); |
| 160 | 154 |
| 161 MDCSnackbarMessageAction* action = | 155 MDCSnackbarMessageAction* action = [[MDCSnackbarMessageAction alloc] init]; |
| 162 [[[MDCSnackbarMessageAction alloc] init] autorelease]; | 156 __weak BookmarkInteractionController* weakSelf = self; |
| 163 base::WeakNSObject<BookmarkInteractionController> weakSelf(self); | 157 __weak Tab* weakTab = tab; |
| 164 base::WeakNSObject<Tab> weakTab(tab); | |
| 165 action.handler = ^{ | 158 action.handler = ^{ |
| 166 base::scoped_nsobject<BookmarkInteractionController> strongSelf( | 159 BookmarkInteractionController* strongSelf = weakSelf; |
| 167 [weakSelf retain]); | |
| 168 if (!strongSelf || !weakTab) | 160 if (!strongSelf || !weakTab) |
| 169 return; | 161 return; |
| 170 [strongSelf presentBookmarkForTab:weakTab]; | 162 [strongSelf presentBookmarkForTab:weakTab]; |
| 171 }; | 163 }; |
| 172 action.title = l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON); | 164 action.title = l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON); |
| 173 action.accessibilityIdentifier = @"Edit"; | 165 action.accessibilityIdentifier = @"Edit"; |
| 174 | 166 |
| 175 NSString* folderTitle = | 167 NSString* folderTitle = |
| 176 bookmark_utils_ios::TitleForBookmarkNode(defaultFolder); | 168 bookmark_utils_ios::TitleForBookmarkNode(defaultFolder); |
| 177 NSString* text = | 169 NSString* text = |
| (...skipping 13 matching lines...) Expand all Loading... |
| 191 DCHECK(!self.bookmarkBrowser && !self.bookmarkEditor); | 183 DCHECK(!self.bookmarkBrowser && !self.bookmarkEditor); |
| 192 DCHECK(tab); | 184 DCHECK(tab); |
| 193 | 185 |
| 194 const BookmarkNode* bookmark = | 186 const BookmarkNode* bookmark = |
| 195 self.bookmarkModel->GetMostRecentlyAddedUserNodeForURL(tab.url); | 187 self.bookmarkModel->GetMostRecentlyAddedUserNodeForURL(tab.url); |
| 196 if (!bookmark) | 188 if (!bookmark) |
| 197 return; | 189 return; |
| 198 | 190 |
| 199 [self dismissSnackbar]; | 191 [self dismissSnackbar]; |
| 200 | 192 |
| 201 base::scoped_nsobject<BookmarkEditViewController> bookmarkEditor( | 193 BookmarkEditViewController* bookmarkEditor = |
| 202 [[BookmarkEditViewController alloc] initWithBookmark:bookmark | 194 [[BookmarkEditViewController alloc] initWithBookmark:bookmark |
| 203 browserState:_browserState]); | 195 browserState:_browserState]; |
| 204 self.bookmarkEditor = bookmarkEditor; | 196 self.bookmarkEditor = bookmarkEditor; |
| 205 self.bookmarkEditor.delegate = self; | 197 self.bookmarkEditor.delegate = self; |
| 206 base::scoped_nsobject<UINavigationController> navController( | 198 UINavigationController* navController = [[BookmarkNavigationController alloc] |
| 207 [[BookmarkNavigationController alloc] | 199 initWithRootViewController:self.bookmarkEditor]; |
| 208 initWithRootViewController:self.bookmarkEditor]); | 200 navController.modalPresentationStyle = UIModalPresentationFormSheet; |
| 209 navController.get().modalPresentationStyle = UIModalPresentationFormSheet; | |
| 210 [_parentController presentViewController:navController | 201 [_parentController presentViewController:navController |
| 211 animated:YES | 202 animated:YES |
| 212 completion:nil]; | 203 completion:nil]; |
| 213 } | 204 } |
| 214 | 205 |
| 215 - (void)presentBookmarkForTab:(Tab*)tab | 206 - (void)presentBookmarkForTab:(Tab*)tab |
| 216 currentlyBookmarked:(BOOL)bookmarked | 207 currentlyBookmarked:(BOOL)bookmarked |
| 217 inView:(UIView*)parentView | 208 inView:(UIView*)parentView |
| 218 originRect:(CGRect)origin { | 209 originRect:(CGRect)origin { |
| 219 if (!self.bookmarkModel->loaded()) | 210 if (!self.bookmarkModel->loaded()) |
| 220 return; | 211 return; |
| 221 if (!tab) | 212 if (!tab) |
| 222 return; | 213 return; |
| 223 | 214 |
| 224 if (bookmarked) | 215 if (bookmarked) |
| 225 [self presentBookmarkForTab:tab]; | 216 [self presentBookmarkForTab:tab]; |
| 226 else | 217 else |
| 227 [self addBookmarkForTab:tab]; | 218 [self addBookmarkForTab:tab]; |
| 228 } | 219 } |
| 229 | 220 |
| 230 - (void)presentBookmarks { | 221 - (void)presentBookmarks { |
| 231 DCHECK(!self.bookmarkBrowser && !self.bookmarkEditor); | 222 DCHECK(!self.bookmarkBrowser && !self.bookmarkEditor); |
| 232 base::scoped_nsobject<BookmarkControllerFactory> bookmarkControllerFactory( | 223 BookmarkControllerFactory* bookmarkControllerFactory = |
| 233 [[BookmarkControllerFactory alloc] init]); | 224 [[BookmarkControllerFactory alloc] init]; |
| 234 self.bookmarkBrowser = [bookmarkControllerFactory | 225 self.bookmarkBrowser = [bookmarkControllerFactory |
| 235 bookmarkControllerWithBrowserState:_currentBrowserState | 226 bookmarkControllerWithBrowserState:_currentBrowserState |
| 236 loader:_loader]; | 227 loader:_loader]; |
| 237 self.bookmarkBrowser.delegate = self; | 228 self.bookmarkBrowser.delegate = self; |
| 238 self.bookmarkBrowser.modalPresentationStyle = UIModalPresentationFormSheet; | 229 self.bookmarkBrowser.modalPresentationStyle = UIModalPresentationFormSheet; |
| 239 [_parentController presentViewController:self.bookmarkBrowser | 230 [_parentController presentViewController:self.bookmarkBrowser |
| 240 animated:YES | 231 animated:YES |
| 241 completion:nil]; | 232 completion:nil]; |
| 242 } | 233 } |
| 243 | 234 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 306 } else { | 297 } else { |
| 307 [_loader loadURL:url | 298 [_loader loadURL:url |
| 308 referrer:web::Referrer() | 299 referrer:web::Referrer() |
| 309 transition:ui::PAGE_TRANSITION_AUTO_BOOKMARK | 300 transition:ui::PAGE_TRANSITION_AUTO_BOOKMARK |
| 310 rendererInitiated:NO]; | 301 rendererInitiated:NO]; |
| 311 } | 302 } |
| 312 } | 303 } |
| 313 } | 304 } |
| 314 | 305 |
| 315 @end | 306 @end |
| OLD | NEW |