[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 5 | #include "base/command_line.h" |
[email protected] | 1876f39 | 2014-01-16 16:13:57 | [diff] [blame] | 6 | #include "base/metrics/histogram.h" |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 7 | #include "chrome/browser/chrome_notification_types.h" |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 8 | #include "chrome/browser/chromeos/first_run/first_run_controller.h" |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 9 | #include "chrome/browser/chromeos/login/user_manager.h" |
| 10 | #include "chrome/browser/extensions/extension_service.h" |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 11 | #include "chrome/browser/ui/extensions/application_launch.h" |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 12 | #include "chrome/common/chrome_switches.h" |
| 13 | #include "chrome/common/pref_names.h" |
| 14 | #include "chromeos/chromeos_switches.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame^] | 15 | #include "components/pref_registry/pref_registry_syncable.h" |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 16 | #include "content/public/browser/notification_observer.h" |
| 17 | #include "content/public/browser/notification_registrar.h" |
| 18 | #include "content/public/browser/notification_service.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 19 | #include "extensions/browser/extension_system.h" |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 20 | #include "extensions/common/constants.h" |
| 21 | |
| 22 | namespace chromeos { |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 23 | namespace first_run { |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 24 | |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 25 | namespace { |
| 26 | |
| 27 | void LaunchDialogForProfile(Profile* profile) { |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 28 | ExtensionService* service = |
| 29 | extensions::ExtensionSystem::Get(profile)->extension_service(); |
| 30 | if (!service) |
| 31 | return; |
| 32 | |
| 33 | const extensions::Extension* extension = |
| 34 | service->GetExtensionById(extension_misc::kFirstRunDialogId, false); |
| 35 | if (!extension) |
| 36 | return; |
| 37 | |
| 38 | OpenApplication(AppLaunchParams( |
[email protected] | ec453fb | 2013-12-06 14:05:27 | [diff] [blame] | 39 | profile, extension, extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW)); |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 40 | profile->GetPrefs()->SetBoolean(prefs::kFirstRunTutorialShown, true); |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 43 | // Object of this class waits for session start. Then it launches or not |
| 44 | // launches first-run dialog depending on user prefs and flags. Than object |
| 45 | // deletes itself. |
| 46 | class DialogLauncher : public content::NotificationObserver { |
| 47 | public: |
| 48 | explicit DialogLauncher(Profile* profile) |
| 49 | : profile_(profile) { |
| 50 | DCHECK(profile); |
| 51 | registrar_.Add(this, |
| 52 | chrome::NOTIFICATION_SESSION_STARTED, |
| 53 | content::NotificationService::AllSources()); |
| 54 | } |
| 55 | |
| 56 | virtual ~DialogLauncher() {} |
| 57 | |
| 58 | virtual void Observe(int type, |
| 59 | const content::NotificationSource& source, |
| 60 | const content::NotificationDetails& details) OVERRIDE { |
| 61 | DCHECK(type == chrome::NOTIFICATION_SESSION_STARTED); |
| 62 | DCHECK(content::Details<const User>(details).ptr() == |
| 63 | UserManager::Get()->GetUserByProfile(profile_)); |
| 64 | CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 65 | bool launched_in_test = command_line->HasSwitch(::switches::kTestType); |
| 66 | bool launched_in_telemetry = |
| 67 | command_line->HasSwitch(switches::kOobeSkipPostLogin); |
| 68 | bool first_run_disabled = |
| 69 | command_line->HasSwitch(switches::kDisableFirstRunUI); |
| 70 | bool is_user_new = chromeos::UserManager::Get()->IsCurrentUserNew(); |
| 71 | bool first_run_forced = command_line->HasSwitch(switches::kForceFirstRunUI); |
| 72 | bool first_run_seen = |
| 73 | profile_->GetPrefs()->GetBoolean(prefs::kFirstRunTutorialShown); |
| 74 | if (!launched_in_telemetry && !first_run_disabled && |
| 75 | ((is_user_new && !first_run_seen && !launched_in_test) || |
| 76 | first_run_forced)) { |
| 77 | LaunchDialogForProfile(profile_); |
| 78 | } |
| 79 | delete this; |
| 80 | } |
| 81 | |
| 82 | private: |
| 83 | Profile* profile_; |
| 84 | content::NotificationRegistrar registrar_; |
| 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(DialogLauncher); |
| 87 | }; |
| 88 | |
| 89 | } // namespace |
| 90 | |
| 91 | void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) { |
| 92 | registry->RegisterBooleanPref( |
| 93 | prefs::kFirstRunTutorialShown, |
| 94 | false, |
| 95 | user_prefs::PrefRegistrySyncable::SYNCABLE_PRIORITY_PREF); |
| 96 | } |
| 97 | |
| 98 | void MaybeLaunchDialogAfterSessionStart() { |
| 99 | UserManager* user_manager = UserManager::Get(); |
| 100 | new DialogLauncher( |
| 101 | user_manager->GetProfileByUser(user_manager->GetActiveUser())); |
| 102 | } |
| 103 | |
| 104 | void LaunchTutorial() { |
[email protected] | 1876f39 | 2014-01-16 16:13:57 | [diff] [blame] | 105 | UMA_HISTOGRAM_BOOLEAN("CrosFirstRun.TutorialLaunched", true); |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 106 | FirstRunController::Start(); |
| 107 | } |
| 108 | |
[email protected] | 980d5f9e | 2013-12-17 17:47:08 | [diff] [blame] | 109 | } // namespace first_run |
[email protected] | 5ee08bf | 2013-11-30 05:25:30 | [diff] [blame] | 110 | } // namespace chromeos |