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