chromeos: bluetooth: tie Proxy lifetime to object, not observer
[chromium-blink-merge.git] / chrome / browser / chrome_quota_permission_context.cc
blob5da3f8d37d7d026ce2545d365c46cd650a26d3bf
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
3 // found in the LICENSE file.
5 #include "chrome/browser/chrome_quota_permission_context.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/utf_string_conversions.h"
11 #include "chrome/browser/infobars/infobar_tab_helper.h"
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"
18 #include "content/public/browser/browser_thread.h"
19 #include "content/public/browser/navigation_details.h"
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"
27 using content::BrowserThread;
28 using content::WebContents;
30 namespace {
32 // If we requested larger quota than this threshold, show a different
33 // message to the user.
34 const int64 kRequestLargeQuotaThreshold = 5 * 1024 * 1024;
36 class RequestQuotaInfoBarDelegate : public ConfirmInfoBarDelegate {
37 public:
38 typedef QuotaPermissionContext::PermissionCallback PermissionCallback;
40 RequestQuotaInfoBarDelegate(
41 InfoBarTabHelper* infobar_helper,
42 ChromeQuotaPermissionContext* context,
43 const GURL& origin_url,
44 int64 requested_quota,
45 const std::string& display_languages,
46 const PermissionCallback& callback)
47 : ConfirmInfoBarDelegate(infobar_helper),
48 context_(context),
49 origin_url_(origin_url),
50 display_languages_(display_languages),
51 requested_quota_(requested_quota),
52 callback_(callback) {}
54 private:
55 virtual ~RequestQuotaInfoBarDelegate() {
56 if (!callback_.is_null())
57 context_->DispatchCallbackOnIOThread(
58 callback_, QuotaPermissionContext::kResponseCancelled);
61 virtual bool ShouldExpire(
62 const content::LoadCommittedDetails& details)
63 const OVERRIDE {
64 return false;
67 virtual string16 GetMessageText() const OVERRIDE;
68 virtual void InfoBarDismissed() OVERRIDE;
69 virtual bool Accept() OVERRIDE;
70 virtual bool Cancel() OVERRIDE;
72 scoped_refptr<ChromeQuotaPermissionContext> context_;
73 GURL origin_url_;
74 std::string display_languages_;
75 int64 requested_quota_;
76 PermissionCallback callback_;
77 DISALLOW_COPY_AND_ASSIGN(RequestQuotaInfoBarDelegate);
80 void RequestQuotaInfoBarDelegate::InfoBarDismissed() {
81 context_->DispatchCallbackOnIOThread(
82 callback_, QuotaPermissionContext::kResponseCancelled);
85 string16 RequestQuotaInfoBarDelegate::GetMessageText() const {
86 return l10n_util::GetStringFUTF16(
87 (requested_quota_ > kRequestLargeQuotaThreshold ?
88 IDS_REQUEST_LARGE_QUOTA_INFOBAR_QUESTION :
89 IDS_REQUEST_QUOTA_INFOBAR_QUESTION),
90 net::FormatUrl(origin_url_, display_languages_));
93 bool RequestQuotaInfoBarDelegate::Accept() {
94 context_->DispatchCallbackOnIOThread(
95 callback_, QuotaPermissionContext::kResponseAllow);
96 return true;
99 bool RequestQuotaInfoBarDelegate::Cancel() {
100 context_->DispatchCallbackOnIOThread(
101 callback_, QuotaPermissionContext::kResponseCancelled);
102 return true;
105 } // anonymous namespace
107 ChromeQuotaPermissionContext::ChromeQuotaPermissionContext() {
110 ChromeQuotaPermissionContext::~ChromeQuotaPermissionContext() {
113 void ChromeQuotaPermissionContext::RequestQuotaPermission(
114 const GURL& origin_url,
115 quota::StorageType type,
116 int64 requested_quota,
117 int render_process_id,
118 int render_view_id,
119 const PermissionCallback& callback) {
120 if (type != quota::kStorageTypePersistent) {
121 // For now we only support requesting quota with this interface
122 // for Persistent storage type.
123 callback.Run(kResponseDisallow);
124 return;
127 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
128 BrowserThread::PostTask(
129 BrowserThread::UI, FROM_HERE,
130 base::Bind(&ChromeQuotaPermissionContext::RequestQuotaPermission, this,
131 origin_url, type, requested_quota, render_process_id,
132 render_view_id, callback));
133 return;
136 WebContents* web_contents =
137 tab_util::GetWebContentsByID(render_process_id, render_view_id);
138 if (!web_contents) {
139 // The tab may have gone away or the request may not be from a tab.
140 LOG(WARNING) << "Attempt to request quota tabless renderer: "
141 << render_process_id << "," << render_view_id;
142 DispatchCallbackOnIOThread(callback, kResponseCancelled);
143 return;
146 TabContentsWrapper* wrapper =
147 TabContentsWrapper::GetCurrentWrapperForContents(web_contents);
148 InfoBarTabHelper* infobar_helper = wrapper->infobar_tab_helper();
149 infobar_helper->AddInfoBar(new RequestQuotaInfoBarDelegate(
150 infobar_helper, this, origin_url, requested_quota,
151 wrapper->profile()->GetPrefs()->GetString(prefs::kAcceptLanguages),
152 callback));
155 void ChromeQuotaPermissionContext::DispatchCallbackOnIOThread(
156 const PermissionCallback& callback,
157 Response response) {
158 DCHECK_EQ(false, callback.is_null());
160 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) {
161 BrowserThread::PostTask(
162 BrowserThread::IO, FROM_HERE,
163 base::Bind(&ChromeQuotaPermissionContext::DispatchCallbackOnIOThread,
164 this, callback, response));
165 return;
168 callback.Run(response);