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