9cc4a00f0e45e34dd5c7f07246fe4be64777b03a
[platform/framework/web/chromium-efl.git] / tizen_src / impl / browser_context_efl.cc
1 /*
2    Copyright (C) 2014 Samsung Electronics
3
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.
8
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.
13
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.
18 */
19
20 #include "browser_context_efl.h"
21
22 #include "base/bind.h"
23 #include "base/file_util.h"
24 #include "base/path_service.h"
25 #include "components/visitedlink/browser/visitedlink_master.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/render_view_host.h"
28 #include "content/public/browser/web_contents.h"
29 #include "eweb_context.h"
30
31 namespace content {
32
33 BrowserContextEfl::ResourceContextEfl::ResourceContextEfl(BrowserContextEfl *ctx)
34     : getter_(NULL),
35       browser_context_(ctx) {
36 }
37
38 BrowserContextEfl::~BrowserContextEfl() {
39   if (resource_context_) {
40     DCHECK(BrowserThread::DeleteSoon(BrowserThread::IO, FROM_HERE, resource_context_));
41     resource_context_ = NULL;
42   }
43 }
44
45 BrowserContextEfl::ResourceContextEfl::~ResourceContextEfl() {
46 }
47
48 net::HostResolver* BrowserContextEfl::ResourceContextEfl::GetHostResolver() {
49   CHECK(getter_);
50   return getter_->host_resolver();
51 }
52
53 net::URLRequestContext* BrowserContextEfl::ResourceContextEfl::GetRequestContext() {
54   CHECK(getter_);
55   return getter_->GetURLRequestContext();
56 }
57
58 bool BrowserContextEfl::ResourceContextEfl::AllowMicAccess(const GURL& origin) {
59   return false;
60 }
61
62 bool BrowserContextEfl::ResourceContextEfl::AllowCameraAccess(const GURL& origin) {
63   return false;
64 }
65
66 void BrowserContextEfl::ResourceContextEfl::set_url_request_context_getter(
67     URLRequestContextGetterEfl* getter) {
68   getter_ = getter;
69 }
70
71 BrowserContextEfl::BrowserContextEfl(EWebContext* web_context)
72   : resource_context_(NULL),
73     web_context_(web_context),
74 #if defined(ENABLE_NOTIFICATIONS)
75     notification_controllerefl_(new NotificationControllerEfl()),
76 #endif
77     temp_dir_creation_attempted_(false) {
78   InitVisitedLinkMaster();
79 }
80
81 net::URLRequestContextGetter* BrowserContextEfl::GetRequestContext() {
82   return GetStoragePartition(this, NULL)->GetURLRequestContext();
83 }
84
85 ResourceContext* BrowserContextEfl::GetResourceContext() {
86   if (!resource_context_)
87     resource_context_ = new ResourceContextEfl(this);
88
89   return resource_context_;
90 }
91
92 base::FilePath BrowserContextEfl::GetPath() const {
93   // TODO: Figure out something better for data storage.
94
95   static base::FilePath path;
96   static bool pathInitialized = false;
97
98   if (!pathInitialized)
99     PathService::Get(base::DIR_TEMP, &path);
100
101   return path;
102 }
103
104 content::NotificationControllerEfl*
105 BrowserContextEfl::GetNotificationController() const {
106 #if defined(ENABLE_NOTIFICATIONS)
107   return notification_controllerefl_.get();
108 #else
109   return NULL;
110 #endif
111 }
112
113 net::URLRequestContextGetter* BrowserContextEfl::CreateRequestContext(
114     content::ProtocolHandlerMap* protocol_handlers,
115     URLRequestInterceptorScopedVector request_interceptors) {
116   // TODO: Implement support for chromium network log
117   request_context_getter_ = new URLRequestContextGetterEfl(
118       *web_context_,
119       false,
120       GetPath(),
121       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::IO),
122       BrowserThread::UnsafeGetMessageLoopForThread(BrowserThread::FILE),
123       protocol_handlers,
124       request_interceptors.Pass(),
125       NULL);
126   GetRequestContext();
127   resource_context_->set_url_request_context_getter(request_context_getter_.get());
128   return request_context_getter_.get();
129 }
130
131 void BrowserContextEfl::SetCertificate(const char* certificate_file) {
132   base::FilePath* certificate_path = new base::FilePath(certificate_file);
133   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
134   BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
135       base::Bind(&BrowserContextEfl::ReadCertificateAndAdd, base::Owned(certificate_path)));
136 }
137
138 void BrowserContextEfl::ReadCertificateAndAdd(base::FilePath* file_path) {
139   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
140   std::string cert_contents;
141   base::ReadFileToString(*file_path, &cert_contents);
142   scoped_refptr<net::X509Certificate> cert(net::X509Certificate::CreateFromBytes(
143       cert_contents.c_str(), cert_contents.size()));
144   if (!cert) {
145     DLOG(ERROR) << "User certificate could not be parsed.";
146     return;
147   }
148   int err_code = net::CertDatabase::GetInstance()->CheckUserCert(cert.get());
149   if (net::OK != err_code) {
150     DLOG(ERROR) << "User certificate is not valid. Error code : " << err_code;
151     return;
152   }
153   err_code = net::CertDatabase::GetInstance()->AddUserCert(cert.get());
154   if (net::OK != err_code) {
155     DLOG(ERROR) << "User certificate could not be added. Error code : " << err_code;
156     return;
157   }
158 }
159
160 void BrowserContextEfl::InitVisitedLinkMaster() {
161   visitedlink_master_.reset(new visitedlink::VisitedLinkMaster(this, this, false));
162   visitedlink_master_->Init();
163 }
164
165 void BrowserContextEfl::AddVisitedURLs(const std::vector<GURL>& urls) {
166   DCHECK(visitedlink_master_);
167   visitedlink_master_->AddURLs(urls);
168 }
169
170 void BrowserContextEfl::RebuildTable(const scoped_refptr<URLEnumerator>& enumerator) {
171   // WebView rebuilds from WebChromeClient.getVisitedHistory. The client
172   // can change in the lifetime of this WebView and may not yet be set here.
173   // Therefore this initialization path is not used.
174   enumerator->OnComplete(true);
175 }
176
177 }