blob: 79f9fcea471d6813098942b7e71af3a978f689b4 [file] [log] [blame]
[email protected]9f9749a2012-03-02 19:37:001// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]317f96c92011-05-31 06:53:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "chrome/browser/chrome_quota_permission_context.h"
6
avie4d7b6f2015-12-26 00:59:187#include <stdint.h>
8
[email protected]317f96c92011-05-31 06:53:419#include <string>
10
[email protected]c8d98dd2011-10-18 23:12:0811#include "base/bind.h"
avie4d7b6f2015-12-26 00:59:1812#include "base/macros.h"
[email protected]135cb802013-06-09 16:44:2013#include "base/strings/utf_string_conversions.h"
Eric Seckler8652dcd52018-09-20 10:42:2814#include "base/task/post_task.h"
avie4d7b6f2015-12-26 00:59:1815#include "build/build_config.h"
tsergeant58defcfb2016-07-19 23:47:2816#include "chrome/browser/permissions/permission_request.h"
timloh5b34b672017-03-08 01:49:3417#include "chrome/browser/permissions/permission_request_manager.h"
[email protected]317f96c92011-05-31 06:53:4118#include "chrome/browser/profiles/profile.h"
[email protected]317f96c92011-05-31 06:53:4119#include "chrome/browser/tab_contents/tab_util.h"
[email protected]af39f002014-08-22 10:18:1820#include "chrome/grit/generated_resources.h"
palmera6615c592015-09-24 00:49:0921#include "components/url_formatter/elide_url.h"
Eric Seckler8652dcd52018-09-20 10:42:2822#include "content/public/browser/browser_task_traits.h"
[email protected]c38831a12011-10-28 12:44:4923#include "content/public/browser/browser_thread.h"
[email protected]5b96836f2011-12-22 07:39:0024#include "content/public/browser/navigation_details.h"
[email protected]4f822f022012-12-20 19:11:4225#include "content/public/browser/web_contents.h"
Blink Reformata30d4232018-04-07 15:31:0626#include "third_party/blink/public/mojom/quota/quota_types.mojom.h"
[email protected]317f96c92011-05-31 06:53:4127#include "ui/base/l10n/l10n_util.h"
[email protected]761fa4702013-07-02 15:25:1528#include "url/gurl.h"
[email protected]317f96c92011-05-31 06:53:4129
estade6d95d1d2015-10-02 18:55:2330#if defined(OS_ANDROID)
estade20c051a92016-10-15 22:53:2231#include "chrome/browser/android/android_theme_resources.h"
estade6d95d1d2015-10-02 18:55:2332#else
Joshua Bell82b9c71d2017-10-14 00:23:5233#include "chrome/app/vector_icons/vector_icons.h"
Evan Stade619a89be2017-07-17 20:24:2634#include "components/vector_icons/vector_icons.h"
estade6d95d1d2015-10-02 18:55:2335#endif
36
[email protected]c032489d2014-02-11 19:07:2437namespace {
38
timlohaa3ce262017-06-01 05:29:4039// On Android, if the site requested larger quota than this threshold, show a
40// different message to the user.
avie4d7b6f2015-12-26 00:59:1841const int64_t kRequestLargeQuotaThreshold = 5 * 1024 * 1024;
[email protected]c032489d2014-02-11 19:07:2442
43// QuotaPermissionRequest ---------------------------------------------
44
tsergeant58defcfb2016-07-19 23:47:2845class QuotaPermissionRequest : public PermissionRequest {
[email protected]c032489d2014-02-11 19:07:2446 public:
47 QuotaPermissionRequest(
48 ChromeQuotaPermissionContext* context,
49 const GURL& origin_url,
timlohaa3ce262017-06-01 05:29:4050 bool is_large_quota_request_,
[email protected]c032489d2014-02-11 19:07:2451 const content::QuotaPermissionContext::PermissionCallback& callback);
52
Daniel Chenga542fca2014-10-21 09:51:2953 ~QuotaPermissionRequest() override;
[email protected]c032489d2014-02-11 19:07:2454
benwells46b02fa2016-04-20 02:37:0255 private:
tsergeant58defcfb2016-07-19 23:47:2856 // PermissionRequest:
estade20c051a92016-10-15 22:53:2257 IconId GetIconId() const override;
timlohaa3ce262017-06-01 05:29:4058#if defined(OS_ANDROID)
59 base::string16 GetMessageText() const override;
60#endif
Daniel Chenga542fca2014-10-21 09:51:2961 base::string16 GetMessageTextFragment() const override;
johnmefe4e96d2016-01-27 16:14:4162 GURL GetOrigin() const override;
Daniel Chenga542fca2014-10-21 09:51:2963 void PermissionGranted() override;
64 void PermissionDenied() override;
65 void Cancelled() override;
66 void RequestFinished() override;
tsergeant58defcfb2016-07-19 23:47:2867 PermissionRequestType GetPermissionRequestType() const override;
[email protected]c032489d2014-02-11 19:07:2468
timlohaa3ce262017-06-01 05:29:4069 const scoped_refptr<ChromeQuotaPermissionContext> context_;
70 const GURL origin_url_;
71 const bool is_large_quota_request_;
[email protected]c032489d2014-02-11 19:07:2472 content::QuotaPermissionContext::PermissionCallback callback_;
73
74 DISALLOW_COPY_AND_ASSIGN(QuotaPermissionRequest);
75};
76
77QuotaPermissionRequest::QuotaPermissionRequest(
78 ChromeQuotaPermissionContext* context,
79 const GURL& origin_url,
timlohaa3ce262017-06-01 05:29:4080 bool is_large_quota_request,
[email protected]c032489d2014-02-11 19:07:2481 const content::QuotaPermissionContext::PermissionCallback& callback)
82 : context_(context),
83 origin_url_(origin_url),
timlohaa3ce262017-06-01 05:29:4084 is_large_quota_request_(is_large_quota_request),
85 callback_(callback) {
86 // Suppress unused private field warning on desktop
87 (void)is_large_quota_request_;
88}
[email protected]c032489d2014-02-11 19:07:2489
90QuotaPermissionRequest::~QuotaPermissionRequest() {}
91
estade20c051a92016-10-15 22:53:2292PermissionRequest::IconId QuotaPermissionRequest::GetIconId() const {
estade20c051a92016-10-15 22:53:2293#if defined(OS_ANDROID)
Joshua Bell82b9c71d2017-10-14 00:23:5294 return IDR_ANDROID_INFOBAR_FOLDER;
estade20c051a92016-10-15 22:53:2295#else
Joshua Bell82b9c71d2017-10-14 00:23:5296 return kFolderIcon;
estade20c051a92016-10-15 22:53:2297#endif
[email protected]d23cdeee2014-03-10 06:39:5398}
99
timlohaa3ce262017-06-01 05:29:40100#if defined(OS_ANDROID)
101base::string16 QuotaPermissionRequest::GetMessageText() const {
102 // If the site requested larger quota than this threshold, show a different
103 // message to the user.
104 return l10n_util::GetStringFUTF16(
Peter Kasting16ac39a2018-01-04 00:48:43105 (is_large_quota_request_ ? IDS_REQUEST_LARGE_QUOTA_INFOBAR_TEXT
106 : IDS_REQUEST_QUOTA_INFOBAR_TEXT),
timlohaa3ce262017-06-01 05:29:40107 url_formatter::FormatUrlForSecurityDisplay(origin_url_));
108}
109#endif
110
[email protected]c032489d2014-02-11 19:07:24111base::string16 QuotaPermissionRequest::GetMessageTextFragment() const {
112 return l10n_util::GetStringUTF16(IDS_REQUEST_QUOTA_PERMISSION_FRAGMENT);
113}
114
johnmefe4e96d2016-01-27 16:14:41115GURL QuotaPermissionRequest::GetOrigin() const {
[email protected]d23cdeee2014-03-10 06:39:53116 return origin_url_;
[email protected]c032489d2014-02-11 19:07:24117}
118
119void QuotaPermissionRequest::PermissionGranted() {
120 context_->DispatchCallbackOnIOThread(
121 callback_,
122 content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW);
123 callback_ = content::QuotaPermissionContext::PermissionCallback();
124}
125
126void QuotaPermissionRequest::PermissionDenied() {
127 context_->DispatchCallbackOnIOThread(
128 callback_,
129 content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_DISALLOW);
130 callback_ = content::QuotaPermissionContext::PermissionCallback();
131}
132
133void QuotaPermissionRequest::Cancelled() {
134}
135
136void QuotaPermissionRequest::RequestFinished() {
137 if (!callback_.is_null()) {
138 context_->DispatchCallbackOnIOThread(
139 callback_,
140 content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED);
141 }
142
143 delete this;
144}
[email protected]631bb742011-11-02 11:29:39145
tsergeant58defcfb2016-07-19 23:47:28146PermissionRequestType QuotaPermissionRequest::GetPermissionRequestType() const {
147 return PermissionRequestType::QUOTA;
benwells46b02fa2016-04-20 02:37:02148}
149
[email protected]49c71ac2013-05-03 01:36:22150} // namespace
151
152
153// ChromeQuotaPermissionContext -----------------------------------------------
[email protected]317f96c92011-05-31 06:53:41154
[email protected]9a88ea992013-07-10 21:21:57155ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() {
156}
[email protected]317f96c92011-05-31 06:53:41157
158void ChromeQuotaPermissionContext::RequestQuotaPermission(
[email protected]f78aa7722014-04-04 00:04:25159 const content::StorageQuotaParams& params,
[email protected]317f96c92011-05-31 06:53:41160 int render_process_id,
[email protected]c8d98dd2011-10-18 23:12:08161 const PermissionCallback& callback) {
Sasha Bermeisterb8957f82018-01-04 04:17:40162 if (params.storage_type != blink::mojom::StorageType::kPersistent) {
[email protected]317f96c92011-05-31 06:53:41163 // For now we only support requesting quota with this interface
164 // for Persistent storage type.
[email protected]9f9749a2012-03-02 19:37:00165 callback.Run(QUOTA_PERMISSION_RESPONSE_DISALLOW);
[email protected]317f96c92011-05-31 06:53:41166 return;
167 }
168
[email protected]9a88ea992013-07-10 21:21:57169 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
Eric Seckler8652dcd52018-09-20 10:42:28170 base::PostTaskWithTraits(
171 FROM_HERE, {content::BrowserThread::UI},
tzik29ea5c72017-04-20 02:16:51172 base::BindOnce(&ChromeQuotaPermissionContext::RequestQuotaPermission,
173 this, params, render_process_id, callback));
[email protected]317f96c92011-05-31 06:53:41174 return;
175 }
176
nick6206d822016-12-09 20:05:03177 content::WebContents* web_contents = tab_util::GetWebContentsByFrameID(
178 render_process_id, params.render_frame_id);
[email protected]83ff91c2012-01-05 20:54:13179 if (!web_contents) {
[email protected]317f96c92011-05-31 06:53:41180 // The tab may have gone away or the request may not be from a tab.
181 LOG(WARNING) << "Attempt to request quota tabless renderer: "
nick6206d822016-12-09 20:05:03182 << render_process_id << "," << params.render_frame_id;
[email protected]9f9749a2012-03-02 19:37:00183 DispatchCallbackOnIOThread(callback, QUOTA_PERMISSION_RESPONSE_CANCELLED);
[email protected]317f96c92011-05-31 06:53:41184 return;
185 }
186
Timothy Loh8786fb32017-09-06 03:04:05187 PermissionRequestManager* permission_request_manager =
188 PermissionRequestManager::FromWebContents(web_contents);
189 if (permission_request_manager) {
190 bool is_large_quota_request =
191 params.requested_size > kRequestLargeQuotaThreshold;
192 permission_request_manager->AddRequest(new QuotaPermissionRequest(
193 this, params.origin_url, is_large_quota_request, callback));
194 return;
timloh5b34b672017-03-08 01:49:34195 }
[email protected]c032489d2014-02-11 19:07:24196
estade6d95d1d2015-10-02 18:55:23197 // The tab has no UI service for presenting the permissions request.
198 LOG(WARNING) << "Attempt to request quota from a background page: "
nick6206d822016-12-09 20:05:03199 << render_process_id << "," << params.render_frame_id;
estade6d95d1d2015-10-02 18:55:23200 DispatchCallbackOnIOThread(callback, QUOTA_PERMISSION_RESPONSE_CANCELLED);
[email protected]317f96c92011-05-31 06:53:41201}
202
203void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread(
[email protected]c8d98dd2011-10-18 23:12:08204 const PermissionCallback& callback,
[email protected]9f9749a2012-03-02 19:37:00205 QuotaPermissionResponse response) {
[email protected]c8d98dd2011-10-18 23:12:08206 DCHECK_EQ(false, callback.is_null());
207
[email protected]9a88ea992013-07-10 21:21:57208 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
Eric Seckler8652dcd52018-09-20 10:42:28209 base::PostTaskWithTraits(
210 FROM_HERE, {content::BrowserThread::IO},
tzik29ea5c72017-04-20 02:16:51211 base::BindOnce(
212 &ChromeQuotaPermissionContext::DispatchCallbackOnIOThread, this,
213 callback, response));
[email protected]317f96c92011-05-31 06:53:41214 return;
215 }
[email protected]c8d98dd2011-10-18 23:12:08216
217 callback.Run(response);
[email protected]317f96c92011-05-31 06:53:41218}
[email protected]649d1c02012-04-27 02:56:21219
220ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() {}