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( | |
achuithb
2015/09/18 19:05:52
Make this a private method of Delegate
| |
34 GoodiesDisplayer::Delegate* delegate) { | |
35 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
36 return delegate->GetTimeSinceOobe() <= | |
37 base::TimeDelta::FromDays(kMaxDaysAfterOobeForGoodies); | |
38 } | |
39 | |
40 // Callback into main thread to set pref to |false| if too long since oobe, or | |
41 // to create GoodiesDisplayer otherwise. | |
42 void UpdateGoodiesPrefCantShow(GoodiesDisplayer::Delegate* delegate, | |
achuithb
2015/09/18 19:05:51
Make this a private method of Delegate
| |
43 bool can_show_goodies) { | |
44 if (can_show_goodies) { | |
45 delegate->CreateGoodiesDisplayer(); | |
achuithb
2015/09/18 19:05:52
Inline the implementation of CreateGoodiesDisplaye
| |
46 } else { | |
47 g_browser_process->local_state()->SetBoolean(prefs::kCanShowOobeGoodiesPage, | |
48 false); | |
49 } | |
50 } | |
51 | |
52 } // namespace | |
53 | |
54 GoodiesDisplayer::Delegate::Delegate() {} | |
55 | |
56 GoodiesDisplayer::Delegate::~Delegate() {} | |
57 | |
58 void GoodiesDisplayer::Delegate::CreateGoodiesDisplayer() { | |
59 if (!goodies_displayer_) | |
60 goodies_displayer_.reset(new first_run::GoodiesDisplayer(this)); | |
61 } | |
62 | |
63 void GoodiesDisplayer::Delegate::DestroyGoodiesDisplayer() { | |
64 goodies_displayer_.reset(); | |
65 } | |
66 | |
67 base::TimeDelta GoodiesDisplayer::Delegate::GetTimeSinceOobe() { | |
68 DCHECK(content::BrowserThread::GetBlockingPool()->RunsTasksOnCurrentThread()); | |
69 return StartupUtils::GetTimeSinceOobeFlagFileCreation(); | |
70 } | |
71 | |
72 GoodiesDisplayer::GoodiesDisplayer(Delegate* delegate) : delegate_(delegate) { | |
73 BrowserList::AddObserver(this); | |
74 } | |
75 | |
76 GoodiesDisplayer::~GoodiesDisplayer() {} | |
77 | |
78 // If Goodies page hasn't been shown yet, and Chromebook isn't too old, create | |
79 // GoodiesDisplayer to observe BrowserList. | |
80 void GoodiesDisplayer::Init(Delegate* delegate) { | |
achuithb
2015/09/18 19:05:52
Make this a non-static method
| |
81 if (g_browser_process->local_state()->GetBoolean( | |
82 prefs::kCanShowOobeGoodiesPage)) | |
83 base::PostTaskAndReplyWithResult( | |
84 content::BrowserThread::GetBlockingPool(), FROM_HERE, | |
85 base::Bind(&CheckGoodiesPrefAgainstOobeTimestamp, delegate), | |
achuithb
2015/09/18 19:05:52
use WeakFactory
| |
86 base::Bind(&UpdateGoodiesPrefCantShow, delegate)); | |
87 } | |
88 | |
89 // If conditions enumerated below are met, this loads the Oobe Goodies page for | |
90 // new Chromebooks; when appropriate, it uses pref to mark page as shown, | |
91 // removes itself from BrowserListObservers, and deletes itself. | |
92 void GoodiesDisplayer::OnBrowserSetLastActive(Browser* browser) { | |
93 // 1. Not guest or incognito session (keep observing). | |
94 if (browser->profile()->IsOffTheRecord()) | |
95 return; | |
96 | |
97 PrefService* local_state = g_browser_process->local_state(); | |
98 // 2. Not previously shown, or otherwise marked as unavailable. | |
99 if (local_state->GetBoolean(prefs::kCanShowOobeGoodiesPage)) { | |
100 // 3. Device not enterprise enrolled. | |
101 if (!g_browser_process->platform_part() | |
102 ->browser_policy_connector_chromeos() | |
103 ->IsEnterpriseManaged()) | |
104 chrome::AddTabAt(browser, GURL(kGoodiesURL), 2, false); | |
105 | |
106 // Set to |false| whether enterprise enrolled or Goodies shown. | |
107 local_state->SetBoolean(prefs::kCanShowOobeGoodiesPage, false); | |
108 } | |
109 | |
110 // Regardless of how we got here, we don't henceforth need to show Goodies. | |
111 BrowserList::RemoveObserver(this); | |
112 delegate_->DestroyGoodiesDisplayer(); | |
113 } | |
114 | |
115 } // namespace first_run | |
116 } // namespace chromeos | |
OLD | NEW |