OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/browsing_data_ui/history_notice_utils.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/callback.h" | |
9 #include "base/location.h" | |
10 #include "base/single_thread_task_runner.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/threading/thread_task_runner_handle.h" | |
13 #include "components/history/core/browser/web_history_service.h" | |
14 #include "components/sync/driver/sync_service.h" | |
15 #include "components/version_info/version_info.h" | |
16 | |
17 namespace { | |
18 | |
19 // Merges several asynchronous boolean callbacks into one that returns a boolean | |
20 // product of their responses. Deletes itself when done. | |
21 class MergeBooleanCallbacks { | |
22 public: | |
23 // Constructor. Upon receiving |expected_call_count| calls to |RunCallback|, | |
24 // |target_callback| will be run with the boolean product of their results. | |
25 MergeBooleanCallbacks( | |
26 int expected_call_count, | |
27 const base::Callback<void(bool)>& target_callback) | |
28 : expected_call_count_(expected_call_count), | |
29 target_callback_(target_callback), | |
30 final_response_(true), | |
31 call_count_(0) {} | |
32 | |
33 // This method is to be bound to all asynchronous callbacks which we want | |
34 // to merge. | |
35 void RunCallback(bool response) { | |
36 final_response_ &= response; | |
37 | |
38 if (++call_count_ < expected_call_count_) | |
39 return; | |
40 | |
41 target_callback_.Run(final_response_); | |
42 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
43 } | |
44 | |
45 private: | |
46 int expected_call_count_; | |
47 base::Callback<void(bool)> target_callback_; | |
48 bool final_response_; | |
49 int call_count_; | |
50 }; | |
51 | |
52 } // namespace | |
53 | |
54 namespace browsing_data_ui { | |
55 | |
56 namespace testing { | |
57 | |
58 bool g_override_other_forms_of_browsing_history_query = false; | |
59 | |
60 } // namespace testing | |
61 | |
62 void ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( | |
63 const sync_driver::SyncService* sync_service, | |
64 history::WebHistoryService* history_service, | |
65 base::Callback<void(bool)> callback) { | |
66 if (!sync_service || | |
67 !sync_service->IsSyncActive() || | |
68 !sync_service->GetActiveDataTypes().Has( | |
69 syncer::HISTORY_DELETE_DIRECTIVES) || | |
70 sync_service->IsUsingSecondaryPassphrase() || | |
71 !history_service) { | |
72 callback.Run(false); | |
73 return; | |
74 } | |
75 | |
76 history_service->QueryWebAndAppActivity(callback); | |
77 } | |
78 | |
79 void ShouldPopupDialogAboutOtherFormsOfBrowsingHistory( | |
80 const sync_driver::SyncService* sync_service, | |
81 history::WebHistoryService* history_service, | |
82 version_info::Channel channel, | |
83 base::Callback<void(bool)> callback) { | |
84 // If the query for other forms of browsing history is overriden for testing, | |
85 // the conditions are identical with | |
86 // ShouldShowNoticeAboutOtherFormsOfBrowsingHistory. | |
87 if (testing::g_override_other_forms_of_browsing_history_query) { | |
88 ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( | |
89 sync_service, history_service, callback); | |
90 return; | |
91 } | |
92 | |
93 if (!sync_service || | |
94 !sync_service->IsSyncActive() || | |
95 !sync_service->GetActiveDataTypes().Has( | |
96 syncer::HISTORY_DELETE_DIRECTIVES) || | |
97 sync_service->IsUsingSecondaryPassphrase() || | |
98 !history_service) { | |
99 callback.Run(false); | |
100 return; | |
101 } | |
102 | |
103 // Return the boolean product of QueryWebAndAppActivity and | |
104 // QueryOtherFormsOfBrowsingHistory. MergeBooleanCallbacks deletes itself | |
105 // after processing both callbacks. | |
106 MergeBooleanCallbacks* merger = new MergeBooleanCallbacks(2, callback); | |
107 history_service->QueryWebAndAppActivity(base::Bind( | |
108 &MergeBooleanCallbacks::RunCallback, base::Unretained(merger))); | |
109 history_service->QueryOtherFormsOfBrowsingHistory( | |
110 channel, | |
111 base::Bind( | |
112 &MergeBooleanCallbacks::RunCallback, base::Unretained(merger))); | |
113 } | |
114 | |
115 } // namespace browsing_data_ui | |
OLD | NEW |