cookie_info_view.cc revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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/message_loop.h"
11#include "base/strings/string16.h"
12#include "base/strings/string_util.h"
13#include "base/strings/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 "third_party/skia/include/core/SkColor.h"
20#include "ui/base/l10n/l10n_util.h"
21#include "ui/gfx/canvas.h"
22#include "ui/gfx/color_utils.h"
23#include "ui/native_theme/native_theme.h"
24#include "ui/views/border.h"
25#include "ui/views/controls/label.h"
26#include "ui/views/controls/textfield/textfield.h"
27#include "ui/views/layout/grid_layout.h"
28#include "ui/views/layout/layout_constants.h"
29#include "ui/views/window/dialog_delegate.h"
30
31namespace {
32
33const int kCookieInfoViewBorderSize = 1;
34const int kCookieInfoViewInsetSize = 3;
35
36// Adjustment to the spacing between subsequent label-field lines.
37const int kExtraLineHeightPadding = 3;
38
39}  // namespace
40
41///////////////////////////////////////////////////////////////////////////////
42// CookieInfoView, public:
43
44CookieInfoView::CookieInfoView()
45    : name_label_(NULL),
46      name_value_field_(NULL),
47      content_label_(NULL),
48      content_value_field_(NULL),
49      domain_label_(NULL),
50      domain_value_field_(NULL),
51      path_label_(NULL),
52      path_value_field_(NULL),
53      send_for_label_(NULL),
54      send_for_value_field_(NULL),
55      created_label_(NULL),
56      created_value_field_(NULL),
57      expires_label_(NULL),
58      expires_value_field_(NULL) {
59}
60
61CookieInfoView::~CookieInfoView() {
62}
63
64void CookieInfoView::SetCookie(const std::string& domain,
65                               const net::CanonicalCookie& cookie) {
66  name_value_field_->SetText(UTF8ToUTF16(cookie.Name()));
67  content_value_field_->SetText(UTF8ToUTF16(cookie.Value()));
68  domain_value_field_->SetText(UTF8ToUTF16(domain));
69  path_value_field_->SetText(UTF8ToUTF16(cookie.Path()));
70  created_value_field_->SetText(
71      base::TimeFormatFriendlyDateAndTime(cookie.CreationDate()));
72
73  string16 expire_text = cookie.IsPersistent() ?
74      base::TimeFormatFriendlyDateAndTime(cookie.ExpiryDate()) :
75      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_EXPIRES_SESSION);
76
77  expires_value_field_->SetText(expire_text);
78  send_for_value_field_->SetText(cookie.IsSecure() ?
79      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_SECURE) :
80      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_ANY));
81  EnableCookieDisplay(true);
82  Layout();
83}
84
85void CookieInfoView::SetCookieString(const GURL& url,
86                                     const std::string& cookie_line) {
87  net::ParsedCookie pc(cookie_line);
88  net::CanonicalCookie cookie(url, pc);
89  SetCookie(pc.HasDomain() ? pc.Domain() : url.host(), cookie);
90}
91
92
93void CookieInfoView::ClearCookieDisplay() {
94  string16 no_cookie_string =
95      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NONESELECTED);
96  name_value_field_->SetText(no_cookie_string);
97  content_value_field_->SetText(no_cookie_string);
98  domain_value_field_->SetText(no_cookie_string);
99  path_value_field_->SetText(no_cookie_string);
100  send_for_value_field_->SetText(no_cookie_string);
101  created_value_field_->SetText(no_cookie_string);
102  expires_value_field_->SetText(no_cookie_string);
103  EnableCookieDisplay(false);
104}
105
106void CookieInfoView::EnableCookieDisplay(bool enabled) {
107  name_value_field_->SetEnabled(enabled);
108  content_value_field_->SetEnabled(enabled);
109  domain_value_field_->SetEnabled(enabled);
110  path_value_field_->SetEnabled(enabled);
111  send_for_value_field_->SetEnabled(enabled);
112  created_value_field_->SetEnabled(enabled);
113  expires_value_field_->SetEnabled(enabled);
114}
115
116///////////////////////////////////////////////////////////////////////////////
117// CookieInfoView, views::View overrides.
118
119void CookieInfoView::ViewHierarchyChanged(
120    const ViewHierarchyChangedDetails& details) {
121  if (details.is_add && details.child == this)
122    Init();
123}
124
125void CookieInfoView::AddLabelRow(int layout_id, views::GridLayout* layout,
126                                 views::Label* label,
127                                 views::Textfield* text_field) {
128  layout->StartRow(0, layout_id);
129  layout->AddView(label);
130  layout->AddView(text_field, 2, 1, views::GridLayout::FILL,
131                  views::GridLayout::CENTER);
132  layout->AddPaddingRow(0, views::DialogDelegate::UseNewStyle() ?
133                           kExtraLineHeightPadding :
134                           views::kRelatedControlSmallVerticalSpacing);
135
136
137  // Now that the Textfield is in the view hierarchy, it can be initialized.
138  text_field->SetReadOnly(true);
139  text_field->RemoveBorder();
140  // Color these borderless text areas the same as the containing dialog.
141  text_field->SetBackgroundColor(GetNativeTheme()->GetSystemColor(
142      ui::NativeTheme::kColorId_DialogBackground));
143  text_field->SetTextColor(SkColorSetRGB(0x78, 0x78, 0x78));
144}
145
146///////////////////////////////////////////////////////////////////////////////
147// CookieInfoView, private:
148
149void CookieInfoView::Init() {
150  // Ensure we don't run this more than once and leak memory.
151  DCHECK(!name_label_);
152
153#if defined(USE_AURA) || !defined(OS_WIN)
154  SkColor border_color = SK_ColorGRAY;
155#else
156  SkColor border_color = color_utils::GetSysSkColor(COLOR_3DSHADOW);
157#endif
158
159  name_label_ = new views::Label(
160      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NAME_LABEL));
161  name_value_field_ = new views::Textfield;
162  content_label_ = new views::Label(
163      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_CONTENT_LABEL));
164  content_value_field_ = new views::Textfield;
165  domain_label_ = new views::Label(
166      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_DOMAIN_LABEL));
167  domain_value_field_ = new views::Textfield;
168  path_label_ = new views::Label(
169      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_PATH_LABEL));
170  path_value_field_ = new views::Textfield;
171  send_for_label_ = new views::Label(
172      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_LABEL));
173  send_for_value_field_ = new views::Textfield;
174  created_label_ = new views::Label(
175      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_CREATED_LABEL));
176  created_value_field_ = new views::Textfield;
177  expires_label_ = new views::Label(
178      l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_EXPIRES_LABEL));
179  expires_value_field_ = new views::Textfield;
180
181  using views::GridLayout;
182  using views::ColumnSet;
183
184  GridLayout* layout = new GridLayout(this);
185  if (views::DialogDelegate::UseNewStyle()) {
186    layout->SetInsets(
187        0, views::kButtonHEdgeMarginNew, 0, views::kButtonHEdgeMarginNew);
188  } else {
189    set_border(views::Border::CreateSolidBorder(kCookieInfoViewBorderSize,
190                                                border_color));
191    layout->SetInsets(kCookieInfoViewInsetSize,
192                      kCookieInfoViewInsetSize,
193                      kCookieInfoViewInsetSize,
194                      kCookieInfoViewInsetSize);
195  }
196  SetLayoutManager(layout);
197
198  int three_column_layout_id = 0;
199  ColumnSet* column_set = layout->AddColumnSet(three_column_layout_id);
200  column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
201                        GridLayout::USE_PREF, 0, 0);
202  column_set->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing);
203  column_set->AddColumn(GridLayout::TRAILING, GridLayout::CENTER, 0,
204                        GridLayout::USE_PREF, 0, 0);
205  column_set->AddColumn(GridLayout::FILL, GridLayout::CENTER, 1,
206                        GridLayout::USE_PREF, 0, 0);
207
208  AddLabelRow(three_column_layout_id, layout, name_label_, name_value_field_);
209  AddLabelRow(three_column_layout_id, layout, content_label_,
210              content_value_field_);
211  AddLabelRow(three_column_layout_id, layout, domain_label_,
212              domain_value_field_);
213  AddLabelRow(three_column_layout_id, layout, path_label_, path_value_field_);
214  AddLabelRow(three_column_layout_id, layout, send_for_label_,
215              send_for_value_field_);
216  AddLabelRow(three_column_layout_id, layout, created_label_,
217              created_value_field_);
218  AddLabelRow(three_column_layout_id, layout, expires_label_,
219              expires_value_field_);
220}
221