cookie_info_view.cc revision 4a5e2dc747d50c653511c68ccb2cfbfb740bd5a7
1// Copyright (c) 2010 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/views/cookie_info_view.h"
6
7#include <algorithm>
8
9#include "app/l10n_util.h"
10#include "base/i18n/time_formatting.h"
11#include "base/message_loop.h"
12#include "base/string16.h"
13#include "base/string_util.h"
14#include "base/utf_string_conversions.h"
15#include "chrome/browser/cookies_tree_model.h"
16#include "chrome/browser/profile.h"
17#include "gfx/canvas.h"
18#include "gfx/color_utils.h"
19#include "grit/generated_resources.h"
20#include "grit/locale_settings.h"
21#include "views/border.h"
22#include "views/grid_layout.h"
23#include "views/controls/label.h"
24#include "views/controls/button/native_button.h"
25#include "views/controls/tree/tree_view.h"
26#include "views/controls/textfield/textfield.h"
27#include "views/standard_layout.h"
28
29static const int kCookieInfoViewBorderSize = 1;
30static const int kCookieInfoViewInsetSize = 3;
31
32///////////////////////////////////////////////////////////////////////////////
33// CookieInfoView, public:
34
35CookieInfoView::CookieInfoView(bool editable_expiration_date)
36    : name_label_(NULL),
37      name_value_field_(NULL),
38      content_label_(NULL),
39      content_value_field_(NULL),
40      domain_label_(NULL),
41      domain_value_field_(NULL),
42      path_label_(NULL),
43      path_value_field_(NULL),
44      send_for_label_(NULL),
45      send_for_value_field_(NULL),
46      created_label_(NULL),
47      created_value_field_(NULL),
48      expires_label_(NULL),
49      expires_value_field_(NULL),
50      expires_value_combobox_(NULL),
51      expire_view_(NULL),
52      editable_expiration_date_(editable_expiration_date),
53      delegate_(NULL) {
54}
55
56CookieInfoView::~CookieInfoView() {
57}
58
59void CookieInfoView::SetCookie(
60    const std::string& domain,
61    const net::CookieMonster::CanonicalCookie& cookie) {
62  name_value_field_->SetText(UTF8ToWide(cookie.Name()));
63  content_value_field_->SetText(UTF8ToWide(cookie.Value()));
64  domain_value_field_->SetText(UTF8ToWide(domain));
65  path_value_field_->SetText(UTF8ToWide(cookie.Path()));
66  created_value_field_->SetText(
67      base::TimeFormatFriendlyDateAndTime(cookie.CreationDate()));
68
69  std::wstring expire_text = cookie.DoesExpire() ?
70      base::TimeFormatFriendlyDateAndTime(cookie.ExpiryDate()) :
71      l10n_util::GetString(IDS_COOKIES_COOKIE_EXPIRES_SESSION);
72
73  if (editable_expiration_date_) {
74    expire_combo_values_.clear();
75    if (cookie.DoesExpire())
76      expire_combo_values_.push_back(expire_text);
77    expire_combo_values_.push_back(
78      l10n_util::GetString(IDS_COOKIES_COOKIE_EXPIRES_SESSION));
79    expires_value_combobox_->ModelChanged();
80    expires_value_combobox_->SetSelectedItem(0);
81    expires_value_combobox_->SetEnabled(true);
82    expires_value_combobox_->set_listener(this);
83  } else {
84    expires_value_field_->SetText(expire_text);
85  }
86
87  send_for_value_field_->SetText(cookie.IsSecure() ?
88      l10n_util::GetString(IDS_COOKIES_COOKIE_SENDFOR_SECURE) :
89      l10n_util::GetString(IDS_COOKIES_COOKIE_SENDFOR_ANY));
90  EnableCookieDisplay(true);
91  Layout();
92}
93
94void CookieInfoView::SetCookieString(const GURL& url,
95                                     const std::string& cookie_line) {
96  net::CookieMonster::ParsedCookie pc(cookie_line);
97  net::CookieMonster::CanonicalCookie cookie(url, pc);
98  SetCookie(pc.HasDomain() ? pc.Domain() : url.host(), cookie);
99}
100
101
102void CookieInfoView::ClearCookieDisplay() {
103  std::wstring no_cookie_string =
104      l10n_util::GetString(IDS_COOKIES_COOKIE_NONESELECTED);
105  name_value_field_->SetText(no_cookie_string);
106  content_value_field_->SetText(no_cookie_string);
107  domain_value_field_->SetText(no_cookie_string);
108  path_value_field_->SetText(no_cookie_string);
109  send_for_value_field_->SetText(no_cookie_string);
110  created_value_field_->SetText(no_cookie_string);
111  if (expires_value_field_)
112    expires_value_field_->SetText(no_cookie_string);
113  EnableCookieDisplay(false);
114}
115
116void CookieInfoView::EnableCookieDisplay(bool enabled) {
117  name_value_field_->SetEnabled(enabled);
118  content_value_field_->SetEnabled(enabled);
119  domain_value_field_->SetEnabled(enabled);
120  path_value_field_->SetEnabled(enabled);
121  send_for_value_field_->SetEnabled(enabled);
122  created_value_field_->SetEnabled(enabled);
123  if (expires_value_field_)
124    expires_value_field_->SetEnabled(enabled);
125}
126
127///////////////////////////////////////////////////////////////////////////////
128// CookieInfoView, views::View overrides.
129
130void CookieInfoView::ViewHierarchyChanged(bool is_add,
131                                          views::View* parent,
132                                          views::View* child) {
133  if (is_add && child == this)
134    Init();
135}
136
137///////////////////////////////////////////////////////////////////////////////
138// CookieInfoView, views::Combobox::Listener overrides.
139
140void CookieInfoView::ItemChanged(views::Combobox* combo_box,
141                                 int prev_index,
142                                 int new_index) {
143  DCHECK(combo_box == expires_value_combobox_);
144  if (delegate_)
145    delegate_->ModifyExpireDate(new_index != 0);
146}
147
148///////////////////////////////////////////////////////////////////////////////
149// CookieInfoView, ComboboxModel overrides.
150int CookieInfoView::GetItemCount() {
151  return static_cast<int>(expire_combo_values_.size());
152}
153
154string16 CookieInfoView::GetItemAt(int index) {
155  return WideToUTF16Hack(expire_combo_values_[index]);
156}
157
158void CookieInfoView::AddLabelRow(int layout_id, views::GridLayout* layout,
159                                 views::View* label, views::View* value) {
160  layout->StartRow(0, layout_id);
161  layout->AddView(label);
162  layout->AddView(value, 2, 1, views::GridLayout::FILL,
163                  views::GridLayout::CENTER);
164  layout->AddPaddingRow(0, kRelatedControlSmallVerticalSpacing);
165}
166
167void CookieInfoView::AddControlRow(int layout_id, views::GridLayout* layout,
168                                 views::View* label, views::View* control) {
169  layout->StartRow(0, layout_id);
170  layout->AddView(label);
171  layout->AddView(control, 1, 1);
172  layout->AddPaddingRow(0, kRelatedControlSmallVerticalSpacing);
173}
174
175///////////////////////////////////////////////////////////////////////////////
176// CookieInfoView, private:
177
178void CookieInfoView::Init() {
179  // Ensure we don't run this more than once and leak memory.
180  DCHECK(!name_label_);
181
182  SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW);
183  views::Border* border = views::Border::CreateSolidBorder(
184      kCookieInfoViewBorderSize, border_color);
185  set_border(border);
186
187  name_label_ = new views::Label(
188      l10n_util::GetString(IDS_COOKIES_COOKIE_NAME_LABEL));
189  name_value_field_ = new views::Textfield;
190  content_label_ = new views::Label(
191      l10n_util::GetString(IDS_COOKIES_COOKIE_CONTENT_LABEL));
192  content_value_field_ = new views::Textfield;
193  domain_label_ = new views::Label(
194      l10n_util::GetString(IDS_COOKIES_COOKIE_DOMAIN_LABEL));
195  domain_value_field_ = new views::Textfield;
196  path_label_ = new views::Label(
197      l10n_util::GetString(IDS_COOKIES_COOKIE_PATH_LABEL));
198  path_value_field_ = new views::Textfield;
199  send_for_label_ = new views::Label(
200      l10n_util::GetString(IDS_COOKIES_COOKIE_SENDFOR_LABEL));
201  send_for_value_field_ = new views::Textfield;
202  created_label_ = new views::Label(
203      l10n_util::GetString(IDS_COOKIES_COOKIE_CREATED_LABEL));
204  created_value_field_ = new views::Textfield;
205  expires_label_ = new views::Label(
206      l10n_util::GetString(IDS_COOKIES_COOKIE_EXPIRES_LABEL));
207  if (editable_expiration_date_)
208    expires_value_combobox_ = new views::Combobox(this);
209  else
210    expires_value_field_ = new views::Textfield;
211
212  using views::GridLayout;
213  using views::ColumnSet;
214
215  GridLayout* layout = new GridLayout(this);
216  layout->SetInsets(kCookieInfoViewInsetSize,
217                    kCookieInfoViewInsetSize,
218                    kCookieInfoViewInsetSize,
219                    kCookieInfoViewInsetSize);
220  SetLayoutManager(layout);
221
222  int three_column_layout_id = 0;
223  ColumnSet* column_set = layout->AddColumnSet(three_column_layout_id);
224  column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
225                        GridLayout::USE_PREF, 0, 0);
226  column_set->AddPaddingColumn(0, kRelatedControlHorizontalSpacing);
227  column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
228                        GridLayout::USE_PREF, 0, 0);
229  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
230                        GridLayout::USE_PREF, 0, 0);
231
232  AddLabelRow(three_column_layout_id, layout, name_label_, name_value_field_);
233  AddLabelRow(three_column_layout_id, layout, content_label_,
234              content_value_field_);
235  AddLabelRow(three_column_layout_id, layout, domain_label_,
236              domain_value_field_);
237  AddLabelRow(three_column_layout_id, layout, path_label_, path_value_field_);
238  AddLabelRow(three_column_layout_id, layout, send_for_label_,
239              send_for_value_field_);
240  AddLabelRow(three_column_layout_id, layout, created_label_,
241              created_value_field_);
242
243  if (editable_expiration_date_) {
244    AddControlRow(three_column_layout_id, layout, expires_label_,
245                  expires_value_combobox_);
246  } else {
247    AddLabelRow(three_column_layout_id, layout, expires_label_,
248                expires_value_field_);
249  }
250
251  // Color these borderless text areas the same as the containing dialog.
252  SkColor text_area_background = color_utils::GetSysSkColor(COLOR_3DFACE);
253  // Now that the Textfields are in the view hierarchy, we can initialize them.
254  name_value_field_->SetReadOnly(true);
255  name_value_field_->RemoveBorder();
256  name_value_field_->SetBackgroundColor(text_area_background);
257  content_value_field_->SetReadOnly(true);
258  content_value_field_->RemoveBorder();
259  content_value_field_->SetBackgroundColor(text_area_background);
260  domain_value_field_->SetReadOnly(true);
261  domain_value_field_->RemoveBorder();
262  domain_value_field_->SetBackgroundColor(text_area_background);
263  path_value_field_->SetReadOnly(true);
264  path_value_field_->RemoveBorder();
265  path_value_field_->SetBackgroundColor(text_area_background);
266  send_for_value_field_->SetReadOnly(true);
267  send_for_value_field_->RemoveBorder();
268  send_for_value_field_->SetBackgroundColor(text_area_background);
269  created_value_field_->SetReadOnly(true);
270  created_value_field_->RemoveBorder();
271  created_value_field_->SetBackgroundColor(text_area_background);
272  if (expires_value_field_) {
273    expires_value_field_->SetReadOnly(true);
274    expires_value_field_->RemoveBorder();
275    expires_value_field_->SetBackgroundColor(text_area_background);
276  }
277}
278
279