Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / first_run / first_run.cc
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 "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/chromeos/first_run/first_run_controller.h"
9 #include "chrome/browser/chromeos/login/user_manager.h"
10 #include "chrome/browser/extensions/extension_service.h"
11 #include "chrome/browser/extensions/extension_system.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/user_prefs/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/common/constants.h"
21
22 namespace chromeos {
23 namespace first_run {
24
25 namespace {
26
27 void LaunchDialogForProfile(Profile* profile) {
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(
39       profile, extension, extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW));
40   profile->GetPrefs()->SetBoolean(prefs::kFirstRunTutorialShown, true);
41 }
42
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() {
105   UMA_HISTOGRAM_BOOLEAN("CrosFirstRun.TutorialLaunched", true);
106   FirstRunController::Start();
107 }
108
109 }  // namespace first_run
110 }  // namespace chromeos