[WRTjs] Refactor popup
[platform/framework/web/chromium-efl.git] / wrt / src / browser / wrt_browser_context.cc
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.
4
5 #include "wrt/src/browser/wrt_browser_context.h"
6
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"
16
17 namespace {
18
19 std::vector<base::WeakPtr<electron::ElectronBrowserContext>>
20     browser_context_list_;
21
22 }  // namespace
23
24 namespace wrt {
25
26 // static
27 WRTBrowserContext* WRTBrowserContext::GetInstance() {
28   if (browser_context_list_.empty())
29     return nullptr;
30   auto* browser_context = browser_context_list_[0].get();
31   return static_cast<WRTBrowserContext*>(browser_context);
32 }
33
34 WRTBrowserContext::WRTBrowserContext(
35     const electron::PartitionOrPath partition_location,
36     bool in_memory,
37     base::Value::Dict options)
38     : electron::ElectronBrowserContext(
39           partition_location, in_memory, std::move(options))
40 #if BUILDFLAG(IS_TIZEN_TV)
41       ,
42       web_cache_manager_(this)
43 #endif
44 {
45   SetUserAgent(EflWebView::VersionInfo::GetInstance()->DefaultUserAgent());
46   InitVisitedLinkWriter();
47
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;
56   }
57   cookie_change_notifier_ =
58       std::make_unique<electron::CookieChangeNotifier>(this);
59
60   browser_context_list_.push_back(GetWeakPtr());
61 }
62
63 WRTBrowserContext::~WRTBrowserContext() {}
64
65 void WRTBrowserContext::InitVisitedLinkWriter() {
66   if (!IsOffTheRecord()) {
67     visitedlink_writer_.reset(
68         new visitedlink::VisitedLinkWriter(this, this, false));
69     visitedlink_writer_->Init();
70   }
71 }
72
73 void WRTBrowserContext::AddVisitedURLs(const std::vector<GURL>& urls) {
74   if (!IsOffTheRecord()) {
75     DCHECK(visitedlink_writer_);
76     visitedlink_writer_->AddURLs(urls);
77   }
78 }
79
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);
87   }
88 }
89
90 storage::SpecialStoragePolicy* WRTBrowserContext::GetSpecialStoragePolicy() {
91   if (wrt_special_storage_policy_)
92     return wrt_special_storage_policy_.get();
93   else
94     return electron::ElectronBrowserContext::GetSpecialStoragePolicy();
95 }
96
97 WRTSpecialStoragePolicy* WRTBrowserContext::GetWRTSpecialStoragePolicy() {
98   if (!wrt_special_storage_policy_) {
99     wrt_special_storage_policy_ =
100         base::MakeRefCounted<WRTSpecialStoragePolicy>();
101   }
102   return wrt_special_storage_policy_.get();
103 }
104
105 content::BackgroundSyncController*
106 WRTBrowserContext::GetBackgroundSyncController() {
107   if (!background_sync_controller_)
108     background_sync_controller_.reset(new BackgroundSyncControllerEfl());
109   return background_sync_controller_.get();
110 }
111
112 void WRTBrowserContext::DeleteCookies() {
113   content::BrowserContext::GetDefaultStoragePartition()
114       ->GetCookieManagerForBrowserProcess()
115       ->DeleteCookies(network::mojom::CookieDeletionFilter::New(),
116                       base::BindOnce([](uint32_t num_deleted) {}));
117 }
118
119 #if BUILDFLAG(IS_TIZEN_TV)
120 void WRTBrowserContext::ClearWebCache() {
121   web_cache_manager_.ClearCache();
122 }
123 #endif
124
125 }  // namespace wrt
126
127 namespace electron {
128
129 // static
130 ElectronBrowserContext* ElectronBrowserContext::From(
131     const std::string& partition,
132     bool in_memory,
133     base::Value::Dict options) {
134   PartitionKey key(partition, in_memory);
135   auto* browser_context = browser_context_map()[key].get();
136   if (browser_context)
137     return browser_context;
138
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);
143   return new_context;
144 }
145
146 // static
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;
154   }
155
156   auto* new_context =
157       new wrt::WRTBrowserContext(std::cref(path), false, std::move(options));
158   browser_context_map()[key] =
159       std::unique_ptr<ElectronBrowserContext>(new_context);
160   return new_context;
161 }
162
163 }  // namespace electron