- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / custom_handlers / register_protocol_handler_infobar_delegate.cc
1 // Copyright (c) 2012 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/custom_handlers/register_protocol_handler_infobar_delegate.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
9 #include "chrome/browser/infobars/infobar_service.h"
10 #include "chrome/common/url_constants.h"
11 #include "content/public/browser/user_metrics.h"
12 #include "content/public/browser/web_contents.h"
13 #include "grit/generated_resources.h"
14 #include "ui/base/l10n/l10n_util.h"
15
16 // static
17 void RegisterProtocolHandlerInfoBarDelegate::Create(
18     InfoBarService* infobar_service,
19     ProtocolHandlerRegistry* registry,
20     const ProtocolHandler& handler) {
21   content::RecordAction(
22       content::UserMetricsAction("RegisterProtocolHandler.InfoBar_Shown"));
23
24   scoped_ptr<InfoBarDelegate> infobar(
25       new RegisterProtocolHandlerInfoBarDelegate(infobar_service, registry,
26                                                  handler));
27
28   for (size_t i = 0; i < infobar_service->infobar_count(); ++i) {
29     RegisterProtocolHandlerInfoBarDelegate* existing_delegate =
30         infobar_service->infobar_at(i)->
31             AsRegisterProtocolHandlerInfoBarDelegate();
32     if ((existing_delegate != NULL) &&
33         existing_delegate->handler_.IsEquivalent(handler)) {
34       infobar_service->ReplaceInfoBar(existing_delegate, infobar.Pass());
35       return;
36     }
37   }
38
39   infobar_service->AddInfoBar(infobar.Pass());
40 }
41
42 RegisterProtocolHandlerInfoBarDelegate::RegisterProtocolHandlerInfoBarDelegate(
43     InfoBarService* infobar_service,
44     ProtocolHandlerRegistry* registry,
45     const ProtocolHandler& handler)
46     : ConfirmInfoBarDelegate(infobar_service),
47       registry_(registry),
48       handler_(handler) {
49 }
50
51 RegisterProtocolHandlerInfoBarDelegate::
52     ~RegisterProtocolHandlerInfoBarDelegate() {
53 }
54
55 InfoBarDelegate::InfoBarAutomationType
56     RegisterProtocolHandlerInfoBarDelegate::GetInfoBarAutomationType() const {
57   return RPH_INFOBAR;
58 }
59
60 InfoBarDelegate::Type
61     RegisterProtocolHandlerInfoBarDelegate::GetInfoBarType() const {
62   return PAGE_ACTION_TYPE;
63 }
64
65 RegisterProtocolHandlerInfoBarDelegate*
66     RegisterProtocolHandlerInfoBarDelegate::
67         AsRegisterProtocolHandlerInfoBarDelegate() {
68   return this;
69 }
70
71 string16 RegisterProtocolHandlerInfoBarDelegate::GetMessageText() const {
72   ProtocolHandler old_handler = registry_->GetHandlerFor(handler_.protocol());
73   return old_handler.IsEmpty() ?
74       l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM,
75           handler_.title(), UTF8ToUTF16(handler_.url().host()),
76           GetProtocolName(handler_)) :
77       l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_CONFIRM_REPLACE,
78           handler_.title(), UTF8ToUTF16(handler_.url().host()),
79           GetProtocolName(handler_), old_handler.title());
80 }
81
82 string16 RegisterProtocolHandlerInfoBarDelegate::GetButtonLabel(
83     InfoBarButton button) const {
84   return (button == BUTTON_OK) ?
85       l10n_util::GetStringFUTF16(IDS_REGISTER_PROTOCOL_HANDLER_ACCEPT,
86                                  handler_.title()) :
87       l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_DENY);
88 }
89
90 bool RegisterProtocolHandlerInfoBarDelegate::NeedElevation(
91     InfoBarButton button) const {
92   return button == BUTTON_OK;
93 }
94
95 bool RegisterProtocolHandlerInfoBarDelegate::Accept() {
96   content::RecordAction(
97       content::UserMetricsAction("RegisterProtocolHandler.Infobar_Accept"));
98   registry_->OnAcceptRegisterProtocolHandler(handler_);
99   return true;
100 }
101
102 bool RegisterProtocolHandlerInfoBarDelegate::Cancel() {
103   content::RecordAction(
104       content::UserMetricsAction("RegisterProtocolHandler.InfoBar_Deny"));
105   registry_->OnIgnoreRegisterProtocolHandler(handler_);
106   return true;
107 }
108
109 string16 RegisterProtocolHandlerInfoBarDelegate::GetLinkText() const {
110   return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
111 }
112
113 bool RegisterProtocolHandlerInfoBarDelegate::LinkClicked(
114     WindowOpenDisposition disposition) {
115   content::RecordAction(
116       content::UserMetricsAction("RegisterProtocolHandler.InfoBar_LearnMore"));
117   web_contents()->OpenURL(content::OpenURLParams(
118       GURL(chrome::kLearnMoreRegisterProtocolHandlerURL), content::Referrer(),
119       (disposition == CURRENT_TAB) ? NEW_FOREGROUND_TAB : disposition,
120       content::PAGE_TRANSITION_LINK, false));
121   return false;
122 }
123
124 string16 RegisterProtocolHandlerInfoBarDelegate::GetProtocolName(
125     const ProtocolHandler& handler) const {
126   if (handler.protocol() == "mailto")
127     return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_MAILTO_NAME);
128   if (handler.protocol() == "webcal")
129     return l10n_util::GetStringUTF16(IDS_REGISTER_PROTOCOL_HANDLER_WEBCAL_NAME);
130   return UTF8ToUTF16(handler.protocol());
131 }