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