Home | History | Annotate | Download | only in gtk
      1 // Copyright (c) 2011 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/ui/gtk/collected_cookies_gtk.h"
      6 
      7 #include <string>
      8 
      9 #include "chrome/browser/cookies_tree_model.h"
     10 #include "chrome/browser/profiles/profile.h"
     11 #include "chrome/browser/ui/collected_cookies_infobar_delegate.h"
     12 #include "chrome/browser/ui/gtk/gtk_chrome_cookie_view.h"
     13 #include "chrome/browser/ui/gtk/gtk_util.h"
     14 #include "content/browser/tab_contents/tab_contents.h"
     15 #include "content/common/notification_source.h"
     16 #include "grit/generated_resources.h"
     17 #include "ui/base/l10n/l10n_util.h"
     18 
     19 namespace {
     20 // Width and height of the cookie tree view.
     21 const int kTreeViewWidth = 450;
     22 const int kTreeViewHeight = 150;
     23 
     24 // The page numbers of the pages in the notebook.
     25 const gint kAllowedPageNumber = 0;
     26 const gint kBlockedPageNumber = 1;
     27 
     28 // Padding within the banner box.
     29 const int kBannerPadding = 3;
     30 
     31 // Returns the text to display in the info bar when content settings were
     32 // created.
     33 const std::string GetInfobarLabel(ContentSetting setting,
     34                                   bool multiple_domains_added,
     35                                   const string16& domain_name) {
     36   if (multiple_domains_added) {
     37     switch (setting) {
     38       case CONTENT_SETTING_BLOCK:
     39         return l10n_util::GetStringUTF8(
     40             IDS_COLLECTED_COOKIES_MULTIPLE_BLOCK_RULES_CREATED);
     41 
     42       case CONTENT_SETTING_ALLOW:
     43         return l10n_util::GetStringUTF8(
     44             IDS_COLLECTED_COOKIES_MULTIPLE_ALLOW_RULES_CREATED);
     45 
     46       case CONTENT_SETTING_SESSION_ONLY:
     47         return l10n_util::GetStringUTF8(
     48             IDS_COLLECTED_COOKIES_MULTIPLE_SESSION_RULES_CREATED);
     49 
     50       default:
     51         NOTREACHED();
     52         return std::string();
     53     }
     54   }
     55 
     56   switch (setting) {
     57     case CONTENT_SETTING_BLOCK:
     58       return l10n_util::GetStringFUTF8(
     59           IDS_COLLECTED_COOKIES_BLOCK_RULE_CREATED, domain_name);
     60 
     61     case CONTENT_SETTING_ALLOW:
     62       return l10n_util::GetStringFUTF8(
     63           IDS_COLLECTED_COOKIES_ALLOW_RULE_CREATED, domain_name);
     64 
     65     case CONTENT_SETTING_SESSION_ONLY:
     66       return l10n_util::GetStringFUTF8(
     67           IDS_COLLECTED_COOKIES_SESSION_RULE_CREATED, domain_name);
     68 
     69     default:
     70       NOTREACHED();
     71       return std::string();
     72   }
     73 }
     74 }  // namespace
     75 
     76 CollectedCookiesGtk::CollectedCookiesGtk(GtkWindow* parent,
     77                                          TabContents* tab_contents)
     78     : tab_contents_(tab_contents),
     79       status_changed_(false) {
     80   TabSpecificContentSettings* content_settings =
     81       tab_contents->GetTabSpecificContentSettings();
     82   registrar_.Add(this, NotificationType::COLLECTED_COOKIES_SHOWN,
     83                  Source<TabSpecificContentSettings>(content_settings));
     84 
     85   Init();
     86 }
     87 
     88 void CollectedCookiesGtk::Init() {
     89   dialog_ = gtk_vbox_new(FALSE, gtk_util::kContentAreaSpacing);
     90   gtk_box_set_spacing(GTK_BOX(dialog_), gtk_util::kContentAreaSpacing);
     91 
     92   GtkWidget* label = gtk_label_new(
     93       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_DIALOG_TITLE).c_str());
     94   gtk_box_pack_start(GTK_BOX(dialog_), label, TRUE, TRUE, 0);
     95 
     96   notebook_ = gtk_notebook_new();
     97   gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook_), GTK_POS_TOP);
     98   gtk_box_pack_start(GTK_BOX(dialog_), notebook_, TRUE, TRUE, 0);
     99 
    100   GtkWidget* allowed_pane = CreateAllowedPane();
    101   label = gtk_label_new(
    102       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_ALLOWED_COOKIES_TAB_LABEL)
    103           .c_str());
    104   gtk_widget_show(label);
    105   gtk_notebook_insert_page(GTK_NOTEBOOK(notebook_), allowed_pane, label,
    106                            kAllowedPageNumber);
    107 
    108   GtkWidget* blocked_pane = CreateBlockedPane();
    109   label = gtk_label_new(
    110       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_TAB_LABEL)
    111           .c_str());
    112   gtk_widget_show(label);
    113   gtk_notebook_insert_page(GTK_NOTEBOOK(notebook_), blocked_pane, label,
    114                            kBlockedPageNumber);
    115   // Hook up the signal only after all pages are inserted, otherwise not
    116   // all member variables used in OnSwitchPage() will be properly initialized.
    117   g_signal_connect(notebook_, "switch-page", G_CALLBACK(OnSwitchPageThunk),
    118                    this);
    119 
    120   // Cookie info view.
    121   cookie_info_view_ = gtk_chrome_cookie_view_new(false);
    122   gtk_box_pack_start(GTK_BOX(dialog_), cookie_info_view_, TRUE, TRUE, 0);
    123   gtk_chrome_cookie_view_clear(GTK_CHROME_COOKIE_VIEW(cookie_info_view_));
    124   gtk_widget_show_all(cookie_info_view_);
    125 
    126   // Infobar.
    127   infobar_ = gtk_frame_new(NULL);
    128   GtkWidget* infobar_contents = gtk_hbox_new(FALSE, kBannerPadding);
    129   gtk_container_set_border_width(GTK_CONTAINER(infobar_contents),
    130                                  kBannerPadding);
    131   gtk_container_add(GTK_CONTAINER(infobar_), infobar_contents);
    132   GtkWidget* info_image =
    133       gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO,
    134                                GTK_ICON_SIZE_SMALL_TOOLBAR);
    135   gtk_box_pack_start(GTK_BOX(infobar_contents), info_image, FALSE, FALSE, 0);
    136   infobar_label_ = gtk_label_new(NULL);
    137   gtk_box_pack_start(
    138       GTK_BOX(infobar_contents), infobar_label_, FALSE, FALSE, 0);
    139   gtk_widget_show_all(infobar_);
    140   gtk_widget_set_no_show_all(infobar_, TRUE);
    141   gtk_widget_hide(infobar_);
    142   gtk_box_pack_start(GTK_BOX(dialog_), infobar_, TRUE, TRUE, 0);
    143 
    144   // Close button.
    145   GtkWidget* button_box = gtk_hbutton_box_new();
    146   gtk_button_box_set_layout(GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_END);
    147   gtk_box_set_spacing(GTK_BOX(button_box), gtk_util::kControlSpacing);
    148   gtk_box_pack_end(GTK_BOX(dialog_), button_box, FALSE, TRUE, 0);
    149   close_button_ = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
    150   gtk_button_set_label(GTK_BUTTON(close_button_),
    151                        l10n_util::GetStringUTF8(IDS_CLOSE).c_str());
    152   g_signal_connect(close_button_, "clicked", G_CALLBACK(OnCloseThunk), this);
    153   gtk_box_pack_end(GTK_BOX(button_box), close_button_, FALSE, TRUE, 0);
    154 
    155   // Show the dialog.
    156   allowed_cookies_tree_adapter_->Init();
    157   blocked_cookies_tree_adapter_->Init();
    158   EnableControls();
    159   ShowCookieInfo(gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_)));
    160   window_ = tab_contents_->CreateConstrainedDialog(this);
    161 }
    162 
    163 GtkWidget* CollectedCookiesGtk::CreateAllowedPane() {
    164   GtkWidget* cookie_list_vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing);
    165 
    166   GtkWidget* label = gtk_label_new(
    167       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_ALLOWED_COOKIES_LABEL).
    168           c_str());
    169   gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
    170   gtk_box_pack_start(GTK_BOX(cookie_list_vbox), label, FALSE, FALSE,
    171                      gtk_util::kControlSpacing);
    172 
    173   GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL);
    174   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
    175                                  GTK_POLICY_AUTOMATIC,
    176                                  GTK_POLICY_AUTOMATIC);
    177   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window),
    178                                       GTK_SHADOW_ETCHED_IN);
    179   gtk_box_pack_start(GTK_BOX(cookie_list_vbox), scroll_window, TRUE, TRUE, 0);
    180 
    181   TabSpecificContentSettings* content_settings =
    182       tab_contents_->GetTabSpecificContentSettings();
    183 
    184   allowed_cookies_tree_model_.reset(
    185       content_settings->GetAllowedCookiesTreeModel());
    186   allowed_cookies_tree_adapter_.reset(
    187       new gtk_tree::TreeAdapter(this, allowed_cookies_tree_model_.get()));
    188   allowed_tree_ = gtk_tree_view_new_with_model(
    189       GTK_TREE_MODEL(allowed_cookies_tree_adapter_->tree_store()));
    190   gtk_widget_set_size_request(allowed_tree_, kTreeViewWidth, kTreeViewHeight);
    191   gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(allowed_tree_), FALSE);
    192   gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(allowed_tree_), TRUE);
    193   gtk_container_add(GTK_CONTAINER(scroll_window), allowed_tree_);
    194 
    195   GtkTreeViewColumn* title_column = gtk_tree_view_column_new();
    196   GtkCellRenderer* pixbuf_renderer = gtk_cell_renderer_pixbuf_new();
    197   gtk_tree_view_column_pack_start(title_column, pixbuf_renderer, FALSE);
    198   gtk_tree_view_column_add_attribute(title_column, pixbuf_renderer, "pixbuf",
    199                                      gtk_tree::TreeAdapter::COL_ICON);
    200   GtkCellRenderer* title_renderer = gtk_cell_renderer_text_new();
    201   gtk_tree_view_column_pack_start(title_column, title_renderer, TRUE);
    202   gtk_tree_view_column_add_attribute(title_column, title_renderer, "text",
    203                                      gtk_tree::TreeAdapter::COL_TITLE);
    204   gtk_tree_view_column_set_title(
    205       title_column, l10n_util::GetStringUTF8(
    206           IDS_COOKIES_DOMAIN_COLUMN_HEADER).c_str());
    207   gtk_tree_view_append_column(GTK_TREE_VIEW(allowed_tree_), title_column);
    208   g_signal_connect(allowed_tree_, "row-expanded",
    209                    G_CALLBACK(OnTreeViewRowExpandedThunk), this);
    210   allowed_selection_ =
    211       gtk_tree_view_get_selection(GTK_TREE_VIEW(allowed_tree_));
    212   gtk_tree_selection_set_mode(allowed_selection_, GTK_SELECTION_MULTIPLE);
    213   g_signal_connect(allowed_selection_, "changed",
    214                    G_CALLBACK(OnTreeViewSelectionChangeThunk), this);
    215 
    216   GtkWidget* button_box = gtk_hbutton_box_new();
    217   gtk_button_box_set_layout(GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_START);
    218   gtk_box_set_spacing(GTK_BOX(button_box), gtk_util::kControlSpacing);
    219   gtk_box_pack_start(GTK_BOX(cookie_list_vbox), button_box, FALSE, FALSE,
    220                      gtk_util::kControlSpacing);
    221   block_allowed_cookie_button_ = gtk_button_new_with_label(
    222       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_BLOCK_BUTTON).c_str());
    223   g_signal_connect(block_allowed_cookie_button_, "clicked",
    224                    G_CALLBACK(OnBlockAllowedButtonClickedThunk), this);
    225   gtk_container_add(GTK_CONTAINER(button_box), block_allowed_cookie_button_);
    226 
    227   // Wrap the vbox inside an hbox so that we can specify padding along the
    228   // horizontal axis.
    229   GtkWidget* box = gtk_hbox_new(FALSE, 0);
    230   gtk_box_pack_start(GTK_BOX(box), cookie_list_vbox, TRUE, TRUE,
    231                      gtk_util::kControlSpacing);
    232   return box;
    233 }
    234 
    235 GtkWidget* CollectedCookiesGtk::CreateBlockedPane() {
    236   HostContentSettingsMap* host_content_settings_map =
    237       tab_contents_->profile()->GetHostContentSettingsMap();
    238 
    239   GtkWidget* cookie_list_vbox = gtk_vbox_new(FALSE, gtk_util::kControlSpacing);
    240 
    241   GtkWidget* label = gtk_label_new(
    242       l10n_util::GetStringUTF8(
    243           host_content_settings_map->BlockThirdPartyCookies() ?
    244               IDS_COLLECTED_COOKIES_BLOCKED_THIRD_PARTY_BLOCKING_ENABLED :
    245               IDS_COLLECTED_COOKIES_BLOCKED_COOKIES_LABEL).c_str());
    246   gtk_widget_set_size_request(label, kTreeViewWidth, -1);
    247   gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    248   gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
    249   gtk_box_pack_start(GTK_BOX(cookie_list_vbox), label, TRUE, TRUE,
    250                      gtk_util::kControlSpacing);
    251 
    252   GtkWidget* scroll_window = gtk_scrolled_window_new(NULL, NULL);
    253   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll_window),
    254                                  GTK_POLICY_AUTOMATIC,
    255                                  GTK_POLICY_AUTOMATIC);
    256   gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scroll_window),
    257                                       GTK_SHADOW_ETCHED_IN);
    258   gtk_box_pack_start(GTK_BOX(cookie_list_vbox), scroll_window, TRUE, TRUE, 0);
    259 
    260   TabSpecificContentSettings* content_settings =
    261       tab_contents_->GetTabSpecificContentSettings();
    262 
    263   blocked_cookies_tree_model_.reset(
    264       content_settings->GetBlockedCookiesTreeModel());
    265   blocked_cookies_tree_adapter_.reset(
    266       new gtk_tree::TreeAdapter(this, blocked_cookies_tree_model_.get()));
    267   blocked_tree_ = gtk_tree_view_new_with_model(
    268       GTK_TREE_MODEL(blocked_cookies_tree_adapter_->tree_store()));
    269   gtk_widget_set_size_request(blocked_tree_, kTreeViewWidth, kTreeViewHeight);
    270   gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(blocked_tree_), FALSE);
    271   gtk_tree_view_set_enable_tree_lines(GTK_TREE_VIEW(blocked_tree_), TRUE);
    272   gtk_container_add(GTK_CONTAINER(scroll_window), blocked_tree_);
    273 
    274   GtkTreeViewColumn* title_column = gtk_tree_view_column_new();
    275   GtkCellRenderer* pixbuf_renderer = gtk_cell_renderer_pixbuf_new();
    276   gtk_tree_view_column_pack_start(title_column, pixbuf_renderer, FALSE);
    277   gtk_tree_view_column_add_attribute(title_column, pixbuf_renderer, "pixbuf",
    278                                      gtk_tree::TreeAdapter::COL_ICON);
    279   GtkCellRenderer* title_renderer = gtk_cell_renderer_text_new();
    280   gtk_tree_view_column_pack_start(title_column, title_renderer, TRUE);
    281   gtk_tree_view_column_add_attribute(title_column, title_renderer, "text",
    282                                      gtk_tree::TreeAdapter::COL_TITLE);
    283   gtk_tree_view_column_set_title(
    284       title_column, l10n_util::GetStringUTF8(
    285           IDS_COOKIES_DOMAIN_COLUMN_HEADER).c_str());
    286   gtk_tree_view_append_column(GTK_TREE_VIEW(blocked_tree_), title_column);
    287   g_signal_connect(blocked_tree_, "row-expanded",
    288                    G_CALLBACK(OnTreeViewRowExpandedThunk), this);
    289   blocked_selection_ =
    290       gtk_tree_view_get_selection(GTK_TREE_VIEW(blocked_tree_));
    291   gtk_tree_selection_set_mode(blocked_selection_, GTK_SELECTION_MULTIPLE);
    292   g_signal_connect(blocked_selection_, "changed",
    293                    G_CALLBACK(OnTreeViewSelectionChangeThunk), this);
    294 
    295   GtkWidget* button_box = gtk_hbutton_box_new();
    296   gtk_button_box_set_layout(GTK_BUTTON_BOX(button_box), GTK_BUTTONBOX_START);
    297   gtk_box_set_spacing(GTK_BOX(button_box), gtk_util::kControlSpacing);
    298   gtk_box_pack_start(GTK_BOX(cookie_list_vbox), button_box, FALSE, FALSE,
    299                      gtk_util::kControlSpacing);
    300   allow_blocked_cookie_button_ = gtk_button_new_with_label(
    301       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_ALLOW_BUTTON).c_str());
    302   g_signal_connect(allow_blocked_cookie_button_, "clicked",
    303                    G_CALLBACK(OnAllowBlockedButtonClickedThunk), this);
    304   gtk_container_add(GTK_CONTAINER(button_box), allow_blocked_cookie_button_);
    305   for_session_blocked_cookie_button_ = gtk_button_new_with_label(
    306       l10n_util::GetStringUTF8(IDS_COLLECTED_COOKIES_SESSION_ONLY_BUTTON).
    307           c_str());
    308   g_signal_connect(for_session_blocked_cookie_button_, "clicked",
    309                    G_CALLBACK(OnForSessionBlockedButtonClickedThunk), this);
    310   gtk_container_add(GTK_CONTAINER(button_box),
    311                     for_session_blocked_cookie_button_);
    312 
    313   // Wrap the vbox inside an hbox so that we can specify padding along the
    314   // horizontal axis.
    315   GtkWidget* box = gtk_hbox_new(FALSE, 0);
    316   gtk_box_pack_start(GTK_BOX(box), cookie_list_vbox, TRUE, TRUE,
    317                      gtk_util::kControlSpacing);
    318   return box;
    319 }
    320 
    321 void CollectedCookiesGtk::ShowCookieInfo(gint current_page) {
    322   if (current_page == kAllowedPageNumber) {
    323     ShowSelectionInfo(allowed_selection_, allowed_cookies_tree_adapter_.get());
    324   } else if (current_page == kBlockedPageNumber) {
    325     ShowSelectionInfo(blocked_selection_, blocked_cookies_tree_adapter_.get());
    326   }
    327 }
    328 
    329 void CollectedCookiesGtk::ShowSelectionInfo(GtkTreeSelection* selection,
    330                                             gtk_tree::TreeAdapter* adapter) {
    331   // Check if one "cookie" node is selected.  Don't allow more than one.
    332   GtkTreeModel* model;
    333   GList* paths = gtk_tree_selection_get_selected_rows(selection, &model);
    334   if (g_list_length(paths) == 1) {
    335     GList* item = g_list_first(paths);
    336     GtkTreeIter iter;
    337     gtk_tree_model_get_iter(model, &iter,
    338                             reinterpret_cast<GtkTreePath*>(item->data));
    339     CookieTreeNode* node =
    340         static_cast<CookieTreeNode*>(adapter->GetNode(&iter));
    341     const CookieTreeNode::DetailedInfo detailed_info = node->GetDetailedInfo();
    342     if (detailed_info.node_type == CookieTreeNode::DetailedInfo::TYPE_COOKIE) {
    343       gtk_chrome_cookie_view_display_cookie(
    344           GTK_CHROME_COOKIE_VIEW(cookie_info_view_),
    345           detailed_info.cookie->Domain(),
    346           *detailed_info.cookie);
    347     } else {
    348       gtk_chrome_cookie_view_clear(GTK_CHROME_COOKIE_VIEW(cookie_info_view_));
    349     }
    350   } else {
    351     gtk_chrome_cookie_view_clear(GTK_CHROME_COOKIE_VIEW(cookie_info_view_));
    352   }
    353 
    354   g_list_foreach(paths, reinterpret_cast<GFunc>(gtk_tree_path_free), NULL);
    355   g_list_free(paths);
    356 }
    357 
    358 CollectedCookiesGtk::~CollectedCookiesGtk() {
    359   gtk_widget_destroy(dialog_);
    360 }
    361 
    362 GtkWidget* CollectedCookiesGtk::GetWidgetRoot() {
    363   return dialog_;
    364 }
    365 
    366 GtkWidget* CollectedCookiesGtk::GetFocusWidget() {
    367   return close_button_;
    368 }
    369 
    370 void CollectedCookiesGtk::DeleteDelegate() {
    371   delete this;
    372 }
    373 
    374 bool CollectedCookiesGtk::SelectionContainsOriginNode(
    375     GtkTreeSelection* selection, gtk_tree::TreeAdapter* adapter) {
    376   // Check whether at least one "origin" node is selected.
    377   GtkTreeModel* model;
    378   GList* paths =
    379       gtk_tree_selection_get_selected_rows(selection, &model);
    380   bool contains_origin_node = false;
    381   for (GList* item = paths; item; item = item->next) {
    382     GtkTreeIter iter;
    383     gtk_tree_model_get_iter(
    384         model, &iter, reinterpret_cast<GtkTreePath*>(item->data));
    385     CookieTreeNode* node =
    386         static_cast<CookieTreeNode*>(adapter->GetNode(&iter));
    387     if (node->GetDetailedInfo().node_type !=
    388         CookieTreeNode::DetailedInfo::TYPE_ORIGIN)
    389       continue;
    390     CookieTreeOriginNode* origin_node = static_cast<CookieTreeOriginNode*>(
    391         node);
    392     if (!origin_node->CanCreateContentException())
    393       continue;
    394     contains_origin_node = true;
    395   }
    396   g_list_foreach(paths, reinterpret_cast<GFunc>(gtk_tree_path_free), NULL);
    397   g_list_free(paths);
    398   return contains_origin_node;
    399 }
    400 
    401 void CollectedCookiesGtk::EnableControls() {
    402   // Update button states.
    403   bool enable_for_allowed_cookies =
    404       SelectionContainsOriginNode(allowed_selection_,
    405                                   allowed_cookies_tree_adapter_.get());
    406   gtk_widget_set_sensitive(block_allowed_cookie_button_,
    407                            enable_for_allowed_cookies);
    408 
    409   bool enable_for_blocked_cookies =
    410       SelectionContainsOriginNode(blocked_selection_,
    411                                   blocked_cookies_tree_adapter_.get());
    412   gtk_widget_set_sensitive(allow_blocked_cookie_button_,
    413                            enable_for_blocked_cookies);
    414   gtk_widget_set_sensitive(for_session_blocked_cookie_button_,
    415                            enable_for_blocked_cookies);
    416 }
    417 
    418 void CollectedCookiesGtk::Observe(NotificationType type,
    419                                   const NotificationSource& source,
    420                                   const NotificationDetails& details) {
    421   DCHECK(type == NotificationType::COLLECTED_COOKIES_SHOWN);
    422   DCHECK_EQ(Source<TabSpecificContentSettings>(source).ptr(),
    423             tab_contents_->GetTabSpecificContentSettings());
    424   window_->CloseConstrainedWindow();
    425 }
    426 
    427 void CollectedCookiesGtk::OnClose(GtkWidget* close_button) {
    428   if (status_changed_) {
    429     tab_contents_->AddInfoBar(
    430         new CollectedCookiesInfoBarDelegate(tab_contents_));
    431   }
    432   window_->CloseConstrainedWindow();
    433 }
    434 
    435 void CollectedCookiesGtk::AddExceptions(GtkTreeSelection* selection,
    436                                         gtk_tree::TreeAdapter* adapter,
    437                                         ContentSetting setting) {
    438   GtkTreeModel* model;
    439   GList* paths =
    440       gtk_tree_selection_get_selected_rows(selection, &model);
    441   string16 last_domain_name;
    442   bool multiple_domains_added = false;
    443   for (GList* item = paths; item; item = item->next) {
    444     GtkTreeIter iter;
    445     gtk_tree_model_get_iter(
    446         model, &iter, reinterpret_cast<GtkTreePath*>(item->data));
    447     CookieTreeNode* node =
    448         static_cast<CookieTreeNode*>(adapter->GetNode(&iter));
    449     if (node->GetDetailedInfo().node_type !=
    450         CookieTreeNode::DetailedInfo::TYPE_ORIGIN)
    451       continue;
    452     CookieTreeOriginNode* origin_node = static_cast<CookieTreeOriginNode*>(
    453         node);
    454     if (origin_node->CanCreateContentException()) {
    455       if (!last_domain_name.empty())
    456         multiple_domains_added = true;
    457       last_domain_name = origin_node->GetTitle();
    458       origin_node->CreateContentException(
    459           tab_contents_->profile()->GetHostContentSettingsMap(), setting);
    460     }
    461   }
    462   g_list_foreach(paths, reinterpret_cast<GFunc>(gtk_tree_path_free), NULL);
    463   g_list_free(paths);
    464   if (last_domain_name.empty()) {
    465     gtk_widget_hide(infobar_);
    466   } else {
    467     gtk_label_set_text(
    468         GTK_LABEL(infobar_label_), GetInfobarLabel(
    469             setting, multiple_domains_added, last_domain_name).c_str());
    470     gtk_widget_show(infobar_);
    471   }
    472   status_changed_ = true;
    473 }
    474 
    475 void CollectedCookiesGtk::OnBlockAllowedButtonClicked(GtkWidget* button) {
    476   AddExceptions(allowed_selection_, allowed_cookies_tree_adapter_.get(),
    477                 CONTENT_SETTING_BLOCK);
    478 }
    479 
    480 void CollectedCookiesGtk::OnAllowBlockedButtonClicked(GtkWidget* button) {
    481   AddExceptions(blocked_selection_, blocked_cookies_tree_adapter_.get(),
    482                 CONTENT_SETTING_ALLOW);
    483 }
    484 
    485 void CollectedCookiesGtk::OnForSessionBlockedButtonClicked(GtkWidget* button) {
    486   AddExceptions(blocked_selection_, blocked_cookies_tree_adapter_.get(),
    487                 CONTENT_SETTING_SESSION_ONLY);
    488 }
    489 
    490 void CollectedCookiesGtk::OnSwitchPage(GtkWidget* notebook,
    491                                        gpointer page,
    492                                        guint page_num) {
    493   EnableControls();
    494   ShowCookieInfo(page_num);
    495 }
    496 
    497 void CollectedCookiesGtk::OnTreeViewRowExpanded(GtkWidget* tree_view,
    498                                                 GtkTreeIter* iter,
    499                                                 GtkTreePath* path) {
    500   // When a row in the tree is expanded, expand all the children too.
    501   g_signal_handlers_block_by_func(
    502       tree_view, reinterpret_cast<gpointer>(OnTreeViewRowExpandedThunk), this);
    503   gtk_tree_view_expand_row(GTK_TREE_VIEW(tree_view), path, TRUE);
    504   g_signal_handlers_unblock_by_func(
    505       tree_view, reinterpret_cast<gpointer>(OnTreeViewRowExpandedThunk), this);
    506 }
    507 
    508 void CollectedCookiesGtk::OnTreeViewSelectionChange(GtkWidget* selection) {
    509   EnableControls();
    510   ShowCookieInfo(gtk_notebook_get_current_page(GTK_NOTEBOOK(notebook_)));
    511 }
    512