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