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