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/common/switches.h"
19 #include "extensions/shell/browser/desktop_controller.h"
20 #include "extensions/shell/browser/shell_browser_main_delegate.h"
21 #include "extensions/shell/browser/shell_content_browser_client.h"
22 #include "extensions/shell/browser/shell_extension_system.h"
23 #include "extensions/shell/common/shell_content_client.h"
24 #include "extensions/shell/common/switches.h"
25 #include "extensions/shell/renderer/shell_content_renderer_client.h"
26 #include "ppapi/c/private/ppb_pdf.h"
27 #include "ui/aura/window.h"
28 #include "ui/aura/window_tree_host.h"
29 #include "ui/base/resource/resource_bundle.h"
30 #include "ui/gfx/geometry/size.h"
35 // We want to load the sample calculator app by default, for a while. Expecting
36 // to run athena_main at src/
37 const char kDefaultAppPath
[] =
38 "chrome/common/extensions/docs/examples/apps/calculator/app";
40 class AthenaDesktopController
: public extensions::DesktopController
{
42 AthenaDesktopController() {}
43 ~AthenaDesktopController() override
{}
46 // extensions::DesktopController:
47 virtual gfx::Size
GetWindowSize() override
{
48 return athena::AthenaEnv::Get()->GetHost()->window()->bounds().size();
51 // Creates a new app window and adds it to the desktop. The desktop maintains
52 // ownership of the window.
53 // TODO(jamescook|oshima): Is this function needed?
54 virtual extensions::AppWindow
* CreateAppWindow(
55 content::BrowserContext
* context
,
56 const extensions::Extension
* extension
) override
{
61 // Adds the window to the desktop.
62 virtual void AddAppWindow(aura::Window
* window
) override
{ NOTIMPLEMENTED(); }
64 virtual void RemoveAppWindow(extensions::AppWindow
* window
) override
{}
66 // Closes and destroys the app windows.
67 virtual void CloseAppWindows() override
{}
69 DISALLOW_COPY_AND_ASSIGN(AthenaDesktopController
);
72 class AthenaBrowserMainDelegate
: public extensions::ShellBrowserMainDelegate
{
74 AthenaBrowserMainDelegate() {}
75 ~AthenaBrowserMainDelegate() override
{}
77 // extensions::ShellBrowserMainDelegate:
78 virtual void Start(content::BrowserContext
* context
) override
{
79 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
81 base::FilePath app_dir
= base::FilePath::FromUTF8Unsafe(
82 command_line
->HasSwitch(extensions::switches::kLoadApps
)
83 ? command_line
->GetSwitchValueNative(
84 extensions::switches::kLoadApps
)
87 base::FilePath app_absolute_dir
= base::MakeAbsoluteFilePath(app_dir
);
88 if (base::DirectoryExists(app_absolute_dir
)) {
89 extensions::ShellExtensionSystem
* extension_system
=
90 static_cast<extensions::ShellExtensionSystem
*>(
91 extensions::ExtensionSystem::Get(context
));
92 extension_system
->LoadApp(app_absolute_dir
);
95 athena::StartAthenaEnv(
96 content::BrowserThread::GetBlockingPool()
97 ->GetTaskRunnerWithShutdownBehavior(
98 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN
));
99 athena::CreateVirtualKeyboardWithContext(context
);
100 athena::StartAthenaSessionWithContext(context
);
103 virtual void Shutdown() override
{
104 athena::AthenaEnv::Get()->OnTerminating();
105 athena::ShutdownAthena();
108 virtual extensions::DesktopController
* CreateDesktopController() override
{
109 return new AthenaDesktopController();
113 DISALLOW_COPY_AND_ASSIGN(AthenaBrowserMainDelegate
);
116 class AthenaContentBrowserClient
117 : public extensions::ShellContentBrowserClient
{
119 AthenaContentBrowserClient()
120 : extensions::ShellContentBrowserClient(new AthenaBrowserMainDelegate()) {
122 ~AthenaContentBrowserClient() override
{}
124 // content::ContentBrowserClient:
125 virtual content::WebContentsViewDelegate
* GetWebContentsViewDelegate(
126 content::WebContents
* web_contents
) override
{
127 return athena::CreateWebContentsViewDelegate(web_contents
);
131 DISALLOW_COPY_AND_ASSIGN(AthenaContentBrowserClient
);
134 class AthenaContentRendererClient
135 : public extensions::ShellContentRendererClient
{
137 AthenaContentRendererClient() {}
138 ~AthenaContentRendererClient() override
{}
140 // content::ContentRendererClient:
141 virtual void RenderFrameCreated(content::RenderFrame
* render_frame
) override
{
142 new athena::AthenaRendererPDFHelper(render_frame
);
143 extensions::ShellContentRendererClient::RenderFrameCreated(render_frame
);
146 virtual const void* CreatePPAPIInterface(
147 const std::string
& interface_name
) override
{
148 if (interface_name
== PPB_PDF_INTERFACE
)
149 return pdf::PPB_PDF_Impl::GetInterface();
150 return extensions::ShellContentRendererClient::CreatePPAPIInterface(
157 content::ContentClient
* AthenaMainDelegate::CreateContentClient() {
158 return new athena::AthenaContentClient();
161 content::ContentBrowserClient
*
162 AthenaMainDelegate::CreateShellContentBrowserClient() {
163 return new AthenaContentBrowserClient();
166 content::ContentRendererClient
*
167 AthenaMainDelegate::CreateShellContentRendererClient() {
168 return new AthenaContentRendererClient();
171 void AthenaMainDelegate::InitializeResourceBundle() {
172 base::FilePath pak_dir
;
173 PathService::Get(base::DIR_MODULE
, &pak_dir
);
174 base::FilePath pak_file
=
175 pak_dir
.Append(FILE_PATH_LITERAL("athena_resources.pak"));
176 ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file
);
179 } // namespace athena