Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/chromeos/first_run/goodies_displayer.h" | |
| 6 | |
| 7 #include "base/files/file_util.h" | |
| 8 #include "chrome/browser/browser_process.h" | |
| 9 #include "chrome/browser/chromeos/login/session/user_session_manager.h" | |
| 10 #include "chrome/browser/chromeos/login/startup_utils.h" | |
| 11 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h" | |
| 12 #include "chrome/browser/prefs/pref_service_syncable.h" | |
| 13 #include "chrome/browser/ui/browser.h" | |
| 14 #include "chrome/browser/ui/browser_tabstrip.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "content/public/browser/browser_thread.h" | |
| 17 | |
| 18 namespace chromeos { | |
| 19 namespace first_run { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 // ChromeOS Goodies page for device's first New Window. | |
| 24 const char kGoodiesURL[] = "https://www.google.com/chrome/devices/goodies.html"; | |
| 25 | |
| 26 // Max days after initial login that we're willing to show Goodies. | |
| 27 static const int kMaxDaysAfterOobeForGoodies = 14; | |
| 28 | |
| 29 // Checks timestamp on OOBE Complete flag file. kCanShowOobeGoodiesPage | |
| 30 // defaults to |true|; we set it to |false| (return |false|) for any device over | |
| 31 // kMaxDaysAfterOobeForGoodies days old, to avoid showing it after update on | |
| 32 // older devices. | |
| 33 bool CheckGoodiesPrefAgainstOobeTimestamp() { | |
| 34 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
| 35 const base::FilePath oobe_timestamp_file = | |
| 36 StartupUtils::GetOobeCompleteFlagPath(); | |
| 37 base::File::Info fileInfo; | |
| 38 if (base::GetFileInfo(oobe_timestamp_file, &fileInfo)) { | |
| 39 const base::TimeDelta time_since_oobe = | |
|
achuithb
2015/09/17 17:04:41
Wonder if it makes sense to add a function to Star
Greg Levin
2015/09/17 23:21:40
Done.
| |
| 40 base::Time::Now() - fileInfo.creation_time; | |
| 41 if (time_since_oobe > | |
| 42 base::TimeDelta::FromDays(kMaxDaysAfterOobeForGoodies)) | |
| 43 return false; | |
| 44 } | |
| 45 return true; | |
| 46 } | |
| 47 | |
| 48 // Callback into main thread to set pref to |false| if too long since oobe, or | |
| 49 // to create GoodiesDisplayer otherwise. | |
| 50 void UpdateGoodiesPrefCantShow(bool can_show_goodies) { | |
| 51 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 52 if (can_show_goodies) { | |
| 53 UserSessionManager::GetInstance()->CreateGoodiesDisplayer(); | |
| 54 } else { | |
| 55 g_browser_process->local_state()->SetBoolean(prefs::kCanShowOobeGoodiesPage, | |
| 56 false); | |
| 57 } | |
| 58 } | |
| 59 | |
| 60 } // namespace | |
| 61 | |
| 62 GoodiesDisplayer::GoodiesDisplayer() { | |
| 63 BrowserList::AddObserver(this); | |
| 64 } | |
| 65 | |
| 66 // If Goodies page hasn't been shown yet, and Chromebook isn't too old, create | |
| 67 // GoodiesDisplayer to observe BrowserList. | |
| 68 void GoodiesDisplayer::Init() { | |
| 69 if (g_browser_process->local_state()->GetBoolean( | |
| 70 prefs::kCanShowOobeGoodiesPage)) | |
| 71 base::PostTaskAndReplyWithResult( | |
| 72 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
| 73 base::Bind(&CheckGoodiesPrefAgainstOobeTimestamp), | |
| 74 base::Bind(&UpdateGoodiesPrefCantShow)); | |
| 75 } | |
| 76 | |
| 77 // If conditions enumerated below are met, this loads the Oobe Goodies page for | |
| 78 // new Chromebooks; when appropriate, it uses pref to mark page as shown, | |
| 79 // removes itself from BrowserListObservers, and deletes itself. | |
| 80 void GoodiesDisplayer::OnBrowserSetLastActive(Browser* browser) { | |
| 81 // 1. Not guest or incognito session (keep observing). | |
| 82 if (browser->profile()->IsOffTheRecord()) | |
| 83 return; | |
| 84 | |
| 85 PrefService* local_state = g_browser_process->local_state(); | |
| 86 // 2. Not previously shown, or otherwise marked as unavailable. | |
| 87 if (local_state->GetBoolean(prefs::kCanShowOobeGoodiesPage)) { | |
| 88 // 3. Device not enterprise enrolled. | |
| 89 if (!g_browser_process->platform_part() | |
| 90 ->browser_policy_connector_chromeos() | |
| 91 ->IsEnterpriseManaged()) | |
| 92 chrome::AddTabAt(browser, GURL(kGoodiesURL), 2, false); | |
| 93 | |
| 94 // Set to |false| whether enterprise enrolled or Goodies shown. | |
| 95 local_state->SetBoolean(prefs::kCanShowOobeGoodiesPage, false); | |
| 96 } | |
| 97 | |
| 98 // Regardless of how we got here, we don't henceforth need to show Goodies. | |
| 99 BrowserList::RemoveObserver(this); | |
| 100 UserSessionManager::GetInstance()->DestroyGoodiesDisplayer(); | |
| 101 } | |
| 102 | |
| 103 } // namespace first_run | |
| 104 } // namespace chromeos | |
| OLD | NEW |