summaryrefslogtreecommitdiffstats
path: root/chrome/browser/ui/gtk/website_settings/permission_selector.cc
blob: 56d02200e8ee6628ddf25de42f21c2d66fd0efc7 (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
// Copyright (c) 2012 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/ui/gtk/website_settings/permission_selector.h"

#include "base/compiler_specific.h"
#include "base/i18n/rtl.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/ui/gtk/gtk_theme_service.h"
#include "chrome/browser/ui/gtk/gtk_util.h"
#include "chrome/browser/ui/gtk/menu_gtk.h"
#include "chrome/browser/ui/website_settings/website_settings_ui.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ui/base/gtk/gtk_hig_constants.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/menu_model.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"

namespace {

ContentSetting CommandIdToContentSetting(int command_id) {
  switch (command_id) {
    case PermissionMenuModel::COMMAND_SET_TO_DEFAULT:
      return CONTENT_SETTING_DEFAULT;
    case PermissionMenuModel::COMMAND_SET_TO_ALLOW:
      return CONTENT_SETTING_ALLOW;
    case PermissionMenuModel::COMMAND_SET_TO_BLOCK:
      return CONTENT_SETTING_BLOCK;
    default:
      NOTREACHED();
      return CONTENT_SETTING_DEFAULT;
  }
}

}  // namespace

PermissionSelector::PermissionSelector(GtkThemeService* theme_service,
                                       const GURL& url,
                                       ContentSettingsType type,
                                       ContentSetting setting,
                                       ContentSetting default_setting,
                                       content_settings::SettingSource source)
    : widget_(NULL),
      menu_button_(NULL),
      icon_(NULL),
      type_(type),
      default_setting_(default_setting),
      setting_(setting) {
  DCHECK_NE(default_setting, CONTENT_SETTING_DEFAULT);

  // Create permission info box.
  const int kChildSpacing = 4;
  widget_ = gtk_hbox_new(FALSE, kChildSpacing);

  // Add permission type icon.
  ContentSetting effective_setting = setting;
  if (effective_setting == CONTENT_SETTING_DEFAULT)
    effective_setting = default_setting;
  GdkPixbuf* pixbuf = WebsiteSettingsUI::GetPermissionIcon(
      type, effective_setting).ToGdkPixbuf();
  icon_ = gtk_image_new_from_pixbuf(pixbuf);
  gtk_box_pack_start(GTK_BOX(widget_), icon_, FALSE, FALSE, 0);

  // Add a label for the permission type.
  GtkWidget* label = theme_service->BuildLabel(l10n_util::GetStringFUTF8(
      IDS_WEBSITE_SETTINGS_PERMISSION_TYPE,
      WebsiteSettingsUI::PermissionTypeToUIString(type)),
      ui::kGdkBlack);
  gtk_label_set_line_wrap_mode(GTK_LABEL(label), PANGO_WRAP_WORD_CHAR);

  gtk_box_pack_start(GTK_BOX(widget_), label, FALSE, FALSE, 0);

  // Add the menu button.
  menu_button_ = theme_service->BuildChromeButton();
  GtkWidget* button_hbox = gtk_hbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(menu_button_), button_hbox);

  GtkWidget* button_label = theme_service->BuildLabel(
      base::UTF16ToUTF8(WebsiteSettingsUI::PermissionActionToUIString(
          setting, default_setting, source)),
      ui::kGdkBlack);
  gtk_box_pack_start(GTK_BOX(button_hbox), button_label, FALSE, FALSE,
                     ui::kControlSpacing);

  bool user_setting = source == content_settings::SETTING_SOURCE_USER;
  gtk_widget_set_sensitive(GTK_WIDGET(menu_button_), user_setting);
  if (user_setting) {
    GtkWidget* arrow = NULL;
    // We don't handle theme changes, which is a bug but they are very unlikely
    // to occur while a bubble is grabbing input.
    if (theme_service->UsingNativeTheme()) {
      arrow = gtk_arrow_new(GTK_ARROW_DOWN, GTK_SHADOW_NONE);
    } else {
      ResourceBundle& rb = ResourceBundle::GetSharedInstance();
      arrow = gtk_image_new_from_pixbuf(
          rb.GetNativeImageNamed(IDR_APP_DROPARROW).ToGdkPixbuf());
    }
    gtk_box_pack_start(GTK_BOX(button_hbox), arrow, FALSE, FALSE, 0);
  }
  gtk_button_set_relief(GTK_BUTTON(menu_button_), GTK_RELIEF_NONE);
  gtk_box_pack_start(GTK_BOX(widget_), menu_button_, FALSE, FALSE, 0);

  menu_model_.reset(new PermissionMenuModel(this, url, type, default_setting,
                                            setting));
  MenuGtk::Delegate* delegate = new MenuGtk::Delegate();
  menu_.reset(new MenuGtk(delegate, menu_model_.get()));
  g_signal_connect(menu_button_, "button-press-event",
                   G_CALLBACK(OnMenuButtonPressEventThunk), this);
}

PermissionSelector::~PermissionSelector() {
}

void PermissionSelector::AddObserver(PermissionSelectorObserver* observer) {
  observer_list_.AddObserver(observer);
}

ContentSetting PermissionSelector::GetSetting() const {
  return setting_;
}

ContentSettingsType PermissionSelector::GetType() const {
  return type_;
}

gboolean PermissionSelector::OnMenuButtonPressEvent(GtkWidget* button,
                                                    GdkEventButton* event) {
  if (event->button != 1)
    return FALSE;
  menu_->PopupForWidget(button, event->button, event->time);
  return TRUE;
}

void PermissionSelector::ExecuteCommand(int command_id) {
  setting_ = CommandIdToContentSetting(command_id);

  // Change the permission icon to reflect the selected setting.
  ContentSetting effective_setting = setting_;
  if (effective_setting == CONTENT_SETTING_DEFAULT)
    effective_setting = default_setting_;
  GdkPixbuf* pixbuf = WebsiteSettingsUI::GetPermissionIcon(
      type_, effective_setting).ToGdkPixbuf();
  gtk_image_set_from_pixbuf(GTK_IMAGE(icon_), pixbuf);

  // Change the text of the menu button to reflect the selected setting.
  gtk_button_set_label(GTK_BUTTON(menu_button_), base::UTF16ToUTF8(
      WebsiteSettingsUI::PermissionActionToUIString(
          setting_,
          default_setting_,
          content_settings::SETTING_SOURCE_USER)).c_str());

  FOR_EACH_OBSERVER(PermissionSelectorObserver,
                    observer_list_,
                    OnPermissionChanged(this));
}

bool PermissionSelector::IsCommandIdChecked(int command_id) {
  return setting_ == CommandIdToContentSetting(command_id);
}