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/options/intents_settings_handler.h" |
| 6 |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/browsing_data_appcache_helper.h" |
| 10 #include "chrome/browser/browsing_data_database_helper.h" |
| 11 #include "chrome/browser/browsing_data_file_system_helper.h" |
| 12 #include "chrome/browser/browsing_data_indexed_db_helper.h" |
| 13 #include "chrome/browser/browsing_data_local_storage_helper.h" |
| 14 #include "chrome/browser/intents/web_intents_registry.h" |
| 15 #include "chrome/browser/intents/web_intents_registry_factory.h" |
| 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "chrome/browser/webdata/web_data_service.h" |
| 18 #include "content/browser/webui/web_ui.h" |
| 19 #include "grit/generated_resources.h" |
| 20 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 |
| 23 IntentsSettingsHandler::IntentsSettingsHandler() : batch_update_(false) { |
| 24 } |
| 25 |
| 26 IntentsSettingsHandler::~IntentsSettingsHandler() { |
| 27 } |
| 28 |
| 29 void IntentsSettingsHandler::GetLocalizedValues( |
| 30 DictionaryValue* localized_strings) { |
| 31 DCHECK(localized_strings); |
| 32 |
| 33 static OptionsStringResource resources[] = { |
| 34 { "intents_tab_label", IDS_INTENTS_TAB_LABEL }, |
| 35 { "intents_allow", IDS_INTENTS_ALLOW_RADIO }, |
| 36 { "intents_block", IDS_INTENTS_BLOCK_RADIO }, |
| 37 { "intents_domain", IDS_INTENTS_DOMAIN_COLUMN_HEADER }, |
| 38 { "intents_service_data", IDS_INTENTS_SERVICE_DATA_COLUMN_HEADER }, |
| 39 { "manage_intents", IDS_INTENTS_MANAGE_BUTTON }, |
| 40 { "remove_intent", 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 IntentsSettingsHandler::RegisterMessages() { |
| 49 web_ui_->RegisterMessageCallback("removeIntent", |
| 50 NewCallback(this, &IntentsSettingsHandler::RemoveIntent)); |
| 51 web_ui_->RegisterMessageCallback("loadIntents", |
| 52 NewCallback(this, &IntentsSettingsHandler::LoadChildren)); |
| 53 } |
| 54 |
| 55 void IntentsSettingsHandler::TreeNodesAdded(ui::TreeModel* model, |
| 56 ui::TreeModelNode* parent, |
| 57 int start, |
| 58 int count) { |
| 59 // Very inefficient, but probably good enough for now. |
| 60 SendChildren(intents_tree_model_->GetRoot()); |
| 61 } |
| 62 |
| 63 void IntentsSettingsHandler::TreeNodesRemoved(ui::TreeModel* model, |
| 64 ui::TreeModelNode* parent, |
| 65 int start, |
| 66 int count) { |
| 67 // Very inefficient, but probably good enough for now. |
| 68 SendChildren(intents_tree_model_->GetRoot()); |
| 69 } |
| 70 |
| 71 void IntentsSettingsHandler::TreeModelBeginBatch(IntentsModel* model) { |
| 72 DCHECK(!batch_update_); // There should be no nested batch begin. |
| 73 batch_update_ = true; |
| 74 } |
| 75 |
| 76 void IntentsSettingsHandler::TreeModelEndBatch(IntentsModel* model) { |
| 77 DCHECK(batch_update_); |
| 78 batch_update_ = false; |
| 79 |
| 80 SendChildren(intents_tree_model_->GetRoot()); |
| 81 } |
| 82 |
| 83 void IntentsSettingsHandler::EnsureIntentsModelCreated() { |
| 84 if (!intents_tree_model_.get()) { |
| 85 Profile* profile = Profile::FromWebUI(web_ui_); |
| 86 web_data_service_ = profile->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| 87 web_intents_registry_ = WebIntentsRegistryFactory::GetForProfile(profile); |
| 88 web_intents_registry_->Initialize(web_data_service_.get()); |
| 89 intents_tree_model_.reset(new IntentsModel(web_intents_registry_)); |
| 90 intents_tree_model_->AddObserver(this); |
| 91 } |
| 92 } |
| 93 |
| 94 void IntentsSettingsHandler::RemoveIntent(const base::ListValue* args) { |
| 95 std::string node_path; |
| 96 if (!args->GetString(0, &node_path)) { |
| 97 return; |
| 98 } |
| 99 |
| 100 EnsureIntentsModelCreated(); |
| 101 |
| 102 IntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); |
| 103 if (node->Type() == IntentsTreeNode::TYPE_ORIGIN) { |
| 104 RemoveOrigin(node); |
| 105 } else if (node->Type() == IntentsTreeNode::TYPE_SERVICE) { |
| 106 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(node); |
| 107 RemoveService(snode); |
| 108 } |
| 109 } |
| 110 |
| 111 void IntentsSettingsHandler::RemoveOrigin(IntentsTreeNode* node) { |
| 112 // TreeModelBeginBatch |
| 113 while (node->child_count() > 0) { |
| 114 IntentsTreeNode* cnode = node->GetChild(0); |
| 115 CHECK(cnode->Type() == IntentsTreeNode::TYPE_SERVICE); |
| 116 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(cnode); |
| 117 RemoveService(snode); |
| 118 } |
| 119 delete intents_tree_model_->Remove(node->parent(), node); |
| 120 // TreeModelEndBatch |
| 121 } |
| 122 |
| 123 void IntentsSettingsHandler::RemoveService(ServiceTreeNode* snode) { |
| 124 WebIntentData provider; |
| 125 provider.service_url = GURL(snode->ServiceUrl()); |
| 126 provider.action = snode->Action(); |
| 127 string16 stype; |
| 128 if (snode->Types().GetString(0, &stype)) { |
| 129 provider.type = stype; // Really need to iterate here. |
| 130 } |
| 131 provider.title = snode->ServiceName(); |
| 132 LOG(INFO) << "Removing service " << snode->ServiceName() |
| 133 << " " << snode->ServiceUrl(); |
| 134 web_intents_registry_->UnregisterIntentProvider(provider); |
| 135 delete intents_tree_model_->Remove(snode->parent(), snode); |
| 136 } |
| 137 |
| 138 void IntentsSettingsHandler::LoadChildren(const base::ListValue* args) { |
| 139 EnsureIntentsModelCreated(); |
| 140 |
| 141 std::string node_path; |
| 142 if (!args->GetString(0, &node_path)) { |
| 143 SendChildren(intents_tree_model_->GetRoot()); |
| 144 return; |
| 145 } |
| 146 |
| 147 IntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); |
| 148 SendChildren(node); |
| 149 } |
| 150 |
| 151 void IntentsSettingsHandler::SendChildren(IntentsTreeNode* parent) { |
| 152 ListValue* children = new ListValue; |
| 153 intents_tree_model_->GetChildNodeList(parent, 0, parent->child_count(), |
| 154 children); |
| 155 |
| 156 ListValue args; |
| 157 args.Append(parent == intents_tree_model_->GetRoot() ? |
| 158 Value::CreateNullValue() : |
| 159 Value::CreateStringValue(intents_tree_model_->GetTreeNodeId(parent))); |
| 160 args.Append(children); |
| 161 |
| 162 web_ui_->CallJavascriptFunction("IntentsView.loadChildren", args); |
| 163 } |
OLD | NEW |