[M47_2526] Chromium upversion to m47_2526 branch
[platform/framework/web/chromium-efl.git] / tizen_src / ewk / efl_integration / ewk_global_data.cc
1 // Copyright 2014 Samsung Electronics. 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 "ewk_global_data.h"
6
7 #include "base/cpu.h"
8 #include "base/logging.h"
9 #include "base/path_service.h"
10 #include "base/message_loop/message_loop.h"
11 #include "browser/autofill/personal_data_manager_factory.h"
12 #include "content/browser/gpu/gpu_process_host.h"
13 #include "content/gpu/in_process_gpu_thread.h"
14 #include "content/public/app/content_main_runner.h"
15 #include "content/public/browser/browser_main_runner.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "content/public/browser/render_process_host.h"
18 #include "content/public/browser/utility_process_host.h"
19 #include "content/public/common/content_switches.h"
20 #include "content/public/app/content_main.h"
21 #include "content/renderer/in_process_renderer_thread.h"
22 #include "content/utility/in_process_utility_thread.h"
23 #include "content/browser/utility_process_host_impl.h"
24 #include "content/browser/renderer_host/render_process_host_impl.h"
25 #include "efl/window_factory.h"
26 #include "eweb_view.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "ui/ozone/public/ozone_platform.h"
29
30 #include "command_line_efl.h"
31 #include "content_main_delegate_efl.h"
32 #include "message_pump_for_ui_efl.h"
33 #include "ui/gfx/screen_efl.h"
34
35 #ifdef OS_TIZEN_MOBILE
36 #include <dlfcn.h>
37 void* EflExtensionHandle = 0;
38 #endif
39
40 using base::MessageLoop;
41 using content::BrowserMainRunner;
42 using content::BrowserThread;
43 using content::ContentMainDelegateEfl;
44 using content::ContentMainRunner;
45 using content::GpuProcessHost;
46 using content::RenderProcessHost;
47 using content::UtilityProcessHost;
48
49 EwkGlobalData* EwkGlobalData::instance_ = NULL;
50
51 namespace {
52
53 scoped_ptr<base::MessagePump> MessagePumpFactory() {
54   return scoped_ptr<base::MessagePump>(new base::MessagePumpForUIEfl);
55 }
56
57 } // namespace
58
59 EwkGlobalData::EwkGlobalData()
60   : content_main_runner_(ContentMainRunner::Create())
61   , browser_main_runner_(BrowserMainRunner::Create()) {
62 }
63
64 EwkGlobalData::~EwkGlobalData() {
65   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
66   // URLRequestContextGetterEfl needs to be deleted before loop stops
67   system_request_context_ = NULL;
68   // We need to pretend that message loop was stopped so chromium unwinds correctly
69   MessageLoop *loop = MessageLoop::current();
70   loop->QuitNow();
71   // browser_main_runner must be deleted first as it depends on content_main_runner
72   delete browser_main_runner_;
73   delete content_main_runner_;
74
75   // content_main_delegate_efl_ must be deleted after content_main_runner_
76   delete content_main_delegate_efl_;
77 }
78
79 EwkGlobalData* EwkGlobalData::GetInstance() {
80   if (instance_) {
81     CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
82     return instance_;
83   }
84
85   // Workaround for cpu info logging asserting if executed on the wrong thread
86   // during cpu info lazy instance initialization.
87   base::CPU cpu;
88   DCHECK(cpu.cpu_brand() != "");
89
90   instance_ = new EwkGlobalData();
91
92   bool message_pump_overridden =
93       base::MessageLoop::InitMessagePumpForUIFactory(&MessagePumpFactory);
94   DCHECK(message_pump_overridden);
95
96   ui::InstallScreenInstance();
97   efl::WindowFactory::SetDelegate(&EWebView::GetHostWindowDelegate);
98
99   instance_->content_main_delegate_efl_ = new ContentMainDelegateEfl();
100   content::ContentMainParams params(instance_->content_main_delegate_efl_);
101   params.argc = CommandLineEfl::GetArgc();
102   params.argv = CommandLineEfl::GetArgv();
103
104   // Call to CommandLineEfl::GetDefaultPortParams() should be before content
105   // main runner initialization in order to pass command line parameters
106   // for current process that are used in content main runner initialization.
107   content::MainFunctionParams main_funtion_params =
108       CommandLineEfl::GetDefaultPortParams();
109
110 #if defined(USE_OZONE)
111   ui::OzonePlatform::InitializeForUI();
112 #endif
113
114   instance_->content_main_runner_->Initialize(params);
115   instance_->browser_main_runner_->Initialize(main_funtion_params);
116
117   base::ThreadRestrictions::SetIOAllowed(true);
118
119   base::FilePath pak_dir;
120   base::FilePath pak_file;
121   PathService::Get(base::DIR_EXE, &pak_dir);
122   pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
123   ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
124
125   if (base::CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) {
126     content::UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
127         content::CreateInProcessUtilityThread);
128     content::RenderProcessHostImpl::RegisterRendererMainThreadFactory(
129         content::CreateInProcessRendererThread);
130     GpuProcessHost::RegisterGpuMainThreadFactory(
131         reinterpret_cast<content::GpuMainThreadFactoryFunction>(content::CreateInProcessGpuThread));
132   }
133
134 #ifndef NDEBUG
135   logging::LoggingSettings settings;
136   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
137   logging::InitLogging(settings);
138   logging::SetLogItems(true, true, true, true);
139 #endif
140
141 #ifdef OS_TIZEN_MOBILE
142   if (!EflExtensionHandle)
143     EflExtensionHandle = dlopen("/usr/lib/libefl-extension.so.0", RTLD_LAZY);
144 #endif
145
146   return instance_;
147 }
148
149 content::URLRequestContextGetterEfl* EwkGlobalData::GetSystemRequestContextGetter() {
150   if (!system_request_context_.get()) {
151     system_request_context_ = new content::URLRequestContextGetterEfl();
152   }
153   return system_request_context_.get();
154 }
155
156 void EwkGlobalData::Delete()
157 {
158   delete instance_;
159   instance_ = NULL;
160 }
161
162 // static
163 bool EwkGlobalData::IsInitialized() {
164   return instance_ != NULL;
165 }