Chromium Code Reviews| Index: chrome/browser/ui/webui/cookies_tree_model_adapter.cc |
| diff --git a/chrome/browser/ui/webui/cookies_tree_model_adapter.cc b/chrome/browser/ui/webui/cookies_tree_model_adapter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6caebeba419485e1676273503ad8b5efd06a3e71 |
| --- /dev/null |
| +++ b/chrome/browser/ui/webui/cookies_tree_model_adapter.cc |
| @@ -0,0 +1,142 @@ |
| +// 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/cookies_tree_model_adapter.h" |
| + |
| +#include "base/string_number_conversions.h" |
| +#include "base/values.h" |
| +#include "chrome/browser/ui/webui/cookies_tree_model_util.h" |
| +#include "content/browser/webui/web_ui.h" |
| +#include "grit/generated_resources.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| + |
| +namespace { |
| + |
| +// Returns a unique callback name for |adapter|. |
| +std::string GetLoadCookieCallbackName(CookiesTreeModelAdapter* adapter) { |
| + static const char kPrefixLoadCookie[] = "loadChildren"; |
| + return std::string(kPrefixLoadCookie) + |
| + base::HexEncode(&adapter, sizeof(adapter)); |
| +} |
| + |
| +} // namespace |
| + |
| +CookiesTreeModelAdapter::CookiesTreeModelAdapter() |
| + : web_ui_(NULL), |
| + model_(NULL), |
| + batch_update_(false) { |
| +} |
| + |
| +CookiesTreeModelAdapter::~CookiesTreeModelAdapter() { |
| + if (model_) |
| + model_->RemoveCookiesTreeObserver(this); |
| +} |
| + |
| +void CookiesTreeModelAdapter::Init(WebUI* web_ui) { |
| + web_ui_ = web_ui; |
| + |
| + web_ui_->RegisterMessageCallback(GetLoadCookieCallbackName(this), |
| + NewCallback(this, &CookiesTreeModelAdapter::LoadChildren)); |
| +} |
| + |
| +void CookiesTreeModelAdapter::Bind(const std::string& tree_id, |
| + CookiesTreeModel* model) { |
|
oshima
2011/03/10 15:41:22
indent
xiyuan
2011/03/10 17:27:05
Done.
|
| + DCHECK(web_ui_); // We should have been initialized. |
| + DCHECK(tree_id_.empty() && !model_); // No existing bindings. |
| + |
| + tree_id_ = tree_id; |
| + model_ = model; |
| + model_->AddCookiesTreeObserver(this); |
| + |
| + ListValue args; |
| + args.Append(Value::CreateStringValue(tree_id_)); |
| + args.Append(Value::CreateStringValue(GetLoadCookieCallbackName(this))); |
| + web_ui_->CallJavascriptFunction(L"ui.CookiesTree.setCallback", args); |
| + |
| + SendChildren(model_->GetRoot()); |
| +} |
| + |
| +void CookiesTreeModelAdapter::TreeNodesAdded(ui::TreeModel* model, |
| + ui::TreeModelNode* parent, |
| + int start, |
| + int count) { |
| + // Skip if there is a batch update in progress. |
| + if (batch_update_) |
| + return; |
| + |
| + CookieTreeNode* parent_node = model_->AsNode(parent); |
| + |
| + ListValue* children = new ListValue; |
| + cookies_tree_model_util::GetChildNodeList(parent_node, start, count, |
| + children); |
| + |
| + ListValue args; |
| + args.Append(Value::CreateStringValue(tree_id_)); |
| + args.Append(parent == model_->GetRoot() ? |
| + Value::CreateNullValue() : |
| + Value::CreateStringValue( |
| + cookies_tree_model_util::GetTreeNodeId(parent_node))); |
| + args.Append(Value::CreateIntegerValue(start)); |
| + args.Append(children); |
| + web_ui_->CallJavascriptFunction(L"ui.CookiesTree.onTreeItemAdded", args); |
| +} |
| + |
| +void CookiesTreeModelAdapter::TreeNodesRemoved(ui::TreeModel* model, |
| + ui::TreeModelNode* parent, |
| + int start, |
| + int count) { |
| + // Skip if there is a batch update in progress. |
| + if (batch_update_) |
| + return; |
| + |
| + ListValue args; |
| + args.Append(Value::CreateStringValue(tree_id_)); |
| + args.Append(parent == model_->GetRoot() ? |
| + Value::CreateNullValue() : |
| + Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId( |
| + model_->AsNode(parent)))); |
| + args.Append(Value::CreateIntegerValue(start)); |
| + args.Append(Value::CreateIntegerValue(count)); |
| + web_ui_->CallJavascriptFunction(L"ui.CookiesTree.onTreeItemRemoved", |
| + args); |
| +} |
| + |
| +void CookiesTreeModelAdapter::TreeModelBeginBatch(CookiesTreeModel* model) { |
| + DCHECK(!batch_update_); // There should be no nested batch begin. |
| + batch_update_ = true; |
| +} |
| + |
| +void CookiesTreeModelAdapter::TreeModelEndBatch(CookiesTreeModel* model) { |
| + DCHECK(batch_update_); |
| + batch_update_ = false; |
| + |
| + SendChildren(model_->GetRoot()); |
| +} |
| + |
| +void CookiesTreeModelAdapter::LoadChildren(const ListValue* args) { |
| + std::string node_path; |
| + if (!args->GetString(0, &node_path)){ |
| + return; |
| + } |
|
oshima
2011/03/10 15:41:22
nuke {}
xiyuan
2011/03/10 17:27:05
Done.
|
| + |
| + CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( |
| + model_->GetRoot(), node_path); |
| + if (node) |
| + SendChildren(node); |
| +} |
| + |
| +void CookiesTreeModelAdapter::SendChildren(CookieTreeNode* parent) { |
| + ListValue* children = new ListValue; |
| + cookies_tree_model_util::GetChildNodeList(parent, 0, parent->GetChildCount(), |
| + children); |
| + |
| + ListValue args; |
| + args.Append(Value::CreateStringValue(tree_id_)); |
| + args.Append(parent == model_->GetRoot() ? |
| + Value::CreateNullValue() : |
| + Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(parent))); |
| + args.Append(children); |
| + |
| + web_ui_->CallJavascriptFunction(L"ui.CookiesTree.loadChildren", args); |
| +} |