OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "chrome/browser/extensions/api/cryptotoken_private/cryptotoken_private_
api.h" |
| 6 |
| 7 #include "base/strings/string_number_conversions.h" |
| 8 #include "base/strings/utf_string_conversions.h" |
| 9 #include "chrome/browser/extensions/api/tabs/tabs_constants.h" |
| 10 #include "chrome/browser/extensions/extension_tab_util.h" |
| 11 #include "chrome/browser/infobars/infobar_service.h" |
| 12 #include "chrome/browser/profiles/profile.h" |
| 13 #include "chrome/grit/generated_resources.h" |
| 14 #include "components/infobars/core/confirm_infobar_delegate.h" |
| 15 #include "components/infobars/core/infobar.h" |
| 16 #include "extensions/common/error_utils.h" |
| 17 #include "grit/theme_resources.h" |
| 18 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 |
| 21 namespace extensions { |
| 22 namespace api { |
| 23 |
| 24 namespace { |
| 25 |
| 26 class CryptotokenPermissionInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 27 public: |
| 28 typedef base::Callback<void(cryptotoken_private::PermissionResult)> |
| 29 InfoBarCallback; |
| 30 |
| 31 static void Create(InfoBarService* infobar_service, |
| 32 const base::string16& message, |
| 33 const InfoBarCallback& callback) { |
| 34 infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( |
| 35 scoped_ptr<ConfirmInfoBarDelegate>( |
| 36 new CryptotokenPermissionInfoBarDelegate(message, callback)))); |
| 37 } |
| 38 |
| 39 private: |
| 40 CryptotokenPermissionInfoBarDelegate(const base::string16& message, |
| 41 const InfoBarCallback& callback) |
| 42 : message_(message), callback_(callback), answered_(false) {} |
| 43 |
| 44 ~CryptotokenPermissionInfoBarDelegate() override { |
| 45 if (!answered_) |
| 46 callback_.Run(cryptotoken_private::PERMISSION_RESULT_DISMISSED); |
| 47 } |
| 48 |
| 49 // ConfirmInfoBarDelegate: |
| 50 base::string16 GetMessageText() const override { return message_; } |
| 51 base::string16 GetButtonLabel(InfoBarButton button) const override { |
| 52 return l10n_util::GetStringUTF16((button == BUTTON_OK) |
| 53 ? IDS_CRYPTOTOKEN_ALLOW_BUTTON |
| 54 : IDS_CRYPTOTOKEN_DENY_BUTTON); |
| 55 } |
| 56 |
| 57 bool Accept() override { |
| 58 answered_ = true; |
| 59 callback_.Run(cryptotoken_private::PERMISSION_RESULT_ALLOWED); |
| 60 return true; |
| 61 } |
| 62 bool Cancel() override { |
| 63 answered_ = true; |
| 64 callback_.Run(cryptotoken_private::PERMISSION_RESULT_DENIED); |
| 65 return true; |
| 66 } |
| 67 |
| 68 // InfoBarDelegate: |
| 69 Type GetInfoBarType() const override { return PAGE_ACTION_TYPE; } |
| 70 int GetIconID() const override { return IDR_INFOBAR_CRYPTOTOKEN; } |
| 71 |
| 72 base::string16 message_; |
| 73 InfoBarCallback callback_; |
| 74 bool answered_; |
| 75 }; |
| 76 |
| 77 } // namespace |
| 78 |
| 79 CryptotokenPrivateRequestPermissionFunction:: |
| 80 CryptotokenPrivateRequestPermissionFunction() |
| 81 : chrome_details_(this) { |
| 82 } |
| 83 |
| 84 ExtensionFunction::ResponseAction |
| 85 CryptotokenPrivateRequestPermissionFunction::Run() { |
| 86 scoped_ptr<cryptotoken_private::RequestPermission::Params> params = |
| 87 cryptotoken_private::RequestPermission::Params::Create(*args_); |
| 88 EXTENSION_FUNCTION_VALIDATE(params); |
| 89 |
| 90 content::WebContents* web_contents = NULL; |
| 91 if (!extensions::ExtensionTabUtil::GetTabById( |
| 92 params->tab_id, chrome_details_.GetProfile(), true, NULL, NULL, |
| 93 &web_contents, NULL)) { |
| 94 return RespondNow(Error(extensions::ErrorUtils::FormatErrorMessage( |
| 95 extensions::tabs_constants::kTabNotFoundError, |
| 96 base::IntToString(params->tab_id)))); |
| 97 } |
| 98 DCHECK(web_contents); |
| 99 |
| 100 // Fetch the eTLD+1, for display purposes only |
| 101 const GURL origin_url(params->security_origin); |
| 102 if (!origin_url.is_valid()) { |
| 103 return RespondNow(Error(extensions::ErrorUtils::FormatErrorMessage( |
| 104 "Security origin * is not a valid URL", params->security_origin))); |
| 105 } |
| 106 const std::string etldp1 = |
| 107 net::registry_controlled_domains::GetDomainAndRegistry( |
| 108 origin_url, |
| 109 net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
| 110 if (etldp1.empty()) { |
| 111 return RespondNow(Error(extensions::ErrorUtils::FormatErrorMessage( |
| 112 "Could not find an eTLD for origin *", params->security_origin))); |
| 113 } |
| 114 |
| 115 CryptotokenPermissionInfoBarDelegate::Create( |
| 116 InfoBarService::FromWebContents(web_contents), |
| 117 l10n_util::GetStringFUTF16(IDS_CRYPTOTOKEN_INFOBAR_QUESTION, |
| 118 base::UTF8ToUTF16(etldp1)), |
| 119 base::Bind( |
| 120 &CryptotokenPrivateRequestPermissionFunction::OnInfobarResponse, |
| 121 this)); |
| 122 |
| 123 return RespondLater(); |
| 124 } |
| 125 |
| 126 void CryptotokenPrivateRequestPermissionFunction::OnInfobarResponse( |
| 127 cryptotoken_private::PermissionResult result) { |
| 128 Respond(ArgumentList( |
| 129 cryptotoken_private::RequestPermission::Results::Create(result))); |
| 130 } |
| 131 |
| 132 } // namespace api |
| 133 } // namespace extensions |
OLD | NEW |