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