a8e776cacdf6e179c0b67600126a153653061fc9
[platform/framework/web/chromium-efl.git] / tizen_src / impl / 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 "gpu/gpu_thread_override_efl.h"
26 #include "tizen_webview/public/tw_web_context.h"
27 #include "ui/base/resource/resource_bundle.h"
28
29 #include "command_line_efl.h"
30 #include "content_main_delegate_efl.h"
31 #include "message_pump_for_ui_efl.h"
32 #include "screen_efl.h"
33
34 #ifdef OS_TIZEN_MOBILE
35 #include <dlfcn.h>
36 void* EflAssistHandle = 0;
37 #endif
38
39 using base::MessageLoop;
40 using content::BrowserMainRunner;
41 using content::BrowserThread;
42 using content::ContentMainDelegateEfl;
43 using content::ContentMainRunner;
44 using content::GpuProcessHost;
45 using content::RenderProcessHost;
46 using content::UtilityProcessHost;
47
48 EwkGlobalData* EwkGlobalData::instance_ = NULL;
49
50 namespace {
51
52 scoped_ptr<base::MessagePump> MessagePumpFactory() {
53   return scoped_ptr<base::MessagePump>(new base::MessagePumpForUIEfl);
54 }
55
56 } // namespace
57
58 EwkGlobalData::EwkGlobalData()
59   : content_main_runner_(ContentMainRunner::Create())
60   , browser_main_runner_(BrowserMainRunner::Create()) {
61 }
62
63 EwkGlobalData::~EwkGlobalData() {
64   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
65   // Release default context
66   tizen_webview::WebContext::DefaultContextRelease();
67   // We need to pretend that message loop was stopped so chromium unwinds correctly
68   MessageLoop *loop = MessageLoop::current();
69   loop->QuitNow();
70   // browser_main_runner must be deleted first as it depends on content_main_runner
71   delete browser_main_runner_;
72   delete content_main_runner_;
73 }
74
75 EwkGlobalData* EwkGlobalData::GetInstance() {
76   if (instance_) {
77     CHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
78     return instance_;
79   }
80
81   // Workaround for cpu info logging asserting if executed on the wrong thread
82   // during cpu info lazy instance initialization.
83   base::CPU cpu;
84   DCHECK(cpu.cpu_brand() != "");
85
86   instance_ = new EwkGlobalData();
87
88   bool message_pump_overridden =
89       base::MessageLoop::InitMessagePumpForUIFactory(&MessagePumpFactory);
90   DCHECK(message_pump_overridden);
91
92   ui::InstallScreenInstance();
93
94   instance_->content_main_delegate_efl_ = new ContentMainDelegateEfl();
95   content::ContentMainParams params(instance_->content_main_delegate_efl_);
96   params.argc = CommandLineEfl::GetArgc();
97   params.argv = CommandLineEfl::GetArgv();
98
99   // Call to CommandLineEfl::GetDefaultPortParams() should be before content
100   // main runner initialization in order to pass command line parameters
101   // for current process that are used in content main runner initialization.
102   content::MainFunctionParams main_funtion_params =
103       CommandLineEfl::GetDefaultPortParams();
104
105   instance_->content_main_runner_->Initialize(params);
106   instance_->browser_main_runner_->Initialize(main_funtion_params);
107
108   base::ThreadRestrictions::SetIOAllowed(true);
109
110   base::FilePath pak_dir;
111   base::FilePath pak_file;
112   PathService::Get(base::DIR_EXE, &pak_dir);
113   pak_file = pak_dir.Append(FILE_PATH_LITERAL("content_shell.pak"));
114   ui::ResourceBundle::InitSharedInstanceWithPakPath(pak_file);
115
116   if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) {
117     content::UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
118         content::CreateInProcessUtilityThread);
119     content::RenderProcessHostImpl::RegisterRendererMainThreadFactory(
120         content::CreateInProcessRendererThread);
121     GpuProcessHost::RegisterGpuMainThreadFactory(
122         CreateInProcessGpuThreadEfl);
123   }
124
125 #ifndef NDEBUG
126   logging::LoggingSettings settings;
127   settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
128   logging::InitLogging(settings);
129   logging::SetLogItems(true, true, true, true);
130 #endif
131
132 #ifdef OS_TIZEN_MOBILE
133   if (!EflAssistHandle)
134     EflAssistHandle = dlopen("/usr/lib/libefl-assist.so.0", RTLD_LAZY);
135 #endif
136
137   return instance_;
138 }
139
140 void EwkGlobalData::Delete()
141 {
142   delete instance_;
143   instance_ = NULL;
144 }
145
146 // static
147 bool EwkGlobalData::IsInitialized() {
148   return instance_ != NULL;
149 }