| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/ntp_snippets/bookmark_last_visit_updater.h" | 5 #include "chrome/browser/ntp_snippets/bookmark_last_visit_updater.h" |
| 6 | 6 |
| 7 #include "chrome/common/features.h" | 7 #include "chrome/common/features.h" |
| 8 #include "components/bookmarks/browser/bookmark_model.h" | 8 #include "components/bookmarks/browser/bookmark_model.h" |
| 9 #include "components/bookmarks/browser/bookmark_node.h" | 9 #include "components/bookmarks/browser/bookmark_node.h" |
| 10 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" | 10 #include "components/ntp_snippets/bookmarks/bookmark_last_visit_utils.h" |
| 11 #include "content/public/browser/browser_context.h" | |
| 12 #include "content/public/browser/navigation_handle.h" | 11 #include "content/public/browser/navigation_handle.h" |
| 13 | 12 |
| 14 namespace { | 13 namespace { |
| 15 | 14 |
| 16 bool IsMobilePlatform() { | 15 bool IsMobilePlatform() { |
| 17 #if BUILDFLAG(ANDROID_JAVA_UI) // There are no tab helpers on iOS. | 16 #if BUILDFLAG(ANDROID_JAVA_UI) // There are no tab helpers on iOS. |
| 18 return true; | 17 return true; |
| 19 #else | 18 #else |
| 20 return false; | 19 return false; |
| 21 #endif | 20 #endif |
| 22 } | 21 } |
| 23 | 22 |
| 24 } // namespace | 23 } // namespace |
| 25 | 24 |
| 26 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkLastVisitUpdater); | 25 DEFINE_WEB_CONTENTS_USER_DATA_KEY(BookmarkLastVisitUpdater); |
| 27 | 26 |
| 28 BookmarkLastVisitUpdater::~BookmarkLastVisitUpdater() { | 27 BookmarkLastVisitUpdater::~BookmarkLastVisitUpdater() { |
| 28 // In unit-tests on desktop, the bookmark_model is null. |
| 29 if (!bookmark_model_) |
| 30 return; |
| 29 bookmark_model_->RemoveObserver(this); | 31 bookmark_model_->RemoveObserver(this); |
| 30 } | 32 } |
| 31 | 33 |
| 32 // static | 34 // static |
| 33 void BookmarkLastVisitUpdater::MaybeCreateForWebContentsWithBookmarkModel( | 35 void BookmarkLastVisitUpdater::CreateForWebContentsWithBookmarkModel( |
| 34 content::WebContents* web_contents, | 36 content::WebContents* web_contents, |
| 35 bookmarks::BookmarkModel* bookmark_model) { | 37 bookmarks::BookmarkModel* bookmark_model) { |
| 36 // Do not create the helper for missing |bookmark_model| (in some unit-tests) | |
| 37 // or for incognito profiles where tracking bookmark visits is not desired. | |
| 38 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
| 39 if (!bookmark_model || browser_context->IsOffTheRecord()) { | |
| 40 return; | |
| 41 } | |
| 42 web_contents->SetUserData(UserDataKey(), new BookmarkLastVisitUpdater( | 38 web_contents->SetUserData(UserDataKey(), new BookmarkLastVisitUpdater( |
| 43 web_contents, bookmark_model)); | 39 web_contents, bookmark_model)); |
| 44 } | 40 } |
| 45 | 41 |
| 46 BookmarkLastVisitUpdater::BookmarkLastVisitUpdater( | 42 BookmarkLastVisitUpdater::BookmarkLastVisitUpdater( |
| 47 content::WebContents* web_contents, | 43 content::WebContents* web_contents, |
| 48 bookmarks::BookmarkModel* bookmark_model) | 44 bookmarks::BookmarkModel* bookmark_model) |
| 49 : content::WebContentsObserver(web_contents), | 45 : content::WebContentsObserver(web_contents), |
| 50 bookmark_model_(bookmark_model), | 46 bookmark_model_(bookmark_model), |
| 51 web_contents_(web_contents) { | 47 web_contents_(web_contents) { |
| 52 DCHECK(bookmark_model_); | 48 // In unit-tests on desktop, the bookmark_model is null. |
| 53 bookmark_model_->AddObserver(this); | 49 if (!bookmark_model_) |
| 50 return; |
| 51 bookmark_model->AddObserver(this); |
| 54 } | 52 } |
| 55 | 53 |
| 56 void BookmarkLastVisitUpdater::DidStartNavigation( | 54 void BookmarkLastVisitUpdater::DidStartNavigation( |
| 57 content::NavigationHandle* navigation_handle) { | 55 content::NavigationHandle* navigation_handle) { |
| 58 // Mark visited bookmark when the navigation starts (may end somewhere else | 56 // Mark visited bookmark when the navigation starts (may end somewhere else |
| 59 // due to server-side redirects). | 57 // due to server-side redirects). |
| 60 NewURLVisited(navigation_handle); | 58 NewURLVisited(navigation_handle); |
| 61 } | 59 } |
| 62 | 60 |
| 63 void BookmarkLastVisitUpdater::DidRedirectNavigation( | 61 void BookmarkLastVisitUpdater::DidRedirectNavigation( |
| 64 content::NavigationHandle* navigation_handle) { | 62 content::NavigationHandle* navigation_handle) { |
| 65 // Mark visited bookmark also after each redirect. | 63 // Mark visited bookmark also after each redirect. |
| 66 NewURLVisited(navigation_handle); | 64 NewURLVisited(navigation_handle); |
| 67 } | 65 } |
| 68 | 66 |
| 69 void BookmarkLastVisitUpdater::NewURLVisited( | 67 void BookmarkLastVisitUpdater::NewURLVisited( |
| 70 content::NavigationHandle* navigation_handle) { | 68 content::NavigationHandle* navigation_handle) { |
| 71 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsErrorPage()) { | 69 // In unit-tests on desktop, the bookmark_model is null. |
| 70 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsErrorPage() || |
| 71 !bookmark_model_) { |
| 72 return; | 72 return; |
| 73 } | 73 } |
| 74 | 74 |
| 75 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( | 75 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( |
| 76 bookmark_model_, navigation_handle->GetURL(), IsMobilePlatform()); | 76 bookmark_model_, navigation_handle->GetURL(), IsMobilePlatform()); |
| 77 } | 77 } |
| 78 | 78 |
| 79 void BookmarkLastVisitUpdater::BookmarkNodeAdded( | 79 void BookmarkLastVisitUpdater::BookmarkNodeAdded( |
| 80 bookmarks::BookmarkModel* model, | 80 bookmarks::BookmarkModel* model, |
| 81 const bookmarks::BookmarkNode* parent, | 81 const bookmarks::BookmarkNode* parent, |
| 82 int index) { | 82 int index) { |
| 83 const GURL& new_bookmark_url = parent->GetChild(index)->url(); | 83 const GURL& new_bookmark_url = parent->GetChild(index)->url(); |
| 84 | 84 |
| 85 if (new_bookmark_url == web_contents_->GetLastCommittedURL()) { | 85 if (new_bookmark_url == web_contents_->GetLastCommittedURL()) { |
| 86 // Consider in this TabHelper only bookmarks created from this tab (and not | 86 // Consider in this TabHelper only bookmarks created from this tab (and not |
| 87 // the ones created from other tabs or created through bookmark sync). | 87 // the ones created from other tabs or created through bookmark sync). |
| 88 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( | 88 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( |
| 89 bookmark_model_, new_bookmark_url, IsMobilePlatform()); | 89 bookmark_model_, new_bookmark_url, IsMobilePlatform()); |
| 90 } | 90 } |
| 91 } | 91 } |
| OLD | NEW |