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