| 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/web_intents_settings_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/utf_string_conversions.h" | |
| 9 #include "base/values.h" | |
| 10 #include "chrome/browser/browsing_data_appcache_helper.h" | |
| 11 #include "chrome/browser/browsing_data_database_helper.h" | |
| 12 #include "chrome/browser/browsing_data_file_system_helper.h" | |
| 13 #include "chrome/browser/browsing_data_indexed_db_helper.h" | |
| 14 #include "chrome/browser/browsing_data_local_storage_helper.h" | |
| 15 #include "chrome/browser/intents/web_intents_registry.h" | |
| 16 #include "chrome/browser/intents/web_intents_registry_factory.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/webdata/web_data_service.h" | |
| 19 #include "content/browser/webui/web_ui.h" | |
| 20 #include "grit/generated_resources.h" | |
| 21 #include "net/url_request/url_request_context_getter.h" | |
| 22 #include "ui/base/l10n/l10n_util.h" | |
| 23 | |
| 24 WebIntentsSettingsHandler::WebIntentsSettingsHandler() | |
| 25 : web_intents_registry_(NULL), | |
| 26 batch_update_(false) { | |
| 27 } | |
| 28 | |
| 29 WebIntentsSettingsHandler::~WebIntentsSettingsHandler() { | |
| 30 } | |
| 31 | |
| 32 void WebIntentsSettingsHandler::GetLocalizedValues( | |
| 33 DictionaryValue* localized_strings) { | |
| 34 DCHECK(localized_strings); | |
| 35 | |
| 36 static OptionsStringResource resources[] = { | |
| 37 { "intentsDomain", IDS_INTENTS_DOMAIN_COLUMN_HEADER }, | |
| 38 { "intentsServiceData", IDS_INTENTS_SERVICE_DATA_COLUMN_HEADER }, | |
| 39 { "manageIntents", IDS_INTENTS_MANAGE_BUTTON }, | |
| 40 { "removeIntent", IDS_INTENTS_REMOVE_INTENT_BUTTON }, | |
| 41 }; | |
| 42 | |
| 43 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 44 RegisterTitle(localized_strings, "intentsViewPage", | |
| 45 IDS_INTENTS_MANAGER_WINDOW_TITLE); | |
| 46 } | |
| 47 | |
| 48 void WebIntentsSettingsHandler::RegisterMessages() { | |
| 49 web_ui_->RegisterMessageCallback("removeIntent", | |
| 50 base::Bind(&WebIntentsSettingsHandler::RemoveIntent, | |
| 51 base::Unretained(this))); | |
| 52 web_ui_->RegisterMessageCallback("loadIntents", | |
| 53 base::Bind(&WebIntentsSettingsHandler::LoadChildren, | |
| 54 base::Unretained(this))); | |
| 55 } | |
| 56 | |
| 57 void WebIntentsSettingsHandler::TreeNodesAdded(ui::TreeModel* model, | |
| 58 ui::TreeModelNode* parent, | |
| 59 int start, | |
| 60 int count) { | |
| 61 SendChildren(intents_tree_model_->GetRoot()); | |
| 62 } | |
| 63 | |
| 64 void WebIntentsSettingsHandler::TreeNodesRemoved(ui::TreeModel* model, | |
| 65 ui::TreeModelNode* parent, | |
| 66 int start, | |
| 67 int count) { | |
| 68 SendChildren(intents_tree_model_->GetRoot()); | |
| 69 } | |
| 70 | |
| 71 void WebIntentsSettingsHandler::TreeModelBeginBatch(WebIntentsModel* model) { | |
| 72 batch_update_ = true; | |
| 73 } | |
| 74 | |
| 75 void WebIntentsSettingsHandler::TreeModelEndBatch(WebIntentsModel* model) { | |
| 76 batch_update_ = false; | |
| 77 | |
| 78 SendChildren(intents_tree_model_->GetRoot()); | |
| 79 } | |
| 80 | |
| 81 void WebIntentsSettingsHandler::EnsureWebIntentsModelCreated() { | |
| 82 if (intents_tree_model_.get()) return; | |
| 83 | |
| 84 Profile* profile = Profile::FromWebUI(web_ui_); | |
| 85 web_intents_registry_ = WebIntentsRegistryFactory::GetForProfile(profile); | |
| 86 intents_tree_model_.reset(new WebIntentsModel(web_intents_registry_)); | |
| 87 intents_tree_model_->AddWebIntentsTreeObserver(this); | |
| 88 } | |
| 89 | |
| 90 void WebIntentsSettingsHandler::RemoveIntent(const base::ListValue* args) { | |
| 91 std::string node_path; | |
| 92 if (!args->GetString(0, &node_path)) { | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 EnsureWebIntentsModelCreated(); | |
| 97 | |
| 98 WebIntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); | |
| 99 if (node->Type() == WebIntentsTreeNode::TYPE_ORIGIN) { | |
| 100 RemoveOrigin(node); | |
| 101 } else if (node->Type() == WebIntentsTreeNode::TYPE_SERVICE) { | |
| 102 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(node); | |
| 103 RemoveService(snode); | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 void WebIntentsSettingsHandler::RemoveOrigin(WebIntentsTreeNode* node) { | |
| 108 // TODO(gbillock): This is a known batch update. Worth optimizing? | |
| 109 while (!node->empty()) { | |
| 110 WebIntentsTreeNode* cnode = node->GetChild(0); | |
| 111 CHECK(cnode->Type() == WebIntentsTreeNode::TYPE_SERVICE); | |
| 112 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(cnode); | |
| 113 RemoveService(snode); | |
| 114 } | |
| 115 delete intents_tree_model_->Remove(node->parent(), node); | |
| 116 } | |
| 117 | |
| 118 void WebIntentsSettingsHandler::RemoveService(ServiceTreeNode* snode) { | |
| 119 webkit_glue::WebIntentServiceData service; | |
| 120 service.service_url = GURL(snode->ServiceUrl()); | |
| 121 service.action = snode->Action(); | |
| 122 string16 stype; | |
| 123 if (snode->Types().GetString(0, &stype)) { | |
| 124 service.type = stype; // Really need to iterate here. | |
| 125 } | |
| 126 service.title = snode->ServiceName(); | |
| 127 web_intents_registry_->UnregisterIntentProvider(service); | |
| 128 delete intents_tree_model_->Remove(snode->parent(), snode); | |
| 129 } | |
| 130 | |
| 131 void WebIntentsSettingsHandler::LoadChildren(const base::ListValue* args) { | |
| 132 EnsureWebIntentsModelCreated(); | |
| 133 | |
| 134 std::string node_path; | |
| 135 if (!args->GetString(0, &node_path)) { | |
| 136 SendChildren(intents_tree_model_->GetRoot()); | |
| 137 return; | |
| 138 } | |
| 139 | |
| 140 WebIntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); | |
| 141 SendChildren(node); | |
| 142 } | |
| 143 | |
| 144 void WebIntentsSettingsHandler::SendChildren(WebIntentsTreeNode* parent) { | |
| 145 // Early bailout during batch updates. We'll get one after the batch concludes | |
| 146 // with batch_update_ set false. | |
| 147 if (batch_update_) return; | |
| 148 | |
| 149 ListValue* children = new ListValue; | |
| 150 intents_tree_model_->GetChildNodeList(parent, 0, parent->child_count(), | |
| 151 children); | |
| 152 | |
| 153 ListValue args; | |
| 154 args.Append(parent == intents_tree_model_->GetRoot() ? | |
| 155 Value::CreateNullValue() : | |
| 156 Value::CreateStringValue(intents_tree_model_->GetTreeNodeId(parent))); | |
| 157 args.Append(children); | |
| 158 | |
| 159 web_ui_->CallJavascriptFunction("IntentsView.loadChildren", args); | |
| 160 } | |
| OLD | NEW |