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/options/cookies_view_handler.h" 6 7 #include <string> 8 9 #include "base/bind.h" 10 #include "base/bind_helpers.h" 11 #include "base/strings/utf_string_conversions.h" 12 #include "base/values.h" 13 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h" 14 #include "chrome/browser/browsing_data/browsing_data_channel_id_helper.h" 15 #include "chrome/browser/browsing_data/browsing_data_cookie_helper.h" 16 #include "chrome/browser/browsing_data/browsing_data_database_helper.h" 17 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h" 18 #include "chrome/browser/browsing_data/browsing_data_flash_lso_helper.h" 19 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h" 20 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" 21 #include "chrome/browser/browsing_data/browsing_data_quota_helper.h" 22 #include "chrome/browser/browsing_data/browsing_data_service_worker_helper.h" 23 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" 25 #include "chrome/grit/generated_resources.h" 26 #include "content/public/browser/browser_context.h" 27 #include "content/public/browser/render_process_host.h" 28 #include "content/public/browser/site_instance.h" 29 #include "content/public/browser/storage_partition.h" 30 #include "content/public/browser/web_ui.h" 31 32 namespace storage { 33 class FileSystemContext; 34 } 35 36 namespace options { 37 38 CookiesViewHandler::CookiesViewHandler() 39 : batch_update_(false), 40 model_util_(new CookiesTreeModelUtil) { 41 } 42 43 CookiesViewHandler::~CookiesViewHandler() { 44 } 45 46 void CookiesViewHandler::GetLocalizedValues( 47 base::DictionaryValue* localized_strings) { 48 DCHECK(localized_strings); 49 50 static OptionsStringResource resources[] = { 51 { "label_cookie_name", IDS_COOKIES_COOKIE_NAME_LABEL }, 52 { "label_cookie_content", IDS_COOKIES_COOKIE_CONTENT_LABEL }, 53 { "label_cookie_domain", IDS_COOKIES_COOKIE_DOMAIN_LABEL }, 54 { "label_cookie_path", IDS_COOKIES_COOKIE_PATH_LABEL }, 55 { "label_cookie_send_for", IDS_COOKIES_COOKIE_SENDFOR_LABEL }, 56 { "label_cookie_accessible_to_script", 57 IDS_COOKIES_COOKIE_ACCESSIBLE_TO_SCRIPT_LABEL }, 58 { "label_cookie_created", IDS_COOKIES_COOKIE_CREATED_LABEL }, 59 { "label_cookie_expires", IDS_COOKIES_COOKIE_EXPIRES_LABEL }, 60 { "label_webdb_desc", IDS_COOKIES_WEB_DATABASE_DESCRIPTION_LABEL }, 61 { "label_local_storage_size", 62 IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL }, 63 { "label_local_storage_last_modified", 64 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL }, 65 { "label_local_storage_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, 66 { "label_indexed_db_size", IDS_COOKIES_LOCAL_STORAGE_SIZE_ON_DISK_LABEL }, 67 { "label_indexed_db_last_modified", 68 IDS_COOKIES_LOCAL_STORAGE_LAST_MODIFIED_LABEL }, 69 { "label_indexed_db_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, 70 { "label_service_worker_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, 71 { "label_service_worker_scopes", IDS_COOKIES_SERVICE_WORKER_SCOPES_LABEL }, 72 { "label_app_cache_manifest", 73 IDS_COOKIES_APPLICATION_CACHE_MANIFEST_LABEL }, 74 { "label_cookie_last_accessed", IDS_COOKIES_LAST_ACCESSED_LABEL }, 75 { "cookie_domain", IDS_COOKIES_DOMAIN_COLUMN_HEADER }, 76 { "cookie_local_data", IDS_COOKIES_DATA_COLUMN_HEADER }, 77 { "cookie_singular", IDS_COOKIES_SINGLE_COOKIE }, 78 { "cookie_plural", IDS_COOKIES_PLURAL_COOKIES }, 79 { "cookie_database_storage", IDS_COOKIES_DATABASE_STORAGE }, 80 { "cookie_indexed_db", IDS_COOKIES_INDEXED_DB }, 81 { "cookie_local_storage", IDS_COOKIES_LOCAL_STORAGE }, 82 { "cookie_app_cache", IDS_COOKIES_APPLICATION_CACHE }, 83 { "cookie_service_worker", IDS_COOKIES_SERVICE_WORKER }, 84 { "cookie_flash_lso", IDS_COOKIES_FLASH_LSO }, 85 { "search_cookies", IDS_COOKIES_SEARCH_COOKIES }, 86 { "remove_cookie", IDS_COOKIES_REMOVE_LABEL }, 87 { "remove_all_cookie", IDS_COOKIES_REMOVE_ALL_LABEL }, 88 { "remove_all_shown_cookie", IDS_COOKIES_REMOVE_ALL_SHOWN_LABEL }, 89 { "cookie_file_system", IDS_COOKIES_FILE_SYSTEM }, 90 { "label_file_system_origin", IDS_COOKIES_LOCAL_STORAGE_ORIGIN_LABEL }, 91 { "label_file_system_temporary_usage", 92 IDS_COOKIES_FILE_SYSTEM_TEMPORARY_USAGE_LABEL }, 93 { "label_file_system_persistent_usage", 94 IDS_COOKIES_FILE_SYSTEM_PERSISTENT_USAGE_LABEL }, 95 { "cookie_channel_id", IDS_COOKIES_CHANNEL_ID }, 96 { "label_channel_id_server_id", 97 IDS_COOKIES_CHANNEL_ID_ORIGIN_LABEL }, 98 { "label_channel_id_type", 99 IDS_COOKIES_CHANNEL_ID_TYPE_LABEL }, 100 { "label_channel_id_created", 101 IDS_COOKIES_CHANNEL_ID_CREATED_LABEL }, 102 { "label_channel_id_expires", 103 IDS_COOKIES_CHANNEL_ID_EXPIRES_LABEL }, 104 { "label_protected_by_apps", 105 IDS_GEOLOCATION_SET_BY_HOVER }, // TODO(bauerb): Use a better string 106 }; 107 108 RegisterStrings(localized_strings, resources, arraysize(resources)); 109 RegisterTitle(localized_strings, "cookiesViewPage", 110 IDS_COOKIES_WEBSITE_PERMISSIONS_WINDOW_TITLE); 111 } 112 113 void CookiesViewHandler::RegisterMessages() { 114 web_ui()->RegisterMessageCallback("updateCookieSearchResults", 115 base::Bind(&CookiesViewHandler::UpdateSearchResults, 116 base::Unretained(this))); 117 web_ui()->RegisterMessageCallback("removeAllCookies", 118 base::Bind(&CookiesViewHandler::RemoveAll, 119 base::Unretained(this))); 120 web_ui()->RegisterMessageCallback("removeCookie", 121 base::Bind(&CookiesViewHandler::Remove, 122 base::Unretained(this))); 123 web_ui()->RegisterMessageCallback("loadCookie", 124 base::Bind(&CookiesViewHandler::LoadChildren, 125 base::Unretained(this))); 126 web_ui()->RegisterMessageCallback("reloadCookies", 127 base::Bind(&CookiesViewHandler::ReloadCookies, 128 base::Unretained(this))); 129 } 130 131 void CookiesViewHandler::TreeNodesAdded(ui::TreeModel* model, 132 ui::TreeModelNode* parent, 133 int start, 134 int count) { 135 // Skip if there is a batch update in progress. 136 if (batch_update_) 137 return; 138 139 CookiesTreeModel* tree_model = static_cast<CookiesTreeModel*>(model); 140 CookieTreeNode* parent_node = tree_model->AsNode(parent); 141 142 base::ListValue* children = new base::ListValue; 143 model_util_->GetChildNodeList(parent_node, start, count, children); 144 145 base::ListValue args; 146 args.Append(parent == tree_model->GetRoot() ? 147 base::Value::CreateNullValue() : 148 new base::StringValue(model_util_->GetTreeNodeId(parent_node))); 149 args.Append(new base::FundamentalValue(start)); 150 args.Append(children); 151 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemAdded", 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 base::ListValue args; 165 args.Append(parent == tree_model->GetRoot() ? 166 base::Value::CreateNullValue() : 167 new base::StringValue(model_util_->GetTreeNodeId( 168 tree_model->AsNode(parent)))); 169 args.Append(new base::FundamentalValue(start)); 170 args.Append(new base::FundamentalValue(count)); 171 web_ui()->CallJavascriptFunction("CookiesView.onTreeItemRemoved", args); 172 } 173 174 void CookiesViewHandler::TreeModelBeginBatch(CookiesTreeModel* model) { 175 DCHECK(!batch_update_); // There should be no nested batch begin. 176 batch_update_ = true; 177 } 178 179 void CookiesViewHandler::TreeModelEndBatch(CookiesTreeModel* model) { 180 DCHECK(batch_update_); 181 batch_update_ = false; 182 183 SendChildren(model->GetRoot()); 184 } 185 186 void CookiesViewHandler::EnsureCookiesTreeModelCreated() { 187 if (!cookies_tree_model_.get()) { 188 Profile* profile = Profile::FromWebUI(web_ui()); 189 content::StoragePartition* storage_partition = 190 content::BrowserContext::GetDefaultStoragePartition(profile); 191 content::IndexedDBContext* indexed_db_context = 192 storage_partition->GetIndexedDBContext(); 193 content::ServiceWorkerContext* service_worker_context = 194 storage_partition->GetServiceWorkerContext(); 195 storage::FileSystemContext* file_system_context = 196 storage_partition->GetFileSystemContext(); 197 LocalDataContainer* container = new LocalDataContainer( 198 new BrowsingDataCookieHelper(profile->GetRequestContext()), 199 new BrowsingDataDatabaseHelper(profile), 200 new BrowsingDataLocalStorageHelper(profile), 201 NULL, 202 new BrowsingDataAppCacheHelper(profile), 203 new BrowsingDataIndexedDBHelper(indexed_db_context), 204 BrowsingDataFileSystemHelper::Create(file_system_context), 205 BrowsingDataQuotaHelper::Create(profile), 206 BrowsingDataChannelIDHelper::Create(profile->GetRequestContext()), 207 new BrowsingDataServiceWorkerHelper(service_worker_context), 208 BrowsingDataFlashLSOHelper::Create(profile)); 209 cookies_tree_model_.reset( 210 new CookiesTreeModel(container, 211 profile->GetExtensionSpecialStoragePolicy(), 212 false)); 213 cookies_tree_model_->AddCookiesTreeObserver(this); 214 } 215 } 216 217 void CookiesViewHandler::UpdateSearchResults(const base::ListValue* args) { 218 base::string16 query; 219 if (!args->GetString(0, &query)) 220 return; 221 222 EnsureCookiesTreeModelCreated(); 223 224 cookies_tree_model_->UpdateSearchResults(query); 225 } 226 227 void CookiesViewHandler::RemoveAll(const base::ListValue* args) { 228 EnsureCookiesTreeModelCreated(); 229 cookies_tree_model_->DeleteAllStoredObjects(); 230 } 231 232 void CookiesViewHandler::Remove(const base::ListValue* args) { 233 std::string node_path; 234 if (!args->GetString(0, &node_path)) 235 return; 236 237 EnsureCookiesTreeModelCreated(); 238 239 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath( 240 cookies_tree_model_->GetRoot(), node_path); 241 if (node) 242 cookies_tree_model_->DeleteCookieNode(const_cast<CookieTreeNode*>(node)); 243 } 244 245 void CookiesViewHandler::LoadChildren(const base::ListValue* args) { 246 std::string node_path; 247 if (!args->GetString(0, &node_path)) 248 return; 249 250 EnsureCookiesTreeModelCreated(); 251 252 const CookieTreeNode* node = model_util_->GetTreeNodeFromPath( 253 cookies_tree_model_->GetRoot(), node_path); 254 if (node) 255 SendChildren(node); 256 } 257 258 void CookiesViewHandler::SendChildren(const CookieTreeNode* parent) { 259 base::ListValue* children = new base::ListValue; 260 model_util_->GetChildNodeList(parent, 0, parent->child_count(), 261 children); 262 263 base::ListValue args; 264 args.Append(parent == cookies_tree_model_->GetRoot() ? 265 base::Value::CreateNullValue() : 266 new base::StringValue(model_util_->GetTreeNodeId(parent))); 267 args.Append(children); 268 269 web_ui()->CallJavascriptFunction("CookiesView.loadChildren", args); 270 } 271 272 void CookiesViewHandler::ReloadCookies(const base::ListValue* args) { 273 cookies_tree_model_.reset(); 274 275 EnsureCookiesTreeModelCreated(); 276 } 277 278 } // namespace options 279