| 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_handler.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/common/extensions/extension.h" | |
| 14 #include "chrome/common/extensions/extension_set.h" | |
| 15 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h" | |
| 16 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" | |
| 17 #include "chrome/browser/browsing_data/browsing_data_database_helper.h" | |
| 18 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h" | |
| 19 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h" | |
| 20 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h" | |
| 21 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" | |
| 22 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h" | |
| 23 #include "chrome/browser/browsing_data/browsing_data_server_bound_cert_helper.h" | |
| 24 #include "chrome/browser/extensions/extension_service.h" | |
| 25 #include "chrome/browser/profiles/profile.h" | |
| 26 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" | |
| 27 #include "content/public/browser/web_ui.h" | |
| 28 #include "grit/generated_resources.h" | |
| 29 #include "ui/base/l10n/l10n_util.h" | |
| 30 | |
| 31 namespace options { | |
| 32 | |
| 33 CookiesViewHandler::CookiesViewHandler() | |
| 34 : batch_update_(false), | |
| 35 app_context_(false), | |
| 36 model_util_(new CookiesTreeModelUtil) { | |
| 37 } | |
| 38 | |
| 39 CookiesViewHandler::~CookiesViewHandler() { | |
| 40 } | |
| 41 | |
| 42 void CookiesViewHandler::GetLocalizedValues( | |
| 43 DictionaryValue* localized_strings) { | |
| 44 DCHECK(localized_strings); | |
| 45 | |
| 46 static OptionsStringResource resources[] = { | |
| 47 { "label_cookie_name", IDS_COOKIES_COOKIE_NAME_LABEL }, | |
| 48 { "label_cookie_content", IDS_COOKIES_COOKIE_CONTENT_LABEL }, | |
| 49 { "label_cookie_domain", IDS_COOKIES_COOKIE_DOMAIN_LABEL }, | |
| 50 { "label_cookie_path", IDS_COOKIES_COOKIE_PATH_LABEL }, | |
| 51 { "label_cookie_send_for", IDS_COOKIES_COOKIE_SENDFOR_LABEL }, | |
| 52 { "label_cookie_accessible_to_script", | |
| 53 IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_LABEL }, | |
| 54 { "label_cookie_created", IDS_COOKIES_COOKIE_CREATED_LABEL }, | |
| 55 { "label_cookie_expires", IDS_COOKIES_COOKIE_EXPIRES_LABEL }, | |
| 56 { "label_webdb_desc", IDS_COOKIES_WEB_DATABASE_DESCRIPTION_LABEL }, | |
| 57 { "label_local_storage_size", | |
| 58 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL }, | |
| 59 { "label_local_storage_last_modified", | |
| 60 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL }, | |
| 61 { "label_local_storage_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, | |
| 62 { "label_indexed_db_size", IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL }, | |
| 63 { "label_indexed_db_last_modified", | |
| 64 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL }, | |
| 65 { "label_indexed_db_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, | |
| 66 { "label_app_cache_manifest", | |
| 67 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL }, | |
| 68 { "label_cookie_last_accessed", IDS_COOKIES_LAST_ACCESSED_LABEL }, | |
| 69 { "cookie_domain", IDS_COOKIES_DOMAIN_COLUMN_HEADER }, | |
| 70 { "cookie_local_data", IDS_COOKIES_DATA_COLUMN_HEADER }, | |
| 71 { "cookie_singular", IDS_COOKIES_SINGLE_COOKIE }, | |
| 72 { "cookie_plural", IDS_COOKIES_PLURAL_COOKIES }, | |
| 73 { "cookie_database_storage", IDS_COOKIES_DATABASE_STORAGE }, | |
| 74 { "cookie_indexed_db", IDS_COOKIES_INDEXED_DB }, | |
| 75 { "cookie_local_storage", IDS_COOKIES_LOCAL_STORAGE }, | |
| 76 { "cookie_app_cache", IDS_COOKIES_APPLICATION_CACHE }, | |
| 77 { "cookie_flash_lso", IDS_COOKIES_FLASH_LSO }, | |
| 78 { "search_cookies", IDS_COOKIES_SEARCH_COOKIES }, | |
| 79 { "remove_cookie", IDS_COOKIES_REMOVE_LABEL }, | |
| 80 { "remove_all_cookie", IDS_COOKIES_REMOVE_ALL_LABEL }, | |
| 81 { "cookie_file_system", IDS_COOKIES_FILE_SYSTEM }, | |
| 82 { "label_file_system_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, | |
| 83 { "label_file_system_temporary_usage", | |
| 84 IDS_COOKIES_FILE_SYSTEM_TEMPORARY_USAGE_LABEL }, | |
| 85 { "label_file_system_persistent_usage", | |
| 86 IDS_COOKIES_FILE_SYSTEM_PERSISTENT_USAGE_LABEL }, | |
| 87 { "cookie_server_bound_cert", IDS_COOKIES_SERVER_BOUND_CERT }, | |
| 88 { "label_server_bound_cert_server_id", | |
| 89 IDS_COOKIES_SERVER_BOUND_CERT_ORIGIN_LABEL }, | |
| 90 { "label_server_bound_cert_type", | |
| 91 IDS_COOKIES_SERVER_BOUND_CERT_TYPE_LABEL }, | |
| 92 { "label_server_bound_cert_created", | |
| 93 IDS_COOKIES_SERVER_BOUND_CERT_CREATED_LABEL }, | |
| 94 { "label_server_bound_cert_expires", | |
| 95 IDS_COOKIES_SERVER_BOUND_CERT_EXPIRES_LABEL }, | |
| 96 { "label_protected_by_apps", | |
| 97 IDS_GEOLOCATION_SET_BY_HOVER }, // TODO(bauerb): Use a better string | |
| 98 }; | |
| 99 | |
| 100 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 101 RegisterTitle(localized_strings, "cookiesViewPage", | |
| 102 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); | |
| 103 RegisterTitle(localized_strings, "appCookiesViewPage", | |
| 104 IDS_APP_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); | |
| 105 } | |
| 106 | |
| 107 void CookiesViewHandler::RegisterMessages() { | |
| 108 web_ui()->RegisterMessageCallback("updateCookieSearchResults", | |
| 109 base::Bind(&CookiesViewHandler::UpdateSearchResults, | |
| 110 base::Unretained(this))); | |
| 111 web_ui()->RegisterMessageCallback("removeAllCookies", | |
| 112 base::Bind(&CookiesViewHandler::RemoveAll, | |
| 113 base::Unretained(this))); | |
| 114 web_ui()->RegisterMessageCallback("removeCookie", | |
| 115 base::Bind(&CookiesViewHandler::Remove, | |
| 116 base::Unretained(this))); | |
| 117 web_ui()->RegisterMessageCallback("loadCookie", | |
| 118 base::Bind(&CookiesViewHandler::LoadChildren, | |
| 119 base::Unretained(this))); | |
| 120 web_ui()->RegisterMessageCallback("setViewContext", | |
| 121 base::Bind(&CookiesViewHandler::SetViewContext, | |
| 122 base::Unretained(this))); | |
| 123 web_ui()->RegisterMessageCallback("reloadCookies", | |
| 124 base::Bind(&CookiesViewHandler::ReloadCookies, | |
| 125 base::Unretained(this))); | |
| 126 } | |
| 127 | |
| 128 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, | |
| 129 ui::TreeModelNode* parent, | |
| 130 int start, | |
| 131 int count) { | |
| 132 // Skip if there is a batch update in progress. | |
| 133 if (batch_update_) | |
| 134 return; | |
| 135 | |
| 136 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model); | |
| 137 CookieTreeNode* parent_node = tree_model->AsNode(parent); | |
| 138 | |
| 139 ListValue* children = new ListValue; | |
| 140 model_util_->GetChildNodeList(parent_node, start, count, | |
| 141 children); | |
| 142 | |
| 143 ListValue args; | |
| 144 args.Append(parent == tree_model->GetRoot() ? | |
| 145 Value::CreateNullValue() : | |
| 146 Value::CreateStringValue( | |
| 147 model_util_->GetTreeNodeId(parent_node))); | |
| 148 args.Append(Value::CreateIntegerValue(start)); | |
| 149 args.Append(children); | |
| 150 web_ui()->CallJavascriptFunction( | |
| 151 GetCallback("onTreeItemAdded", tree_model), args); | |
| 152 } | |
| 153 | |
| 154 void CookiesViewHandler::TreeNodesRemoved(ui::TreeModel* model, | |
| 155 ui::TreeModelNode* parent, | |
| 156 int start, | |
| 157 int count) { | |
| 158 // Skip if there is a batch update in progress. | |
| 159 if (batch_update_) | |
| 160 return; | |
| 161 | |
| 162 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model); | |
| 163 | |
| 164 ListValue args; | |
| 165 args.Append(parent == tree_model->GetRoot() ? | |
| 166 Value::CreateNullValue() : | |
| 167 Value::CreateStringValue(model_util_->GetTreeNodeId( | |
| 168 tree_model->AsNode(parent)))); | |
| 169 args.Append(Value::CreateIntegerValue(start)); | |
| 170 args.Append(Value::CreateIntegerValue(count)); | |
| 171 web_ui()->CallJavascriptFunction( | |
| 172 GetCallback("onTreeItemRemoved", tree_model), args); | |
| 173 } | |
| 174 | |
| 175 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { | |
| 176 DCHECK(!batch_update_); // There should be no nested batch begin. | |
| 177 batch_update_ = true; | |
| 178 } | |
| 179 | |
| 180 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel* model) { | |
| 181 DCHECK(batch_update_); | |
| 182 batch_update_ = false; | |
| 183 | |
| 184 SendChildren(model->GetRoot()); | |
| 185 } | |
| 186 | |
| 187 void CookiesViewHandler::EnsureCookiesTreeModelCreated() { | |
| 188 if (!app_context_ && !cookies_tree_model_.get()) { | |
| 189 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 190 ContainerMap apps_map; | |
| 191 apps_map[std::string()] = new LocalDataContainer( | |
| 192 "Site Data", std::string(), | |
| 193 new BrowsingDataCookieHelper(profile->GetRequestContext()), | |
| 194 new BrowsingDataDatabaseHelper(profile), | |
| 195 new BrowsingDataLocalStorageHelper(profile), | |
| 196 NULL, | |
| 197 new BrowsingDataAppCacheHelper(profile), | |
| 198 BrowsingDataIndexedDBHelper::Create(profile), | |
| 199 BrowsingDataFileSystemHelper::Create(profile), | |
| 200 BrowsingDataQuotaHelper::Create(profile), | |
| 201 BrowsingDataServerBoundCertHelper::Create(profile), | |
| 202 BrowsingDataFlashLSOHelper::Create(profile)); | |
| 203 cookies_tree_model_.reset( | |
| 204 new CookiesTreeModel(apps_map, | |
| 205 profile->GetExtensionSpecialStoragePolicy(), | |
| 206 false)); | |
| 207 cookies_tree_model_->AddCookiesTreeObserver(this); | |
| 208 } | |
| 209 | |
| 210 if (app_context_ && !app_cookies_tree_model_.get()) { | |
| 211 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 212 ContainerMap apps_map; | |
| 213 const ExtensionService* service = profile->GetExtensionService(); | |
| 214 if (service) { | |
| 215 const ExtensionSet* extensions = service->extensions(); | |
| 216 for (ExtensionSet::const_iterator it = extensions->begin(); | |
| 217 it != extensions->end(); ++it) { | |
| 218 if ((*it)->is_storage_isolated()) { | |
| 219 net::URLRequestContextGetter* context_getter = | |
| 220 profile->GetRequestContextForIsolatedApp((*it)->id()); | |
| 221 // TODO(nasko): When new types of storage are isolated, add the | |
| 222 // appropriate browsing data helper objects to the constructor. | |
| 223 // For now, just cookies are isolated, so other parameters are NULL. | |
| 224 apps_map[(*it)->id()] = new LocalDataContainer( | |
| 225 (*it)->name(), (*it)->id(), | |
| 226 new BrowsingDataCookieHelper(context_getter), | |
| 227 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL); | |
| 228 } | |
| 229 } | |
| 230 app_cookies_tree_model_.reset( | |
| 231 new CookiesTreeModel(apps_map, | |
| 232 profile->GetExtensionSpecialStoragePolicy(), | |
| 233 false)); | |
| 234 app_cookies_tree_model_->AddCookiesTreeObserver(this); | |
| 235 } | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 void CookiesViewHandler::UpdateSearchResults(const ListValue* args) { | |
| 240 string16 query; | |
| 241 if (!args->GetString(0, &query)) | |
| 242 return; | |
| 243 | |
| 244 EnsureCookiesTreeModelCreated(); | |
| 245 | |
| 246 GetTreeModel()->UpdateSearchResults(query); | |
| 247 } | |
| 248 | |
| 249 void CookiesViewHandler::RemoveAll(const ListValue* args) { | |
| 250 EnsureCookiesTreeModelCreated(); | |
| 251 GetTreeModel()->DeleteAllStoredObjects(); | |
| 252 } | |
| 253 | |
| 254 void CookiesViewHandler::Remove(const ListValue* args) { | |
| 255 std::string node_path; | |
| 256 if (!args->GetString(0, &node_path)) | |
| 257 return; | |
| 258 | |
| 259 EnsureCookiesTreeModelCreated(); | |
| 260 | |
| 261 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath( | |
| 262 GetTreeModel()->GetRoot(), node_path); | |
| 263 if (node) | |
| 264 GetTreeModel()->DeleteCookieNode(const_cast<CookieTreeNode*>(node)); | |
| 265 } | |
| 266 | |
| 267 void CookiesViewHandler::LoadChildren(const ListValue* args) { | |
| 268 std::string node_path; | |
| 269 if (!args->GetString(0, &node_path)) | |
| 270 return; | |
| 271 | |
| 272 EnsureCookiesTreeModelCreated(); | |
| 273 | |
| 274 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath( | |
| 275 GetTreeModel()->GetRoot(), node_path); | |
| 276 if (node) | |
| 277 SendChildren(node); | |
| 278 } | |
| 279 | |
| 280 void CookiesViewHandler::SendChildren(const CookieTreeNode* parent) { | |
| 281 ListValue* children = new ListValue; | |
| 282 model_util_->GetChildNodeList(parent, 0, parent->child_count(), | |
| 283 children); | |
| 284 | |
| 285 ListValue args; | |
| 286 args.Append(parent == GetTreeModel()->GetRoot() ? | |
| 287 Value::CreateNullValue() : | |
| 288 Value::CreateStringValue(model_util_->GetTreeNodeId(parent))); | |
| 289 args.Append(children); | |
| 290 | |
| 291 web_ui()->CallJavascriptFunction( | |
| 292 GetCallback("loadChildren", GetTreeModel()), args); | |
| 293 } | |
| 294 | |
| 295 void CookiesViewHandler::SetViewContext(const base::ListValue* args) { | |
| 296 bool app_context = false; | |
| 297 if (args->GetBoolean(0, &app_context)) | |
| 298 app_context_ = app_context; | |
| 299 } | |
| 300 | |
| 301 void CookiesViewHandler::ReloadCookies(const base::ListValue* args) { | |
| 302 cookies_tree_model_.reset(); | |
| 303 app_cookies_tree_model_.reset(); | |
| 304 | |
| 305 EnsureCookiesTreeModelCreated(); | |
| 306 } | |
| 307 | |
| 308 CookiesTreeModel* CookiesViewHandler::GetTreeModel() { | |
| 309 CookiesTreeModel* model = app_context_ ? | |
| 310 app_cookies_tree_model_.get() : cookies_tree_model_.get(); | |
| 311 DCHECK(model); | |
| 312 return model; | |
| 313 } | |
| 314 | |
| 315 std::string CookiesViewHandler::GetCallback( | |
| 316 std::string method, CookiesTreeModel* model) { | |
| 317 std::string callback("CookiesView"); | |
| 318 | |
| 319 if (model == app_cookies_tree_model_) | |
| 320 callback.append("App"); | |
| 321 return callback.append(".").append(method); | |
| 322 } | |
| 323 | |
| 324 } // namespace options | |
| OLD | NEW |