Chromium Code Reviews| Index: chrome/browser/ui/webui/options/intents_settings_handler.cc |
| diff --git a/chrome/browser/ui/webui/options/intents_settings_handler.cc b/chrome/browser/ui/webui/options/intents_settings_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..65e0e6b4d7a89c30f922d42222c9f0f049b83f07 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/options/intents_settings_handler.cc |
| @@ -0,0 +1,163 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/ui/webui/options/intents_settings_handler.h" |
| + |
| +#include "base/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/browsing_data_appcache_helper.h" |
| +#include "chrome/browser/browsing_data_database_helper.h" |
| +#include "chrome/browser/browsing_data_file_system_helper.h" |
| +#include "chrome/browser/browsing_data_indexed_db_helper.h" |
| +#include "chrome/browser/browsing_data_local_storage_helper.h" |
| +#include "chrome/browser/intents/web_intents_registry.h" |
| +#include "chrome/browser/intents/web_intents_registry_factory.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/webdata/web_data_service.h" |
| +#include "content/browser/webui/web_ui.h" |
| +#include "grit/generated_resources.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +IntentsSettingsHandler::IntentsSettingsHandler() : batch_update_(false) { |
| +} |
| + |
| +IntentsSettingsHandler::~IntentsSettingsHandler() { |
| +} |
| + |
| +void IntentsSettingsHandler::GetLocalizedValues( |
| + DictionaryValue* localized_strings) { |
| + DCHECK(localized_strings); |
| + |
| + static OptionsStringResource resources[] = { |
| + { "intentsTabLabel", IDS_INTENTS_TAB_LABEL }, |
| + { "intentsAllow", IDS_INTENTS_ALLOW_RADIO }, |
| + { "intentsBlock", IDS_INTENTS_BLOCK_RADIO }, |
| + { "intentsDomain", IDS_INTENTS_DOMAIN_COLUMN_HEADER }, |
| + { "intentsServiceData", IDS_INTENTS_SERVICE_DATA_COLUMN_HEADER }, |
| + { "manageIntents", IDS_INTENTS_MANAGE_BUTTON }, |
| + { "remove_intent", IDS_INTENTS_REMOVE_INTENT_BUTTON }, |
| + }; |
| + |
| + RegisterStrings(localized_strings, resources, arraysize(resources)); |
| + RegisterTitle(localized_strings, "intentsViewPage", |
| + IDS_INTENTS_MANAGER_WINDOW_TITLE); |
| +} |
| + |
| +void IntentsSettingsHandler::RegisterMessages() { |
| + web_ui_->RegisterMessageCallback("removeIntent", |
| + NewCallback(this, &IntentsSettingsHandler::RemoveIntent)); |
| + web_ui_->RegisterMessageCallback("loadIntents", |
| + NewCallback(this, &IntentsSettingsHandler::LoadChildren)); |
| +} |
| + |
| +void IntentsSettingsHandler::TreeNodesAdded(ui::TreeModel* model, |
| + ui::TreeModelNode* parent, |
| + int start, |
| + int count) { |
| + // Very inefficient, but probably good enough for now. |
| + SendChildren(intents_tree_model_->GetRoot()); |
| +} |
| + |
| +void IntentsSettingsHandler::TreeNodesRemoved(ui::TreeModel* model, |
| + ui::TreeModelNode* parent, |
| + int start, |
| + int count) { |
| + // Very inefficient, but probably good enough for now. |
| + SendChildren(intents_tree_model_->GetRoot()); |
| +} |
| + |
| +void IntentsSettingsHandler::TreeModelBeginBatch(IntentsModel* model) { |
| + DCHECK(!batch_update_); // There should be no nested batch begin. |
| + batch_update_ = true; |
| +} |
| + |
| +void IntentsSettingsHandler::TreeModelEndBatch(IntentsModel* model) { |
| + DCHECK(batch_update_); |
| + batch_update_ = false; |
| + |
| + SendChildren(intents_tree_model_->GetRoot()); |
| +} |
| + |
| +void IntentsSettingsHandler::EnsureIntentsModelCreated() { |
| + if (!intents_tree_model_.get()) { |
| + Profile* profile = Profile::FromWebUI(web_ui_); |
|
James Hawkins
2011/08/17 21:34:15
Save indentation, return early.
Greg Billock
2011/08/17 22:44:02
Done.
|
| + web_data_service_ = profile->GetWebDataService(Profile::EXPLICIT_ACCESS); |
| + web_intents_registry_ = WebIntentsRegistryFactory::GetForProfile(profile); |
| + web_intents_registry_->Initialize(web_data_service_.get()); |
| + intents_tree_model_.reset(new IntentsModel(web_intents_registry_)); |
| + intents_tree_model_->AddObserver(this); |
| + } |
| +} |
| + |
| +void IntentsSettingsHandler::RemoveIntent(const base::ListValue* args) { |
| + std::string node_path; |
| + if (!args->GetString(0, &node_path)) { |
| + return; |
| + } |
| + |
| + EnsureIntentsModelCreated(); |
| + |
| + IntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); |
| + if (node->Type() == IntentsTreeNode::TYPE_ORIGIN) { |
| + RemoveOrigin(node); |
| + } else if (node->Type() == IntentsTreeNode::TYPE_SERVICE) { |
| + ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(node); |
| + RemoveService(snode); |
| + } |
| +} |
| + |
| +void IntentsSettingsHandler::RemoveOrigin(IntentsTreeNode* node) { |
| + // TreeModelBeginBatch |
|
James Hawkins
2011/08/17 21:34:15
What are these comments about?
Greg Billock
2011/08/17 22:44:02
Fixed to a TODO: there's a batch update optimizati
|
| + while (node->child_count() > 0) { |
| + IntentsTreeNode* cnode = node->GetChild(0); |
| + CHECK(cnode->Type() == IntentsTreeNode::TYPE_SERVICE); |
| + ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(cnode); |
| + RemoveService(snode); |
| + } |
| + delete intents_tree_model_->Remove(node->parent(), node); |
| + // TreeModelEndBatch |
| +} |
| + |
| +void IntentsSettingsHandler::RemoveService(ServiceTreeNode* snode) { |
| + WebIntentData provider; |
| + provider.service_url = GURL(snode->ServiceUrl()); |
| + provider.action = snode->Action(); |
| + string16 stype; |
| + if (snode->Types().GetString(0, &stype)) { |
| + provider.type = stype; // Really need to iterate here. |
| + } |
| + provider.title = snode->ServiceName(); |
| + LOG(INFO) << "Removing service " << snode->ServiceName() |
| + << " " << snode->ServiceUrl(); |
| + web_intents_registry_->UnregisterIntentProvider(provider); |
| + delete intents_tree_model_->Remove(snode->parent(), snode); |
| +} |
| + |
| +void IntentsSettingsHandler::LoadChildren(const base::ListValue* args) { |
| + EnsureIntentsModelCreated(); |
| + |
| + std::string node_path; |
| + if (!args->GetString(0, &node_path)) { |
| + SendChildren(intents_tree_model_->GetRoot()); |
| + return; |
| + } |
| + |
| + IntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); |
| + SendChildren(node); |
| +} |
| + |
| +void IntentsSettingsHandler::SendChildren(IntentsTreeNode* parent) { |
| + ListValue* children = new ListValue; |
| + intents_tree_model_->GetChildNodeList(parent, 0, parent->child_count(), |
| + children); |
| + |
| + ListValue args; |
| + args.Append(parent == intents_tree_model_->GetRoot() ? |
| + Value::CreateNullValue() : |
| + Value::CreateStringValue(intents_tree_model_->GetTreeNodeId(parent))); |
| + args.Append(children); |
| + |
| + web_ui_->CallJavascriptFunction("IntentsView.loadChildren", args); |
| +} |