Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(268)

Side by Side Diff: chrome/browser/ntp_snippets/bookmark_last_visit_updater.cc

Issue 2603323002: Disable tracking of visits to bookmarks in incognito (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 bookmark_model_->RemoveObserver(this); 29 bookmark_model_->RemoveObserver(this);
32 } 30 }
33 31
34 // static 32 // static
35 void BookmarkLastVisitUpdater::CreateForWebContentsWithBookmarkModel( 33 void BookmarkLastVisitUpdater::MaybeCreateForWebContentsWithBookmarkModel(
36 content::WebContents* web_contents, 34 content::WebContents* web_contents,
37 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 }
38 web_contents->SetUserData(UserDataKey(), new BookmarkLastVisitUpdater( 42 web_contents->SetUserData(UserDataKey(), new BookmarkLastVisitUpdater(
39 web_contents, bookmark_model)); 43 web_contents, bookmark_model));
40 } 44 }
41 45
42 BookmarkLastVisitUpdater::BookmarkLastVisitUpdater( 46 BookmarkLastVisitUpdater::BookmarkLastVisitUpdater(
43 content::WebContents* web_contents, 47 content::WebContents* web_contents,
44 bookmarks::BookmarkModel* bookmark_model) 48 bookmarks::BookmarkModel* bookmark_model)
45 : content::WebContentsObserver(web_contents), 49 : content::WebContentsObserver(web_contents),
46 bookmark_model_(bookmark_model), 50 bookmark_model_(bookmark_model),
47 web_contents_(web_contents) { 51 web_contents_(web_contents) {
48 // In unit-tests on desktop, the bookmark_model is null. 52 DCHECK(bookmark_model_);
49 if (!bookmark_model_) 53 bookmark_model_->AddObserver(this);
50 return;
51 bookmark_model->AddObserver(this);
52 } 54 }
53 55
54 void BookmarkLastVisitUpdater::DidStartNavigation( 56 void BookmarkLastVisitUpdater::DidStartNavigation(
55 content::NavigationHandle* navigation_handle) { 57 content::NavigationHandle* navigation_handle) {
56 // Mark visited bookmark when the navigation starts (may end somewhere else 58 // Mark visited bookmark when the navigation starts (may end somewhere else
57 // due to server-side redirects). 59 // due to server-side redirects).
58 NewURLVisited(navigation_handle); 60 NewURLVisited(navigation_handle);
59 } 61 }
60 62
61 void BookmarkLastVisitUpdater::DidRedirectNavigation( 63 void BookmarkLastVisitUpdater::DidRedirectNavigation(
62 content::NavigationHandle* navigation_handle) { 64 content::NavigationHandle* navigation_handle) {
63 // Mark visited bookmark also after each redirect. 65 // Mark visited bookmark also after each redirect.
64 NewURLVisited(navigation_handle); 66 NewURLVisited(navigation_handle);
65 } 67 }
66 68
67 void BookmarkLastVisitUpdater::NewURLVisited( 69 void BookmarkLastVisitUpdater::NewURLVisited(
68 content::NavigationHandle* navigation_handle) { 70 content::NavigationHandle* navigation_handle) {
69 // In unit-tests on desktop, the bookmark_model is null. 71 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsErrorPage()) {
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 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698