1 // Copyright 2019 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.
5 #include "wrt/src/browser/wrt_browser_context.h"
7 #include "components/visitedlink/browser/visitedlink_writer.h"
8 #include "content/public/browser/storage_partition.h"
9 #include "electron/shell/browser/cookie_change_notifier.h"
10 #include "tizen_src/chromium_impl/content/common/content_client_export.h"
11 #include "tizen_src/ewk/efl_integration/browser/background_sync_controller_efl.h"
12 #include "tizen_src/ewk/efl_integration/common/version_info.h"
13 #include "wrt/src/browser/native_web_runtime.h"
14 #include "wrt/src/browser/wrt_special_storage_policy.h"
15 #include "wrt/src/common/application_data.h"
19 std::vector<base::WeakPtr<electron::ElectronBrowserContext>>
20 browser_context_list_;
27 WRTBrowserContext* WRTBrowserContext::GetInstance() {
28 if (browser_context_list_.empty())
30 auto* browser_context = browser_context_list_[0].get();
31 return static_cast<WRTBrowserContext*>(browser_context);
34 WRTBrowserContext::WRTBrowserContext(
35 const electron::PartitionOrPath partition_location,
37 base::Value::Dict options)
38 : electron::ElectronBrowserContext(
39 partition_location, in_memory, std::move(options))
40 #if BUILDFLAG(IS_TIZEN_TV)
42 web_cache_manager_(this)
45 SetUserAgent(EflWebView::VersionInfo::GetInstance()->DefaultUserAgent());
46 InitVisitedLinkWriter();
48 auto& app_data = ApplicationData::GetInstance();
49 if (!app_data.IsElectronApp()) {
50 auto wrt_special_storage_policy =
51 base::MakeRefCounted<WRTSpecialStoragePolicy>();
52 wrt_special_storage_policy->SetQuotaExceededCallback(
53 base::BindRepeating(&NativeWebRuntime::HandleQuotaExceed,
54 wrt_special_storage_policy));
55 wrt_special_storage_policy_ = wrt_special_storage_policy;
57 cookie_change_notifier_ =
58 std::make_unique<electron::CookieChangeNotifier>(this);
60 browser_context_list_.push_back(GetWeakPtr());
63 WRTBrowserContext::~WRTBrowserContext() {}
65 void WRTBrowserContext::InitVisitedLinkWriter() {
66 if (!IsOffTheRecord()) {
67 visitedlink_writer_.reset(
68 new visitedlink::VisitedLinkWriter(this, this, false));
69 visitedlink_writer_->Init();
73 void WRTBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
74 if (!IsOffTheRecord()) {
75 DCHECK(visitedlink_writer_);
76 visitedlink_writer_->AddURLs(urls);
80 void WRTBrowserContext::RebuildTable(
81 const scoped_refptr<URLEnumerator>& enumerator) {
82 if (!IsOffTheRecord()) {
83 // WebView rebuilds from WebChromeClient.getVisitedHistory. The client
84 // can change in the lifetime of this WebView and may not yet be set here.
85 // Therefore this initialization path is not used.
86 enumerator->OnComplete(true);
90 storage::SpecialStoragePolicy* WRTBrowserContext::GetSpecialStoragePolicy() {
91 if (wrt_special_storage_policy_)
92 return wrt_special_storage_policy_.get();
94 return electron::ElectronBrowserContext::GetSpecialStoragePolicy();
97 WRTSpecialStoragePolicy* WRTBrowserContext::GetWRTSpecialStoragePolicy() {
98 if (!wrt_special_storage_policy_) {
99 wrt_special_storage_policy_ =
100 base::MakeRefCounted<WRTSpecialStoragePolicy>();
102 return wrt_special_storage_policy_.get();
105 content::BackgroundSyncController*
106 WRTBrowserContext::GetBackgroundSyncController() {
107 if (!background_sync_controller_)
108 background_sync_controller_.reset(new BackgroundSyncControllerEfl());
109 return background_sync_controller_.get();
112 void WRTBrowserContext::DeleteCookies() {
113 content::BrowserContext::GetDefaultStoragePartition()
114 ->GetCookieManagerForBrowserProcess()
115 ->DeleteCookies(network::mojom::CookieDeletionFilter::New(),
116 base::BindOnce([](uint32_t num_deleted) {}));
119 #if BUILDFLAG(IS_TIZEN_TV)
120 void WRTBrowserContext::ClearWebCache() {
121 web_cache_manager_.ClearCache();
130 ElectronBrowserContext* ElectronBrowserContext::From(
131 const std::string& partition,
133 base::Value::Dict options) {
134 PartitionKey key(partition, in_memory);
135 auto* browser_context = browser_context_map()[key].get();
137 return browser_context;
139 auto* new_context = new wrt::WRTBrowserContext(
140 std::cref(partition), in_memory, std::move(options));
141 browser_context_map()[key] =
142 std::unique_ptr<ElectronBrowserContext>(new_context);
147 ElectronBrowserContext* ElectronBrowserContext::FromPath(
148 const base::FilePath& path,
149 base::Value::Dict options) {
150 PartitionKey key(path);
151 auto* browser_context = browser_context_map()[key].get();
152 if (browser_context) {
153 return browser_context;
157 new wrt::WRTBrowserContext(std::cref(path), false, std::move(options));
158 browser_context_map()[key] =
159 std::unique_ptr<ElectronBrowserContext>(new_context);
163 } // namespace electron