Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / athena / main / athena_main_delegate.cc
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 "athena/main/athena_main_delegate.h"
6
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"
27
28 namespace athena {
29 namespace {
30
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";
35
36 class AthenaDesktopController : public extensions::DesktopController {
37  public:
38   AthenaDesktopController() {}
39   ~AthenaDesktopController() override {}
40
41  private:
42   // extensions::DesktopController:
43   virtual aura::WindowTreeHost* GetHost() override {
44     return athena::AthenaEnv::Get()->GetHost();
45   }
46
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 {
53     NOTIMPLEMENTED();
54     return nullptr;
55   }
56
57   // Adds the window to the desktop.
58   virtual void AddAppWindow(aura::Window* window) override { NOTIMPLEMENTED(); }
59
60   virtual void RemoveAppWindow(extensions::AppWindow* window) override {}
61
62   // Closes and destroys the app windows.
63   virtual void CloseAppWindows() override {}
64
65   DISALLOW_COPY_AND_ASSIGN(AthenaDesktopController);
66 };
67
68 class AthenaBrowserMainDelegate : public extensions::ShellBrowserMainDelegate {
69  public:
70   AthenaBrowserMainDelegate() {}
71   ~AthenaBrowserMainDelegate() override {}
72
73   // extensions::ShellBrowserMainDelegate:
74   virtual void Start(content::BrowserContext* context) override {
75     base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
76
77     base::FilePath app_dir = base::FilePath::FromUTF8Unsafe(
78         command_line->HasSwitch(extensions::switches::kAppShellAppPath)
79             ? command_line->GetSwitchValueNative(
80                   extensions::switches::kAppShellAppPath)
81             : kDefaultAppPath);
82
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);
89     }
90
91     athena::StartAthenaEnv(
92         content::BrowserThread::GetBlockingPool()
93             ->GetTaskRunnerWithShutdownBehavior(
94                 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN));
95     athena::CreateVirtualKeyboardWithContext(context);
96     athena::StartAthenaSessionWithContext(context);
97   }
98
99   virtual void Shutdown() override {
100     athena::AthenaEnv::Get()->OnTerminating();
101     athena::ShutdownAthena();
102   }
103
104   virtual extensions::DesktopController* CreateDesktopController() override {
105     return new AthenaDesktopController();
106   }
107
108  private:
109   DISALLOW_COPY_AND_ASSIGN(AthenaBrowserMainDelegate);
110 };
111
112 class AthenaContentBrowserClient
113     : public extensions::ShellContentBrowserClient {
114  public:
115   AthenaContentBrowserClient()
116       : extensions::ShellContentBrowserClient(new AthenaBrowserMainDelegate()) {
117   }
118   ~AthenaContentBrowserClient() override {}
119
120   // content::ContentBrowserClient:
121   virtual content::WebContentsViewDelegate* GetWebContentsViewDelegate(
122       content::WebContents* web_contents) override {
123     return athena::CreateWebContentsViewDelegate(web_contents);
124   }
125
126  private:
127   DISALLOW_COPY_AND_ASSIGN(AthenaContentBrowserClient);
128 };
129
130 class AthenaContentRendererClient
131     : public extensions::ShellContentRendererClient {
132  public:
133   AthenaContentRendererClient() {}
134   ~AthenaContentRendererClient() override {}
135
136   // content::ContentRendererClient:
137   virtual void RenderFrameCreated(content::RenderFrame* render_frame) override {
138     new athena::AthenaRendererPDFHelper(render_frame);
139     extensions::ShellContentRendererClient::RenderFrameCreated(render_frame);
140   }
141
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(
147         interface_name);
148   }
149 };
150
151 }  // namespace
152
153 content::ContentClient* AthenaMainDelegate::CreateContentClient() {
154   return new athena::AthenaContentClient();
155 }
156
157 content::ContentBrowserClient*
158 AthenaMainDelegate::CreateShellContentBrowserClient() {
159   return new AthenaContentBrowserClient();
160 }
161
162 content::ContentRendererClient*
163 AthenaMainDelegate::CreateShellContentRendererClient() {
164   return new AthenaContentRendererClient();
165 }
166
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);
173 }
174
175 }  // namespace athena