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. |
(...skipping 14 matching lines...) Expand all Loading... | |
109 ProfileSigninConfirmationHelper::ProfileSigninConfirmationHelper( | 108 ProfileSigninConfirmationHelper::ProfileSigninConfirmationHelper( |
110 Profile* profile, | 109 Profile* profile, |
111 const base::Callback<void(bool)>& return_result) | 110 const base::Callback<void(bool)>& return_result) |
112 : profile_(profile), | 111 : profile_(profile), |
113 pending_requests_(0), | 112 pending_requests_(0), |
114 result_returned_(false), | 113 result_returned_(false), |
115 return_result_(return_result) { | 114 return_result_(return_result) { |
116 } | 115 } |
117 | 116 |
118 ProfileSigninConfirmationHelper::~ProfileSigninConfirmationHelper() { | 117 ProfileSigninConfirmationHelper::~ProfileSigninConfirmationHelper() { |
118 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
119 } | 119 } |
120 | 120 |
121 void ProfileSigninConfirmationHelper::OnHistoryQueryResults( | 121 void ProfileSigninConfirmationHelper::OnHistoryQueryResults( |
122 size_t max_entries, | 122 size_t max_entries, |
123 history::QueryResults* results) { | 123 history::QueryResults* results) { |
124 history::QueryResults owned_results; | 124 history::QueryResults owned_results; |
125 results->Swap(&owned_results); | 125 results->Swap(&owned_results); |
126 bool too_much_history = owned_results.size() >= max_entries; | 126 bool too_much_history = owned_results.size() >= max_entries; |
127 if (too_much_history) { | 127 if (too_much_history) { |
128 DVLOG(1) << "ProfileSigninConfirmationHelper: profile contains " | 128 DVLOG(1) << "ProfileSigninConfirmationHelper: profile contains " |
129 << owned_results.size() << " history entries"; | 129 << owned_results.size() << " history entries"; |
130 } | 130 } |
131 ReturnResult(too_much_history); | 131 ReturnResult(too_much_history); |
132 } | 132 } |
133 | 133 |
134 void ProfileSigninConfirmationHelper::CheckHasHistory(int max_entries) { | 134 void ProfileSigninConfirmationHelper::CheckHasHistory(int max_entries) { |
135 HistoryService* service = | 135 HistoryService* service = |
136 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 136 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
137 if (!service) { | 137 if (!service) { |
138 ReturnResult(false); | 138 ReturnResult(false); |
139 return; | 139 return; |
140 } | 140 } |
141 history::QueryOptions opts; | 141 history::QueryOptions opts; |
142 opts.max_count = max_entries; | 142 opts.max_count = max_entries; |
143 service->QueryHistory( | 143 service->QueryHistory( |
144 base::string16(), | 144 base::string16(), |
145 opts, | 145 opts, |
146 base::Bind(&ProfileSigninConfirmationHelper::OnHistoryQueryResults, | 146 base::Bind(&ProfileSigninConfirmationHelper::OnHistoryQueryResults, |
147 this, | 147 base::Unretained(this), |
148 max_entries), | 148 max_entries), |
149 &task_tracker_); | 149 &task_tracker_); |
150 pending_requests_++; | |
150 } | 151 } |
151 | 152 |
152 void ProfileSigninConfirmationHelper::CheckHasTypedURLs() { | 153 void ProfileSigninConfirmationHelper::CheckHasTypedURLs() { |
153 HistoryService* service = | 154 HistoryService* service = |
154 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); | 155 HistoryServiceFactory::GetForProfileWithoutCreating(profile_); |
155 if (!service) { | 156 if (!service) { |
156 ReturnResult(false); | 157 ReturnResult(false); |
157 return; | 158 return; |
158 } | 159 } |
159 service->ScheduleDBTask( | 160 service->ScheduleDBTask( |
160 new HasTypedURLsTask( | 161 new HasTypedURLsTask( |
161 base::Bind(&ProfileSigninConfirmationHelper::ReturnResult, this)), | 162 base::Bind(&ProfileSigninConfirmationHelper::ReturnResult, |
163 base::Unretained(this))), | |
162 &task_tracker_); | 164 &task_tracker_); |
163 } | 165 pending_requests_++; |
164 | |
165 void ProfileSigninConfirmationHelper::set_pending_requests(int requests) { | |
166 pending_requests_ = requests; | |
167 } | 166 } |
168 | 167 |
169 void ProfileSigninConfirmationHelper::ReturnResult(bool result) { | 168 void ProfileSigninConfirmationHelper::ReturnResult(bool result) { |
169 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
170 // Pass |true| into the callback as soon as one of the tasks passes a | 170 // Pass |true| into the callback as soon as one of the tasks passes a |
171 // result of |true|, otherwise pass the last returned result. | 171 // result of |true|, otherwise pass the last returned result. |
172 if (!result_returned_ && (--pending_requests_ == 0 || result)) { | 172 if (!result_returned_ && (--pending_requests_ == 0 || result)) { |
173 result_returned_ = true; | 173 result_returned_ = true; |
174 task_tracker_.TryCancelAll(); | 174 task_tracker_.TryCancelAll(); |
175 return_result_.Run(result); | 175 return_result_.Run(result); |
176 delete this; | |
Andrew T Wilson (Slow)
2014/07/14 11:04:45
Does this mean we leak if our callbacks aren't inv
Bernhard Bauer
2014/07/14 13:08:29
True, if the history backend shuts down, this won'
Andrew T Wilson (Slow)
2014/07/14 14:32:57
Just comment that it leaks during profile shutdown
dconnelly
2014/07/15 11:20:01
I agree with just adding the comment.
| |
176 } | 177 } |
177 } | 178 } |
178 | 179 |
179 } // namespace | 180 } // namespace |
180 | 181 |
181 namespace ui { | 182 namespace ui { |
182 | 183 |
183 SkColor GetSigninConfirmationPromptBarColor(SkAlpha alpha) { | 184 SkColor GetSigninConfirmationPromptBarColor(SkAlpha alpha) { |
184 static const SkColor kBackgroundColor = | 185 static const SkColor kBackgroundColor = |
185 ui::NativeTheme::instance()->GetSystemColor( | 186 ui::NativeTheme::instance()->GetSystemColor( |
(...skipping 27 matching lines...) Expand all Loading... | |
213 return true; | 214 return true; |
214 } | 215 } |
215 } | 216 } |
216 } | 217 } |
217 return false; | 218 return false; |
218 } | 219 } |
219 | 220 |
220 void CheckShouldPromptForNewProfile( | 221 void CheckShouldPromptForNewProfile( |
221 Profile* profile, | 222 Profile* profile, |
222 const base::Callback<void(bool)>& return_result) { | 223 const base::Callback<void(bool)>& return_result) { |
224 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
225 | |
223 if (HasBeenShutdown(profile) || | 226 if (HasBeenShutdown(profile) || |
224 HasBookmarks(profile) || | 227 HasBookmarks(profile) || |
225 HasSyncedExtensions(profile)) { | 228 HasSyncedExtensions(profile)) { |
226 return_result.Run(true); | 229 return_result.Run(true); |
227 return; | 230 return; |
228 } | 231 } |
229 // Fire asynchronous queries for profile data. | 232 // Fire asynchronous queries for profile data. |
230 scoped_refptr<ProfileSigninConfirmationHelper> helper = | 233 ProfileSigninConfirmationHelper* helper = |
231 new ProfileSigninConfirmationHelper(profile, return_result); | 234 new ProfileSigninConfirmationHelper(profile, return_result); |
232 const int requests = 2; | |
233 helper->set_pending_requests(requests); | |
234 helper->CheckHasHistory(kHistoryEntriesBeforeNewProfilePrompt); | 235 helper->CheckHasHistory(kHistoryEntriesBeforeNewProfilePrompt); |
235 helper->CheckHasTypedURLs(); | 236 helper->CheckHasTypedURLs(); |
236 } | 237 } |
237 | 238 |
238 } // namespace ui | 239 } // namespace ui |
OLD | NEW |