blob: 48ac49de52620f67c2d7d509e697fafe782b5409 [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
7#include <string>
8
[email protected]c8d98dd2011-10-18 23:12:089#include "base/bind.h"
[email protected]317f96c92011-05-31 06:53:4110#include "base/utf_string_conversions.h"
[email protected]7e204122011-09-01 18:56:2111#include "chrome/browser/infobars/infobar_tab_helper.h"
[email protected]317f96c92011-05-31 06:53:4112#include "chrome/browser/prefs/pref_service.h"
13#include "chrome/browser/profiles/profile.h"
14#include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
15#include "chrome/browser/tab_contents/tab_util.h"
16#include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
17#include "chrome/common/pref_names.h"
[email protected]c38831a12011-10-28 12:44:4918#include "content/public/browser/browser_thread.h"
[email protected]5b96836f2011-12-22 07:39:0019#include "content/public/browser/navigation_details.h"
[email protected]317f96c92011-05-31 06:53:4120#include "googleurl/src/gurl.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"
25#include "webkit/quota/quota_types.h"
26
[email protected]631bb742011-11-02 11:29:3927using content::BrowserThread;
[email protected]9f9749a2012-03-02 19:37:0028using content::QuotaPermissionContext;
[email protected]83ff91c2012-01-05 20:54:1329using content::WebContents;
[email protected]631bb742011-11-02 11:29:3930
[email protected]317f96c92011-05-31 06:53:4131namespace {
32
33// If we requested larger quota than this threshold, show a different
34// message to the user.
35const int64 kRequestLargeQuotaThreshold = 5 * 1024 * 1024;
36
37class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate {
38 public:
39 typedef QuotaPermissionContext::PermissionCallback PermissionCallback;
40
41 RequestQuotaInfoBarDelegate(
[email protected]95a33ed62011-09-30 15:07:0842 InfoBarTabHelper* infobar_helper,
[email protected]317f96c92011-05-31 06:53:4143 ChromeQuotaPermissionContext* context,
44 const GURL& origin_url,
45 int64 requested_quota,
46 const std::string& display_languages,
[email protected]c8d98dd2011-10-18 23:12:0847 const PermissionCallback& callback)
[email protected]95a33ed62011-09-30 15:07:0848 : ConfirmInfoBarDelegate(infobar_helper),
[email protected]317f96c92011-05-31 06:53:4149 context_(context),
50 origin_url_(origin_url),
51 display_languages_(display_languages),
52 requested_quota_(requested_quota),
53 callback_(callback) {}
54
55 private:
[email protected]84791392011-08-18 09:29:4656 virtual ~RequestQuotaInfoBarDelegate() {
[email protected]c8d98dd2011-10-18 23:12:0857 if (!callback_.is_null())
[email protected]84791392011-08-18 09:29:4658 context_->DispatchCallbackOnIOThread(
[email protected]9f9749a2012-03-02 19:37:0059 callback_,
60 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED);
[email protected]84791392011-08-18 09:29:4661 }
[email protected]317f96c92011-05-31 06:53:4162
63 virtual bool ShouldExpire(
[email protected]8286f51a2011-05-31 17:39:1364 const content::LoadCommittedDetails& details)
[email protected]317f96c92011-05-31 06:53:4165 const OVERRIDE {
66 return false;
67 }
[email protected]c8d98dd2011-10-18 23:12:0868
[email protected]317f96c92011-05-31 06:53:4169 virtual string16 GetMessageText() const OVERRIDE;
70 virtual void InfoBarDismissed() OVERRIDE;
71 virtual bool Accept() OVERRIDE;
72 virtual bool Cancel() OVERRIDE;
73
74 scoped_refptr<ChromeQuotaPermissionContext> context_;
75 GURL origin_url_;
76 std::string display_languages_;
77 int64 requested_quota_;
[email protected]c8d98dd2011-10-18 23:12:0878 PermissionCallback callback_;
[email protected]317f96c92011-05-31 06:53:4179 DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate);
80};
81
82void RequestQuotaInfoBarDelegate::InfoBarDismissed() {
83 context_->DispatchCallbackOnIOThread(
[email protected]9f9749a2012-03-02 19:37:0084 callback_,
85 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED);
[email protected]317f96c92011-05-31 06:53:4186}
87
88string16 RequestQuotaInfoBarDelegate::GetMessageText() const {
89 return l10n_util::GetStringFUTF16(
90 (requested_quota_ > kRequestLargeQuotaThreshold ?
91 IDS_REQUEST_LARGE_QUOTA_INFOBAR_QUESTION :
92 IDS_REQUEST_QUOTA_INFOBAR_QUESTION),
93 net::FormatUrl(origin_url_, display_languages_));
94}
95
96bool RequestQuotaInfoBarDelegate::Accept() {
97 context_->DispatchCallbackOnIOThread(
[email protected]9f9749a2012-03-02 19:37:0098 callback_,
99 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW);
[email protected]317f96c92011-05-31 06:53:41100 return true;
101}
102
103bool RequestQuotaInfoBarDelegate::Cancel() {
104 context_->DispatchCallbackOnIOThread(
[email protected]9f9749a2012-03-02 19:37:00105 callback_,
106 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED);
[email protected]317f96c92011-05-31 06:53:41107 return true;
108}
109
110} // anonymous namespace
111
112ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() {
113}
114
115ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() {
116}
117
118void ChromeQuotaPermissionContext::RequestQuotaPermission(
119 const GURL& origin_url,
120 quota::StorageType type,
121 int64 requested_quota,
122 int render_process_id,
123 int render_view_id,
[email protected]c8d98dd2011-10-18 23:12:08124 const PermissionCallback& callback) {
[email protected]317f96c92011-05-31 06:53:41125 if (type != quota::kStorageTypePersistent) {
126 // For now we only support requesting quota with this interface
127 // for Persistent storage type.
[email protected]9f9749a2012-03-02 19:37:00128 callback.Run(QUOTA_PERMISSION_RESPONSE_DISALLOW);
[email protected]317f96c92011-05-31 06:53:41129 return;
130 }
131
132 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
133 BrowserThread::PostTask(
134 BrowserThread::UI, FROM_HERE,
[email protected]1c6bfbb2011-11-30 04:36:14135 base::Bind(&ChromeQuotaPermissionContext::RequestQuotaPermission, this,
136 origin_url, type, requested_quota, render_process_id,
137 render_view_id, callback));
[email protected]317f96c92011-05-31 06:53:41138 return;
139 }
140
[email protected]83ff91c2012-01-05 20:54:13141 WebContents* web_contents =
142 tab_util::GetWebContentsByID(render_process_id, render_view_id);
143 if (!web_contents) {
[email protected]317f96c92011-05-31 06:53:41144 // The tab may have gone away or the request may not be from a tab.
145 LOG(WARNING) << "Attempt to request quota tabless renderer: "
146 << render_process_id << "," << render_view_id;
[email protected]9f9749a2012-03-02 19:37:00147 DispatchCallbackOnIOThread(callback, QUOTA_PERMISSION_RESPONSE_CANCELLED);
[email protected]317f96c92011-05-31 06:53:41148 return;
149 }
150
151 TabContentsWrapper* wrapper =
[email protected]83ff91c2012-01-05 20:54:13152 TabContentsWrapper::GetCurrentWrapperForContents(web_contents);
[email protected]95a33ed62011-09-30 15:07:08153 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper();
154 infobar_helper->AddInfoBar(new RequestQuotaInfoBarDelegate(
[email protected]c8d98dd2011-10-18 23:12:08155 infobar_helper, this, origin_url, requested_quota,
[email protected]cafe4ad2011-07-28 18:34:56156 wrapper->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages),
[email protected]c8d98dd2011-10-18 23:12:08157 callback));
[email protected]317f96c92011-05-31 06:53:41158}
159
160void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread(
[email protected]c8d98dd2011-10-18 23:12:08161 const PermissionCallback& callback,
[email protected]9f9749a2012-03-02 19:37:00162 QuotaPermissionResponse response) {
[email protected]c8d98dd2011-10-18 23:12:08163 DCHECK_EQ(false, callback.is_null());
164
[email protected]317f96c92011-05-31 06:53:41165 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
166 BrowserThread::PostTask(
167 BrowserThread::IO, FROM_HERE,
[email protected]c8d98dd2011-10-18 23:12:08168 base::Bind(&ChromeQuotaPermissionContext::DispatchCallbackOnIOThread,
169 this, callback, response));
[email protected]317f96c92011-05-31 06:53:41170 return;
171 }
[email protected]c8d98dd2011-10-18 23:12:08172
173 callback.Run(response);
[email protected]317f96c92011-05-31 06:53:41174}