| OLD | NEW |
| (Empty) |
| 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/webui/options2/cookies_view_handler2.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/bind_helpers.h" | |
| 11 #include "base/utf_string_conversions.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/browsing_data_appcache_helper.h" | |
| 14 #include "chrome/browser/browsing_data_cookie_helper.h" | |
| 15 #include "chrome/browser/browsing_data_database_helper.h" | |
| 16 #include "chrome/browser/browsing_data_file_system_helper.h" | |
| 17 #include "chrome/browser/browsing_data_indexed_db_helper.h" | |
| 18 #include "chrome/browser/browsing_data_local_storage_helper.h" | |
| 19 #include "chrome/browser/browsing_data_quota_helper.h" | |
| 20 #include "chrome/browser/browsing_data_server_bound_cert_helper.h" | |
| 21 #include "chrome/browser/profiles/profile.h" | |
| 22 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" | |
| 23 #include "content/public/browser/web_ui.h" | |
| 24 #include "grit/generated_resources.h" | |
| 25 #include "ui/base/l10n/l10n_util.h" | |
| 26 | |
| 27 namespace options2 { | |
| 28 | |
| 29 CookiesViewHandler::CookiesViewHandler() | |
| 30 : batch_update_(false), | |
| 31 model_util_(new CookiesTreeModelUtil) { | |
| 32 } | |
| 33 | |
| 34 CookiesViewHandler::~CookiesViewHandler() { | |
| 35 } | |
| 36 | |
| 37 void CookiesViewHandler::GetLocalizedValues( | |
| 38 DictionaryValue* localized_strings) { | |
| 39 DCHECK(localized_strings); | |
| 40 | |
| 41 static OptionsStringResource resources[] = { | |
| 42 { "label_cookie_name", IDS_COOKIES_COOKIE_NAME_LABEL }, | |
| 43 { "label_cookie_content", IDS_COOKIES_COOKIE_CONTENT_LABEL }, | |
| 44 { "label_cookie_domain", IDS_COOKIES_COOKIE_DOMAIN_LABEL }, | |
| 45 { "label_cookie_path", IDS_COOKIES_COOKIE_PATH_LABEL }, | |
| 46 { "label_cookie_send_for", IDS_COOKIES_COOKIE_SENDFOR_LABEL }, | |
| 47 { "label_cookie_accessible_to_script", | |
| 48 IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_LABEL }, | |
| 49 { "label_cookie_created", IDS_COOKIES_COOKIE_CREATED_LABEL }, | |
| 50 { "label_cookie_expires", IDS_COOKIES_COOKIE_EXPIRES_LABEL }, | |
| 51 { "label_webdb_desc", IDS_COOKIES_WEB_DATABASE_DESCRIPTION_LABEL }, | |
| 52 { "label_local_storage_size", | |
| 53 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL }, | |
| 54 { "label_local_storage_last_modified", | |
| 55 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL }, | |
| 56 { "label_local_storage_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, | |
| 57 { "label_indexed_db_size", IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL }, | |
| 58 { "label_indexed_db_last_modified", | |
| 59 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL }, | |
| 60 { "label_indexed_db_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, | |
| 61 { "label_app_cache_manifest", | |
| 62 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL }, | |
| 63 { "label_cookie_last_accessed", IDS_COOKIES_LAST_ACCESSED_LABEL }, | |
| 64 { "cookie_domain", IDS_COOKIES_DOMAIN_COLUMN_HEADER }, | |
| 65 { "cookie_local_data", IDS_COOKIES_DATA_COLUMN_HEADER }, | |
| 66 { "cookie_singular", IDS_COOKIES_SINGLE_COOKIE }, | |
| 67 { "cookie_plural", IDS_COOKIES_PLURAL_COOKIES }, | |
| 68 { "cookie_database_storage", IDS_COOKIES_DATABASE_STORAGE }, | |
| 69 { "cookie_indexed_db", IDS_COOKIES_INDEXED_DB }, | |
| 70 { "cookie_local_storage", IDS_COOKIES_LOCAL_STORAGE }, | |
| 71 { "cookie_app_cache", IDS_COOKIES_APPLICATION_CACHE }, | |
| 72 { "search_cookies", IDS_COOKIES_SEARCH_COOKIES }, | |
| 73 { "remove_cookie", IDS_COOKIES_REMOVE_LABEL }, | |
| 74 { "remove_all_cookie", IDS_COOKIES_REMOVE_ALL_LABEL }, | |
| 75 { "cookie_file_system", IDS_COOKIES_FILE_SYSTEM }, | |
| 76 { "label_file_system_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, | |
| 77 { "label_file_system_temporary_usage", | |
| 78 IDS_COOKIES_FILE_SYSTEM_TEMPORARY_USAGE_LABEL }, | |
| 79 { "label_file_system_persistent_usage", | |
| 80 IDS_COOKIES_FILE_SYSTEM_PERSISTENT_USAGE_LABEL }, | |
| 81 { "cookie_server_bound_cert", IDS_COOKIES_SERVER_BOUND_CERT }, | |
| 82 { "label_server_bound_cert_server_id", | |
| 83 IDS_COOKIES_SERVER_BOUND_CERT_ORIGIN_LABEL }, | |
| 84 { "label_server_bound_cert_type", | |
| 85 IDS_COOKIES_SERVER_BOUND_CERT_TYPE_LABEL }, | |
| 86 { "label_server_bound_cert_created", | |
| 87 IDS_COOKIES_SERVER_BOUND_CERT_CREATED_LABEL }, | |
| 88 { "label_server_bound_cert_expires", | |
| 89 IDS_COOKIES_SERVER_BOUND_CERT_EXPIRES_LABEL }, | |
| 90 }; | |
| 91 | |
| 92 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 93 RegisterTitle(localized_strings, "cookiesViewPage", | |
| 94 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); | |
| 95 } | |
| 96 | |
| 97 void CookiesViewHandler::RegisterMessages() { | |
| 98 web_ui()->RegisterMessageCallback("updateCookieSearchResults", | |
| 99 base::Bind(&CookiesViewHandler::UpdateSearchResults, | |
| 100 base::Unretained(this))); | |
| 101 web_ui()->RegisterMessageCallback("removeAllCookies", | |
| 102 base::Bind(&CookiesViewHandler::RemoveAll, | |
| 103 base::Unretained(this))); | |
| 104 web_ui()->RegisterMessageCallback("removeCookie", | |
| 105 base::Bind(&CookiesViewHandler::Remove, | |
| 106 base::Unretained(this))); | |
| 107 web_ui()->RegisterMessageCallback("loadCookie", | |
| 108 base::Bind(&CookiesViewHandler::LoadChildren, | |
| 109 base::Unretained(this))); | |
| 110 } | |
| 111 | |
| 112 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, | |
| 113 ui::TreeModelNode* parent, | |
| 114 int start, | |
| 115 int count) { | |
| 116 // Skip if there is a batch update in progress. | |
| 117 if (batch_update_) | |
| 118 return; | |
| 119 | |
| 120 CookieTreeNode* parent_node = cookies_tree_model_->AsNode(parent); | |
| 121 | |
| 122 ListValue* children = new ListValue; | |
| 123 model_util_->GetChildNodeList(parent_node, start, count, | |
| 124 children); | |
| 125 | |
| 126 ListValue args; | |
| 127 args.Append(parent == cookies_tree_model_->GetRoot() ? | |
| 128 Value::CreateNullValue() : | |
| 129 Value::CreateStringValue( | |
| 130 model_util_->GetTreeNodeId(parent_node))); | |
| 131 args.Append(Value::CreateIntegerValue(start)); | |
| 132 args.Append(children); | |
| 133 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", args); | |
| 134 } | |
| 135 | |
| 136 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model, | |
| 137 ui::TreeModelNode* parent, | |
| 138 int start, | |
| 139 int count) { | |
| 140 // Skip if there is a batch update in progress. | |
| 141 if (batch_update_) | |
| 142 return; | |
| 143 | |
| 144 ListValue args; | |
| 145 args.Append(parent == cookies_tree_model_->GetRoot() ? | |
| 146 Value::CreateNullValue() : | |
| 147 Value::CreateStringValue(model_util_->GetTreeNodeId( | |
| 148 cookies_tree_model_->AsNode(parent)))); | |
| 149 args.Append(Value::CreateIntegerValue(start)); | |
| 150 args.Append(Value::CreateIntegerValue(count)); | |
| 151 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args); | |
| 152 } | |
| 153 | |
| 154 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { | |
| 155 DCHECK(!batch_update_); // There should be no nested batch begin. | |
| 156 batch_update_ = true; | |
| 157 } | |
| 158 | |
| 159 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel* model) { | |
| 160 DCHECK(batch_update_); | |
| 161 batch_update_ = false; | |
| 162 | |
| 163 SendChildren(cookies_tree_model_->GetRoot()); | |
| 164 } | |
| 165 | |
| 166 void CookiesViewHandler::EnsureCookiesTreeModelCreated() { | |
| 167 if (!cookies_tree_model_.get()) { | |
| 168 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 169 cookies_tree_model_.reset(new CookiesTreeModel( | |
| 170 new BrowsingDataCookieHelper(profile->GetRequestContext()), | |
| 171 new BrowsingDataDatabaseHelper(profile), | |
| 172 new BrowsingDataLocalStorageHelper(profile), | |
| 173 NULL, | |
| 174 new BrowsingDataAppCacheHelper(profile), | |
| 175 BrowsingDataIndexedDBHelper::Create(profile), | |
| 176 BrowsingDataFileSystemHelper::Create(profile), | |
| 177 BrowsingDataQuotaHelper::Create(profile), | |
| 178 BrowsingDataServerBoundCertHelper::Create(profile), | |
| 179 false)); | |
| 180 cookies_tree_model_->AddCookiesTreeObserver(this); | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 void CookiesViewHandler::UpdateSearchResults(const ListValue* args) { | |
| 185 std::string query; | |
| 186 if (!args->GetString(0, &query)) { | |
| 187 return; | |
| 188 } | |
| 189 | |
| 190 EnsureCookiesTreeModelCreated(); | |
| 191 | |
| 192 cookies_tree_model_->UpdateSearchResults(UTF8ToWide(query)); | |
| 193 } | |
| 194 | |
| 195 void CookiesViewHandler::RemoveAll(const ListValue* args) { | |
| 196 EnsureCookiesTreeModelCreated(); | |
| 197 cookies_tree_model_->DeleteAllStoredObjects(); | |
| 198 } | |
| 199 | |
| 200 void CookiesViewHandler::Remove(const ListValue* args) { | |
| 201 std::string node_path; | |
| 202 if (!args->GetString(0, &node_path)) { | |
| 203 return; | |
| 204 } | |
| 205 | |
| 206 EnsureCookiesTreeModelCreated(); | |
| 207 | |
| 208 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath( | |
| 209 cookies_tree_model_->GetRoot(), node_path); | |
| 210 if (node) | |
| 211 cookies_tree_model_->DeleteCookieNode(const_cast<CookieTreeNode*>(node)); | |
| 212 } | |
| 213 | |
| 214 void CookiesViewHandler::LoadChildren(const ListValue* args) { | |
| 215 std::string node_path; | |
| 216 if (!args->GetString(0, &node_path)) { | |
| 217 return; | |
| 218 } | |
| 219 | |
| 220 EnsureCookiesTreeModelCreated(); | |
| 221 | |
| 222 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath( | |
| 223 cookies_tree_model_->GetRoot(), node_path); | |
| 224 if (node) | |
| 225 SendChildren(node); | |
| 226 } | |
| 227 | |
| 228 void CookiesViewHandler::SendChildren(const CookieTreeNode* parent) { | |
| 229 ListValue* children = new ListValue; | |
| 230 model_util_->GetChildNodeList(parent, 0, parent->child_count(), | |
| 231 children); | |
| 232 | |
| 233 ListValue args; | |
| 234 args.Append(parent == cookies_tree_model_->GetRoot() ? | |
| 235 Value::CreateNullValue() : | |
| 236 Value::CreateStringValue(model_util_->GetTreeNodeId(parent))); | |
| 237 args.Append(children); | |
| 238 | |
| 239 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args); | |
| 240 } | |
| 241 | |
| 242 } // namespace options2 | |
| OLD | NEW |