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/ui/views/athena/chrome_browser_main_extra_parts_athena.h"
6
7#include "athena/env/public/athena_env.h"
8#include "athena/extensions/public/extensions_delegate.h"
9#include "athena/main/public/athena_launcher.h"
10#include "base/command_line.h"
11#include "base/macros.h"
12#include "chrome/browser/browser_process.h"
13#include "chrome/browser/chrome_browser_main_extra_parts.h"
14#include "chrome/browser/chrome_notification_types.h"
15#include "chrome/browser/lifetime/application_lifetime.h"
16#include "chrome/browser/profiles/profile.h"
17#include "chrome/browser/profiles/profile_manager.h"
18#include "chrome/browser/ui/views/select_file_dialog_extension_factory.h"
19#include "chrome/common/chrome_switches.h"
20#include "chromeos/chromeos_switches.h"
21#include "content/public/browser/browser_thread.h"
22#include "content/public/browser/notification_observer.h"
23#include "content/public/browser/notification_registrar.h"
24#include "content/public/browser/notification_service.h"
25
26namespace {
27
28class ChromeBrowserMainExtraPartsAthena : public ChromeBrowserMainExtraParts,
29                                          public content::NotificationObserver {
30 public:
31  ChromeBrowserMainExtraPartsAthena() {
32    registrar_.Add(this,
33                   chrome::NOTIFICATION_APP_TERMINATING,
34                   content::NotificationService::AllSources());
35  }
36
37  virtual ~ChromeBrowserMainExtraPartsAthena() {}
38
39 private:
40  // Overridden from ChromeBrowserMainExtraParts:
41  virtual void PreProfileInit() OVERRIDE {
42    athena::StartAthenaEnv(content::BrowserThread::GetBlockingPool()->
43        GetTaskRunnerWithShutdownBehavior(
44            base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
45
46    ui::SelectFileDialog::SetFactory(new SelectFileDialogExtensionFactory);
47  }
48  virtual void PostProfileInit() OVERRIDE {
49    if (!CommandLine::ForCurrentProcess()->HasSwitch(
50            switches::kDisableZeroBrowsersOpenForTests)) {
51      chrome::IncrementKeepAliveCount();
52    }
53    Profile* profile =
54        g_browser_process->profile_manager()->GetActiveUserProfile();
55    if (!CommandLine::ForCurrentProcess()->HasSwitch(
56            chromeos::switches::kLoginManager)) {
57      athena::ExtensionsDelegate::CreateExtensionsDelegateForChrome(profile);
58      athena::CreateVirtualKeyboardWithContext(profile);
59      athena::StartAthenaSessionWithContext(profile);
60    } else {
61      // Only initialize virtual keyboard with login profile, user session will
62      // start after login.
63      athena::CreateVirtualKeyboardWithContext(profile);
64    }
65  }
66  virtual void PostMainMessageLoopRun() OVERRIDE { athena::ShutdownAthena(); }
67
68  // content::NotificationObserver:
69  virtual void Observe(int type,
70                       const content::NotificationSource& source,
71                       const content::NotificationDetails& details) OVERRIDE {
72    switch (type) {
73      case chrome::NOTIFICATION_APP_TERMINATING:
74        athena::AthenaEnv::Get()->OnTerminating();
75        break;
76    }
77  }
78
79  content::NotificationRegistrar registrar_;
80
81  DISALLOW_COPY_AND_ASSIGN(ChromeBrowserMainExtraPartsAthena);
82};
83
84}  // namespace
85
86ChromeBrowserMainExtraParts* CreateChromeBrowserMainExtraPartsAthena() {
87  return new ChromeBrowserMainExtraPartsAthena();
88}
89