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.
5 #include "athena/main/athena_main_delegate.h"
7 #include "athena/content/public/web_contents_view_delegate_creator.h"
8 #include "athena/env/public/athena_env.h"
9 #include "athena/main/athena_content_client.h"
10 #include "athena/main/athena_renderer_pdf_helper.h"
11 #include "athena/main/public/athena_launcher.h"
12 #include "base/command_line.h"
13 #include "base/files/file_util.h"
14 #include "base/path_service.h"
15 #include "components/pdf/renderer/ppb_pdf_impl.h"
16 #include "content/public/app/content_main.h"
17 #include "content/public/browser/browser_thread.h"
18 #include "extensions/shell/browser/desktop_controller.h"
19 #include "extensions/shell/browser/shell_browser_main_delegate.h"
20 #include "extensions/shell/browser/shell_content_browser_client.h"
21 #include "extensions/shell/browser/shell_extension_system.h"
22 #include "extensions/shell/common/shell_content_client.h"
23 #include "extensions/shell/common/switches.h"
24 #include "extensions/shell/renderer/shell_content_renderer_client.h"
25 #include "ppapi/c/private/ppb_pdf.h"
26 #include "ui/base/resource/resource_bundle.h"
31 // We want to load the sample calculator app by default, for a while. Expecting
32 // to run athena_main at src/
33 const char kDefaultAppPath[] =
34 "chrome/common/extensions/docs/examples/apps/calculator/app";
36 class AthenaDesktopController : public extensions::DesktopController {
38 AthenaDesktopController() {}
39 ~AthenaDesktopController() override {}
42 // extensions::DesktopController:
43 virtual aura::WindowTreeHost* GetHost() override {
44 return athena::AthenaEnv::Get()->GetHost();
47 // Creates a new app window and adds it to the desktop. The desktop maintains
48 // ownership of the window.
49 // TODO(jamescook|oshima): Is this function needed?
50 virtual extensions::AppWindow* CreateAppWindow(
51 content::BrowserContext* context,
52 const extensions::Extension* extension) override {
57 // Adds the window to the desktop.
58 virtual void AddAppWindow(aura::Window* window) override { NOTIMPLEMENTED(); }
60 virtual void RemoveAppWindow(extensions::AppWindow* window) override {}
62 // Closes and destroys the app windows.
63 virtual void CloseAppWindows() override {}
65 DISALLOW_COPY_AND_ASSIGN(AthenaDesktopController);
68 class AthenaBrowserMainDelegate : public extensions::ShellBrowserMainDelegate {
70 AthenaBrowserMainDelegate() {}
71 ~AthenaBrowserMainDelegate() override {}
73 // extensions::ShellBrowserMainDelegate:
74 virtual void Start(content::BrowserContext* context) override {
75 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
77 base::FilePath app_dir = base::FilePath::FromUTF8Unsafe(
78 command_line->HasSwitch(extensions::switches::kAppShellAppPath)
79 ? command_line->GetSwitchValueNative(
80 extensions::switches::kAppShellAppPath)
83 base::FilePath app_absolute_dir = base::MakeAbsoluteFilePath(app_dir);
84 if (base::DirectoryExists(app_absolute_dir)) {
85 extensions::ShellExtensionSystem* extension_system =
86 static_cast<extensions::ShellExtensionSystem*>(
87 extensions::ExtensionSystem::Get(context));
88 extension_system->LoadApp(app_absolute_dir);
91 athena::StartAthenaEnv(
92 content::BrowserThread::GetBlockingPool()
93 ->GetTaskRunnerWithShutdownBehavior(
94 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
95 athena::CreateVirtualKeyboardWithContext(context);
96 athena::StartAthenaSessionWithContext(context);
99 virtual void Shutdown() override {
100 athena::AthenaEnv::Get()->OnTerminating();
101 athena::ShutdownAthena();
104 virtual extensions::DesktopController* CreateDesktopController() override {
105 return new AthenaDesktopController();
109 DISALLOW_COPY_AND_ASSIGN(AthenaBrowserMainDelegate);
112 class AthenaContentBrowserClient
113 : public extensions::ShellContentBrowserClient {
115 AthenaContentBrowserClient()
116 : extensions::ShellContentBrowserClient(new AthenaBrowserMainDelegate()) {
118 ~AthenaContentBrowserClient() override {}
120 // content::ContentBrowserClient:
121 virtual content::WebContentsViewDelegate* GetWebContentsViewDelegate(
122 content::WebContents* web_contents) override {
123 return athena::CreateWebContentsViewDelegate(web_contents);
127 DISALLOW_COPY_AND_ASSIGN(AthenaContentBrowserClient);
130 class AthenaContentRendererClient
131 : public extensions::ShellContentRendererClient {
133 AthenaContentRendererClient() {}
134 ~AthenaContentRendererClient() override {}
136 // content::ContentRendererClient:
137 virtual void RenderFrameCreated(content::RenderFrame* render_frame) override {
138 new athena::AthenaRendererPDFHelper(render_frame);
139 extensions::ShellContentRendererClient::RenderFrameCreated(render_frame);
142 virtual const void* CreatePPAPIInterface(
143 const std::string& interface_name) override {
144 if (interface_name == PPB_PDF_INTERFACE)
145 return pdf::PPB_PDF_Impl::GetInterface();
146 return extensions::ShellContentRendererClient::CreatePPAPIInterface(
153 content::ContentClient* AthenaMainDelegate::CreateContentClient() {
154 return new athena::AthenaContentClient();
157 content::ContentBrowserClient*
158 AthenaMainDelegate::CreateShellContentBrowserClient() {
159 return new AthenaContentBrowserClient();
162 content::ContentRendererClient*
163 AthenaMainDelegate::CreateShellContentRendererClient() {
164 return new AthenaContentRendererClient();
167 void AthenaMainDelegate::InitializeResourceBundle() {
168 base::FilePath pak_dir;
169 PathService::Get(base::DIR_MODULE, &pak_dir);
170 base::FilePath pak_file =
171 pak_dir.Append(FILE_PATH_LITERAL("athena_resources.pak"));
172 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
175 } // namespace athena