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 // In unit-tests on desktop, the bookmark_model is null. |
29 if (!bookmark_model_) { | 30 if (!bookmark_model_ || !tracking_enabled_) { |
30 return; | 31 return; |
Bernhard Bauer
2016/12/12 15:06:37
FWIW, using a base::ScopedObserver would let us av
jkrcal
2016/12/14 13:40:44
Done.
| |
31 } | 32 } |
32 bookmark_model_->RemoveObserver(this); | 33 bookmark_model_->RemoveObserver(this); |
33 } | 34 } |
34 | 35 |
35 // static | 36 // static |
36 void BookmarkLastVisitUpdater::CreateForWebContentsWithBookmarkModel( | 37 void BookmarkLastVisitUpdater::CreateForWebContentsWithBookmarkModel( |
37 content::WebContents* web_contents, | 38 content::WebContents* web_contents, |
38 bookmarks::BookmarkModel* bookmark_model) { | 39 bookmarks::BookmarkModel* bookmark_model) { |
39 web_contents->SetUserData(UserDataKey(), new BookmarkLastVisitUpdater( | 40 web_contents->SetUserData(UserDataKey(), new BookmarkLastVisitUpdater( |
40 web_contents, bookmark_model)); | 41 web_contents, bookmark_model)); |
41 } | 42 } |
42 | 43 |
43 BookmarkLastVisitUpdater::BookmarkLastVisitUpdater( | 44 BookmarkLastVisitUpdater::BookmarkLastVisitUpdater( |
44 content::WebContents* web_contents, | 45 content::WebContents* web_contents, |
45 bookmarks::BookmarkModel* bookmark_model) | 46 bookmarks::BookmarkModel* bookmark_model) |
46 : content::WebContentsObserver(web_contents), | 47 : content::WebContentsObserver(web_contents), |
47 bookmark_model_(bookmark_model), | 48 bookmark_model_(bookmark_model), |
48 web_contents_(web_contents) { | 49 web_contents_(web_contents) { |
50 // Switch off tracking visits for off the record profiles. | |
Bernhard Bauer
2016/12/12 15:06:37
Nit: the code here uses the term "off the record"
jkrcal
2016/12/14 13:40:44
Done.
| |
51 content::BrowserContext* browser_context = web_contents->GetBrowserContext(); | |
52 tracking_enabled_ = !browser_context->IsOffTheRecord(); | |
53 | |
49 // In unit-tests on desktop, the bookmark_model is null. | 54 // In unit-tests on desktop, the bookmark_model is null. |
50 if (!bookmark_model_) { | 55 if (!bookmark_model_ || !tracking_enabled_) { |
Bernhard Bauer
2016/12/12 15:06:38
So, what does this class actually do if tracking i
jkrcal
2016/12/14 13:40:44
Done.
| |
51 return; | 56 return; |
52 } | 57 } |
53 bookmark_model->AddObserver(this); | 58 bookmark_model->AddObserver(this); |
54 } | 59 } |
55 | 60 |
56 void BookmarkLastVisitUpdater::DidStartNavigation( | 61 void BookmarkLastVisitUpdater::DidStartNavigation( |
57 content::NavigationHandle* navigation_handle) { | 62 content::NavigationHandle* navigation_handle) { |
58 // Mark visited bookmark when the navigation starts (may end somewhere else | 63 // Mark visited bookmark when the navigation starts (may end somewhere else |
59 // due to server-side redirects). | 64 // due to server-side redirects). |
60 NewURLVisited(navigation_handle); | 65 NewURLVisited(navigation_handle); |
61 } | 66 } |
62 | 67 |
63 void BookmarkLastVisitUpdater::DidRedirectNavigation( | 68 void BookmarkLastVisitUpdater::DidRedirectNavigation( |
64 content::NavigationHandle* navigation_handle) { | 69 content::NavigationHandle* navigation_handle) { |
65 // Mark visited bookmark also after each redirect. | 70 // Mark visited bookmark also after each redirect. |
66 NewURLVisited(navigation_handle); | 71 NewURLVisited(navigation_handle); |
67 } | 72 } |
68 | 73 |
69 void BookmarkLastVisitUpdater::NewURLVisited( | 74 void BookmarkLastVisitUpdater::NewURLVisited( |
70 content::NavigationHandle* navigation_handle) { | 75 content::NavigationHandle* navigation_handle) { |
71 // In unit-tests on desktop, the bookmark_model is null. | 76 // In unit-tests on desktop, the bookmark_model is null. |
72 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsErrorPage() || | 77 if (!navigation_handle->IsInMainFrame() || navigation_handle->IsErrorPage() || |
73 !bookmark_model_) { | 78 !bookmark_model_ || !tracking_enabled_) { |
74 return; | 79 return; |
75 } | 80 } |
76 | 81 |
77 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( | 82 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( |
78 bookmark_model_, navigation_handle->GetURL(), IsMobilePlatform()); | 83 bookmark_model_, navigation_handle->GetURL(), IsMobilePlatform()); |
79 } | 84 } |
80 | 85 |
81 void BookmarkLastVisitUpdater::BookmarkNodeAdded( | 86 void BookmarkLastVisitUpdater::BookmarkNodeAdded( |
82 bookmarks::BookmarkModel* model, | 87 bookmarks::BookmarkModel* model, |
83 const bookmarks::BookmarkNode* parent, | 88 const bookmarks::BookmarkNode* parent, |
84 int index) { | 89 int index) { |
85 const GURL& new_bookmark_url = parent->GetChild(index)->url(); | 90 const GURL& new_bookmark_url = parent->GetChild(index)->url(); |
86 | 91 |
87 if (new_bookmark_url == web_contents_->GetLastCommittedURL()) { | 92 if (new_bookmark_url == web_contents_->GetLastCommittedURL()) { |
88 // Consider in this TabHelper only bookmarks created from this tab (and not | 93 // Consider in this TabHelper only bookmarks created from this tab (and not |
89 // the ones created from other tabs or created through bookmark sync). | 94 // the ones created from other tabs or created through bookmark sync). |
90 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( | 95 ntp_snippets::UpdateBookmarkOnURLVisitedInMainFrame( |
91 bookmark_model_, new_bookmark_url, IsMobilePlatform()); | 96 bookmark_model_, new_bookmark_url, IsMobilePlatform()); |
92 } | 97 } |
93 } | 98 } |
OLD | NEW |