summaryrefslogtreecommitdiffstats
path: root/apps/shell/app_shell_browser_main_parts.cc
blob: 1cc945690a9ed8dc302b7d7f4e0d4490f70503f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "apps/shell/app_shell_browser_main_parts.h"

#include "apps/app_load_service.h"
#include "apps/shell/app_shell_browser_context.h"
#include "apps/shell/web_view_window.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/path_service.h"
#include "base/run_loop.h"
#include "chrome/common/chrome_paths.h"
#include "chromeos/chromeos_paths.h"
#include "content/public/common/result_codes.h"
#include "extensions/common/extension_paths.h"
#include "ui/aura/env.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/test_screen.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/screen.h"
#include "ui/wm/test/wm_test_helper.h"

namespace apps {

AppShellBrowserMainParts::AppShellBrowserMainParts(
    const content::MainFunctionParams& parameters) {
}

AppShellBrowserMainParts::~AppShellBrowserMainParts() {
}

void AppShellBrowserMainParts::CreateRootWindow() {
  // TODO(jamescook): Replace this with a real Screen implementation.
  gfx::Screen::SetScreenInstance(
      gfx::SCREEN_TYPE_NATIVE, aura::TestScreen::Create());
  // Set up basic pieces of views::corewm.
  wm_test_helper_.reset(new wm::WMTestHelper(gfx::Size(800, 600)));
  // Ensure the X window gets mapped.
  wm_test_helper_->root_window()->host()->Show();
}

void AppShellBrowserMainParts::LoadAndLaunchApp(const base::FilePath& app_dir) {
  base::FilePath current_dir;
  CHECK(file_util::GetCurrentDirectory(&current_dir));

  // HACK: This allows us to see how far we can get without crashing.
  Profile* profile = reinterpret_cast<Profile*>(browser_context_.get());
  LOG(WARNING) << "-----------------------------------";
  LOG(WARNING) << "app_shell is expected to crash now.";
  LOG(WARNING) << "-----------------------------------";

  apps::AppLoadService* app_load_service =
      apps::AppLoadService::Get(profile);
  DCHECK(app_load_service);
  if (!app_load_service->LoadAndLaunch(
           app_dir, *CommandLine::ForCurrentProcess(), current_dir)) {
    LOG(ERROR) << "Unable to launch app at \"" << app_dir.value() << "\"";
  }
}

void AppShellBrowserMainParts::PreMainMessageLoopStart() {
  // TODO(jamescook): Initialize touch here?
}

void AppShellBrowserMainParts::PostMainMessageLoopStart() {
}

void AppShellBrowserMainParts::PreEarlyInitialization() {
}

int AppShellBrowserMainParts::PreCreateThreads() {
  // TODO(jamescook): Initialize chromeos::CrosSettings here?

  // Return no error.
  return 0;
}

void AppShellBrowserMainParts::PreMainMessageLoopRun() {
  // NOTE: Much of this is culled from chrome/test/base/chrome_test_suite.cc
  // Set up all the paths to load files.
  chrome::RegisterPathProvider();
  chromeos::RegisterPathProvider();
  extensions::RegisterPathProvider();

  // The extensions system needs manifest data from the Chrome PAK file.
  base::FilePath resources_pack_path;
  PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
  ResourceBundle::GetSharedInstance().AddDataPackFromPath(
      resources_pack_path, ui::SCALE_FACTOR_NONE);

  // TODO(jamescook): Initialize chromeos::UserManager.

  // TODO(jamescook): Initialize ExtensionsClient and ExtensionsBrowserClient.

  // Initialize our "profile" equivalent.
  browser_context_.reset(new AppShellBrowserContext);

  // TODO(jamescook): Initialize policy::ProfilePolicyConnector.
  // TODO(jamescook): Initialize ExtensionSystem and InitForRegularProfile.
  // TODO(jamescook): CreateBrowserContextServices using
  // BrowserContextDependencyManager.

  CreateRootWindow();

  const std::string kAppSwitch = "app";
  CommandLine* command_line = CommandLine::ForCurrentProcess();
  if (command_line->HasSwitch(kAppSwitch)) {
    base::FilePath app_dir(command_line->GetSwitchValueNative(kAppSwitch));
    LoadAndLaunchApp(app_dir);
  } else {
    // TODO(jamescook): Create an apps::ShellWindow here. For now, create a
    // window with a WebView just to ensure that the content module is properly
    // initialized.
    ShowWebViewWindow(browser_context_.get(),
                      wm_test_helper_->root_window()->window());
  }
}

bool AppShellBrowserMainParts::MainMessageLoopRun(int* result_code)  {
  base::RunLoop run_loop;
  run_loop.Run();
  *result_code = content::RESULT_CODE_NORMAL_EXIT;
  return true;
}

void AppShellBrowserMainParts::PostMainMessageLoopRun() {
  browser_context_.reset();
  wm_test_helper_.reset();
  aura::Env::DeleteInstance();
}

}  // namespace apps