1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/android/history_report/data_observer.h"
7 #include "base/time/time.h"
8 #include "chrome/browser/android/history_report/delta_file_commons.h"
9 #include "chrome/browser/android/history_report/delta_file_service.h"
10 #include "chrome/browser/android/history_report/usage_report_util.h"
11 #include "chrome/browser/android/history_report/usage_reports_buffer_service.h"
12 #include "components/bookmarks/browser/bookmark_model.h"
13 #include "components/history/core/browser/history_service.h"
15 using bookmarks::BookmarkModel
;
16 using bookmarks::BookmarkNode
;
18 namespace history_report
{
20 DataObserver::DataObserver(
21 base::Callback
<void(void)> data_changed_callback
,
22 base::Callback
<void(void)> data_cleared_callback
,
23 base::Callback
<void(void)> stop_reporting_callback
,
24 DeltaFileService
* delta_file_service
,
25 UsageReportsBufferService
* usage_reports_buffer_service
,
26 BookmarkModel
* bookmark_model
,
27 history::HistoryService
* history_service
)
28 : data_changed_callback_(data_changed_callback
),
29 data_cleared_callback_(data_cleared_callback
),
30 stop_reporting_callback_(stop_reporting_callback
),
31 delta_file_service_(delta_file_service
),
32 usage_reports_buffer_service_(usage_reports_buffer_service
) {
33 bookmark_model
->AddObserver(this);
34 history_service
->AddObserver(this);
37 DataObserver::~DataObserver() {}
39 void DataObserver::BookmarkModelLoaded(BookmarkModel
* model
,
40 bool ids_reassigned
) {}
42 void DataObserver::BookmarkModelBeingDeleted(BookmarkModel
* model
) {}
44 void DataObserver::BookmarkNodeMoved(BookmarkModel
* model
,
45 const BookmarkNode
* old_parent
,
47 const BookmarkNode
* new_parent
,
50 void DataObserver::BookmarkNodeAdded(BookmarkModel
* model
,
51 const BookmarkNode
* parent
,
53 delta_file_service_
->PageAdded(parent
->GetChild(index
)->url());
54 data_changed_callback_
.Run();
57 void DataObserver::BookmarkNodeRemoved(BookmarkModel
* model
,
58 const BookmarkNode
* parent
,
60 const BookmarkNode
* node
,
61 const std::set
<GURL
>& removed_urls
) {
62 DeleteBookmarks(removed_urls
);
65 void DataObserver::BookmarkAllUserNodesRemoved(
67 const std::set
<GURL
>& removed_urls
) {
68 DeleteBookmarks(removed_urls
);
71 void DataObserver::BookmarkNodeChanged(BookmarkModel
* model
,
72 const BookmarkNode
* node
) {
73 delta_file_service_
->PageAdded(node
->url());
74 data_changed_callback_
.Run();
77 void DataObserver::BookmarkNodeFaviconChanged(BookmarkModel
* model
,
78 const BookmarkNode
* node
) {}
80 void DataObserver::BookmarkNodeChildrenReordered(BookmarkModel
* model
,
81 const BookmarkNode
* node
) {}
83 void DataObserver::DeleteBookmarks(const std::set
<GURL
>& removed_urls
) {
84 for (std::set
<GURL
>::const_iterator it
= removed_urls
.begin();
85 it
!= removed_urls
.end();
87 delta_file_service_
->PageDeleted(*it
);
89 if (!removed_urls
.empty())
90 data_changed_callback_
.Run();
93 void DataObserver::OnURLVisited(history::HistoryService
* history_service
,
94 ui::PageTransition transition
,
95 const history::URLRow
& row
,
96 const history::RedirectList
& redirects
,
97 base::Time visit_time
) {
101 delta_file_service_
->PageAdded(row
.url());
102 // TODO(haaawk): check if this is really a data change not just a
103 // visit of already seen page.
104 data_changed_callback_
.Run();
105 std::string id
= DeltaFileEntryWithData::UrlToId(row
.url().spec());
106 usage_reports_buffer_service_
->AddVisit(
108 visit_time
.ToJavaTime(),
109 usage_report_util::IsTypedVisit(transition
));
110 // We stop any usage reporting to wait for gmscore to query the provider
111 // for this url. We do not want to report usage for a URL which might
112 // not be known to gmscore.
113 stop_reporting_callback_
.Run();
116 void DataObserver::OnURLsModified(history::HistoryService
* history_service
,
117 const history::URLRows
& changed_urls
) {
118 for (const auto& row
: changed_urls
) {
120 delta_file_service_
->PageAdded(row
.url());
123 data_changed_callback_
.Run();
126 void DataObserver::OnURLsDeleted(history::HistoryService
* history_service
,
129 const history::URLRows
& deleted_rows
,
130 const std::set
<GURL
>& favicon_urls
) {
132 delta_file_service_
->Clear();
133 data_cleared_callback_
.Run();
135 for (const auto& row
: deleted_rows
) {
137 delta_file_service_
->PageDeleted(row
.url());
140 data_changed_callback_
.Run();
144 } // namespace history_report