Chromium Code Reviews| 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|. | |
| 17 std::string GetLoadCookieCallbackName(CookiesTreeModelAdapter* adapter) { | |
| 18 static const char kPrefixLoadCookie[] = "loadChildren"; | |
| 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(GetLoadCookieCallbackName(this), | |
| 40 NewCallback(this, &CookiesTreeModelAdapter::LoadChildren)); | |
| 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 ListValue args; | |
| 53 args.Append(Value::CreateStringValue(tree_id_)); | |
| 54 args.Append(Value::CreateStringValue(GetLoadCookieCallbackName(this))); | |
| 55 web_ui_->CallJavascriptFunction("ui.CookiesTree.setCallback", args); | |
| 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 ListValue* children = new ListValue; | |
| 71 cookies_tree_model_util::GetChildNodeList(parent_node, start, count, | |
| 72 children); | |
| 73 | |
| 74 ListValue args; | |
| 75 args.Append(Value::CreateStringValue(tree_id_)); | |
| 76 args.Append(parent == model_->GetRoot() ? | |
| 77 Value::CreateNullValue() : | |
|
Evan Stade
2011/03/10 18:27:59
indentation here is kinda weird, might be easier t
xiyuan
2011/03/10 19:24:00
Wrapped the logic into a helper function to make i
| |
| 78 Value::CreateStringValue( | |
| 79 cookies_tree_model_util::GetTreeNodeId(parent_node))); | |
| 80 args.Append(Value::CreateIntegerValue(start)); | |
| 81 args.Append(children); | |
| 82 web_ui_->CallJavascriptFunction("ui.CookiesTree.onTreeItemAdded", args); | |
| 83 } | |
| 84 | |
| 85 void CookiesTreeModelAdapter::TreeNodesRemoved(ui::TreeModel* model, | |
| 86 ui::TreeModelNode* parent, | |
| 87 int start, | |
| 88 int count) { | |
| 89 // Skip if there is a batch update in progress. | |
| 90 if (batch_update_) | |
| 91 return; | |
| 92 | |
| 93 ListValue args; | |
| 94 args.Append(Value::CreateStringValue(tree_id_)); | |
| 95 args.Append(parent == model_->GetRoot() ? | |
| 96 Value::CreateNullValue() : | |
| 97 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId( | |
| 98 model_->AsNode(parent)))); | |
| 99 args.Append(Value::CreateIntegerValue(start)); | |
| 100 args.Append(Value::CreateIntegerValue(count)); | |
| 101 web_ui_->CallJavascriptFunction("ui.CookiesTree.onTreeItemRemoved", | |
| 102 args); | |
| 103 } | |
| 104 | |
| 105 void CookiesTreeModelAdapter::TreeModelBeginBatch(CookiesTreeModel* model) { | |
| 106 DCHECK(!batch_update_); // There should be no nested batch begin. | |
| 107 batch_update_ = true; | |
| 108 } | |
| 109 | |
| 110 void CookiesTreeModelAdapter::TreeModelEndBatch(CookiesTreeModel* model) { | |
| 111 DCHECK(batch_update_); | |
| 112 batch_update_ = false; | |
| 113 | |
| 114 SendChildren(model_->GetRoot()); | |
| 115 } | |
| 116 | |
| 117 void CookiesTreeModelAdapter::LoadChildren(const ListValue* args) { | |
| 118 std::string node_path; | |
| 119 if (!args->GetString(0, &node_path)) | |
|
Evan Stade
2011/03/10 18:27:59
this can never fail, right? So it should be a CHEC
xiyuan
2011/03/10 19:24:00
Done. Changed to CHECK.
| |
| 120 return; | |
| 121 | |
| 122 CookieTreeNode* node = cookies_tree_model_util::GetTreeNodeFromPath( | |
| 123 model_->GetRoot(), node_path); | |
| 124 if (node) | |
| 125 SendChildren(node); | |
| 126 } | |
| 127 | |
| 128 void CookiesTreeModelAdapter::SendChildren(CookieTreeNode* parent) { | |
| 129 ListValue* children = new ListValue; | |
| 130 cookies_tree_model_util::GetChildNodeList(parent, 0, parent->GetChildCount(), | |
| 131 children); | |
| 132 | |
| 133 ListValue args; | |
| 134 args.Append(Value::CreateStringValue(tree_id_)); | |
| 135 args.Append(parent == model_->GetRoot() ? | |
| 136 Value::CreateNullValue() : | |
| 137 Value::CreateStringValue(cookies_tree_model_util::GetTreeNodeId(parent))); | |
| 138 args.Append(children); | |
| 139 | |
| 140 web_ui_->CallJavascriptFunction("ui.CookiesTree.loadChildren", args); | |
| 141 } | |
| OLD | NEW |