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/cookies_tree_model_adapter.h" |
| 6 |
| 7 #include "base/string_number_conversions.h" |
| 8 #include "base/values.h" |
| 9 #include "chrome/browser/ui/webui/cookies_tree_model_util.h" |
| 10 #include "content/browser/webui/web_ui.h" |
| 11 #include "grit/generated_resources.h" |
| 12 #include "ui/base/l10n/l10n_util.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // Returns a unique callback name for |adapter|'s requestChildren. |
| 17 std::string GetRequestChildrenCallbackName(CookiesTreeModelAdapter* adapter) { |
| 18 static const char kPrefixLoadCookie[] = "requestChildren"; |
| 19 return std::string(kPrefixLoadCookie) + |
| 20 base::HexEncode(&adapter, sizeof(adapter)); |
| 21 } |
| 22 |
| 23 } // namespace |
| 24 |
| 25 CookiesTreeModelAdapter::CookiesTreeModelAdapter() |
| 26 : web_ui_(NULL), |
| 27 model_(NULL), |
| 28 batch_update_(false) { |
| 29 } |
| 30 |
| 31 CookiesTreeModelAdapter::~CookiesTreeModelAdapter() { |
| 32 if (model_) |
| 33 model_->RemoveCookiesTreeObserver(this); |
| 34 } |
| 35 |
| 36 void CookiesTreeModelAdapter::Init(WebUI* web_ui) { |
| 37 web_ui_ = web_ui; |
| 38 |
| 39 web_ui_->RegisterMessageCallback(GetRequestChildrenCallbackName(this), |
| 40 NewCallback(this, &CookiesTreeModelAdapter::RequestChildren)); |
| 41 } |
| 42 |
| 43 void CookiesTreeModelAdapter::Bind(const std::string& tree_id, |
| 44 CookiesTreeModel* model) { |
| 45 DCHECK(web_ui_); // We should have been initialized. |
| 46 DCHECK(tree_id_.empty() && !model_); // No existing bindings. |
| 47 |
| 48 tree_id_ = tree_id; |
| 49 model_ = model; |
| 50 model_->AddCookiesTreeObserver(this); |
| 51 |
| 52 web_ui_->CallJavascriptFunction("ui.CookiesTree.setCallback", |
| 53 StringValue(tree_id_), |
| 54 StringValue(GetRequestChildrenCallbackName(this))); |
| 55 |
| 56 SendChildren(model_->GetRoot()); |
| 57 } |
| 58 |
| 59 void CookiesTreeModelAdapter::TreeNodesAdded(ui::TreeModel* model, |
| 60 ui::TreeModelNode* parent, |
| 61 int start, |
| 62 int count) { |
| 63 // Skip if there is a batch update in progress. |
| 64 if (batch_update_) |
| 65 return; |
| 66 |
| 67 CookieTreeNode* parent_node = model_->AsNode(parent); |
| 68 |
| 69 ListValue* children = new ListValue; |
| 70 cookies_tree_model_util::GetChildNodeList(parent_node, start, count, |
| 71 children); |
| 72 |
| 73 ListValue args; |
| 74 args.Append(GetTreeNodeId(parent_node)); |
| 75 args.Append(Value::CreateIntegerValue(start)); |
| 76 args.Append(children); |
| 77 web_ui_->CallJavascriptFunction("ui.CookiesTree.onTreeItemAdded", |
| 78 StringValue(tree_id_), args); |
| 79 } |
| 80 |
| 81 void CookiesTreeModelAdapter::TreeNodesRemoved(ui::TreeModel* model, |
| 82 ui::TreeModelNode* parent, |
| 83 int start, |
| 84 int count) { |
| 85 // Skip if there is a batch update in progress. |
| 86 if (batch_update_) |
| 87 return; |
| 88 |
| 89 ListValue args; |
| 90 args.Append(GetTreeNodeId(model_->AsNode(parent))); |
| 91 args.Append(Value::CreateIntegerValue(start)); |
| 92 args.Append(Value::CreateIntegerValue(count)); |
| 93 web_ui_->CallJavascriptFunction("ui.CookiesTree.onTreeItemRemoved", |
| 94 StringValue(tree_id_), args); |
| 95 } |
| 96 |
| 97 void CookiesTreeModelAdapter::TreeModelBeginBatch(CookiesTreeModel* model) { |
| 98 DCHECK(!batch_update_); // There should be no nested batch begin. |
| 99 batch_update_ = true; |
| 100 } |
| 101 |
| 102 void CookiesTreeModelAdapter::TreeModelEndBatch(CookiesTreeModel* model) { |
| 103 DCHECK(batch_update_); |
| 104 batch_update_ = false; |
| 105 |
| 106 SendChildren(model_->GetRoot()); |
| 107 } |
| 108 |
| 109 void CookiesTreeModelAdapter::RequestChildren(const ListValue* args) { |
| 110 std::string node_path; |
| 111 CHECK(args->GetString(0, &node_path)); |
| 112 |
| 113 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( |
| 114 model_->GetRoot(), node_path); |
| 115 if (node) |
| 116 SendChildren(node); |
| 117 } |
| 118 |
| 119 void CookiesTreeModelAdapter::SendChildren(CookieTreeNode* parent) { |
| 120 ListValue* children = new ListValue; |
| 121 cookies_tree_model_util::GetChildNodeList(parent, 0, parent->GetChildCount(), |
| 122 children); |
| 123 |
| 124 ListValue args; |
| 125 args.Append(GetTreeNodeId(model_->AsNode(parent))); |
| 126 args.Append(children); |
| 127 |
| 128 web_ui_->CallJavascriptFunction("ui.CookiesTree.setChildren", |
| 129 StringValue(tree_id_), args); |
| 130 } |
| 131 |
| 132 Value* CookiesTreeModelAdapter::GetTreeNodeId(CookieTreeNode* node) { |
| 133 if (node == model_->GetRoot()) |
| 134 return Value::CreateNullValue(); |
| 135 |
| 136 return Value::CreateStringValue( |
| 137 cookies_tree_model_util::GetTreeNodeId(node)); |
| 138 } |
OLD | NEW |