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 "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"
29 ///////////////////////////////////////////////////////////////////////////////
30 // CookieInfoView, public:
32 CookieInfoView::CookieInfoView()
34 name_value_field_(NULL
),
36 content_value_field_(NULL
),
38 domain_value_field_(NULL
),
40 path_value_field_(NULL
),
41 send_for_label_(NULL
),
42 send_for_value_field_(NULL
),
44 created_value_field_(NULL
),
46 expires_value_field_(NULL
) {
49 CookieInfoView::~CookieInfoView() {
52 void 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()));
61 base::string16 expire_text
= cookie
.IsPersistent() ?
62 base::TimeFormatFriendlyDateAndTime(cookie
.ExpiryDate()) :
63 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_EXPIRES_SESSION
);
65 expires_value_field_
->SetText(expire_text
);
66 send_for_value_field_
->SetText(
67 l10n_util::GetStringUTF16(CookiesTreeModel::GetSendForMessageID(cookie
)));
68 EnableCookieDisplay(true);
72 void CookieInfoView::SetCookieString(const GURL
& url
,
73 const std::string
& cookie_line
) {
74 net::ParsedCookie
pc(cookie_line
);
75 net::CanonicalCookie
cookie(url
, pc
);
76 SetCookie(pc
.HasDomain() ? pc
.Domain() : url
.host(), cookie
);
80 void CookieInfoView::ClearCookieDisplay() {
81 base::string16 no_cookie_string
=
82 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NONESELECTED
);
83 name_value_field_
->SetText(no_cookie_string
);
84 content_value_field_
->SetText(no_cookie_string
);
85 domain_value_field_
->SetText(no_cookie_string
);
86 path_value_field_
->SetText(no_cookie_string
);
87 send_for_value_field_
->SetText(no_cookie_string
);
88 created_value_field_
->SetText(no_cookie_string
);
89 expires_value_field_
->SetText(no_cookie_string
);
90 EnableCookieDisplay(false);
93 void CookieInfoView::EnableCookieDisplay(bool enabled
) {
94 name_value_field_
->SetEnabled(enabled
);
95 content_value_field_
->SetEnabled(enabled
);
96 domain_value_field_
->SetEnabled(enabled
);
97 path_value_field_
->SetEnabled(enabled
);
98 send_for_value_field_
->SetEnabled(enabled
);
99 created_value_field_
->SetEnabled(enabled
);
100 expires_value_field_
->SetEnabled(enabled
);
103 ///////////////////////////////////////////////////////////////////////////////
104 // CookieInfoView, views::View overrides.
106 void CookieInfoView::ViewHierarchyChanged(
107 const ViewHierarchyChangedDetails
& details
) {
108 if (details
.is_add
&& details
.child
== this)
112 void CookieInfoView::AddLabelRow(int layout_id
,
113 views::GridLayout
* layout
,
115 views::Textfield
* textfield
) {
116 layout
->StartRow(0, layout_id
);
117 layout
->AddView(label
);
119 textfield
, 2, 1, views::GridLayout::FILL
, views::GridLayout::CENTER
);
121 // Now that the Textfield is in the view hierarchy, it can be initialized.
122 textfield
->SetReadOnly(true);
123 textfield
->SetBorder(views::Border::NullBorder());
124 // Color these borderless text areas the same as the containing dialog.
125 textfield
->SetBackgroundColor(GetNativeTheme()->GetSystemColor(
126 ui::NativeTheme::kColorId_DialogBackground
));
127 textfield
->SetTextColor(SkColorSetRGB(0x78, 0x78, 0x78));
130 ///////////////////////////////////////////////////////////////////////////////
131 // CookieInfoView, private:
133 void CookieInfoView::Init() {
134 // Ensure we don't run this more than once and leak memory.
135 DCHECK(!name_label_
);
136 name_label_
= new views::Label(
137 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_NAME_LABEL
));
138 name_value_field_
= new views::Textfield
;
139 content_label_
= new views::Label(
140 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_CONTENT_LABEL
));
141 content_value_field_
= new views::Textfield
;
142 domain_label_
= new views::Label(
143 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_DOMAIN_LABEL
));
144 domain_value_field_
= new views::Textfield
;
145 path_label_
= new views::Label(
146 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_PATH_LABEL
));
147 path_value_field_
= new views::Textfield
;
148 send_for_label_
= new views::Label(
149 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_LABEL
));
150 send_for_value_field_
= new views::Textfield
;
151 created_label_
= new views::Label(
152 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_CREATED_LABEL
));
153 created_value_field_
= new views::Textfield
;
154 expires_label_
= new views::Label(
155 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_EXPIRES_LABEL
));
156 expires_value_field_
= new views::Textfield
;
158 views::GridLayout
* layout
= new views::GridLayout(this);
159 layout
->SetInsets(0, views::kButtonHEdgeMarginNew
,
160 0, views::kButtonHEdgeMarginNew
);
161 SetLayoutManager(layout
);
163 int three_column_layout_id
= 0;
164 views::ColumnSet
* column_set
= layout
->AddColumnSet(three_column_layout_id
);
165 column_set
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::CENTER
,
166 0, views::GridLayout::USE_PREF
, 0, 0);
167 column_set
->AddPaddingColumn(0, views::kRelatedControlHorizontalSpacing
);
168 column_set
->AddColumn(views::GridLayout::TRAILING
, views::GridLayout::CENTER
,
169 0, views::GridLayout::USE_PREF
, 0, 0);
170 column_set
->AddColumn(views::GridLayout::FILL
, views::GridLayout::CENTER
,
171 1, views::GridLayout::USE_PREF
, 0, 0);
173 AddLabelRow(three_column_layout_id
, layout
, name_label_
, name_value_field_
);
174 AddLabelRow(three_column_layout_id
, layout
, content_label_
,
175 content_value_field_
);
176 AddLabelRow(three_column_layout_id
, layout
, domain_label_
,
177 domain_value_field_
);
178 AddLabelRow(three_column_layout_id
, layout
, path_label_
, path_value_field_
);
179 AddLabelRow(three_column_layout_id
, layout
, send_for_label_
,
180 send_for_value_field_
);
181 AddLabelRow(three_column_layout_id
, layout
, created_label_
,
182 created_value_field_
);
183 AddLabelRow(three_column_layout_id
, layout
, expires_label_
,
184 expires_value_field_
);