permission_bubble_request_impl.cc revision 116680a4aac90f2aa7413d9095a592090648e557
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 "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    case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
41      icon_id = IDR_ALLOWED_MIDI_SYSEX;
42      break;
43    case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
44      icon_id = IDR_INFOBAR_WARNING;
45      break;
46    default:
47      NOTREACHED();
48      return IDR_INFOBAR_WARNING;
49  }
50  return icon_id;
51}
52
53base::string16 PermissionBubbleRequestImpl::GetMessageText() const {
54  int message_id;
55  switch (type_) {
56      case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
57        message_id = IDS_MIDI_SYSEX_INFOBAR_QUESTION;
58        break;
59      case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
60        message_id = IDS_PUSH_MESSAGES_PERMISSION_QUESTION;
61        break;
62      default:
63        NOTREACHED();
64        return base::string16();
65    }
66  return l10n_util::GetStringFUTF16(
67      message_id,
68      net::FormatUrl(request_origin_, display_languages_,
69                     net::kFormatUrlOmitUsernamePassword |
70                     net::kFormatUrlOmitTrailingSlashOnBareHostname,
71                     net::UnescapeRule::SPACES, NULL, NULL, NULL));
72}
73
74base::string16 PermissionBubbleRequestImpl::GetMessageTextFragment() const {
75  int message_id;
76  switch (type_) {
77    case CONTENT_SETTINGS_TYPE_MIDI_SYSEX:
78      message_id = IDS_MIDI_SYSEX_PERMISSION_FRAGMENT;
79      break;
80    case CONTENT_SETTINGS_TYPE_PUSH_MESSAGING:
81      message_id = IDS_PUSH_MESSAGES_BUBBLE_FRAGMENT;
82      break;
83    default:
84      NOTREACHED();
85      return base::string16();
86  }
87  return l10n_util::GetStringUTF16(message_id);
88}
89
90bool PermissionBubbleRequestImpl::HasUserGesture() const {
91  return user_gesture_;
92}
93
94GURL PermissionBubbleRequestImpl::GetRequestingHostname() const {
95  return request_origin_;
96}
97
98void PermissionBubbleRequestImpl::PermissionGranted() {
99  RegisterActionTaken();
100  permission_decided_callback_.Run(true, true);
101}
102
103void PermissionBubbleRequestImpl::PermissionDenied() {
104  RegisterActionTaken();
105  permission_decided_callback_.Run(true, false);
106}
107
108void PermissionBubbleRequestImpl::Cancelled() {
109  RegisterActionTaken();
110  permission_decided_callback_.Run(false, false);
111}
112
113void PermissionBubbleRequestImpl::RequestFinished() {
114  is_finished_ = true;
115  delete_callback_.Run();
116}
117