Home | History | Annotate | Download | only in shell
      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 "apps/shell/shell_browser_main_parts.h"
      6 
      7 #include "apps/app_load_service.h"
      8 #include "apps/shell/shell_browser_context.h"
      9 #include "apps/shell/shell_extensions_browser_client.h"
     10 #include "apps/shell/shell_extensions_client.h"
     11 #include "apps/shell/web_view_window.h"
     12 #include "base/command_line.h"
     13 #include "base/file_util.h"
     14 #include "base/files/file_path.h"
     15 #include "base/path_service.h"
     16 #include "base/run_loop.h"
     17 #include "chrome/common/chrome_paths.h"
     18 #include "chromeos/chromeos_paths.h"
     19 #include "content/public/common/result_codes.h"
     20 #include "extensions/common/extension_paths.h"
     21 #include "ui/aura/env.h"
     22 #include "ui/aura/root_window.h"
     23 #include "ui/aura/test/test_screen.h"
     24 #include "ui/base/resource/resource_bundle.h"
     25 #include "ui/gfx/screen.h"
     26 #include "ui/wm/test/wm_test_helper.h"
     27 
     28 namespace apps {
     29 
     30 ShellBrowserMainParts::ShellBrowserMainParts(
     31     const content::MainFunctionParams& parameters) {
     32 }
     33 
     34 ShellBrowserMainParts::~ShellBrowserMainParts() {
     35 }
     36 
     37 void ShellBrowserMainParts::CreateRootWindow() {
     38   // TODO(jamescook): Replace this with a real Screen implementation.
     39   gfx::Screen::SetScreenInstance(
     40       gfx::SCREEN_TYPE_NATIVE, aura::TestScreen::Create());
     41   // Set up basic pieces of views::corewm.
     42   wm_test_helper_.reset(new wm::WMTestHelper(gfx::Size(800, 600)));
     43   // Ensure the X window gets mapped.
     44   wm_test_helper_->root_window()->host()->Show();
     45 }
     46 
     47 void ShellBrowserMainParts::LoadAndLaunchApp(const base::FilePath& app_dir) {
     48   base::FilePath current_dir;
     49   CHECK(file_util::GetCurrentDirectory(&current_dir));
     50 
     51   // HACK: This allows us to see how far we can get without crashing.
     52   Profile* profile = reinterpret_cast<Profile*>(browser_context_.get());
     53   LOG(WARNING) << "-----------------------------------";
     54   LOG(WARNING) << "app_shell is expected to crash now.";
     55   LOG(WARNING) << "-----------------------------------";
     56 
     57   apps::AppLoadService* app_load_service =
     58       apps::AppLoadService::Get(profile);
     59   DCHECK(app_load_service);
     60   if (!app_load_service->LoadAndLaunch(
     61            app_dir, *CommandLine::ForCurrentProcess(), current_dir)) {
     62     LOG(ERROR) << "Unable to launch app at \"" << app_dir.value() << "\"";
     63   }
     64 }
     65 
     66 void ShellBrowserMainParts::PreMainMessageLoopStart() {
     67   // TODO(jamescook): Initialize touch here?
     68 }
     69 
     70 void ShellBrowserMainParts::PostMainMessageLoopStart() {
     71 }
     72 
     73 void ShellBrowserMainParts::PreEarlyInitialization() {
     74 }
     75 
     76 int ShellBrowserMainParts::PreCreateThreads() {
     77   // TODO(jamescook): Initialize chromeos::CrosSettings here?
     78 
     79   // Return no error.
     80   return 0;
     81 }
     82 
     83 void ShellBrowserMainParts::PreMainMessageLoopRun() {
     84   // NOTE: Much of this is culled from chrome/test/base/chrome_test_suite.cc
     85   // Set up all the paths to load files.
     86   chrome::RegisterPathProvider();
     87   chromeos::RegisterPathProvider();
     88   extensions::RegisterPathProvider();
     89 
     90   // The extensions system needs manifest data from the Chrome PAK file.
     91   base::FilePath resources_pack_path;
     92   PathService::Get(chrome::FILE_RESOURCES_PACK, &resources_pack_path);
     93   ResourceBundle::GetSharedInstance().AddDataPackFromPath(
     94       resources_pack_path, ui::SCALE_FACTOR_NONE);
     95 
     96   // TODO(jamescook): Initialize chromeos::UserManager.
     97 
     98   // Initialize our "profile" equivalent.
     99   browser_context_.reset(new ShellBrowserContext);
    100 
    101   extensions_client_.reset(new ShellExtensionsClient());
    102   extensions::ExtensionsClient::Set(extensions_client_.get());
    103 
    104   extensions_browser_client_.reset(
    105       new ShellExtensionsBrowserClient(browser_context_.get()));
    106   extensions::ExtensionsBrowserClient::Set(extensions_browser_client_.get());
    107 
    108   // TODO(jamescook): Initialize policy::ProfilePolicyConnector.
    109   // TODO(jamescook): Initialize ExtensionSystem and InitForRegularProfile.
    110   // TODO(jamescook): CreateBrowserContextServices using
    111   // BrowserContextDependencyManager.
    112 
    113   CreateRootWindow();
    114 
    115   const std::string kAppSwitch = "app";
    116   CommandLine* command_line = CommandLine::ForCurrentProcess();
    117   if (command_line->HasSwitch(kAppSwitch)) {
    118     base::FilePath app_dir(command_line->GetSwitchValueNative(kAppSwitch));
    119     LoadAndLaunchApp(app_dir);
    120   } else {
    121     // TODO(jamescook): Create an apps::ShellWindow here. For now, create a
    122     // window with a WebView just to ensure that the content module is properly
    123     // initialized.
    124     ShowWebViewWindow(browser_context_.get(),
    125                       wm_test_helper_->root_window()->window());
    126   }
    127 }
    128 
    129 bool ShellBrowserMainParts::MainMessageLoopRun(int* result_code)  {
    130   base::RunLoop run_loop;
    131   run_loop.Run();
    132   *result_code = content::RESULT_CODE_NORMAL_EXIT;
    133   return true;
    134 }
    135 
    136 void ShellBrowserMainParts::PostMainMessageLoopRun() {
    137   extensions::ExtensionsBrowserClient::Set(NULL);
    138   extensions_browser_client_.reset();
    139   browser_context_.reset();
    140   wm_test_helper_.reset();
    141   aura::Env::DeleteInstance();
    142 }
    143 
    144 }  // namespace apps
    145