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