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