| 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 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 | 12 |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/debug/trace_event.h" | 15 #include "base/debug/trace_event.h" |
| 16 #include "base/message_loop.h" | 16 #include "base/message_loop.h" |
| 17 #include "base/string_util.h" | 17 #include "base/string_util.h" |
| 18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
| 19 #include "chrome/browser/browser_process.h" | 19 #include "chrome/browser/browser_process.h" |
| 20 #include "chrome/browser/google/google_util.h" | 20 #include "chrome/browser/google/google_util.h" |
| 21 #include "chrome/browser/prefs/pref_service.h" | 21 #include "chrome/browser/prefs/pref_service.h" |
| 22 #include "chrome/browser/prefs/session_startup_pref.h" | 22 #include "chrome/browser/prefs/session_startup_pref.h" |
| 23 #include "chrome/browser/search_engines/template_url.h" | 23 #include "chrome/browser/search_engines/template_url.h" |
| 24 #include "chrome/browser/search_engines/template_url_service.h" | 24 #include "chrome/browser/search_engines/template_url_service.h" |
| 25 #include "chrome/browser/search_engines/template_url_service_factory.h" | 25 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 26 #include "chrome/browser/ui/startup/startup_browser_creator.h" | 26 #include "chrome/browser/ui/startup/startup_browser_creator.h" |
| 27 #include "chrome/common/chrome_notification_types.h" | 27 #include "chrome/common/chrome_notification_types.h" |
| 28 #include "chrome/common/chrome_switches.h" |
| 28 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 29 #include "content/public/browser/browser_thread.h" | 30 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/navigation_entry.h" | 31 #include "content/public/browser/navigation_entry.h" |
| 31 #include "content/public/browser/notification_service.h" | 32 #include "content/public/browser/notification_service.h" |
| 32 #include "net/http/http_util.h" | 33 #include "net/http/http_util.h" |
| 33 | 34 |
| 34 #if defined(OS_WIN) | 35 #if defined(OS_WIN) |
| 35 #include "chrome/installer/util/google_update_settings.h" | 36 #include "chrome/installer/util/google_update_settings.h" |
| 36 #else | 37 #else |
| 37 namespace GoogleUpdateSettings { | 38 namespace GoogleUpdateSettings { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 52 } // namespace GoogleUpdateSettings | 53 } // namespace GoogleUpdateSettings |
| 53 #endif | 54 #endif |
| 54 | 55 |
| 55 using content::BrowserThread; | 56 using content::BrowserThread; |
| 56 using content::NavigationEntry; | 57 using content::NavigationEntry; |
| 57 | 58 |
| 58 namespace { | 59 namespace { |
| 59 | 60 |
| 60 // Maximum and minimum delay for financial ping we would allow to be set through | 61 // 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. | 62 // master preferences. Somewhat arbitrary, may need to be adjusted in future. |
| 62 const int kMaxDelay = 200 * 1000; | 63 const base::TimeDelta kMaxInitDelay = base::TimeDelta::FromSeconds(200); |
| 63 const int kMinDelay = 20 * 1000; | 64 const base::TimeDelta kMinInitDelay = base::TimeDelta::FromSeconds(20); |
| 64 | 65 |
| 65 bool IsGoogleUrl(const GURL& url) { | 66 bool IsGoogleUrl(const GURL& url) { |
| 66 return google_util::IsGoogleHomePageUrl(url.possibly_invalid_spec()); | 67 return google_util::IsGoogleHomePageUrl(url.possibly_invalid_spec()); |
| 67 } | 68 } |
| 68 | 69 |
| 69 bool IsBrandOrganic(const std::string& brand) { | 70 bool IsBrandOrganic(const std::string& brand) { |
| 70 return brand.empty() || google_util::IsOrganic(brand); | 71 return brand.empty() || google_util::IsOrganic(brand); |
| 71 } | 72 } |
| 72 | 73 |
| 73 void RecordProductEvents(bool first_run, | 74 void RecordProductEvents(bool first_run, |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 RLZTracker::RLZTracker() | 183 RLZTracker::RLZTracker() |
| 183 : first_run_(false), | 184 : first_run_(false), |
| 184 send_ping_immediately_(false), | 185 send_ping_immediately_(false), |
| 185 is_google_default_search_(false), | 186 is_google_default_search_(false), |
| 186 is_google_homepage_(false), | 187 is_google_homepage_(false), |
| 187 is_google_in_startpages_(false), | 188 is_google_in_startpages_(false), |
| 188 worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()), | 189 worker_pool_token_(BrowserThread::GetBlockingPool()->GetSequenceToken()), |
| 189 already_ran_(false), | 190 already_ran_(false), |
| 190 omnibox_used_(false), | 191 omnibox_used_(false), |
| 191 homepage_used_(false), | 192 homepage_used_(false), |
| 192 min_delay_(kMinDelay) { | 193 min_init_delay_(kMinInitDelay) { |
| 193 } | 194 } |
| 194 | 195 |
| 195 RLZTracker::~RLZTracker() { | 196 RLZTracker::~RLZTracker() { |
| 196 } | 197 } |
| 197 | 198 |
| 198 // static | 199 // static |
| 199 bool RLZTracker::InitRlzDelayed(bool first_run, | 200 bool RLZTracker::InitRlzDelayed(bool first_run, |
| 200 int delay, | 201 bool send_ping_immediately, |
| 202 base::TimeDelta delay, |
| 201 bool is_google_default_search, | 203 bool is_google_default_search, |
| 202 bool is_google_homepage, | 204 bool is_google_homepage, |
| 203 bool is_google_in_startpages) { | 205 bool is_google_in_startpages) { |
| 204 return GetInstance()->Init(first_run, delay, is_google_default_search, | 206 return GetInstance()->Init(first_run, send_ping_immediately, delay, |
| 205 is_google_homepage, is_google_in_startpages); | 207 is_google_default_search, is_google_homepage, |
| 208 is_google_in_startpages); |
| 206 } | 209 } |
| 207 | 210 |
| 208 // static | 211 // static |
| 209 bool RLZTracker::InitRlzFromProfileDelayed(Profile* profile, | 212 bool RLZTracker::InitRlzFromProfileDelayed(Profile* profile, |
| 210 bool first_run, | 213 bool first_run, |
| 211 int delay) { | 214 bool send_ping_immediately, |
| 215 base::TimeDelta delay) { |
| 212 bool is_google_default_search = false; | 216 bool is_google_default_search = false; |
| 213 TemplateURLService* template_url_service = | 217 TemplateURLService* template_url_service = |
| 214 TemplateURLServiceFactory::GetForProfile(profile); | 218 TemplateURLServiceFactory::GetForProfile(profile); |
| 215 if (template_url_service) { | 219 if (template_url_service) { |
| 216 const TemplateURL* url_template = | 220 const TemplateURL* url_template = |
| 217 template_url_service->GetDefaultSearchProvider(); | 221 template_url_service->GetDefaultSearchProvider(); |
| 218 is_google_default_search = | 222 is_google_default_search = |
| 219 url_template && url_template->url_ref().HasGoogleBaseURLs(); | 223 url_template && url_template->url_ref().HasGoogleBaseURLs(); |
| 220 } | 224 } |
| 221 | 225 |
| 222 PrefService* pref_service = profile->GetPrefs(); | 226 PrefService* pref_service = profile->GetPrefs(); |
| 223 bool is_google_homepage = google_util::IsGoogleHomePageUrl( | 227 bool is_google_homepage = google_util::IsGoogleHomePageUrl( |
| 224 pref_service->GetString(prefs::kHomePage)); | 228 pref_service->GetString(prefs::kHomePage)); |
| 225 | 229 |
| 226 bool is_google_in_startpages = false; | 230 bool is_google_in_startpages = false; |
| 227 SessionStartupPref session_startup_prefs = | 231 SessionStartupPref session_startup_prefs = |
| 228 StartupBrowserCreator::GetSessionStartupPref( | 232 StartupBrowserCreator::GetSessionStartupPref( |
| 229 *CommandLine::ForCurrentProcess(), profile); | 233 *CommandLine::ForCurrentProcess(), profile); |
| 230 if (session_startup_prefs.type == SessionStartupPref::URLS) { | 234 if (session_startup_prefs.type == SessionStartupPref::URLS) { |
| 231 is_google_in_startpages = std::count_if(session_startup_prefs.urls.begin(), | 235 is_google_in_startpages = std::count_if(session_startup_prefs.urls.begin(), |
| 232 session_startup_prefs.urls.end(), | 236 session_startup_prefs.urls.end(), |
| 233 IsGoogleUrl) > 0; | 237 IsGoogleUrl) > 0; |
| 234 } | 238 } |
| 235 | 239 |
| 236 if (!InitRlzDelayed(first_run, delay, | 240 if (!InitRlzDelayed(first_run, send_ping_immediately, delay, |
| 237 is_google_default_search, is_google_homepage, | 241 is_google_default_search, is_google_homepage, |
| 238 is_google_in_startpages)) { | 242 is_google_in_startpages)) { |
| 239 return false; | 243 return false; |
| 240 } | 244 } |
| 241 | 245 |
| 242 // Prime the RLZ cache for the home page access point so that its avaiable | 246 // 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 | 247 // for the startup page if needed (i.e., when the startup page is set to |
| 244 // the home page). | 248 // the home page). |
| 245 GetAccessPointRlz(CHROME_HOME_PAGE, NULL); | 249 GetAccessPointRlz(CHROME_HOME_PAGE, NULL); |
| 246 | 250 |
| 247 return true; | 251 return true; |
| 248 } | 252 } |
| 249 | 253 |
| 250 bool RLZTracker::Init(bool first_run, | 254 bool RLZTracker::Init(bool first_run, |
| 251 int delay, | 255 bool send_ping_immediately, |
| 256 base::TimeDelta delay, |
| 252 bool is_google_default_search, | 257 bool is_google_default_search, |
| 253 bool is_google_homepage, | 258 bool is_google_homepage, |
| 254 bool is_google_in_startpages) { | 259 bool is_google_in_startpages) { |
| 255 first_run_ = first_run; | 260 first_run_ = first_run; |
| 256 is_google_default_search_ = is_google_default_search; | 261 is_google_default_search_ = is_google_default_search; |
| 257 is_google_homepage_ = is_google_homepage; | 262 is_google_homepage_ = is_google_homepage; |
| 258 is_google_in_startpages_ = is_google_in_startpages; | 263 is_google_in_startpages_ = is_google_in_startpages; |
| 264 send_ping_immediately_ = send_ping_immediately; |
| 259 | 265 |
| 260 // A negative delay means that a financial ping should be sent immediately | 266 // Enable zero delays for testing. |
| 261 // after a first search is recorded, without waiting for the next restart | 267 if (CommandLine::ForCurrentProcess()->HasSwitch(::switches::kTestType)) |
| 262 // of chrome. However, we only want this behaviour on first run. | 268 EnableZeroDelayForTesting(); |
| 263 send_ping_immediately_ = false; | |
| 264 if (delay < 0) { | |
| 265 send_ping_immediately_ = true; | |
| 266 delay = -delay; | |
| 267 } | |
| 268 | 269 |
| 269 delay *= 1000; | 270 delay = std::min(kMaxInitDelay, std::max(min_init_delay_, delay)); |
| 270 delay = (delay < min_delay_) ? min_delay_ : delay; | |
| 271 delay = (delay > kMaxDelay) ? kMaxDelay : delay; | |
| 272 | 271 |
| 273 if (google_util::GetBrand(&brand_) && !IsBrandOrganic(brand_)) { | 272 if (google_util::GetBrand(&brand_) && !IsBrandOrganic(brand_)) { |
| 274 // Register for notifications from the omnibox so that we can record when | 273 // Register for notifications from the omnibox so that we can record when |
| 275 // the user performs a first search. | 274 // the user performs a first search. |
| 276 registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, | 275 registrar_.Add(this, chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
| 277 content::NotificationService::AllSources()); | 276 content::NotificationService::AllSources()); |
| 278 // If instant is enabled we'll start searching as soon as the user starts | 277 // If instant is enabled we'll start searching as soon as the user starts |
| 279 // typing in the omnibox (which triggers INSTANT_CONTROLLER_UPDATED). | 278 // typing in the omnibox (which triggers INSTANT_CONTROLLER_UPDATED). |
| 280 registrar_.Add(this, chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, | 279 registrar_.Add(this, chrome::NOTIFICATION_INSTANT_CONTROLLER_UPDATED, |
| 281 content::NotificationService::AllSources()); | 280 content::NotificationService::AllSources()); |
| 282 | 281 |
| 283 // Register for notifications from navigations, to see if the user has used | 282 // Register for notifications from navigations, to see if the user has used |
| 284 // the home page. | 283 // the home page. |
| 285 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 284 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 286 content::NotificationService::AllSources()); | 285 content::NotificationService::AllSources()); |
| 287 } | 286 } |
| 288 google_util::GetReactivationBrand(&reactivation_brand_); | 287 google_util::GetReactivationBrand(&reactivation_brand_); |
| 289 | 288 |
| 290 rlz_lib::SetURLRequestContext(g_browser_process->system_request_context()); | 289 rlz_lib::SetURLRequestContext(g_browser_process->system_request_context()); |
| 291 ScheduleDelayedInit(delay); | 290 ScheduleDelayedInit(delay); |
| 292 | 291 |
| 293 return true; | 292 return true; |
| 294 } | 293 } |
| 295 | 294 |
| 296 void RLZTracker::ScheduleDelayedInit(int delay) { | 295 void RLZTracker::ScheduleDelayedInit(base::TimeDelta delay) { |
| 297 // The RLZTracker is a singleton object that outlives any runnable tasks | 296 // The RLZTracker is a singleton object that outlives any runnable tasks |
| 298 // that will be queued up. | 297 // that will be queued up. |
| 299 BrowserThread::GetBlockingPool()->PostDelayedSequencedWorkerTask( | 298 BrowserThread::GetBlockingPool()->PostDelayedSequencedWorkerTask( |
| 300 worker_pool_token_, | 299 worker_pool_token_, |
| 301 FROM_HERE, | 300 FROM_HERE, |
| 302 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), | 301 base::Bind(&RLZTracker::DelayedInit, base::Unretained(this)), |
| 303 base::TimeDelta::FromMilliseconds(delay)); | 302 delay); |
| 304 } | 303 } |
| 305 | 304 |
| 306 void RLZTracker::DelayedInit() { | 305 void RLZTracker::DelayedInit() { |
| 307 bool schedule_ping = false; | 306 bool schedule_ping = false; |
| 308 | 307 |
| 309 // For organic brandcodes do not use rlz at all. Empty brandcode usually | 308 // For organic brandcodes do not use rlz at all. Empty brandcode usually |
| 310 // means a chromium install. This is ok. | 309 // means a chromium install. This is ok. |
| 311 if (!IsBrandOrganic(brand_)) { | 310 if (!IsBrandOrganic(brand_)) { |
| 312 RecordProductEvents(first_run_, is_google_default_search_, | 311 RecordProductEvents(first_run_, is_google_default_search_, |
| 313 is_google_homepage_, is_google_in_startpages_, | 312 is_google_homepage_, is_google_in_startpages_, |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 450 return; | 449 return; |
| 451 | 450 |
| 452 bool* record_used = point == CHROME_OMNIBOX ? | 451 bool* record_used = point == CHROME_OMNIBOX ? |
| 453 &omnibox_used_ : &homepage_used_; | 452 &omnibox_used_ : &homepage_used_; |
| 454 | 453 |
| 455 // Try to record event now, else set the flag to try later when we | 454 // Try to record event now, else set the flag to try later when we |
| 456 // attempt the ping. | 455 // attempt the ping. |
| 457 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) | 456 if (!RecordProductEvent(rlz_lib::CHROME, point, rlz_lib::FIRST_SEARCH)) |
| 458 *record_used = true; | 457 *record_used = true; |
| 459 else if (send_ping_immediately_ && point == CHROME_OMNIBOX) | 458 else if (send_ping_immediately_ && point == CHROME_OMNIBOX) |
| 460 ScheduleDelayedInit(0); | 459 ScheduleDelayedInit(base::TimeDelta()); |
| 461 } | 460 } |
| 462 | 461 |
| 463 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { | 462 bool RLZTracker::ScheduleRecordFirstSearch(rlz_lib::AccessPoint point) { |
| 464 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) | 463 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) |
| 465 return false; | 464 return false; |
| 466 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( | 465 BrowserThread::GetBlockingPool()->PostSequencedWorkerTask( |
| 467 worker_pool_token_, | 466 worker_pool_token_, |
| 468 FROM_HERE, | 467 FROM_HERE, |
| 469 base::Bind(&RLZTracker::RecordFirstSearch, | 468 base::Bind(&RLZTracker::RecordFirstSearch, |
| 470 base::Unretained(this), point)); | 469 base::Unretained(this), point)); |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 566 #endif | 565 #endif |
| 567 | 566 |
| 568 // static | 567 // static |
| 569 void RLZTracker::CleanupRlz() { | 568 void RLZTracker::CleanupRlz() { |
| 570 GetInstance()->rlz_cache_.clear(); | 569 GetInstance()->rlz_cache_.clear(); |
| 571 GetInstance()->registrar_.RemoveAll(); | 570 GetInstance()->registrar_.RemoveAll(); |
| 572 } | 571 } |
| 573 | 572 |
| 574 // static | 573 // static |
| 575 void RLZTracker::EnableZeroDelayForTesting() { | 574 void RLZTracker::EnableZeroDelayForTesting() { |
| 576 GetInstance()->min_delay_ = 0; | 575 GetInstance()->min_init_delay_ = base::TimeDelta(); |
| 577 } | 576 } |
| OLD | NEW |