| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 // This code glues the RLZ library DLL with Chrome. It allows Chrome to work | 5 // This code glues the RLZ library DLL with Chrome. It allows Chrome to work |
| 6 // with or without the DLL being present. If the DLL is not present the | 6 // with or without the DLL being present. If the DLL is not present the |
| 7 // functions do nothing and just return false. | 7 // functions do nothing and just return false. |
| 8 | 8 |
| 9 #include "chrome/browser/rlz/rlz.h" | 9 #include "chrome/browser/rlz/rlz.h" |
| 10 | 10 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 } // namespace GoogleUpdateSettings | 52 } // namespace GoogleUpdateSettings |
| 53 #endif | 53 #endif |
| 54 | 54 |
| 55 using content::BrowserThread; | 55 using content::BrowserThread; |
| 56 using content::NavigationEntry; | 56 using content::NavigationEntry; |
| 57 | 57 |
| 58 namespace { | 58 namespace { |
| 59 | 59 |
| 60 // Maximum and minimum delay for financial ping we would allow to be set through | 60 // Maximum and minimum delay for financial ping we would allow to be set through |
| 61 // master preferences. Somewhat arbitrary, may need to be adjusted in future. | 61 // master preferences. Somewhat arbitrary, may need to be adjusted in future. |
| 62 const int kMaxDelay = 200 * 1000; | 62 const base::TimeDelta kMaxInitDelay = base::TimeDelta::FromSeconds(200); |
| 63 const int kMinDelay = 20 * 1000; | 63 const base::TimeDelta kMinInitDelay = base::TimeDelta::FromSeconds(20); |
| 64 | 64 |
| 65 bool IsGoogleUrl(const GURL& url) { | 65 bool IsGoogleUrl(const GURL& url) { |
| 66 return google_util::IsGoogleHomePageUrl(url.possibly_invalid_spec()); | 66 return google_util::IsGoogleHomePageUrl(url.possibly_invalid_spec()); |
| 67 } | 67 } |
| 68 | 68 |
| 69 bool IsBrandOrganic(const std::string& brand) { | 69 bool IsBrandOrganic(const std::string& brand) { |
| 70 return brand.empty() || google_util::IsOrganic(brand); | 70 return brand.empty() || google_util::IsOrganic(brand); |
| 71 } | 71 } |
| 72 | 72 |
| 73 void RecordProductEvents(bool first_run, | 73 void RecordProductEvents(bool first_run, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 RLZTracker::RLZTracker() | 182 RLZTracker::RLZTracker() |
| 183 : first_run_(false), | 183 : first_run_(false), |
| 184 send_ping_immediately_(false), | 184 send_ping_immediately_(false), |
| 185 is_google_default_search_(false), | 185 is_google_default_search_(false), |
| 186 is_google_homepage_(false), | 186 is_google_homepage_(false), |
| 187 is_google_in_startpages_(false), | 187 is_google_in_startpages_(false), |
| 188 worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()), | 188 worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()), |
| 189 already_ran_(false), | 189 already_ran_(false), |
| 190 omnibox_used_(false), | 190 omnibox_used_(false), |
| 191 homepage_used_(false), | 191 homepage_used_(false), |
| 192 min_delay_(kMinDelay) { | 192 min_init_delay_(kMinInitDelay) { |
| 193 } | 193 } |
| 194 | 194 |
| 195 RLZTracker::~RLZTracker() { | 195 RLZTracker::~RLZTracker() { |
| 196 } | 196 } |
| 197 | 197 |
| 198 // static | 198 // static |
| 199 bool RLZTracker::InitRlzDelayed(bool first_run, | 199 bool RLZTracker::InitRlzDelayed(bool first_run, |
| 200 int delay, | 200 bool send_ping_immediately, |
| 201 base::TimeDelta delay, |
| 201 bool is_google_default_search, | 202 bool is_google_default_search, |
| 202 bool is_google_homepage, | 203 bool is_google_homepage, |
| 203 bool is_google_in_startpages) { | 204 bool is_google_in_startpages) { |
| 204 return GetInstance()->Init(first_run, delay, is_google_default_search, | 205 return GetInstance()->Init(first_run, send_ping_immediately, delay, |
| 205 is_google_homepage, is_google_in_startpages); | 206 is_google_default_search, is_google_homepage, |
| 207 is_google_in_startpages); |
| 206 } | 208 } |
| 207 | 209 |
| 208 // static | 210 // static |
| 209 bool RLZTracker::InitRlzFromProfileDelayed(Profile* profile, | 211 bool RLZTracker::InitRlzFromProfileDelayed(Profile* profile, |
| 210 bool first_run, | 212 bool first_run, |
| 211 int delay) { | 213 bool send_ping_immediately, |
| 214 base::TimeDelta delay) { |
| 212 bool is_google_default_search = false; | 215 bool is_google_default_search = false; |
| 213 TemplateURLService* template_url_service = | 216 TemplateURLService* template_url_service = |
| 214 TemplateURLServiceFactory::GetForProfile(profile); | 217 TemplateURLServiceFactory::GetForProfile(profile); |
| 215 if (template_url_service) { | 218 if (template_url_service) { |
| 216 const TemplateURL* url_template = | 219 const TemplateURL* url_template = |
| 217 template_url_service->GetDefaultSearchProvider(); | 220 template_url_service->GetDefaultSearchProvider(); |
| 218 is_google_default_search = | 221 is_google_default_search = |
| 219 url_template && url_template->url_ref().HasGoogleBaseURLs(); | 222 url_template && url_template->url_ref().HasGoogleBaseURLs(); |
| 220 } | 223 } |
| 221 | 224 |
| 222 PrefService* pref_service = profile->GetPrefs(); | 225 PrefService* pref_service = profile->GetPrefs(); |
| 223 bool is_google_homepage = google_util::IsGoogleHomePageUrl( | 226 bool is_google_homepage = google_util::IsGoogleHomePageUrl( |
| 224 pref_service->GetString(prefs::kHomePage)); | 227 pref_service->GetString(prefs::kHomePage)); |
| 225 | 228 |
| 226 bool is_google_in_startpages = false; | 229 bool is_google_in_startpages = false; |
| 227 SessionStartupPref session_startup_prefs = | 230 SessionStartupPref session_startup_prefs = |
| 228 StartupBrowserCreator::GetSessionStartupPref( | 231 StartupBrowserCreator::GetSessionStartupPref( |
| 229 *CommandLine::ForCurrentProcess(), profile); | 232 *CommandLine::ForCurrentProcess(), profile); |
| 230 if (session_startup_prefs.type == SessionStartupPref::URLS) { | 233 if (session_startup_prefs.type == SessionStartupPref::URLS) { |
| 231 is_google_in_startpages = std::count_if(session_startup_prefs.urls.begin(), | 234 is_google_in_startpages = std::count_if(session_startup_prefs.urls.begin(), |
| 232 session_startup_prefs.urls.end(), | 235 session_startup_prefs.urls.end(), |
| 233 IsGoogleUrl) > 0; | 236 IsGoogleUrl) > 0; |
| 234 } | 237 } |
| 235 | 238 |
| 236 if (!InitRlzDelayed(first_run, delay, | 239 if (!InitRlzDelayed(first_run, send_ping_immediately, delay, |
| 237 is_google_default_search, is_google_homepage, | 240 is_google_default_search, is_google_homepage, |
| 238 is_google_in_startpages)) { | 241 is_google_in_startpages)) { |
| 239 return false; | 242 return false; |
| 240 } | 243 } |
| 241 | 244 |
| 242 // Prime the RLZ cache for the home page access point so that its avaiable | 245 // Prime the RLZ cache for the home page access point so that its avaiable |
| 243 // for the startup page if needed (i.e., when the startup page is set to | 246 // for the startup page if needed (i.e., when the startup page is set to |
| 244 // the home page). | 247 // the home page). |
| 245 GetAccessPointRlz(CHROME_HOME_PAGE, NULL); | 248 GetAccessPointRlz(CHROME_HOME_PAGE, NULL); |
| 246 | 249 |
| 247 return true; | 250 return true; |
| 248 } | 251 } |
| 249 | 252 |
| 250 bool RLZTracker::Init(bool first_run, | 253 bool RLZTracker::Init(bool first_run, |
| 251 int delay, | 254 bool send_ping_immediately, |
| 255 base::TimeDelta delay, |
| 252 bool is_google_default_search, | 256 bool is_google_default_search, |
| 253 bool is_google_homepage, | 257 bool is_google_homepage, |
| 254 bool is_google_in_startpages) { | 258 bool is_google_in_startpages) { |
| 255 first_run_ = first_run; | 259 first_run_ = first_run; |
| 256 is_google_default_search_ = is_google_default_search; | 260 is_google_default_search_ = is_google_default_search; |
| 257 is_google_homepage_ = is_google_homepage; | 261 is_google_homepage_ = is_google_homepage; |
| 258 is_google_in_startpages_ = is_google_in_startpages; | 262 is_google_in_startpages_ = is_google_in_startpages; |
| 263 send_ping_immediately_ = send_ping_immediately; |
| 259 | 264 |
| 260 // A negative delay means that a financial ping should be sent immediately | 265 delay = std::min(kMinInitDelay, std::max(min_init_delay_, delay)); |
| 261 // after a first search is recorded, without waiting for the next restart | |
| 262 // of chrome. However, we only want this behaviour on first run. | |
| 263 send_ping_immediately_ = false; | |
| 264 if (delay < 0) { | |
| 265 send_ping_immediately_ = true; | |
| 266 delay = -delay; | |
| 267 } | |
| 268 | |
| 269 delay *= 1000; | |
| 270 delay = (delay < min_delay_) ? min_delay_ : delay; | |
| 271 delay = (delay > kMaxDelay) ? kMaxDelay : delay; | |
| 272 | 266 |
| 273 if (google_util::GetBrand(&brand_) && !IsBrandOrganic(brand_)) { | 267 if (google_util::GetBrand(&brand_) && !IsBrandOrganic(brand_)) { |
| 274 // Register for notifications from the omnibox so that we can record when | 268 // Register for notifications from the omnibox so that we can record when |
| 275 // the user performs a first search. | 269 // the user performs a first search. |
| 276 registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, | 270 registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
| 277 content::NotificationService::AllSources()); | 271 content::NotificationService::AllSources()); |
| 278 // If instant is enabled we'll start searching as soon as the user starts | 272 // If instant is enabled we'll start searching as soon as the user starts |
| 279 // typing in the omnibox (which triggers INSTANT_CONTROLLER_UPDATED). | 273 // typing in the omnibox (which triggers INSTANT_CONTROLLER_UPDATED). |
| 280 registrar_.Add(this, chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 274 registrar_.Add(this, chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
| 281 content::NotificationService::AllSources()); | 275 content::NotificationService::AllSources()); |
| 282 | 276 |
| 283 // Register for notifications from navigations, to see if the user has used | 277 // Register for notifications from navigations, to see if the user has used |
| 284 // the home page. | 278 // the home page. |
| 285 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 279 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 286 content::NotificationService::AllSources()); | 280 content::NotificationService::AllSources()); |
| 287 } | 281 } |
| 288 google_util::GetReactivationBrand(&reactivation_brand_); | 282 google_util::GetReactivationBrand(&reactivation_brand_); |
| 289 | 283 |
| 290 rlz_lib::SetURLRequestContext(g_browser_process->system_request_context()); | 284 rlz_lib::SetURLRequestContext(g_browser_process->system_request_context()); |
| 291 ScheduleDelayedInit(delay); | 285 ScheduleDelayedInit(delay); |
| 292 | 286 |
| 293 return true; | 287 return true; |
| 294 } | 288 } |
| 295 | 289 |
| 296 void RLZTracker::ScheduleDelayedInit(int delay) { | 290 void RLZTracker::ScheduleDelayedInit(base::TimeDelta delay) { |
| 297 // The RLZTracker is a singleton object that outlives any runnable tasks | 291 // The RLZTracker is a singleton object that outlives any runnable tasks |
| 298 // that will be queued up. | 292 // that will be queued up. |
| 299 BrowserThread::GetBlockingPool()->PostDelayedSequencedWorkerTask( | 293 BrowserThread::GetBlockingPool()->PostDelayedSequencedWorkerTask( |
| 300 worker_pool_token_, | 294 worker_pool_token_, |
| 301 FROM_HERE, | 295 FROM_HERE, |
| 302 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), | 296 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), |
| 303 base::TimeDelta::FromMilliseconds(delay)); | 297 delay); |
| 304 } | 298 } |
| 305 | 299 |
| 306 void RLZTracker::DelayedInit() { | 300 void RLZTracker::DelayedInit() { |
| 307 bool schedule_ping = false; | 301 bool schedule_ping = false; |
| 308 | 302 |
| 309 // For organic brandcodes do not use rlz at all. Empty brandcode usually | 303 // For organic brandcodes do not use rlz at all. Empty brandcode usually |
| 310 // means a chromium install. This is ok. | 304 // means a chromium install. This is ok. |
| 311 if (!IsBrandOrganic(brand_)) { | 305 if (!IsBrandOrganic(brand_)) { |
| 312 RecordProductEvents(first_run_, is_google_default_search_, | 306 RecordProductEvents(first_run_, is_google_default_search_, |
| 313 is_google_homepage_, is_google_in_startpages_, | 307 is_google_homepage_, is_google_in_startpages_, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 325 schedule_ping = true; | 319 schedule_ping = true; |
| 326 } | 320 } |
| 327 | 321 |
| 328 already_ran_ = true; | 322 already_ran_ = true; |
| 329 | 323 |
| 330 if (schedule_ping) | 324 if (schedule_ping) |
| 331 ScheduleFinancialPing(); | 325 ScheduleFinancialPing(); |
| 332 } | 326 } |
| 333 | 327 |
| 334 void RLZTracker::EnableZeroDelayForTesting() { | 328 void RLZTracker::EnableZeroDelayForTesting() { |
| 335 GetInstance()->min_delay_ = 0; | 329 GetInstance()->min_init_delay_ = base::TimeDelta(); |
| 336 } | 330 } |
| 337 | 331 |
| 338 void RLZTracker::ScheduleFinancialPing() { | 332 void RLZTracker::ScheduleFinancialPing() { |
| 339 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( | 333 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( |
| 340 worker_pool_token_, | 334 worker_pool_token_, |
| 341 FROM_HERE, | 335 FROM_HERE, |
| 342 base::Bind(&RLZTracker::PingNowImpl, base::Unretained(this))); | 336 base::Bind(&RLZTracker::PingNowImpl, base::Unretained(this))); |
| 343 } | 337 } |
| 344 | 338 |
| 345 void RLZTracker::PingNowImpl() { | 339 void RLZTracker::PingNowImpl() { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 return; | 448 return; |
| 455 | 449 |
| 456 bool* record_used = point == CHROME_OMNIBOX ? | 450 bool* record_used = point == CHROME_OMNIBOX ? |
| 457 &omnibox_used_ : &homepage_used_; | 451 &omnibox_used_ : &homepage_used_; |
| 458 | 452 |
| 459 // Try to record event now, else set the flag to try later when we | 453 // Try to record event now, else set the flag to try later when we |
| 460 // attempt the ping. | 454 // attempt the ping. |
| 461 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) | 455 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) |
| 462 *record_used = true; | 456 *record_used = true; |
| 463 else if (send_ping_immediately_ && point == CHROME_OMNIBOX) | 457 else if (send_ping_immediately_ && point == CHROME_OMNIBOX) |
| 464 ScheduleDelayedInit(0); | 458 ScheduleDelayedInit(base::TimeDelta()); |
| 465 } | 459 } |
| 466 | 460 |
| 467 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { | 461 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { |
| 468 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 462 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 469 return false; | 463 return false; |
| 470 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( | 464 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( |
| 471 worker_pool_token_, | 465 worker_pool_token_, |
| 472 FROM_HERE, | 466 FROM_HERE, |
| 473 base::Bind(&RLZTracker::RecordFirstSearch, | 467 base::Bind(&RLZTracker::RecordFirstSearch, |
| 474 base::Unretained(this), point)); | 468 base::Unretained(this), point)); |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 base::Bind(base::IgnoreResult(&RLZTracker::GetAccessPointRlz), point, | 536 base::Bind(base::IgnoreResult(&RLZTracker::GetAccessPointRlz), point, |
| 543 not_used)); | 537 not_used)); |
| 544 return true; | 538 return true; |
| 545 } | 539 } |
| 546 | 540 |
| 547 // static | 541 // static |
| 548 void RLZTracker::CleanupRlz() { | 542 void RLZTracker::CleanupRlz() { |
| 549 GetInstance()->rlz_cache_.clear(); | 543 GetInstance()->rlz_cache_.clear(); |
| 550 GetInstance()->registrar_.RemoveAll(); | 544 GetInstance()->registrar_.RemoveAll(); |
| 551 } | 545 } |
| OLD | NEW |