| 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 } |
| 29 |
| 30 // This method is to be bound to all asynchronous callbacks which we want |
| 31 // to merge. |
| 32 void RunCallback(bool response) { |
| 33 final_response_ &= response; |
| 34 |
| 35 if (++call_count_ < expected_call_count_) |
| 36 return; |
| 37 |
| 38 target_callback_.Run(final_response_); |
| 39 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
| 40 } |
| 41 |
| 42 private: |
| 43 int expected_call_count_; |
| 44 base::Callback<void(bool)> target_callback_; |
| 45 bool final_response_ = true; |
| 46 int call_count_ = 0; |
| 47 }; |
| 48 |
| 49 } // namespace |
| 10 | 50 |
| 11 namespace browsing_data_ui { | 51 namespace browsing_data_ui { |
| 12 | 52 |
| 13 namespace testing { | 53 namespace testing { |
| 14 | 54 |
| 15 bool g_override_other_forms_of_browsing_history_query = false; | 55 bool g_override_other_forms_of_browsing_history_query = false; |
| 16 | 56 |
| 17 } | 57 } // namespace testing |
| 18 | 58 |
| 19 void ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( | 59 void ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( |
| 20 const ProfileSyncService* sync_service, | 60 const sync_driver::SyncService* sync_service, |
| 21 history::WebHistoryService* history_service, | 61 history::WebHistoryService* history_service, |
| 22 base::Callback<void(bool)> callback) { | 62 base::Callback<void(bool)> callback) { |
| 23 if (!sync_service || | 63 if (!sync_service || |
| 24 !sync_service->IsSyncActive() || | 64 !sync_service->IsSyncActive() || |
| 65 !sync_service->GetActiveDataTypes().Has( |
| 66 syncer::HISTORY_DELETE_DIRECTIVES) || |
| 25 sync_service->IsUsingSecondaryPassphrase() || | 67 sync_service->IsUsingSecondaryPassphrase() || |
| 26 !history_service) { | 68 !history_service) { |
| 27 callback.Run(false); | 69 callback.Run(false); |
| 28 return; | 70 return; |
| 29 } | 71 } |
| 30 | 72 |
| 31 history_service->QueryWebAndAppActivity(callback); | 73 history_service->QueryWebAndAppActivity(callback); |
| 32 } | 74 } |
| 33 | 75 |
| 34 void ShouldPopupDialogAboutOtherFormsOfBrowsingHistory( | 76 void ShouldPopupDialogAboutOtherFormsOfBrowsingHistory( |
| 35 const ProfileSyncService* sync_service, | 77 const sync_driver::SyncService* sync_service, |
| 36 history::WebHistoryService* history_service, | 78 history::WebHistoryService* history_service, |
| 79 version_info::Channel channel, |
| 80 const std::string& user_agent, |
| 37 base::Callback<void(bool)> callback) { | 81 base::Callback<void(bool)> callback) { |
| 38 if (!history_service || | 82 // If the query for other forms of browsing history is overriden for testing, |
| 39 (!testing::g_override_other_forms_of_browsing_history_query && | 83 // the conditions are identical with |
| 40 !history_service->HasOtherFormsOfBrowsingHistory())) { | 84 // ShouldShowNoticeAboutOtherFormsOfBrowsingHistory. |
| 85 if (testing::g_override_other_forms_of_browsing_history_query) { |
| 86 ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( |
| 87 sync_service, history_service, callback); |
| 88 return; |
| 89 } |
| 90 |
| 91 if (!sync_service || |
| 92 !sync_service->IsSyncActive() || |
| 93 !sync_service->GetActiveDataTypes().Has( |
| 94 syncer::HISTORY_DELETE_DIRECTIVES) || |
| 95 sync_service->IsUsingSecondaryPassphrase() || |
| 96 !history_service) { |
| 41 callback.Run(false); | 97 callback.Run(false); |
| 42 return; | 98 return; |
| 43 } | 99 } |
| 44 | 100 |
| 45 ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( | 101 // Return the boolean product of QueryWebAndAppActivity and |
| 46 sync_service, history_service, callback); | 102 // QueryOtherFormsOfBrowsingHistory. MergeBooleanCallbacks deletes itself |
| 103 // after processing both callbacks. |
| 104 MergeBooleanCallbacks* merger = new MergeBooleanCallbacks(2, callback); |
| 105 history_service->QueryWebAndAppActivity(base::Bind( |
| 106 &MergeBooleanCallbacks::RunCallback, base::Unretained(merger))); |
| 107 history_service->QueryOtherFormsOfBrowsingHistory( |
| 108 channel, user_agent, |
| 109 base::Bind( |
| 110 &MergeBooleanCallbacks::RunCallback, base::Unretained(merger))); |
| 47 } | 111 } |
| 48 | 112 |
| 49 } // namespace browsing_data_ui | 113 } // namespace browsing_data_ui |
| OLD | NEW |