1 // Copyright (c) 2012 The Chromium Authors. 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 "chrome/browser/chrome_quota_permission_context.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
13 #include "chrome/browser/infobars/infobar.h"
14 #include "chrome/browser/infobars/infobar_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/tab_contents/tab_util.h"
17 #include "chrome/common/pref_names.h"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/navigation_details.h"
20 #include "content/public/browser/web_contents.h"
21 #include "grit/generated_resources.h"
22 #include "grit/locale_settings.h"
23 #include "net/base/net_util.h"
24 #include "ui/base/l10n/l10n_util.h"
26 #include "webkit/common/quota/quota_types.h"
30 // RequestQuotaInfoBarDelegate ------------------------------------------------
34 class RequestQuotaInfoBarDelegate
: public ConfirmInfoBarDelegate
{
36 // Creates a request quota infobar and delegate and adds the infobar to
39 InfoBarService
* infobar_service
,
40 ChromeQuotaPermissionContext
* context
,
41 const GURL
& origin_url
,
42 int64 requested_quota
,
43 const std::string
& display_languages
,
44 const content::QuotaPermissionContext::PermissionCallback
& callback
);
47 RequestQuotaInfoBarDelegate(
48 ChromeQuotaPermissionContext
* context
,
49 const GURL
& origin_url
,
50 int64 requested_quota
,
51 const std::string
& display_languages
,
52 const content::QuotaPermissionContext::PermissionCallback
& callback
);
53 virtual ~RequestQuotaInfoBarDelegate();
55 // ConfirmInfoBarDelegate:
56 virtual bool ShouldExpireInternal(
57 const content::LoadCommittedDetails
& details
) const OVERRIDE
;
58 virtual base::string16
GetMessageText() const OVERRIDE
;
59 virtual bool Accept() OVERRIDE
;
60 virtual bool Cancel() OVERRIDE
;
62 scoped_refptr
<ChromeQuotaPermissionContext
> context_
;
64 std::string display_languages_
;
65 int64 requested_quota_
;
66 content::QuotaPermissionContext::PermissionCallback callback_
;
68 DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate
);
72 void RequestQuotaInfoBarDelegate::Create(
73 InfoBarService
* infobar_service
,
74 ChromeQuotaPermissionContext
* context
,
75 const GURL
& origin_url
,
76 int64 requested_quota
,
77 const std::string
& display_languages
,
78 const content::QuotaPermissionContext::PermissionCallback
& callback
) {
79 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
80 scoped_ptr
<ConfirmInfoBarDelegate
>(new RequestQuotaInfoBarDelegate(
81 context
, origin_url
, requested_quota
, display_languages
, callback
))));
84 RequestQuotaInfoBarDelegate::RequestQuotaInfoBarDelegate(
85 ChromeQuotaPermissionContext
* context
,
86 const GURL
& origin_url
,
87 int64 requested_quota
,
88 const std::string
& display_languages
,
89 const content::QuotaPermissionContext::PermissionCallback
& callback
)
90 : ConfirmInfoBarDelegate(),
92 origin_url_(origin_url
),
93 display_languages_(display_languages
),
94 requested_quota_(requested_quota
),
98 RequestQuotaInfoBarDelegate::~RequestQuotaInfoBarDelegate() {
99 if (!callback_
.is_null()) {
100 context_
->DispatchCallbackOnIOThread(
102 content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED
);
106 bool RequestQuotaInfoBarDelegate::ShouldExpireInternal(
107 const content::LoadCommittedDetails
& details
) const {
111 base::string16
RequestQuotaInfoBarDelegate::GetMessageText() const {
112 // If the site requested larger quota than this threshold, show a different
113 // message to the user.
114 const int64 kRequestLargeQuotaThreshold
= 5 * 1024 * 1024;
115 return l10n_util::GetStringFUTF16(
116 (requested_quota_
> kRequestLargeQuotaThreshold
?
117 IDS_REQUEST_LARGE_QUOTA_INFOBAR_QUESTION
:
118 IDS_REQUEST_QUOTA_INFOBAR_QUESTION
),
119 net::FormatUrl(origin_url_
, display_languages_
));
122 bool RequestQuotaInfoBarDelegate::Accept() {
123 context_
->DispatchCallbackOnIOThread(
125 content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW
);
129 bool RequestQuotaInfoBarDelegate::Cancel() {
130 context_
->DispatchCallbackOnIOThread(
132 content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED
);
139 // ChromeQuotaPermissionContext -----------------------------------------------
141 ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() {
144 void ChromeQuotaPermissionContext::RequestQuotaPermission(
145 const GURL
& origin_url
,
146 quota::StorageType type
,
147 int64 requested_quota
,
148 int render_process_id
,
150 const PermissionCallback
& callback
) {
151 if (type
!= quota::kStorageTypePersistent
) {
152 // For now we only support requesting quota with this interface
153 // for Persistent storage type.
154 callback
.Run(QUOTA_PERMISSION_RESPONSE_DISALLOW
);
158 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI
)) {
159 content::BrowserThread::PostTask(
160 content::BrowserThread::UI
, FROM_HERE
,
161 base::Bind(&ChromeQuotaPermissionContext::RequestQuotaPermission
, this,
162 origin_url
, type
, requested_quota
, render_process_id
,
163 render_view_id
, callback
));
167 content::WebContents
* web_contents
=
168 tab_util::GetWebContentsByID(render_process_id
, render_view_id
);
170 // The tab may have gone away or the request may not be from a tab.
171 LOG(WARNING
) << "Attempt to request quota tabless renderer: "
172 << render_process_id
<< "," << render_view_id
;
173 DispatchCallbackOnIOThread(callback
, QUOTA_PERMISSION_RESPONSE_CANCELLED
);
177 InfoBarService
* infobar_service
=
178 InfoBarService::FromWebContents(web_contents
);
179 if (!infobar_service
) {
180 // The tab has no infobar service.
181 LOG(WARNING
) << "Attempt to request quota from a background page: "
182 << render_process_id
<< "," << render_view_id
;
183 DispatchCallbackOnIOThread(callback
, QUOTA_PERMISSION_RESPONSE_CANCELLED
);
186 RequestQuotaInfoBarDelegate::Create(
187 infobar_service
, this, origin_url
, requested_quota
,
188 Profile::FromBrowserContext(web_contents
->GetBrowserContext())->
189 GetPrefs()->GetString(prefs::kAcceptLanguages
),
193 void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread(
194 const PermissionCallback
& callback
,
195 QuotaPermissionResponse response
) {
196 DCHECK_EQ(false, callback
.is_null());
198 if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::IO
)) {
199 content::BrowserThread::PostTask(
200 content::BrowserThread::IO
, FROM_HERE
,
201 base::Bind(&ChromeQuotaPermissionContext::DispatchCallbackOnIOThread
,
202 this, callback
, response
));
206 callback
.Run(response
);
209 ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() {}