| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/chrome_quota_permission_context.h" | 5 #include "chrome/browser/chrome_quota_permission_context.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "chrome/browser/infobars/infobar_tab_helper.h" | 11 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 12 #include "chrome/browser/prefs/pref_service.h" | 12 #include "chrome/browser/prefs/pref_service.h" |
| 13 #include "chrome/browser/profiles/profile.h" | 13 #include "chrome/browser/profiles/profile.h" |
| 14 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 14 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
| 15 #include "chrome/browser/tab_contents/tab_util.h" | 15 #include "chrome/browser/tab_contents/tab_util.h" |
| 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 16 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 17 #include "chrome/common/pref_names.h" | 17 #include "chrome/common/pref_names.h" |
| 18 #include "content/public/browser/browser_thread.h" | 18 #include "content/public/browser/browser_thread.h" |
| 19 #include "content/public/browser/navigation_details.h" | 19 #include "content/public/browser/navigation_details.h" |
| 20 #include "googleurl/src/gurl.h" | 20 #include "googleurl/src/gurl.h" |
| 21 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 22 #include "grit/locale_settings.h" | 22 #include "grit/locale_settings.h" |
| 23 #include "net/base/net_util.h" | 23 #include "net/base/net_util.h" |
| 24 #include "ui/base/l10n/l10n_util.h" | 24 #include "ui/base/l10n/l10n_util.h" |
| 25 #include "webkit/quota/quota_types.h" | 25 #include "webkit/quota/quota_types.h" |
| 26 | 26 |
| 27 using content::BrowserThread; | 27 using content::BrowserThread; |
| 28 using content::QuotaPermissionContext; |
| 28 using content::WebContents; | 29 using content::WebContents; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // If we requested larger quota than this threshold, show a different | 33 // If we requested larger quota than this threshold, show a different |
| 33 // message to the user. | 34 // message to the user. |
| 34 const int64 kRequestLargeQuotaThreshold = 5 * 1024 * 1024; | 35 const int64 kRequestLargeQuotaThreshold = 5 * 1024 * 1024; |
| 35 | 36 |
| 36 class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate { | 37 class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 37 public: | 38 public: |
| (...skipping 10 matching lines...) Expand all Loading... |
| 48 context_(context), | 49 context_(context), |
| 49 origin_url_(origin_url), | 50 origin_url_(origin_url), |
| 50 display_languages_(display_languages), | 51 display_languages_(display_languages), |
| 51 requested_quota_(requested_quota), | 52 requested_quota_(requested_quota), |
| 52 callback_(callback) {} | 53 callback_(callback) {} |
| 53 | 54 |
| 54 private: | 55 private: |
| 55 virtual ~RequestQuotaInfoBarDelegate() { | 56 virtual ~RequestQuotaInfoBarDelegate() { |
| 56 if (!callback_.is_null()) | 57 if (!callback_.is_null()) |
| 57 context_->DispatchCallbackOnIOThread( | 58 context_->DispatchCallbackOnIOThread( |
| 58 callback_, QuotaPermissionContext::kResponseCancelled); | 59 callback_, |
| 60 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED); |
| 59 } | 61 } |
| 60 | 62 |
| 61 virtual bool ShouldExpire( | 63 virtual bool ShouldExpire( |
| 62 const content::LoadCommittedDetails& details) | 64 const content::LoadCommittedDetails& details) |
| 63 const OVERRIDE { | 65 const OVERRIDE { |
| 64 return false; | 66 return false; |
| 65 } | 67 } |
| 66 | 68 |
| 67 virtual string16 GetMessageText() const OVERRIDE; | 69 virtual string16 GetMessageText() const OVERRIDE; |
| 68 virtual void InfoBarDismissed() OVERRIDE; | 70 virtual void InfoBarDismissed() OVERRIDE; |
| 69 virtual bool Accept() OVERRIDE; | 71 virtual bool Accept() OVERRIDE; |
| 70 virtual bool Cancel() OVERRIDE; | 72 virtual bool Cancel() OVERRIDE; |
| 71 | 73 |
| 72 scoped_refptr<ChromeQuotaPermissionContext> context_; | 74 scoped_refptr<ChromeQuotaPermissionContext> context_; |
| 73 GURL origin_url_; | 75 GURL origin_url_; |
| 74 std::string display_languages_; | 76 std::string display_languages_; |
| 75 int64 requested_quota_; | 77 int64 requested_quota_; |
| 76 PermissionCallback callback_; | 78 PermissionCallback callback_; |
| 77 DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate); | 79 DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate); |
| 78 }; | 80 }; |
| 79 | 81 |
| 80 void RequestQuotaInfoBarDelegate::InfoBarDismissed() { | 82 void RequestQuotaInfoBarDelegate::InfoBarDismissed() { |
| 81 context_->DispatchCallbackOnIOThread( | 83 context_->DispatchCallbackOnIOThread( |
| 82 callback_, QuotaPermissionContext::kResponseCancelled); | 84 callback_, |
| 85 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED); |
| 83 } | 86 } |
| 84 | 87 |
| 85 string16 RequestQuotaInfoBarDelegate::GetMessageText() const { | 88 string16 RequestQuotaInfoBarDelegate::GetMessageText() const { |
| 86 return l10n_util::GetStringFUTF16( | 89 return l10n_util::GetStringFUTF16( |
| 87 (requested_quota_ > kRequestLargeQuotaThreshold ? | 90 (requested_quota_ > kRequestLargeQuotaThreshold ? |
| 88 IDS_REQUEST_LARGE_QUOTA_INFOBAR_QUESTION : | 91 IDS_REQUEST_LARGE_QUOTA_INFOBAR_QUESTION : |
| 89 IDS_REQUEST_QUOTA_INFOBAR_QUESTION), | 92 IDS_REQUEST_QUOTA_INFOBAR_QUESTION), |
| 90 net::FormatUrl(origin_url_, display_languages_)); | 93 net::FormatUrl(origin_url_, display_languages_)); |
| 91 } | 94 } |
| 92 | 95 |
| 93 bool RequestQuotaInfoBarDelegate::Accept() { | 96 bool RequestQuotaInfoBarDelegate::Accept() { |
| 94 context_->DispatchCallbackOnIOThread( | 97 context_->DispatchCallbackOnIOThread( |
| 95 callback_, QuotaPermissionContext::kResponseAllow); | 98 callback_, |
| 99 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW); |
| 96 return true; | 100 return true; |
| 97 } | 101 } |
| 98 | 102 |
| 99 bool RequestQuotaInfoBarDelegate::Cancel() { | 103 bool RequestQuotaInfoBarDelegate::Cancel() { |
| 100 context_->DispatchCallbackOnIOThread( | 104 context_->DispatchCallbackOnIOThread( |
| 101 callback_, QuotaPermissionContext::kResponseCancelled); | 105 callback_, |
| 106 QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED); |
| 102 return true; | 107 return true; |
| 103 } | 108 } |
| 104 | 109 |
| 105 } // anonymous namespace | 110 } // anonymous namespace |
| 106 | 111 |
| 107 ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() { | 112 ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() { |
| 108 } | 113 } |
| 109 | 114 |
| 110 ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() { | 115 ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() { |
| 111 } | 116 } |
| 112 | 117 |
| 113 void ChromeQuotaPermissionContext::RequestQuotaPermission( | 118 void ChromeQuotaPermissionContext::RequestQuotaPermission( |
| 114 const GURL& origin_url, | 119 const GURL& origin_url, |
| 115 quota::StorageType type, | 120 quota::StorageType type, |
| 116 int64 requested_quota, | 121 int64 requested_quota, |
| 117 int render_process_id, | 122 int render_process_id, |
| 118 int render_view_id, | 123 int render_view_id, |
| 119 const PermissionCallback& callback) { | 124 const PermissionCallback& callback) { |
| 120 if (type != quota::kStorageTypePersistent) { | 125 if (type != quota::kStorageTypePersistent) { |
| 121 // For now we only support requesting quota with this interface | 126 // For now we only support requesting quota with this interface |
| 122 // for Persistent storage type. | 127 // for Persistent storage type. |
| 123 callback.Run(kResponseDisallow); | 128 callback.Run(QUOTA_PERMISSION_RESPONSE_DISALLOW); |
| 124 return; | 129 return; |
| 125 } | 130 } |
| 126 | 131 |
| 127 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 132 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 128 BrowserThread::PostTask( | 133 BrowserThread::PostTask( |
| 129 BrowserThread::UI, FROM_HERE, | 134 BrowserThread::UI, FROM_HERE, |
| 130 base::Bind(&ChromeQuotaPermissionContext::RequestQuotaPermission, this, | 135 base::Bind(&ChromeQuotaPermissionContext::RequestQuotaPermission, this, |
| 131 origin_url, type, requested_quota, render_process_id, | 136 origin_url, type, requested_quota, render_process_id, |
| 132 render_view_id, callback)); | 137 render_view_id, callback)); |
| 133 return; | 138 return; |
| 134 } | 139 } |
| 135 | 140 |
| 136 WebContents* web_contents = | 141 WebContents* web_contents = |
| 137 tab_util::GetWebContentsByID(render_process_id, render_view_id); | 142 tab_util::GetWebContentsByID(render_process_id, render_view_id); |
| 138 if (!web_contents) { | 143 if (!web_contents) { |
| 139 // The tab may have gone away or the request may not be from a tab. | 144 // The tab may have gone away or the request may not be from a tab. |
| 140 LOG(WARNING) << "Attempt to request quota tabless renderer: " | 145 LOG(WARNING) << "Attempt to request quota tabless renderer: " |
| 141 << render_process_id << "," << render_view_id; | 146 << render_process_id << "," << render_view_id; |
| 142 DispatchCallbackOnIOThread(callback, kResponseCancelled); | 147 DispatchCallbackOnIOThread(callback, QUOTA_PERMISSION_RESPONSE_CANCELLED); |
| 143 return; | 148 return; |
| 144 } | 149 } |
| 145 | 150 |
| 146 TabContentsWrapper* wrapper = | 151 TabContentsWrapper* wrapper = |
| 147 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); | 152 TabContentsWrapper::GetCurrentWrapperForContents(web_contents); |
| 148 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); | 153 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper(); |
| 149 infobar_helper->AddInfoBar(new RequestQuotaInfoBarDelegate( | 154 infobar_helper->AddInfoBar(new RequestQuotaInfoBarDelegate( |
| 150 infobar_helper, this, origin_url, requested_quota, | 155 infobar_helper, this, origin_url, requested_quota, |
| 151 wrapper->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages), | 156 wrapper->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages), |
| 152 callback)); | 157 callback)); |
| 153 } | 158 } |
| 154 | 159 |
| 155 void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread( | 160 void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread( |
| 156 const PermissionCallback& callback, | 161 const PermissionCallback& callback, |
| 157 Response response) { | 162 QuotaPermissionResponse response) { |
| 158 DCHECK_EQ(false, callback.is_null()); | 163 DCHECK_EQ(false, callback.is_null()); |
| 159 | 164 |
| 160 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 165 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 161 BrowserThread::PostTask( | 166 BrowserThread::PostTask( |
| 162 BrowserThread::IO, FROM_HERE, | 167 BrowserThread::IO, FROM_HERE, |
| 163 base::Bind(&ChromeQuotaPermissionContext::DispatchCallbackOnIOThread, | 168 base::Bind(&ChromeQuotaPermissionContext::DispatchCallbackOnIOThread, |
| 164 this, callback, response)); | 169 this, callback, response)); |
| 165 return; | 170 return; |
| 166 } | 171 } |
| 167 | 172 |
| 168 callback.Run(response); | 173 callback.Run(response); |
| 169 } | 174 } |
| OLD | NEW |