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.
5 #include "chrome/browser/ui/views/cookie_info_view.h"
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/native_theme/native_theme.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"
31 // Adjustment to the spacing between subsequent label-field lines.
32 const int kExtraLineHeightPadding
= 3;
36 ///////////////////////////////////////////////////////////////////////////////
37 // CookieInfoView, public:
39 CookieInfoView::CookieInfoView()
41 name_value_field_(NULL
),
43 content_value_field_(NULL
),
45 domain_value_field_(NULL
),
47 path_value_field_(NULL
),
48 send_for_label_(NULL
),
49 send_for_value_field_(NULL
),
51 created_value_field_(NULL
),
53 expires_value_field_(NULL
) {
56 CookieInfoView::~CookieInfoView() {
59 void CookieInfoView::SetCookie(const std::string
& domain
,
60 const net::CanonicalCookie
& cookie
) {
61 name_value_field_
->SetText(base::UTF8ToUTF16(cookie
.Name()));
62 content_value_field_
->SetText(base::UTF8ToUTF16(cookie
.Value()));
63 domain_value_field_
->SetText(base::UTF8ToUTF16(domain
));
64 path_value_field_
->SetText(base::UTF8ToUTF16(cookie
.Path()));
65 created_value_field_
->SetText(
66 base::TimeFormatFriendlyDateAndTime(cookie
.CreationDate()));
68 base::string16 expire_text
= cookie
.IsPersistent() ?
69 base::TimeFormatFriendlyDateAndTime(cookie
.ExpiryDate()) :
70 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_EXPIRES_SESSION
);
72 expires_value_field_
->SetText(expire_text
);
73 send_for_value_field_
->SetText(cookie
.IsSecure() ?
74 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_SECURE
) :
75 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_ANY
));
76 EnableCookieDisplay(true);
80 void CookieInfoView::SetCookieString(const GURL
& url
,
81 const std::string
& cookie_line
) {
82 net::ParsedCookie
pc(cookie_line
);
83 net::CanonicalCookie
cookie(url
, pc
);
84 SetCookie(pc
.HasDomain() ? pc
.Domain() : url
.host(), cookie
);
88 void CookieInfoView::ClearCookieDisplay() {
89 base::string16 no_cookie_string
=
90 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NONESELECTED
);
91 name_value_field_
->SetText(no_cookie_string
);
92 content_value_field_
->SetText(no_cookie_string
);
93 domain_value_field_
->SetText(no_cookie_string
);
94 path_value_field_
->SetText(no_cookie_string
);
95 send_for_value_field_
->SetText(no_cookie_string
);
96 created_value_field_
->SetText(no_cookie_string
);
97 expires_value_field_
->SetText(no_cookie_string
);
98 EnableCookieDisplay(false);
101 void CookieInfoView::EnableCookieDisplay(bool enabled
) {
102 name_value_field_
->SetEnabled(enabled
);
103 content_value_field_
->SetEnabled(enabled
);
104 domain_value_field_
->SetEnabled(enabled
);
105 path_value_field_
->SetEnabled(enabled
);
106 send_for_value_field_
->SetEnabled(enabled
);
107 created_value_field_
->SetEnabled(enabled
);
108 expires_value_field_
->SetEnabled(enabled
);
111 ///////////////////////////////////////////////////////////////////////////////
112 // CookieInfoView, views::View overrides.
114 void CookieInfoView::ViewHierarchyChanged(
115 const ViewHierarchyChangedDetails
& details
) {
116 if (details
.is_add
&& details
.child
== this)
120 void CookieInfoView::AddLabelRow(int layout_id
, views::GridLayout
* layout
,
122 views::Textfield
* text_field
) {
123 layout
->StartRow(0, layout_id
);
124 layout
->AddView(label
);
125 layout
->AddView(text_field
, 2, 1, views::GridLayout::FILL
,
126 views::GridLayout::CENTER
);
127 layout
->AddPaddingRow(0, kExtraLineHeightPadding
);
129 // Now that the Textfield is in the view hierarchy, it can be initialized.
130 text_field
->SetReadOnly(true);
131 text_field
->set_border(NULL
);
132 // Color these borderless text areas the same as the containing dialog.
133 text_field
->SetBackgroundColor(GetNativeTheme()->GetSystemColor(
134 ui::NativeTheme::kColorId_DialogBackground
));
135 text_field
->SetTextColor(SkColorSetRGB(0x78, 0x78, 0x78));
138 ///////////////////////////////////////////////////////////////////////////////
139 // CookieInfoView, private:
141 void CookieInfoView::Init() {
142 // Ensure we don't run this more than once and leak memory.
143 DCHECK(!name_label_
);
144 name_label_
= new views::Label(
145 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NAME_LABEL
));
146 name_value_field_
= new views::Textfield
;
147 content_label_
= new views::Label(
148 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_CONTENT_LABEL
));
149 content_value_field_
= new views::Textfield
;
150 domain_label_
= new views::Label(
151 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_DOMAIN_LABEL
));
152 domain_value_field_
= new views::Textfield
;
153 path_label_
= new views::Label(
154 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_PATH_LABEL
));
155 path_value_field_
= new views::Textfield
;
156 send_for_label_
= new views::Label(
157 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_LABEL
));
158 send_for_value_field_
= new views::Textfield
;
159 created_label_
= new views::Label(
160 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_CREATED_LABEL
));
161 created_value_field_
= new views::Textfield
;
162 expires_label_
= new views::Label(
163 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_EXPIRES_LABEL
));
164 expires_value_field_
= new views::Textfield
;
166 views::GridLayout
* layout
= new views::GridLayout(this);
167 layout
->SetInsets(0, views::kButtonHEdgeMarginNew
,
168 0, views::kButtonHEdgeMarginNew
);
169 SetLayoutManager(layout
);
171 int three_column_layout_id
= 0;
172 views::ColumnSet
* column_set
= layout
->AddColumnSet(three_column_layout_id
);
173 column_set
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::CENTER
,
174 0, views::GridLayout::USE_PREF
, 0, 0);
175 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
176 column_set
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::CENTER
,
177 0, views::GridLayout::USE_PREF
, 0, 0);
178 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::CENTER
,
179 1, views::GridLayout::USE_PREF
, 0, 0);
181 AddLabelRow(three_column_layout_id
, layout
, name_label_
, name_value_field_
);
182 AddLabelRow(three_column_layout_id
, layout
, content_label_
,
183 content_value_field_
);
184 AddLabelRow(three_column_layout_id
, layout
, domain_label_
,
185 domain_value_field_
);
186 AddLabelRow(three_column_layout_id
, layout
, path_label_
, path_value_field_
);
187 AddLabelRow(three_column_layout_id
, layout
, send_for_label_
,
188 send_for_value_field_
);
189 AddLabelRow(three_column_layout_id
, layout
, created_label_
,
190 created_value_field_
);
191 AddLabelRow(three_column_layout_id
, layout
, expires_label_
,
192 expires_value_field_
);