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