permission_bubble_request_impl.cc revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
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/content_settings/permission_bubble_request_impl.h"
6
7#include "chrome/browser/content_settings/permission_context_base.h"
8#include "chrome/browser/content_settings/permission_context_uma_util.h"
9#include "chrome/grit/generated_resources.h"
10#include "grit/theme_resources.h"
11#include "net/base/net_util.h"
12#include "ui/base/l10n/l10n_util.h"
13
14PermissionBubbleRequestImpl::PermissionBubbleRequestImpl(
15    const GURL& request_origin,
16    bool user_gesture,
17    ContentSettingsType type,
18    const std::string& display_languages,
19    const base::Callback<void(bool, bool)> permission_decided_callback,
20    const base::Closure delete_callback)
21    : request_origin_(request_origin),
22      user_gesture_(user_gesture),
23      type_(type),
24      display_languages_(display_languages),
25      permission_decided_callback_(permission_decided_callback),
26      delete_callback_(delete_callback),
27      is_finished_(false),
28      action_taken_(false) {
29}
30
31PermissionBubbleRequestImpl::~PermissionBubbleRequestImpl() {
32  DCHECK(is_finished_);
33  if (!action_taken_)
34    PermissionContextUmaUtil::PermissionIgnored(type_);
35}
36
37int PermissionBubbleRequestImpl::GetIconID() const {
38  int icon_id;
39  switch (type_) {
40#if defined(ENABLE_NOTIFICATIONS)
41    case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
42      icon_id = IDR_INFOBAR_DESKTOP_NOTIFICATIONS;
43      break;
44#endif
45    case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
46      icon_id = IDR_ALLOWED_MIDI_SYSEX;
47      break;
48    case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
49      icon_id = IDR_INFOBAR_WARNING;
50      break;
51    default:
52      NOTREACHED();
53      return IDR_INFOBAR_WARNING;
54  }
55  return icon_id;
56}
57
58base::string16 PermissionBubbleRequestImpl::GetMessageText() const {
59  int message_id;
60  switch (type_) {
61#if defined(ENABLE_NOTIFICATIONS)
62      case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
63        message_id = IDS_NOTIFICATION_PERMISSIONS;
64        break;
65#endif
66      case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
67        message_id = IDS_MIDI_SYSEX_INFOBAR_QUESTION;
68        break;
69      case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
70        message_id = IDS_PUSH_MESSAGES_PERMISSION_QUESTION;
71        break;
72      default:
73        NOTREACHED();
74        return base::string16();
75    }
76  return l10n_util::GetStringFUTF16(
77      message_id,
78      net::FormatUrl(request_origin_, display_languages_,
79                     net::kFormatUrlOmitUsernamePassword |
80                     net::kFormatUrlOmitTrailingSlashOnBareHostname,
81                     net::UnescapeRule::SPACES, NULL, NULL, NULL));
82}
83
84base::string16 PermissionBubbleRequestImpl::GetMessageTextFragment() const {
85  int message_id;
86  switch (type_) {
87#if defined(ENABLE_NOTIFICATIONS)
88    case CONTENT_SETTINGS_TYPE_NOTIFICATIONS:
89      message_id = IDS_NOTIFICATION_PERMISSIONS_FRAGMENT;
90      break;
91#endif
92    case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
93      message_id = IDS_MIDI_SYSEX_PERMISSION_FRAGMENT;
94      break;
95    case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
96      message_id = IDS_PUSH_MESSAGES_BUBBLE_FRAGMENT;
97      break;
98    default:
99      NOTREACHED();
100      return base::string16();
101  }
102  return l10n_util::GetStringUTF16(message_id);
103}
104
105bool PermissionBubbleRequestImpl::HasUserGesture() const {
106  return user_gesture_;
107}
108
109GURL PermissionBubbleRequestImpl::GetRequestingHostname() const {
110  return request_origin_;
111}
112
113void PermissionBubbleRequestImpl::PermissionGranted() {
114  RegisterActionTaken();
115  permission_decided_callback_.Run(true, true);
116}
117
118void PermissionBubbleRequestImpl::PermissionDenied() {
119  RegisterActionTaken();
120  permission_decided_callback_.Run(true, false);
121}
122
123void PermissionBubbleRequestImpl::Cancelled() {
124  RegisterActionTaken();
125  permission_decided_callback_.Run(false, false);
126}
127
128void PermissionBubbleRequestImpl::RequestFinished() {
129  is_finished_ = true;
130  delete_callback_.Run();
131}
132