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(cookie
.IsSecure() ?
67 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_SECURE
) :
68 l10n_util::GetStringUTF16(IDS_COOKIES_COOKIE_SENDFOR_ANY
));
69 EnableCookieDisplay(true);
73 void 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
);
81 void 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);
94 void 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
);
104 ///////////////////////////////////////////////////////////////////////////////
105 // CookieInfoView, views::View overrides.
107 void CookieInfoView::ViewHierarchyChanged(
108 const ViewHierarchyChangedDetails
& details
) {
109 if (details
.is_add
&& details
.child
== this)
113 void CookieInfoView::AddLabelRow(int layout_id
,
114 views::GridLayout
* layout
,
116 views::Textfield
* textfield
) {
117 layout
->StartRow(0, layout_id
);
118 layout
->AddView(label
);
120 textfield
, 2, 1, views::GridLayout::FILL
, views::GridLayout::CENTER
);
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));
131 ///////////////////////////////////////////////////////////////////////////////
132 // CookieInfoView, private:
134 void 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
;
159 views::GridLayout
* layout
= new views::GridLayout(this);
160 layout
->SetInsets(0, views::kButtonHEdgeMarginNew
,
161 0, views::kButtonHEdgeMarginNew
);
162 SetLayoutManager(layout
);
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);
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_
);