OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/api/messaging/incognito_connectability.h" | 5 #include "chrome/browser/extensions/api/messaging/incognito_connectability.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/strings/string16.h" | 9 #include "base/strings/string16.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 class IncognitoConnectabilityInfoBarDelegate : public ConfirmInfoBarDelegate { | 31 class IncognitoConnectabilityInfoBarDelegate : public ConfirmInfoBarDelegate { |
32 public: | 32 public: |
33 typedef base::Callback<void( | 33 typedef base::Callback<void( |
34 IncognitoConnectability::ScopedAlertTracker::Mode)> InfoBarCallback; | 34 IncognitoConnectability::ScopedAlertTracker::Mode)> InfoBarCallback; |
35 | 35 |
36 // Creates a confirmation infobar and delegate and adds the infobar to | 36 // Creates a confirmation infobar and delegate and adds the infobar to |
37 // |infobar_service|. | 37 // |infobar_service|. |
38 static InfoBar* Create(InfoBarManager* infobar_manager, | 38 static InfoBar* Create(InfoBarManager* infobar_manager, |
39 const base::string16& message, | 39 const base::string16& message, |
40 const InfoBarCallback& callback) { | 40 const InfoBarCallback& callback); |
41 return infobar_manager->AddInfoBar(infobar_manager->CreateConfirmInfoBar( | |
42 scoped_ptr<ConfirmInfoBarDelegate>( | |
43 new IncognitoConnectabilityInfoBarDelegate(message, callback)))); | |
44 } | |
45 | 41 |
46 // Set the infobar answered so that the callback is not executed when the | 42 // Marks the infobar as answered so that the callback is not executed when the |
47 // delegate is destroyed. | 43 // delegate is destroyed. |
48 void SetAnswered() { answered_ = true; } | 44 void set_answered() { answered_ = true; } |
49 | 45 |
50 private: | 46 private: |
51 IncognitoConnectabilityInfoBarDelegate(const base::string16& message, | 47 IncognitoConnectabilityInfoBarDelegate(const base::string16& message, |
52 const InfoBarCallback& callback) | 48 const InfoBarCallback& callback); |
53 : message_(message), answered_(false), callback_(callback) {} | 49 ~IncognitoConnectabilityInfoBarDelegate() override; |
54 | |
55 ~IncognitoConnectabilityInfoBarDelegate() override { | |
56 if (!answered_) { | |
57 // The infobar has closed without the user expressing an explicit | |
58 // preference. The current request should be denied but further requests | |
59 // should show an interactive prompt. | |
60 callback_.Run(IncognitoConnectability::ScopedAlertTracker::INTERACTIVE); | |
61 } | |
62 } | |
63 | 50 |
64 // ConfirmInfoBarDelegate: | 51 // ConfirmInfoBarDelegate: |
65 base::string16 GetMessageText() const override { return message_; } | 52 Type GetInfoBarType() const override; |
66 base::string16 GetButtonLabel(InfoBarButton button) const override { | 53 base::string16 GetMessageText() const override; |
67 return l10n_util::GetStringUTF16( | 54 base::string16 GetButtonLabel(InfoBarButton button) const override; |
68 (button == BUTTON_OK) ? IDS_PERMISSION_ALLOW : IDS_PERMISSION_DENY); | 55 bool Accept() override; |
69 } | 56 bool Cancel() override; |
70 | |
71 bool Accept() override { | |
72 callback_.Run(IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW); | |
73 answered_ = true; | |
74 return true; | |
75 } | |
76 bool Cancel() override { | |
77 callback_.Run(IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY); | |
78 answered_ = true; | |
79 return true; | |
80 } | |
81 | |
82 // InfoBarDelegate: | |
83 Type GetInfoBarType() const override { return PAGE_ACTION_TYPE; } | |
84 | 57 |
85 base::string16 message_; | 58 base::string16 message_; |
86 bool answered_; | 59 bool answered_; |
87 InfoBarCallback callback_; | 60 InfoBarCallback callback_; |
88 }; | 61 }; |
89 | 62 |
| 63 // static |
| 64 InfoBar* IncognitoConnectabilityInfoBarDelegate::Create( |
| 65 InfoBarManager* infobar_manager, |
| 66 const base::string16& message, |
| 67 const IncognitoConnectabilityInfoBarDelegate::InfoBarCallback& callback) { |
| 68 return infobar_manager->AddInfoBar(infobar_manager->CreateConfirmInfoBar( |
| 69 scoped_ptr<ConfirmInfoBarDelegate>( |
| 70 new IncognitoConnectabilityInfoBarDelegate(message, callback)))); |
| 71 } |
| 72 |
| 73 IncognitoConnectabilityInfoBarDelegate::IncognitoConnectabilityInfoBarDelegate( |
| 74 const base::string16& message, |
| 75 const InfoBarCallback& callback) |
| 76 : message_(message), answered_(false), callback_(callback) { |
| 77 } |
| 78 |
| 79 IncognitoConnectabilityInfoBarDelegate:: |
| 80 ~IncognitoConnectabilityInfoBarDelegate() { |
| 81 if (!answered_) { |
| 82 // The infobar has closed without the user expressing an explicit |
| 83 // preference. The current request should be denied but further requests |
| 84 // should show an interactive prompt. |
| 85 callback_.Run(IncognitoConnectability::ScopedAlertTracker::INTERACTIVE); |
| 86 } |
| 87 } |
| 88 |
| 89 infobars::InfoBarDelegate::Type |
| 90 IncognitoConnectabilityInfoBarDelegate::GetInfoBarType() const { |
| 91 return PAGE_ACTION_TYPE; |
| 92 } |
| 93 |
| 94 base::string16 IncognitoConnectabilityInfoBarDelegate::GetMessageText() const { |
| 95 return message_; |
| 96 } |
| 97 |
| 98 base::string16 IncognitoConnectabilityInfoBarDelegate::GetButtonLabel( |
| 99 InfoBarButton button) const { |
| 100 return l10n_util::GetStringUTF16( |
| 101 (button == BUTTON_OK) ? IDS_PERMISSION_ALLOW : IDS_PERMISSION_DENY); |
| 102 } |
| 103 |
| 104 bool IncognitoConnectabilityInfoBarDelegate::Accept() { |
| 105 callback_.Run(IncognitoConnectability::ScopedAlertTracker::ALWAYS_ALLOW); |
| 106 answered_ = true; |
| 107 return true; |
| 108 } |
| 109 |
| 110 bool IncognitoConnectabilityInfoBarDelegate::Cancel() { |
| 111 callback_.Run(IncognitoConnectability::ScopedAlertTracker::ALWAYS_DENY); |
| 112 answered_ = true; |
| 113 return true; |
| 114 } |
| 115 |
90 } // namespace | 116 } // namespace |
91 | 117 |
92 IncognitoConnectability::ScopedAlertTracker::ScopedAlertTracker(Mode mode) | 118 IncognitoConnectability::ScopedAlertTracker::ScopedAlertTracker(Mode mode) |
93 : last_checked_invocation_count_(g_alert_count) { | 119 : last_checked_invocation_count_(g_alert_count) { |
94 DCHECK_EQ(INTERACTIVE, g_alert_mode); | 120 DCHECK_EQ(INTERACTIVE, g_alert_mode); |
95 DCHECK_NE(INTERACTIVE, mode); | 121 DCHECK_NE(INTERACTIVE, mode); |
96 g_alert_mode = mode; | 122 g_alert_mode = mode; |
97 } | 123 } |
98 | 124 |
99 IncognitoConnectability::ScopedAlertTracker::~ScopedAlertTracker() { | 125 IncognitoConnectability::ScopedAlertTracker::~ScopedAlertTracker() { |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 // other infobars and answer all the callbacks. | 250 // other infobars and answer all the callbacks. |
225 for (const auto& map_entry : pending_origin) { | 251 for (const auto& map_entry : pending_origin) { |
226 InfoBarManager* other_infobar_manager = map_entry.first; | 252 InfoBarManager* other_infobar_manager = map_entry.first; |
227 const TabContext& other_tab_context = map_entry.second; | 253 const TabContext& other_tab_context = map_entry.second; |
228 if (other_infobar_manager != infobar_manager) { | 254 if (other_infobar_manager != infobar_manager) { |
229 // Disarm the delegate so that it doesn't think the infobar has been | 255 // Disarm the delegate so that it doesn't think the infobar has been |
230 // dismissed. | 256 // dismissed. |
231 IncognitoConnectabilityInfoBarDelegate* delegate = | 257 IncognitoConnectabilityInfoBarDelegate* delegate = |
232 static_cast<IncognitoConnectabilityInfoBarDelegate*>( | 258 static_cast<IncognitoConnectabilityInfoBarDelegate*>( |
233 other_tab_context.infobar->delegate()); | 259 other_tab_context.infobar->delegate()); |
234 delegate->SetAnswered(); | 260 delegate->set_answered(); |
235 other_infobar_manager->RemoveInfoBar(other_tab_context.infobar); | 261 other_infobar_manager->RemoveInfoBar(other_tab_context.infobar); |
236 } | 262 } |
237 callbacks.insert(callbacks.end(), other_tab_context.callbacks.begin(), | 263 callbacks.insert(callbacks.end(), other_tab_context.callbacks.begin(), |
238 other_tab_context.callbacks.end()); | 264 other_tab_context.callbacks.end()); |
239 } | 265 } |
240 pending_origins_.erase(make_pair(extension_id, origin)); | 266 pending_origins_.erase(make_pair(extension_id, origin)); |
241 } | 267 } |
242 | 268 |
243 DCHECK(!callbacks.empty()); | 269 DCHECK(!callbacks.empty()); |
244 for (const auto& callback : callbacks) { | 270 for (const auto& callback : callbacks) { |
(...skipping 13 matching lines...) Expand all Loading... |
258 BrowserContextKeyedAPIFactory<IncognitoConnectability> > g_factory = | 284 BrowserContextKeyedAPIFactory<IncognitoConnectability> > g_factory = |
259 LAZY_INSTANCE_INITIALIZER; | 285 LAZY_INSTANCE_INITIALIZER; |
260 | 286 |
261 // static | 287 // static |
262 BrowserContextKeyedAPIFactory<IncognitoConnectability>* | 288 BrowserContextKeyedAPIFactory<IncognitoConnectability>* |
263 IncognitoConnectability::GetFactoryInstance() { | 289 IncognitoConnectability::GetFactoryInstance() { |
264 return g_factory.Pointer(); | 290 return g_factory.Pointer(); |
265 } | 291 } |
266 | 292 |
267 } // namespace extensions | 293 } // namespace extensions |
OLD | NEW |