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