Chromium Code Reviews| 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 package org.chromium.chrome.browser; | 5 package org.chromium.chrome.browser; |
| 6 | 6 |
| 7 import org.chromium.base.CalledByNative; | 7 import org.chromium.base.CalledByNative; |
| 8 import org.chromium.base.ObserverList; | 8 import org.chromium.base.ObserverList; |
| 9 import org.chromium.chrome.browser.profiles.Profile; | 9 import org.chromium.chrome.browser.profiles.Profile; |
| 10 import org.chromium.components.bookmarks.BookmarkId; | 10 import org.chromium.components.bookmarks.BookmarkId; |
| 11 | 11 |
| 12 import java.util.ArrayList; | 12 import java.util.ArrayList; |
| 13 import java.util.List; | 13 import java.util.List; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * Provides the communication channel for Android to fetch and manipulate the | 16 * Provides the communication channel for Android to fetch and manipulate the |
| 17 * bookmark model stored in native. | 17 * bookmark model stored in native. |
| 18 */ | 18 */ |
| 19 public class BookmarksBridge { | 19 public class BookmarksBridge { |
| 20 private final Profile mProfile; | 20 private final Profile mProfile; |
| 21 private boolean mExtensiveBookmarkChangesHappening; | |
| 21 private long mNativeBookmarksBridge; | 22 private long mNativeBookmarksBridge; |
| 22 private boolean mIsNativeBookmarkModelLoaded; | 23 private boolean mIsNativeBookmarkModelLoaded; |
| 23 private final List<DelayedBookmarkCallback> mDelayedBookmarkCallbacks = | 24 private final List<DelayedBookmarkCallback> mDelayedBookmarkCallbacks = |
| 24 new ArrayList<DelayedBookmarkCallback>(); | 25 new ArrayList<DelayedBookmarkCallback>(); |
| 25 private final ObserverList<BookmarkModelObserver> mObservers = | 26 private final ObserverList<BookmarkModelObserver> mObservers = |
| 26 new ObserverList<BookmarkModelObserver>(); | 27 new ObserverList<BookmarkModelObserver>(); |
| 27 | 28 |
| 28 /** | 29 /** |
| 29 * Interface for callback object for fetching bookmarks and folder hierarchy . | 30 * Interface for callback object for fetching bookmarks and folder hierarchy . |
| 30 */ | 31 */ |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 41 * Callback method for fetching the folder hierarchy. | 42 * Callback method for fetching the folder hierarchy. |
| 42 * @param folderId The folder id to which the bookmarks belong. | 43 * @param folderId The folder id to which the bookmarks belong. |
| 43 * @param bookmarksList List holding the fetched folder details. | 44 * @param bookmarksList List holding the fetched folder details. |
| 44 */ | 45 */ |
| 45 @CalledByNative("BookmarksCallback") | 46 @CalledByNative("BookmarksCallback") |
| 46 void onBookmarksFolderHierarchyAvailable(BookmarkId folderId, | 47 void onBookmarksFolderHierarchyAvailable(BookmarkId folderId, |
| 47 List<BookmarkItem> bookmarksList); | 48 List<BookmarkItem> bookmarksList); |
| 48 } | 49 } |
| 49 | 50 |
| 50 /** | 51 /** |
| 51 * Interface that provides listeners to be notified of changes to the bookma rk model. | 52 * Base empty implementation observer class that provides listeners to be no tified of changes |
| 53 * to the bookmark model. It's a mandatory to implement one method, | |
|
Ian Wen
2014/09/10 21:48:51
s It's a mandatory/It's mandatory
Kibeom Kim (inactive)
2014/09/10 22:39:16
Done.
| |
| 54 * bookmarkModelChangedNotDuringExtensiveChange. Other methods are optional and if they aren't | |
| 55 * overridden, the default implementation of them will eventually call | |
| 56 * bookmarkModelChangedNotDuringExtensiveChange. | |
| 52 */ | 57 */ |
| 53 public interface BookmarkModelObserver { | 58 public abstract static class BookmarkModelObserver { |
| 54 /** | 59 /** |
| 55 * Invoked when a node has moved. | 60 * Invoked when a node has been removed, the item may still be starred t hough. |
| 61 * @param parent The parent of the node that was removed. | |
| 62 * @param oldIndex The index of the removed node in the parent before it was removed. | |
| 63 * @param node The node that was removed. | |
| 64 * @param isExtensiveBookmarkChangesHappening whether extensive changes are happening. | |
| 65 */ | |
| 66 public void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookm arkItem node, | |
| 67 boolean isExtensiveBookmarkChangesHappening) { | |
| 68 if (!isExtensiveBookmarkChangesHappening) { | |
| 69 bookmarkNodeRemovedNotDuringExtensiveChange(parent, oldIndex, no de); | |
| 70 } | |
| 71 } | |
| 72 | |
| 73 /** | |
| 74 * Invoked when a node has moved. But only when it's not during extensiv e change. | |
|
Ted C
2014/09/10 21:57:40
I wouldn't include "But only when it's not during
Kibeom Kim (inactive)
2014/09/10 22:39:16
Done.
| |
| 56 * @param oldParent The parent before the move. | 75 * @param oldParent The parent before the move. |
| 57 * @param oldIndex The index of the node in the old parent. | 76 * @param oldIndex The index of the node in the old parent. |
| 58 * @param newParent The parent after the move. | 77 * @param newParent The parent after the move. |
| 59 * @param newIndex The index of the node in the new parent. | 78 * @param newIndex The index of the node in the new parent. |
| 60 */ | 79 */ |
| 61 void bookmarkNodeMoved( | 80 public void bookmarkNodeMovedNotDuringExtensiveChange( |
|
Ian Wen
2014/09/10 21:48:51
I wonder if we could rename these functions to be
Ted C
2014/09/10 21:57:39
I don't think we need these NotDuringExtensiveChan
| |
| 62 BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, in t newIndex); | 81 BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, in t newIndex) { |
| 82 bookmarkModelChangedNotDuringExtensiveChange(); | |
| 83 } | |
| 63 | 84 |
| 64 /** | 85 /** |
| 65 * Invoked when a node has been added. | 86 * Invoked when a node has been added. But only when it's not during ext ensive change. |
| 66 * @param parent The parent of the node being added. | 87 * @param parent The parent of the node being added. |
| 67 * @param index The index of the added node. | 88 * @param index The index of the added node. |
| 68 */ | 89 */ |
| 69 void bookmarkNodeAdded(BookmarkItem parent, int index); | 90 public void bookmarkNodeAddedNotDuringExtensiveChange(BookmarkItem paren t, int index) { |
| 91 bookmarkModelChangedNotDuringExtensiveChange(); | |
| 92 } | |
| 70 | 93 |
| 71 /** | 94 /** |
| 72 * Invoked when a node has been removed, the item may still be starred t hough. | 95 * Invoked when a node has been removed, the item may still be starred t hough. |
| 96 * But only when it's not during extensive change. | |
| 73 * @param parent The parent of the node that was removed. | 97 * @param parent The parent of the node that was removed. |
| 74 * @param oldIndex The index of the removed node in the parent before it was removed. | 98 * @param oldIndex The index of the removed node in the parent before it was removed. |
| 75 * @param node The node that was removed. | 99 * @param node The node that was removed. |
| 76 */ | 100 */ |
| 77 void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, BookmarkItem node); | 101 public void bookmarkNodeRemovedNotDuringExtensiveChange(BookmarkItem par ent, int oldIndex, |
| 102 BookmarkItem node) { | |
| 103 bookmarkModelChangedNotDuringExtensiveChange(); | |
| 104 } | |
| 78 | 105 |
| 79 /** | 106 /** |
| 80 * Invoked when the title or url of a node changes. | 107 * Invoked when the title or url of a node changes. But only when it's n ot during extensive |
| 108 * change. | |
| 81 * @param node The node being changed. | 109 * @param node The node being changed. |
| 82 */ | 110 */ |
| 83 void bookmarkNodeChanged(BookmarkItem node); | 111 public void bookmarkNodeChangedNotDuringExtensiveChange(BookmarkItem nod e) { |
| 112 bookmarkModelChangedNotDuringExtensiveChange(); | |
| 113 } | |
| 84 | 114 |
| 85 /** | 115 /** |
| 86 * Invoked when the children (just direct children, not descendants) of a node have been | 116 * Invoked when the children (just direct children, not descendants) of a node have been |
| 87 * reordered in some way, such as sorted. | 117 * reordered in some way, such as sorted. But only when it's not during extensive change. |
| 88 * @param node The node whose children are being reordered. | 118 * @param node The node whose children are being reordered. |
| 89 */ | 119 */ |
| 90 void bookmarkNodeChildrenReordered(BookmarkItem node); | 120 public void bookmarkNodeChildrenReorderedNotDuringExtensiveChange(Bookma rkItem node) { |
| 91 | 121 bookmarkModelChangedNotDuringExtensiveChange(); |
| 92 /** | 122 } |
| 93 * Invoked before an extensive set of model changes is about to begin. This tells UI | |
| 94 * intensive observers to wait until the updates finish to update themse lves. These methods | |
| 95 * should only be used for imports and sync. Observers should still resp ond to | |
| 96 * BookmarkNodeRemoved immediately, to avoid holding onto stale node ref erences. | |
| 97 */ | |
| 98 void extensiveBookmarkChangesBeginning(); | |
| 99 | |
| 100 /** | |
| 101 * Invoked after an extensive set of model changes has ended. This tell s observers to | |
| 102 * update themselves if they were waiting for the update to finish. | |
| 103 */ | |
| 104 void extensiveBookmarkChangesEnded(); | |
| 105 | |
| 106 /** | |
| 107 * Called when there are changes to the bookmark model that don't trigg er any of the other | |
| 108 * callback methods. For example, this is called when partner bookmarks change. | |
| 109 */ | |
| 110 void bookmarkModelChanged(); | |
| 111 | 123 |
| 112 /** | 124 /** |
| 113 * Called when the native side of bookmark is loaded and now in usable s tate. | 125 * Called when the native side of bookmark is loaded and now in usable s tate. |
| 114 */ | 126 */ |
| 115 void bookmarkModelLoaded(); | 127 public void bookmarkModelLoaded() { |
| 128 bookmarkModelChangedNotDuringExtensiveChange(); | |
| 129 } | |
| 130 | |
| 131 /** | |
| 132 * Called when there are changes to the bookmark model that don't trigg er any of the other | |
| 133 * callback methods or it wasn't handled by other callback methods. Als o note that this | |
|
Ted C
2014/09/10 21:57:39
This behavior is odd to me. I fear that this will
Kibeom Kim (inactive)
2014/09/10 22:39:16
I think it should be a common pattern. First, we c
| |
| 134 * isn't called during extensive change. | |
| 135 * Examples: | |
| 136 * - On partner bookmarks change. | |
| 137 * - On extensive change finished. | |
| 138 * - Falling back from other methods that are not overridden in this cl ass. | |
| 139 */ | |
| 140 public abstract void bookmarkModelChangedNotDuringExtensiveChange(); | |
| 116 } | 141 } |
| 117 | 142 |
| 118 /** | 143 /** |
| 119 * Handler to fetch the bookmarks, titles, urls and folder hierarchy. | 144 * Handler to fetch the bookmarks, titles, urls and folder hierarchy. |
| 120 * @param profile Profile instance corresponding to the active profile. | 145 * @param profile Profile instance corresponding to the active profile. |
| 121 */ | 146 */ |
| 122 public BookmarksBridge(Profile profile) { | 147 public BookmarksBridge(Profile profile) { |
| 123 mProfile = profile; | 148 mProfile = profile; |
| 124 mNativeBookmarksBridge = nativeInit(profile); | 149 mNativeBookmarksBridge = nativeInit(profile); |
|
Ted C
2014/09/10 21:57:39
I think you should expose a private native call to
Kibeom Kim (inactive)
2014/09/10 22:39:16
Done. (good point!)
| |
| 125 } | 150 } |
| 126 | 151 |
| 127 /** | 152 /** |
| 128 * Destroys this instance so no further calls can be executed. | 153 * Destroys this instance so no further calls can be executed. |
| 129 */ | 154 */ |
| 130 public void destroy() { | 155 public void destroy() { |
| 131 if (mNativeBookmarksBridge != 0) { | 156 if (mNativeBookmarksBridge != 0) { |
| 132 nativeDestroy(mNativeBookmarksBridge); | 157 nativeDestroy(mNativeBookmarksBridge); |
| 133 mNativeBookmarksBridge = 0; | 158 mNativeBookmarksBridge = 0; |
| 134 mIsNativeBookmarkModelLoaded = false; | 159 mIsNativeBookmarkModelLoaded = false; |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 365 } | 390 } |
| 366 | 391 |
| 367 @CalledByNative | 392 @CalledByNative |
| 368 private void bookmarkModelDeleted() { | 393 private void bookmarkModelDeleted() { |
| 369 destroy(); | 394 destroy(); |
| 370 } | 395 } |
| 371 | 396 |
| 372 @CalledByNative | 397 @CalledByNative |
| 373 private void bookmarkNodeMoved( | 398 private void bookmarkNodeMoved( |
| 374 BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, int ne wIndex) { | 399 BookmarkItem oldParent, int oldIndex, BookmarkItem newParent, int ne wIndex) { |
| 375 for (BookmarkModelObserver observer : mObservers) { | 400 if (!mExtensiveBookmarkChangesHappening) { |
|
Ted C
2014/09/10 21:57:40
for all these i would early exit
if (mExtensiveBo
Kibeom Kim (inactive)
2014/09/10 22:39:16
Done.
| |
| 376 observer.bookmarkNodeMoved(oldParent, oldIndex, newParent, newIndex) ; | 401 for (BookmarkModelObserver observer : mObservers) { |
| 402 observer.bookmarkNodeMovedNotDuringExtensiveChange(oldParent, ol dIndex, newParent, | |
| 403 newIndex); | |
| 404 } | |
| 377 } | 405 } |
| 378 } | 406 } |
| 379 | 407 |
| 380 @CalledByNative | 408 @CalledByNative |
| 381 private void bookmarkNodeAdded(BookmarkItem parent, int index) { | 409 private void bookmarkNodeAdded(BookmarkItem parent, int index) { |
| 382 for (BookmarkModelObserver observer : mObservers) { | 410 if (!mExtensiveBookmarkChangesHappening) { |
| 383 observer.bookmarkNodeAdded(parent, index); | 411 for (BookmarkModelObserver observer : mObservers) { |
| 412 observer.bookmarkNodeAddedNotDuringExtensiveChange(parent, index ); | |
| 413 } | |
| 384 } | 414 } |
| 385 } | 415 } |
| 386 | 416 |
| 387 @CalledByNative | 417 @CalledByNative |
| 388 private void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookmark Item node) { | 418 private void bookmarkNodeRemoved(BookmarkItem parent, int oldIndex, Bookmark Item node) { |
| 389 for (BookmarkModelObserver observer : mObservers) { | 419 for (BookmarkModelObserver observer : mObservers) { |
| 390 observer.bookmarkNodeRemoved(parent, oldIndex, node); | 420 observer.bookmarkNodeRemoved(parent, oldIndex, node, |
| 421 mExtensiveBookmarkChangesHappening); | |
| 391 } | 422 } |
| 392 } | 423 } |
| 393 | 424 |
| 394 @CalledByNative | 425 @CalledByNative |
| 395 private void bookmarkNodeChanged(BookmarkItem node) { | 426 private void bookmarkNodeChanged(BookmarkItem node) { |
| 396 for (BookmarkModelObserver observer : mObservers) { | 427 if (!mExtensiveBookmarkChangesHappening) { |
| 397 observer.bookmarkNodeChanged(node); | 428 for (BookmarkModelObserver observer : mObservers) { |
| 429 observer.bookmarkNodeChangedNotDuringExtensiveChange(node); | |
| 430 } | |
| 398 } | 431 } |
| 399 } | 432 } |
| 400 | 433 |
| 401 @CalledByNative | 434 @CalledByNative |
| 402 private void bookmarkNodeChildrenReordered(BookmarkItem node) { | 435 private void bookmarkNodeChildrenReordered(BookmarkItem node) { |
| 403 for (BookmarkModelObserver observer : mObservers) { | 436 if (!mExtensiveBookmarkChangesHappening) { |
| 404 observer.bookmarkNodeChildrenReordered(node); | 437 for (BookmarkModelObserver observer : mObservers) { |
| 438 observer.bookmarkNodeChildrenReorderedNotDuringExtensiveChange(n ode); | |
| 439 } | |
| 405 } | 440 } |
| 406 } | 441 } |
| 407 | 442 |
| 408 @CalledByNative | 443 @CalledByNative |
| 409 private void extensiveBookmarkChangesBeginning() { | 444 private void extensiveBookmarkChangesBeginning() { |
| 410 for (BookmarkModelObserver observer : mObservers) { | 445 mExtensiveBookmarkChangesHappening = true; |
| 411 observer.extensiveBookmarkChangesBeginning(); | |
| 412 } | |
| 413 } | 446 } |
| 414 | 447 |
| 415 @CalledByNative | 448 @CalledByNative |
| 416 private void extensiveBookmarkChangesEnded() { | 449 private void extensiveBookmarkChangesEnded() { |
| 417 for (BookmarkModelObserver observer : mObservers) { | 450 mExtensiveBookmarkChangesHappening = false; |
| 418 observer.extensiveBookmarkChangesEnded(); | 451 bookmarkModelChanged(); |
| 419 } | |
| 420 } | 452 } |
| 421 | 453 |
| 422 @CalledByNative | 454 @CalledByNative |
| 423 private void bookmarkModelChanged() { | 455 private void bookmarkModelChanged() { |
| 424 for (BookmarkModelObserver observer : mObservers) { | 456 if (!mExtensiveBookmarkChangesHappening) { |
| 425 observer.bookmarkModelChanged(); | 457 for (BookmarkModelObserver observer : mObservers) { |
| 458 observer.bookmarkModelChangedNotDuringExtensiveChange(); | |
| 459 } | |
| 426 } | 460 } |
| 427 } | 461 } |
| 428 | 462 |
| 429 @CalledByNative | 463 @CalledByNative |
| 430 private static BookmarkItem createBookmarkItem(long id, int type, String tit le, String url, | 464 private static BookmarkItem createBookmarkItem(long id, int type, String tit le, String url, |
| 431 boolean isFolder, long parentId, int parentIdType, boolean isEditabl e, | 465 boolean isFolder, long parentId, int parentIdType, boolean isEditabl e, |
| 432 boolean isManaged) { | 466 boolean isManaged) { |
| 433 return new BookmarkItem(new BookmarkId(id, type), title, url, isFolder, | 467 return new BookmarkItem(new BookmarkId(id, type), title, url, isFolder, |
| 434 new BookmarkId(parentId, parentIdType), isEditable, isManaged); | 468 new BookmarkId(parentId, parentIdType), isEditable, isManaged); |
| 435 } | 469 } |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 575 case GET_CURRENT_FOLDER_HIERARCHY: | 609 case GET_CURRENT_FOLDER_HIERARCHY: |
| 576 mHandler.getCurrentFolderHierarchy(mFolderId, mCallback); | 610 mHandler.getCurrentFolderHierarchy(mFolderId, mCallback); |
| 577 break; | 611 break; |
| 578 default: | 612 default: |
| 579 assert false; | 613 assert false; |
| 580 break; | 614 break; |
| 581 } | 615 } |
| 582 } | 616 } |
| 583 } | 617 } |
| 584 } | 618 } |
| OLD | NEW |