Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / login / demo_mode / demo_app_launcher.cc
1 // Copyright 2014 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 "chrome/browser/chromeos/login/demo_mode/demo_app_launcher.h"
6
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "chrome/browser/chromeos/app_mode/app_session_lifetime.h"
11 #include "chrome/browser/chromeos/login/ui/login_display_host.h"
12 #include "chrome/browser/chromeos/login/ui/login_display_host_impl.h"
13 #include "chrome/browser/extensions/component_loader.h"
14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/lifetime/application_lifetime.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/extensions/application_launch.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/extensions/extension_constants.h"
20 #include "chrome/grit/browser_resources.h"
21 #include "chromeos/network/network_handler.h"
22 #include "chromeos/network/network_state_handler.h"
23 #include "components/user_manager/user_manager.h"
24 #include "extensions/browser/extension_system.h"
25 #include "extensions/common/extension.h"
26 #include "ui/base/window_open_disposition.h"
27
28 namespace chromeos {
29
30 const char DemoAppLauncher::kDemoUserName[] = "demouser@demo.app.local";
31 const char DemoAppLauncher::kDemoAppId[] = "klimoghijjogocdbaikffefjfcfheiel";
32 const base::FilePath::CharType kDefaultDemoAppPath[] =
33     FILE_PATH_LITERAL("/usr/share/chromeos-assets/demo_app");
34
35 // static
36 base::FilePath* DemoAppLauncher::demo_app_path_ = NULL;
37
38 DemoAppLauncher::DemoAppLauncher() {
39   if (!demo_app_path_)
40     demo_app_path_ = new base::FilePath(kDefaultDemoAppPath);
41 }
42
43 DemoAppLauncher::~DemoAppLauncher() {
44   delete demo_app_path_;
45 }
46
47 void DemoAppLauncher::StartDemoAppLaunch() {
48   DVLOG(1) << "Launching demo app...";
49   // user_id = DemoAppUserId, force_emphemeral = true, delegate = this.
50   kiosk_profile_loader_.reset(
51       new KioskProfileLoader(kDemoUserName, true, this));
52   kiosk_profile_loader_->Start();
53 }
54
55 // static
56 bool DemoAppLauncher::IsDemoAppSession(const std::string& user_id) {
57   return user_id == kDemoUserName;
58 }
59
60 // static
61 void DemoAppLauncher::SetDemoAppPathForTesting(const base::FilePath& path) {
62   delete demo_app_path_;
63   demo_app_path_ = new base::FilePath(path);
64 }
65
66 void DemoAppLauncher::OnProfileLoaded(Profile* profile) {
67   DVLOG(1) << "Profile loaded... Starting demo app launch.";
68
69   kiosk_profile_loader_.reset();
70
71   // Load our demo app, then launch it.
72   ExtensionService* extension_service =
73       extensions::ExtensionSystem::Get(profile)->extension_service();
74   CHECK(demo_app_path_);
75   const std::string extension_id = extension_service->component_loader()->Add(
76       IDR_DEMO_APP_MANIFEST,
77       *demo_app_path_);
78
79   const extensions::Extension* extension =
80       extension_service->GetExtensionById(extension_id, true);
81   if (!extension) {
82     // We've already done too much setup at this point to just return out, it
83     // is safer to just restart.
84     chrome::AttemptUserExit();
85     return;
86   }
87
88   // Disable network before launching the app.
89   LOG(WARNING) << "Disabling network before launching demo app..";
90   NetworkStateHandler* handler = NetworkHandler::Get()->network_state_handler();
91   handler->SetTechnologyEnabled(NetworkTypePattern::NonVirtual(),
92                                 false,
93                                 chromeos::network_handler::ErrorCallback());
94
95   OpenApplication(AppLaunchParams(
96       profile, extension, extensions::LAUNCH_CONTAINER_WINDOW, NEW_WINDOW));
97   InitAppSession(profile, extension_id);
98
99   user_manager::UserManager::Get()->SessionStarted();
100
101   LoginDisplayHostImpl::default_host()->Finalize();
102 }
103
104 void DemoAppLauncher::OnProfileLoadFailed(KioskAppLaunchError::Error error) {
105   LOG(ERROR) << "Loading the Kiosk Profile failed: " <<
106       KioskAppLaunchError::GetErrorMessage(error);
107 }
108
109 }  // namespace chromeos