2 Copyright (C) 2014 Samsung Electronics
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "eweb_context.h"
22 #include "base/synchronization/waitable_event.h"
23 #include "content/common/plugin_list.h"
24 #include "content/public/browser/appcache_service.h"
25 #include "content/public/browser/browser_context.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/storage_partition.h"
28 #include "content/public/browser/render_process_host.h"
29 #include "content/public/browser/local_storage_usage_info.h"
30 #include "content/public/browser/dom_storage_context.h"
31 #include "browser/favicon/favicon_service.h"
32 #include "net/http/http_cache.h"
33 #include "net/proxy/proxy_config_service_fixed.h"
34 #include "net/proxy/proxy_service.h"
35 #include "webkit/browser/database/database_quota_client.h"
36 #include "webkit/browser/fileapi/file_system_quota_client.h"
37 #include "webkit/browser/quota/quota_manager.h"
39 #include "browser_context_efl.h"
40 #include "ewk_global_data.h"
41 #include "memory_purger.h"
42 #include "browser/renderer_host/browsing_data_remover_efl.h"
43 #include "browser/vibration/vibration_provider_client.h"
44 #include "common/render_messages_efl.h"
45 #include "memory_sampler.h"
47 #include "tizen_webview/public/tw_security_origin.h"
48 #include "tizen_webview/public/tw_wrt.h"
49 #include "tizen_webview/tw_misc_utility.h"
51 using content::BrowserThread;
52 using content::BrowserContext;
53 using content::BrowserContextEfl;
59 using tizen_webview::Cache_Model;
60 using tizen_webview::Security_Origin;
61 using tizen_webview::GetGURL;
62 using tizen_webview::GetURL;
64 tizen_webview::Mime_Override_Callback EWebContext::mime_override_callback_ = 0;
69 * @brief Helper class for obtaining WebStorage origins
71 class WebStorageGetAllOriginsDispatcher: public base::RefCountedThreadSafe<WebStorageGetAllOriginsDispatcher> {
73 WebStorageGetAllOriginsDispatcher(tizen_webview::Web_Storage_Origins_Get_Callback callback, void* user_data)
75 , user_data_(user_data) {
80 void Dispatch(const std::vector<content::LocalStorageUsageInfo> &local_storage) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
83 Eina_List* lorigins = NULL;
85 std::vector<content::LocalStorageUsageInfo>::const_iterator it;
87 for (it = local_storage.begin(); it != local_storage.end(); it++) {
88 Security_Origin* origin = new Security_Origin(GetURL(it->origin));
89 lorigins = eina_list_append(lorigins, origin);
92 callback_(lorigins, user_data_);
96 tizen_webview::Web_Storage_Origins_Get_Callback callback_;
100 void SetProxyConfigCallbackOnIOThread(base::WaitableEvent* done,
101 net::URLRequestContextGetter* url_request_context_getter,
102 const net::ProxyConfig& proxy_config) {
103 net::ProxyService* proxy_service =
104 url_request_context_getter->GetURLRequestContext()->proxy_service();
105 proxy_service->ResetConfigService(
106 new net::ProxyConfigServiceFixed(proxy_config));
110 void OnOriginsWithApplicationCacheObtained(tizen_webview::Web_Application_Cache_Origins_Get_Callback callback,
112 scoped_refptr<content::AppCacheInfoCollection> collection,
114 Eina_List* origins = 0;
115 for (map<GURL, content::AppCacheInfoVector>::iterator iter = collection->infos_by_origin.begin();
116 iter != collection->infos_by_origin.end();
118 Security_Origin* origin = new Security_Origin(GetURL(iter->first));
119 origins = eina_list_append(origins, origin);
121 callback(origins, user_data);
124 void OnTemporaryUsageAndQuotaObtained(
125 tizen_webview::Web_Application_Cache_Usage_For_Origin_Get_Callback callback,
127 quota::QuotaStatusCode status_code,
130 if (status_code != quota::kQuotaStatusOk) {
131 LOG(ERROR) << "Error in retrieving usage information";
132 // We still trigger callback.
135 BrowserThread::PostTask(BrowserThread::UI,
137 base::Bind(callback, usage, user_data));
140 void OnGetWebDBOrigins(
141 tizen_webview::Web_Database_Origins_Get_Callback callback,
143 const std::set<GURL>& origins_ref) {
144 Eina_List* origins = 0;
145 for (std::set<GURL>::iterator iter =
147 iter != origins_ref.end(); ++iter) {
148 Security_Origin* sec_origin = new Security_Origin(GetURL(*iter));
149 origins = eina_list_append(origins, sec_origin);
151 BrowserThread::PostTask(BrowserThread::UI,
153 base::Bind(callback, origins, user_data));
156 void GetWebDBOriginsOnDBThread(tizen_webview::Web_Database_Origins_Get_Callback callback,
158 content::StoragePartition* partition) {
159 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::DB));
160 webkit_database::DatabaseQuotaClient client(base::MessageLoopProxy::current().get(), partition->GetDatabaseTracker());
161 client.GetOriginsForType(quota::kStorageTypeTemporary,
162 base::Bind(&OnGetWebDBOrigins, callback, user_data));
165 void OnGetFileSystemOrigins(
166 tizen_webview::Local_File_System_Origins_Get_Callback callback,
168 const std::set<GURL>& origins_ref) {
169 Eina_List* origins = 0;
170 for (std::set<GURL>::iterator iter =
172 iter != origins_ref.end(); ++iter) {
173 Security_Origin* sec_origin = new Security_Origin(GetURL(*iter));
174 origins = eina_list_append(origins, sec_origin);
176 BrowserThread::PostTask(BrowserThread::UI,
178 base::Bind(callback, origins, user_data));
181 void GetFileSystemOriginsOnFILEThread(tizen_webview::Web_Database_Origins_Get_Callback callback,
183 content::StoragePartition* partition) {
184 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
185 scoped_ptr<fileapi::FileSystemQuotaClient> client( new fileapi::FileSystemQuotaClient(partition->GetFileSystemContext(), false));
187 client->GetOriginsForType(quota::kStorageTypeTemporary,
188 base::Bind(&OnGetFileSystemOrigins, callback, user_data));
193 void EwkDidStartDownloadCallback::TriggerCallback(const string& url) {
195 (*callback_)(url.c_str(),user_data_);
198 void EWebContext::SendWidgetInfo(int widget_id, double scale, const string &theme, const string &encoded_bundle) {
199 #if !defined(EWK_BRINGUP)
200 content::RenderProcessHost::iterator i(content::RenderProcessHost::AllHostsIterator());
201 for (; !i.IsAtEnd(); i.Advance()) {
202 i.GetCurrentValue()->Send(
203 new EwkViewMsg_SetWidgetInfo(widget_id, scale, theme, encoded_bundle));
208 void EWebContext::SendWrtMessage(const tizen_webview::WrtIpcMessageData& data) {
209 #if !defined(EWK_BRINGUP)
210 content::RenderProcessHost::iterator i(content::RenderProcessHost::AllHostsIterator());
211 for (; !i.IsAtEnd(); i.Advance()) {
212 i.GetCurrentValue()->Send(new EwkViewMsg_SendWrtMessage(data));
217 void EWebContext::SetMimeOverrideCallback(
218 tizen_webview::Mime_Override_Callback callback) {
219 mime_override_callback_ = callback;
222 bool EWebContext::ShouldOverrideMimeForURL(
223 const GURL& url, std::string& mime_type) {
224 if (mime_override_callback_) {
225 char *new_mime = NULL;
226 bool overriden = mime_override_callback_(
227 url.spec().c_str(), mime_type.c_str(), &new_mime);
230 mime_type.assign(new_mime);
238 EWebContext::EWebContext()
240 EwkGlobalData::Ensure();
242 browser_context_.reset(new BrowserContextEfl(this));
243 // Notification Service gets init in BrowserMainRunner init,
244 // so cache manager can register for notifications only after that.
245 web_cache_manager_.reset(new WebCacheManagerEfl(browser_context_.get()));
248 EWebContext::~EWebContext() {
249 VibrationProviderClient::DeleteInstance();
252 void EWebContext::ClearNetworkCache() {
253 BrowsingDataRemoverEfl* remover = BrowsingDataRemoverEfl::CreateForUnboundedRange(browser_context_.get());
254 remover->ClearNetworkCache();
255 //remover deletes itself once it is done with clearing operation.
259 void EWebContext::ClearWebkitCache() {
260 #if !defined(EWK_BRINGUP)
261 web_cache_manager_->ClearCache();
265 void EWebContext::SetCacheModel(Cache_Model model) {
266 #if !defined(EWK_BRINGUP)
267 web_cache_manager_->SetCacheModel(model);
271 Cache_Model EWebContext::GetCacheModel() const {
272 return web_cache_manager_->GetCacheModel();
275 void EWebContext::SetNetworkCacheEnable(bool enable) {
276 net::URLRequestContext* url_context = browser_context_->GetRequestContext()->GetURLRequestContext();
280 net::HttpTransactionFactory* transaction_factory = url_context->http_transaction_factory();
281 if (!transaction_factory)
284 net::HttpCache* http_cache = transaction_factory->GetCache();
289 http_cache->set_mode(net::HttpCache::NORMAL);
291 http_cache->set_mode(net::HttpCache::DISABLE);
294 bool EWebContext::GetNetworkCacheEnable() const {
295 net::HttpCache* http_cache = browser_context_->GetRequestContext()->GetURLRequestContext()->
296 http_transaction_factory()->GetCache();
300 return (http_cache->mode() != net::HttpCache::DISABLE);
303 bool EWebContext::HTTPCustomHeaderAdd(const char* name, const char* value) {
304 pair<map<string, string>::iterator,bool> ret;
305 ret = http_custom_headers_.insert(std::make_pair(string(name), string(value)));
309 bool EWebContext::HTTPCustomHeaderRemove(const char* name) {
310 return http_custom_headers_.erase(string(name));
313 void EWebContext::HTTPCustomHeaderClear() {
314 http_custom_headers_.clear();
317 const HTTPCustomHeadersEflMap& EWebContext::GetHTTPCustomHeadersEflMap() const {
318 return http_custom_headers_;
321 void EWebContext::AddExtraPluginDir(const char *path) {
322 content::PluginList::Singleton()->AddExtraPluginDir(base::FilePath(path));
325 void EWebContext::SetCertificate(const char* certificate_file) {
326 browser_context_->SetCertificate(certificate_file);
329 void EWebContext::NotifyLowMemory() {
330 MemoryPurger::PurgeAll(browser_context_.get());
333 void EWebContext::SetProxyUri(const char* uri) {
334 proxy_uri_ = string(uri);
335 net::ProxyConfig config;
336 config.proxy_rules().ParseFromString(proxy_uri_);
337 base::WaitableEvent done(false, false);
338 BrowserThread::PostTask(
339 BrowserThread::IO, FROM_HERE,
340 base::Bind(&SetProxyConfigCallbackOnIOThread, &done,
341 make_scoped_refptr(browser_context()->GetRequestContext()),
346 void EWebContext::SetDidStartDownloadCallback(tizen_webview::Context_Did_Start_Download_Callback callback,
348 DCHECK(start_download_callback_.get() == NULL);
349 start_download_callback_.reset(new EwkDidStartDownloadCallback(callback,user_data));
352 EwkDidStartDownloadCallback* EWebContext::DidStartDownloadCallback() {
353 return start_download_callback_.get();
356 Ewk_Cookie_Manager* EWebContext::ewkCookieManager() {
357 if (!ewk_cookie_manager_)
358 ewk_cookie_manager_.reset(Ewk_Cookie_Manager::create(browser_context_->GetRequestContextEfl()));
359 return ewk_cookie_manager_.get();
362 void EWebContext::DeleteAllApplicationCache() {
363 BrowsingDataRemoverEfl* remover = BrowsingDataRemoverEfl::CreateForUnboundedRange(browser_context_.get());
364 remover->RemoveImpl(BrowsingDataRemoverEfl::REMOVE_APPCACHE, GURL());
367 void EWebContext::DeleteApplicationCacheForSite(const tizen_webview::URL& site) {
368 content::StoragePartition* partition = BrowserContext::GetStoragePartitionForSite(browser_context_.get(),
370 partition->ClearDataForOrigin(content::StoragePartition::REMOVE_DATA_MASK_APPCACHE,
371 content::StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL,
373 partition->GetURLRequestContext(), base::Bind(&base::DoNothing));
376 void EWebContext::GetAllOriginsWithApplicationCache(tizen_webview::Web_Application_Cache_Origins_Get_Callback callback,
378 content::StoragePartition* partition =
379 BrowserContext::GetStoragePartition(browser_context_.get(), NULL);
381 scoped_refptr<content::AppCacheInfoCollection> collection(new content::AppCacheInfoCollection());
382 // As per comments on AppCacheService,
383 // there is only one instance of AppCacheService per profile.(i.e. context in our case).
384 // So, we don't need to iterate over all StoragePartitions.
385 partition->GetAppCacheService()->GetAllAppCacheInfo(collection,
386 base::Bind(&OnOriginsWithApplicationCacheObtained, callback, user_data, collection));
389 void EWebContext::GetApplicationCacheUsage(
390 const tizen_webview::URL& url,
391 tizen_webview::Web_Application_Cache_Usage_For_Origin_Get_Callback callback,
393 content::StoragePartition* partition =
394 BrowserContext::GetStoragePartition(browser_context_.get(), NULL);
396 BrowserThread::PostTask(
399 base::Bind("a::QuotaManager::GetUsageAndQuota,
400 partition->GetQuotaManager(),
402 quota::kStorageTypeTemporary,
403 base::Bind(&OnTemporaryUsageAndQuotaObtained, callback, user_data)));
406 void EWebContext::WebStorageDelete() {
407 BrowsingDataRemoverEfl* remover = BrowsingDataRemoverEfl::CreateForUnboundedRange(browser_context_.get());
408 remover->RemoveImpl(BrowsingDataRemoverEfl::REMOVE_LOCAL_STORAGE, GURL());
411 void EWebContext::WebStorageDelete(const tizen_webview::URL& origin) {
412 content::StoragePartition* partition =
413 BrowserContext::GetStoragePartition(browser_context_.get(), NULL);
415 partition->GetDOMStorageContext()->DeleteLocalStorage(GetGURL(origin));
418 void EWebContext::WebStorageOriginsAllGet(tizen_webview::Web_Storage_Origins_Get_Callback callback,
420 content::StoragePartition* partition =
421 BrowserContext::GetStoragePartition(browser_context_.get(), NULL);
423 WebStorageGetAllOriginsDispatcher* dispatcher = new WebStorageGetAllOriginsDispatcher(callback, user_data);
425 partition->GetDOMStorageContext()->GetLocalStorageUsage(base::Bind(&WebStorageGetAllOriginsDispatcher::Dispatch, dispatcher));
428 void EWebContext::IndexedDBDelete() {
429 BrowsingDataRemoverEfl* remover = BrowsingDataRemoverEfl::CreateForUnboundedRange(browser_context_.get());
430 remover->RemoveImpl(BrowsingDataRemoverEfl::REMOVE_INDEXEDDB, GURL());
433 void EWebContext::WebDBDelete(const tizen_webview::URL& host) {
434 BrowsingDataRemoverEfl* remover = BrowsingDataRemoverEfl::CreateForUnboundedRange(browser_context_.get());
435 remover->RemoveImpl(BrowsingDataRemoverEfl::REMOVE_WEBSQL, GetGURL(host));
438 void EWebContext::GetAllOriginsWithWebDB(tizen_webview::Web_Database_Origins_Get_Callback callback, void* user_data) {
439 content::StoragePartition* partition = BrowserContext::GetStoragePartition(browser_context_.get(), NULL);
440 BrowserThread::PostTask(
443 base::Bind(&GetWebDBOriginsOnDBThread,
444 callback, user_data, partition));
447 void EWebContext::FileSystemDelete(const tizen_webview::URL& host) {
448 BrowsingDataRemoverEfl* remover = BrowsingDataRemoverEfl::CreateForUnboundedRange(browser_context_.get());
449 remover->RemoveImpl(BrowsingDataRemoverEfl::REMOVE_FILE_SYSTEMS, GetGURL(host));
452 void EWebContext::GetAllOriginsWithFileSystem(tizen_webview::Local_File_System_Origins_Get_Callback callback, void* user_data) const {
453 content::StoragePartition* partition = BrowserContext::GetStoragePartition(browser_context_.get(), NULL);
454 BrowserThread::PostTask(
457 base::Bind(&GetFileSystemOriginsOnFILEThread, callback, user_data, partition));
460 bool EWebContext::SetFaviconDatabasePath(const char* path) {
462 return fs.SetDatabasePath(path);
465 Evas_Object *EWebContext::AddFaviconObject(const char* uri, Evas* canvas) const {
466 if (uri == NULL || canvas == NULL) {
470 SkBitmap bitmap = fs.GetBitmapForPageURL(GURL(uri));
471 if (bitmap.isNull()) {
475 Evas_Object *favicon = evas_object_image_filled_add(canvas);
476 evas_object_image_size_set(favicon, bitmap.width(), bitmap.height());
477 evas_object_image_colorspace_set(favicon, EVAS_COLORSPACE_ARGB8888);
478 bitmap.copyPixelsTo(evas_object_image_data_get(favicon, EINA_TRUE), bitmap.getSize());
483 void EWebContext::StartMemorySampler(double timerInterval) const {
484 WebMemorySampler::shared()->start(timerInterval);
487 void EWebContext::StopMemorySampler() const {
488 WebMemorySampler::shared()->stop();