summaryrefslogtreecommitdiffstats
path: root/chrome/browser/permissions/permission_update_infobar_delegate_android.cc
blob: 4e6d6c684a1eb8e773282fa11434b1c779b671e6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/permissions/permission_update_infobar_delegate_android.h"

#include "base/android/jni_array.h"
#include "base/callback_helpers.h"
#include "base/memory/scoped_ptr.h"
#include "chrome/browser/android/preferences/pref_service_bridge.h"
#include "chrome/browser/infobars/infobar_service.h"
#include "chrome/grit/chromium_strings.h"
#include "chrome/grit/generated_resources.h"
#include "chrome/grit/theme_resources.h"
#include "components/infobars/core/infobar.h"
#include "content/public/browser/android/content_view_core.h"
#include "content/public/browser/web_contents.h"
#include "jni/PermissionUpdateInfoBarDelegate_jni.h"
#include "ui/android/window_android.h"
#include "ui/base/l10n/l10n_util.h"

// static
infobars::InfoBar* PermissionUpdateInfoBarDelegate::Create(
    content::WebContents* web_contents,
    const std::vector<ContentSettingsType>& content_settings_types,
    const PermissionUpdatedCallback& callback) {
  DCHECK(ShouldShowPermissionInfobar(web_contents, content_settings_types))
      << "Caller should check ShouldShowPermissionInfobar before creating the "
      << "infobar.";

  content::ContentViewCore* cvc =
      content::ContentViewCore::FromWebContents(web_contents);
  ui::WindowAndroid* window_android = cvc->GetWindowAndroid();

  std::vector<std::string> permissions;
  int message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;

  for (ContentSettingsType content_settings_type : content_settings_types) {
    std::string android_permission =
        PrefServiceBridge::GetAndroidPermissionForContentSetting(
            content_settings_type);

    if (!android_permission.empty() &&
        !window_android->HasPermission(android_permission)) {
      permissions.push_back(android_permission);

      if (content_settings_type == CONTENT_SETTINGS_TYPE_GEOLOCATION) {
          message_id = IDS_INFOBAR_MISSING_LOCATION_PERMISSION_TEXT;
      } else if (content_settings_type ==
                 CONTENT_SETTINGS_TYPE_MEDIASTREAM_MIC) {
          message_id = IDS_INFOBAR_MISSING_MICROPHONE_PERMISSION_TEXT;
      } else if (content_settings_type ==
                 CONTENT_SETTINGS_TYPE_MEDIASTREAM_CAMERA) {
          message_id = IDS_INFOBAR_MISSING_CAMERA_PERMISSION_TEXT;
      } else {
          NOTREACHED();
          message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;
      }
    }
  }
  if (permissions.size() > 1)
    message_id = IDS_INFOBAR_MISSING_MULTIPLE_PERMISSIONS_TEXT;

  return PermissionUpdateInfoBarDelegate::Create(
      web_contents, permissions, message_id, callback);
}

// static
infobars::InfoBar* PermissionUpdateInfoBarDelegate::Create(
    content::WebContents* web_contents,
    const std::vector<std::string>& android_permissions,
    int permission_msg_id,
    const PermissionUpdatedCallback& callback) {
  InfoBarService* infobar_service =
      InfoBarService::FromWebContents(web_contents);
  if (!infobar_service) {
    callback.Run(false);
    return nullptr;
  }

  return infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
      scoped_ptr<ConfirmInfoBarDelegate>(new PermissionUpdateInfoBarDelegate(
          web_contents, android_permissions, permission_msg_id, callback))));
}

// static
bool PermissionUpdateInfoBarDelegate::ShouldShowPermissionInfobar(
    content::WebContents* web_contents,
    const std::vector<ContentSettingsType>& content_settings_types) {
  if (!web_contents)
    return false;

  content::ContentViewCore* cvc =
      content::ContentViewCore::FromWebContents(web_contents);
  if (!cvc || !cvc->GetWindowAndroid())
    return false;
  ui::WindowAndroid* window_android = cvc->GetWindowAndroid();

  for (ContentSettingsType content_settings_type : content_settings_types) {
    std::string android_permission =
        PrefServiceBridge::GetAndroidPermissionForContentSetting(
            content_settings_type);

    if (!android_permission.empty() &&
        !window_android->HasPermission(android_permission)) {
      return true;
    }
  }

  return false;
}

// static
bool PermissionUpdateInfoBarDelegate::RegisterPermissionUpdateInfoBarDelegate(
    JNIEnv* env) {
  return RegisterNativesImpl(env);
}

void PermissionUpdateInfoBarDelegate::OnPermissionResult(
    JNIEnv* env,
    const JavaParamRef<jobject>& obj,
    jboolean all_permissions_granted) {
  base::ResetAndReturn(&callback_).Run(all_permissions_granted);
  infobar()->RemoveSelf();
}

PermissionUpdateInfoBarDelegate::PermissionUpdateInfoBarDelegate(
    content::WebContents* web_contents,
    const std::vector<std::string>& android_permissions,
    int permission_msg_id,
    const PermissionUpdatedCallback& callback)
    : ConfirmInfoBarDelegate(),
      android_permissions_(android_permissions),
      permission_msg_id_(permission_msg_id),
      callback_(callback) {
  JNIEnv* env = base::android::AttachCurrentThread();
  java_delegate_.Reset(Java_PermissionUpdateInfoBarDelegate_create(
      env,
      reinterpret_cast<intptr_t>(this),
      web_contents->GetJavaWebContents().obj(),
      base::android::ToJavaArrayOfStrings(env, android_permissions_).obj()));
}

PermissionUpdateInfoBarDelegate::~PermissionUpdateInfoBarDelegate() {
  Java_PermissionUpdateInfoBarDelegate_onNativeDestroyed(
      base::android::AttachCurrentThread(), java_delegate_.obj());
}

infobars::InfoBarDelegate::InfoBarIdentifier
PermissionUpdateInfoBarDelegate::GetIdentifier() const {
  return PERMISSION_UPDATE_INFOBAR_DELEGATE;
}

int PermissionUpdateInfoBarDelegate::GetIconId() const {
  return IDR_INFOBAR_WARNING;
}

base::string16 PermissionUpdateInfoBarDelegate::GetMessageText() const {
  return l10n_util::GetStringUTF16(permission_msg_id_);
}

int PermissionUpdateInfoBarDelegate::GetButtons() const {
  return BUTTON_OK;
}

base::string16 PermissionUpdateInfoBarDelegate::GetButtonLabel(
    InfoBarButton button) const {
  DCHECK_EQ(button, BUTTON_OK);
  return l10n_util::GetStringUTF16(IDS_INFOBAR_UPDATE_PERMISSIONS_BUTTON_TEXT);
}

bool PermissionUpdateInfoBarDelegate::Accept() {
  Java_PermissionUpdateInfoBarDelegate_requestPermissions(
      base::android::AttachCurrentThread(), java_delegate_.obj());
  return false;
}

bool PermissionUpdateInfoBarDelegate::Cancel() {
  base::ResetAndReturn(&callback_).Run(false);
  return true;
}

void PermissionUpdateInfoBarDelegate::InfoBarDismissed() {
  base::ResetAndReturn(&callback_).Run(false);
}