[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 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 "chrome/browser/chrome_quota_permission_context.h" |
| 6 | |
| 7 | #include <string> |
| 8 | |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 9 | #include "base/bind.h" |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 10 | #include "base/utf_string_conversions.h" |
[email protected] | 7e20412 | 2011-09-01 18:56:21 | [diff] [blame] | 11 | #include "chrome/browser/infobars/infobar_tab_helper.h" |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 12 | #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] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
[email protected] | 5b96836f | 2011-12-22 07:39:00 | [diff] [blame] | 19 | #include "content/public/browser/navigation_details.h" |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 20 | #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] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 27 | using content::BrowserThread; |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 28 | using content::QuotaPermissionContext; |
[email protected] | 83ff91c | 2012-01-05 20:54:13 | [diff] [blame] | 29 | using content::WebContents; |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 30 | |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | // If we requested larger quota than this threshold, show a different |
| 34 | // message to the user. |
| 35 | const int64 kRequestLargeQuotaThreshold = 5 * 1024 * 1024; |
| 36 | |
| 37 | class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 38 | public: |
| 39 | typedef QuotaPermissionContext::PermissionCallback PermissionCallback; |
| 40 | |
| 41 | RequestQuotaInfoBarDelegate( |
[email protected] | 95a33ed6 | 2011-09-30 15:07:08 | [diff] [blame] | 42 | InfoBarTabHelper* infobar_helper, |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 43 | ChromeQuotaPermissionContext* context, |
| 44 | const GURL& origin_url, |
| 45 | int64 requested_quota, |
| 46 | const std::string& display_languages, |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 47 | const PermissionCallback& callback) |
[email protected] | 95a33ed6 | 2011-09-30 15:07:08 | [diff] [blame] | 48 | : ConfirmInfoBarDelegate(infobar_helper), |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 49 | 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] | 8479139 | 2011-08-18 09:29:46 | [diff] [blame] | 56 | virtual ~RequestQuotaInfoBarDelegate() { |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 57 | if (!callback_.is_null()) |
[email protected] | 8479139 | 2011-08-18 09:29:46 | [diff] [blame] | 58 | context_->DispatchCallbackOnIOThread( |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 59 | callback_, |
| 60 | QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED); |
[email protected] | 8479139 | 2011-08-18 09:29:46 | [diff] [blame] | 61 | } |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 62 | |
| 63 | virtual bool ShouldExpire( |
[email protected] | 8286f51a | 2011-05-31 17:39:13 | [diff] [blame] | 64 | const content::LoadCommittedDetails& details) |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 65 | const OVERRIDE { |
| 66 | return false; |
| 67 | } |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 68 | |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 69 | 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] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 78 | PermissionCallback callback_; |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate); |
| 80 | }; |
| 81 | |
| 82 | void RequestQuotaInfoBarDelegate::InfoBarDismissed() { |
| 83 | context_->DispatchCallbackOnIOThread( |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 84 | callback_, |
| 85 | QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | string16 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 | |
| 96 | bool RequestQuotaInfoBarDelegate::Accept() { |
| 97 | context_->DispatchCallbackOnIOThread( |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 98 | callback_, |
| 99 | QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 100 | return true; |
| 101 | } |
| 102 | |
| 103 | bool RequestQuotaInfoBarDelegate::Cancel() { |
| 104 | context_->DispatchCallbackOnIOThread( |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 105 | callback_, |
| 106 | QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 107 | return true; |
| 108 | } |
| 109 | |
| 110 | } // anonymous namespace |
| 111 | |
| 112 | ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() { |
| 113 | } |
| 114 | |
| 115 | ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() { |
| 116 | } |
| 117 | |
| 118 | void 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] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 124 | const PermissionCallback& callback) { |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 125 | if (type != quota::kStorageTypePersistent) { |
| 126 | // For now we only support requesting quota with this interface |
| 127 | // for Persistent storage type. |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 128 | callback.Run(QUOTA_PERMISSION_RESPONSE_DISALLOW); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 129 | return; |
| 130 | } |
| 131 | |
| 132 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 133 | BrowserThread::PostTask( |
| 134 | BrowserThread::UI, FROM_HERE, |
[email protected] | 1c6bfbb | 2011-11-30 04:36:14 | [diff] [blame] | 135 | base::Bind(&ChromeQuotaPermissionContext::RequestQuotaPermission, this, |
| 136 | origin_url, type, requested_quota, render_process_id, |
| 137 | render_view_id, callback)); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
[email protected] | 83ff91c | 2012-01-05 20:54:13 | [diff] [blame] | 141 | WebContents* web_contents = |
| 142 | tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 143 | if (!web_contents) { |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 144 | // 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] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 147 | DispatchCallbackOnIOThread(callback, QUOTA_PERMISSION_RESPONSE_CANCELLED); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | |
| 151 | TabContentsWrapper* wrapper = |
[email protected] | 83ff91c | 2012-01-05 20:54:13 | [diff] [blame] | 152 | TabContentsWrapper::GetCurrentWrapperForContents(web_contents); |
[email protected] | 95a33ed6 | 2011-09-30 15:07:08 | [diff] [blame] | 153 | InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 154 | infobar_helper->AddInfoBar(new RequestQuotaInfoBarDelegate( |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 155 | infobar_helper, this, origin_url, requested_quota, |
[email protected] | cafe4ad | 2011-07-28 18:34:56 | [diff] [blame] | 156 | wrapper->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages), |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 157 | callback)); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread( |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 161 | const PermissionCallback& callback, |
[email protected] | 9f9749a | 2012-03-02 19:37:00 | [diff] [blame^] | 162 | QuotaPermissionResponse response) { |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 163 | DCHECK_EQ(false, callback.is_null()); |
| 164 | |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 165 | if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 166 | BrowserThread::PostTask( |
| 167 | BrowserThread::IO, FROM_HERE, |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 168 | base::Bind(&ChromeQuotaPermissionContext::DispatchCallbackOnIOThread, |
| 169 | this, callback, response)); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 170 | return; |
| 171 | } |
[email protected] | c8d98dd | 2011-10-18 23:12:08 | [diff] [blame] | 172 | |
| 173 | callback.Run(response); |
[email protected] | 317f96c9 | 2011-05-31 06:53:41 | [diff] [blame] | 174 | } |