OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/ui/sync/profile_signin_confirmation_helper.h" | 5 #include "chrome/browser/ui/sync/profile_signin_confirmation_helper.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
11 #include "base/task/cancelable_task_tracker.h" | 11 #include "base/task/cancelable_task_tracker.h" |
12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 12 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
13 #include "chrome/browser/common/cancelable_request.h" | 13 #include "chrome/browser/common/cancelable_request.h" |
14 #include "chrome/browser/extensions/extension_service.h" | 14 #include "chrome/browser/extensions/extension_service.h" |
15 #include "chrome/browser/history/history_backend.h" | 15 #include "chrome/browser/history/history_backend.h" |
16 #include "chrome/browser/history/history_db_task.h" | 16 #include "chrome/browser/history/history_db_task.h" |
17 #include "chrome/browser/history/history_service.h" | 17 #include "chrome/browser/history/history_service.h" |
18 #include "chrome/browser/history/history_service_factory.h" | 18 #include "chrome/browser/history/history_service_factory.h" |
19 #include "chrome/browser/history/history_types.h" | 19 #include "chrome/browser/history/history_types.h" |
20 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
21 #include "chrome/common/extensions/extension_constants.h" | 21 #include "chrome/common/extensions/extension_constants.h" |
22 #include "chrome/common/extensions/sync_helper.h" | 22 #include "chrome/common/extensions/sync_helper.h" |
23 #include "components/bookmarks/browser/bookmark_model.h" | 23 #include "components/bookmarks/browser/bookmark_model.h" |
| 24 #include "content/public/browser/browser_thread.h" |
24 #include "extensions/browser/extension_system.h" | 25 #include "extensions/browser/extension_system.h" |
25 #include "extensions/common/extension.h" | 26 #include "extensions/common/extension.h" |
26 #include "extensions/common/extension_set.h" | 27 #include "extensions/common/extension_set.h" |
27 #include "ui/gfx/color_utils.h" | 28 #include "ui/gfx/color_utils.h" |
28 #include "ui/native_theme/native_theme.h" | 29 #include "ui/native_theme/native_theme.h" |
29 | 30 |
30 namespace { | 31 namespace { |
31 | 32 |
32 const int kHistoryEntriesBeforeNewProfilePrompt = 10; | 33 const int kHistoryEntriesBeforeNewProfilePrompt = 10; |
33 | 34 |
(...skipping 15 matching lines...) Expand all Loading... |
49 } | 50 } |
50 return true; | 51 return true; |
51 } | 52 } |
52 | 53 |
53 virtual void DoneRunOnMainThread() OVERRIDE { | 54 virtual void DoneRunOnMainThread() OVERRIDE { |
54 cb_.Run(has_typed_urls_); | 55 cb_.Run(has_typed_urls_); |
55 } | 56 } |
56 | 57 |
57 private: | 58 private: |
58 virtual ~HasTypedURLsTask() {} | 59 virtual ~HasTypedURLsTask() {} |
| 60 |
59 bool has_typed_urls_; | 61 bool has_typed_urls_; |
60 base::Callback<void(bool)> cb_; | 62 base::Callback<void(bool)> cb_; |
61 }; | 63 }; |
62 | 64 |
63 bool HasBookmarks(Profile* profile) { | 65 bool HasBookmarks(Profile* profile) { |
64 BookmarkModel* bookmarks = BookmarkModelFactory::GetForProfile(profile); | 66 BookmarkModel* bookmarks = BookmarkModelFactory::GetForProfile(profile); |
65 bool has_bookmarks = bookmarks && bookmarks->HasBookmarks(); | 67 bool has_bookmarks = bookmarks && bookmarks->HasBookmarks(); |
66 if (has_bookmarks) | 68 if (has_bookmarks) |
67 DVLOG(1) << "ProfileSigninConfirmationHelper: profile contains bookmarks"; | 69 DVLOG(1) << "ProfileSigninConfirmationHelper: profile contains bookmarks"; |
68 return has_bookmarks; | 70 return has_bookmarks; |
69 } | 71 } |
70 | 72 |
71 // Helper functions for Chrome profile signin. | 73 // Helper functions for Chrome profile signin. |
72 class ProfileSigninConfirmationHelper | 74 class ProfileSigninConfirmationHelper { |
73 : public base::RefCounted<ProfileSigninConfirmationHelper> { | |
74 public: | 75 public: |
75 ProfileSigninConfirmationHelper( | 76 ProfileSigninConfirmationHelper( |
76 Profile* profile, | 77 Profile* profile, |
77 const base::Callback<void(bool)>& return_result); | 78 const base::Callback<void(bool)>& return_result); |
78 void CheckHasHistory(int max_entries); | 79 void CheckHasHistory(int max_entries); |
79 void CheckHasTypedURLs(); | 80 void CheckHasTypedURLs(); |
80 void set_pending_requests(int requests); | |
81 | 81 |
82 private: | 82 private: |
83 friend class base::RefCounted<ProfileSigninConfirmationHelper>; | 83 // Deletes itself. |
84 | |
85 ~ProfileSigninConfirmationHelper(); | 84 ~ProfileSigninConfirmationHelper(); |
86 | 85 |
87 void OnHistoryQueryResults(size_t max_entries, | 86 void OnHistoryQueryResults(size_t max_entries, |
88 history::QueryResults* results); | 87 history::QueryResults* results); |
89 void ReturnResult(bool result); | 88 void ReturnResult(bool result); |
90 | 89 |
91 // Weak pointer to the profile being signed-in. | 90 // Weak pointer to the profile being signed-in. |
92 Profile* profile_; | 91 Profile* profile_; |
93 | 92 |
94 // Used for async tasks. | 93 // Used for async tasks. |
95 base::CancelableTaskTracker task_tracker_; | 94 base::CancelableTaskTracker task_tracker_; |
96 | 95 |
97 // Keep track of how many async requests are pending. | 96 // Keep track of how many async requests are pending. |
98 int pending_requests_; | 97 int pending_requests_; |
99 | 98 |
100 // Indicates whether the result has already been returned to caller. | |
101 bool result_returned_; | |
102 | |
103 // Callback to pass the result back to the caller. | 99 // Callback to pass the result back to the caller. |
104 const base::Callback<void(bool)> return_result_; | 100 const base::Callback<void(bool)> return_result_; |
105 | 101 |
106 DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationHelper); | 102 DISALLOW_COPY_AND_ASSIGN(ProfileSigninConfirmationHelper); |
107 }; | 103 }; |
108 | 104 |
109 ProfileSigninConfirmationHelper::ProfileSigninConfirmationHelper( | 105 ProfileSigninConfirmationHelper::ProfileSigninConfirmationHelper( |
110 Profile* profile, | 106 Profile* profile, |
111 const base::Callback<void(bool)>& return_result) | 107 const base::Callback<void(bool)>& return_result) |
112 : profile_(profile), | 108 : profile_(profile), |
113 pending_requests_(0), | 109 pending_requests_(0), |
114 result_returned_(false), | |
115 return_result_(return_result) { | 110 return_result_(return_result) { |
116 } | 111 } |
117 | 112 |
118 ProfileSigninConfirmationHelper::~ProfileSigninConfirmationHelper() { | 113 ProfileSigninConfirmationHelper::~ProfileSigninConfirmationHelper() { |
| 114 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
119 } | 115 } |
120 | 116 |
121 void ProfileSigninConfirmationHelper::OnHistoryQueryResults( | 117 void ProfileSigninConfirmationHelper::OnHistoryQueryResults( |
122 size_t max_entries, | 118 size_t max_entries, |
123 history::QueryResults* results) { | 119 history::QueryResults* results) { |
124 history::QueryResults owned_results; | 120 history::QueryResults owned_results; |
125 results->Swap(&owned_results); | 121 results->Swap(&owned_results); |
126 bool too_much_history = owned_results.size() >= max_entries; | 122 bool too_much_history = owned_results.size() >= max_entries; |
127 if (too_much_history) { | 123 if (too_much_history) { |
128 DVLOG(1) << "ProfileSigninConfirmationHelper: profile contains " | 124 DVLOG(1) << "ProfileSigninConfirmationHelper: profile contains " |
129 << owned_results.size() << " history entries"; | 125 << owned_results.size() << " history entries"; |
130 } | 126 } |
131 ReturnResult(too_much_history); | 127 ReturnResult(too_much_history); |
132 } | 128 } |
133 | 129 |
134 void ProfileSigninConfirmationHelper::CheckHasHistory(int max_entries) { | 130 void ProfileSigninConfirmationHelper::CheckHasHistory(int max_entries) { |
135 HistoryService* service = | 131 HistoryService* service = |
136 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 132 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
| 133 pending_requests_++; |
137 if (!service) { | 134 if (!service) { |
138 ReturnResult(false); | 135 ReturnResult(false); |
139 return; | 136 return; |
140 } | 137 } |
141 history::QueryOptions opts; | 138 history::QueryOptions opts; |
142 opts.max_count = max_entries; | 139 opts.max_count = max_entries; |
143 service->QueryHistory( | 140 service->QueryHistory( |
144 base::string16(), | 141 base::string16(), |
145 opts, | 142 opts, |
146 base::Bind(&ProfileSigninConfirmationHelper::OnHistoryQueryResults, | 143 base::Bind(&ProfileSigninConfirmationHelper::OnHistoryQueryResults, |
147 this, | 144 base::Unretained(this), |
148 max_entries), | 145 max_entries), |
149 &task_tracker_); | 146 &task_tracker_); |
150 } | 147 } |
151 | 148 |
152 void ProfileSigninConfirmationHelper::CheckHasTypedURLs() { | 149 void ProfileSigninConfirmationHelper::CheckHasTypedURLs() { |
153 HistoryService* service = | 150 HistoryService* service = |
154 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 151 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
| 152 pending_requests_++; |
155 if (!service) { | 153 if (!service) { |
156 ReturnResult(false); | 154 ReturnResult(false); |
157 return; | 155 return; |
158 } | 156 } |
159 service->ScheduleDBTask( | 157 service->ScheduleDBTask( |
160 new HasTypedURLsTask( | 158 new HasTypedURLsTask( |
161 base::Bind(&ProfileSigninConfirmationHelper::ReturnResult, this)), | 159 base::Bind(&ProfileSigninConfirmationHelper::ReturnResult, |
| 160 base::Unretained(this))), |
162 &task_tracker_); | 161 &task_tracker_); |
163 } | 162 } |
164 | 163 |
165 void ProfileSigninConfirmationHelper::set_pending_requests(int requests) { | |
166 pending_requests_ = requests; | |
167 } | |
168 | |
169 void ProfileSigninConfirmationHelper::ReturnResult(bool result) { | 164 void ProfileSigninConfirmationHelper::ReturnResult(bool result) { |
| 165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
170 // Pass |true| into the callback as soon as one of the tasks passes a | 166 // Pass |true| into the callback as soon as one of the tasks passes a |
171 // result of |true|, otherwise pass the last returned result. | 167 // result of |true|, otherwise pass the last returned result. |
172 if (!result_returned_ && (--pending_requests_ == 0 || result)) { | 168 if (--pending_requests_ == 0 || result) { |
173 result_returned_ = true; | |
174 task_tracker_.TryCancelAll(); | |
175 return_result_.Run(result); | 169 return_result_.Run(result); |
| 170 |
| 171 // This leaks at shutdown if the HistoryService is destroyed, but |
| 172 // the process is going to die anyway. |
| 173 delete this; |
176 } | 174 } |
177 } | 175 } |
178 | 176 |
179 } // namespace | 177 } // namespace |
180 | 178 |
181 namespace ui { | 179 namespace ui { |
182 | 180 |
183 SkColor GetSigninConfirmationPromptBarColor(SkAlpha alpha) { | 181 SkColor GetSigninConfirmationPromptBarColor(SkAlpha alpha) { |
184 static const SkColor kBackgroundColor = | 182 static const SkColor kBackgroundColor = |
185 ui::NativeTheme::instance()->GetSystemColor( | 183 ui::NativeTheme::instance()->GetSystemColor( |
(...skipping 27 matching lines...) Expand all Loading... |
213 return true; | 211 return true; |
214 } | 212 } |
215 } | 213 } |
216 } | 214 } |
217 return false; | 215 return false; |
218 } | 216 } |
219 | 217 |
220 void CheckShouldPromptForNewProfile( | 218 void CheckShouldPromptForNewProfile( |
221 Profile* profile, | 219 Profile* profile, |
222 const base::Callback<void(bool)>& return_result) { | 220 const base::Callback<void(bool)>& return_result) { |
| 221 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 222 |
223 if (HasBeenShutdown(profile) || | 223 if (HasBeenShutdown(profile) || |
224 HasBookmarks(profile) || | 224 HasBookmarks(profile) || |
225 HasSyncedExtensions(profile)) { | 225 HasSyncedExtensions(profile)) { |
226 return_result.Run(true); | 226 return_result.Run(true); |
227 return; | 227 return; |
228 } | 228 } |
229 // Fire asynchronous queries for profile data. | 229 // Fire asynchronous queries for profile data. |
230 scoped_refptr<ProfileSigninConfirmationHelper> helper = | 230 ProfileSigninConfirmationHelper* helper = |
231 new ProfileSigninConfirmationHelper(profile, return_result); | 231 new ProfileSigninConfirmationHelper(profile, return_result); |
232 const int requests = 2; | |
233 helper->set_pending_requests(requests); | |
234 helper->CheckHasHistory(kHistoryEntriesBeforeNewProfilePrompt); | 232 helper->CheckHasHistory(kHistoryEntriesBeforeNewProfilePrompt); |
235 helper->CheckHasTypedURLs(); | 233 helper->CheckHasTypedURLs(); |
236 } | 234 } |
237 | 235 |
238 } // namespace ui | 236 } // namespace ui |
OLD | NEW |