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/intents/intents_model.h" | |
| 6 #include "base/string_split.h" | |
| 7 #include "base/string_util.h" | |
| 8 #include "base/stringprintf.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "chrome/browser/intents/web_intents_registry.h" | |
| 11 | |
| 12 IntentsModel::IntentsModel(WebIntentsRegistry* intents_registry) | |
| 13 : ui::TreeNodeModel<IntentsTreeNode>(new IntentsTreeNode()), | |
| 14 intents_registry_(intents_registry), | |
| 15 batch_update_(0) { | |
| 16 LoadModel(); | |
| 17 } | |
| 18 | |
| 19 void IntentsModel::AddIntentsTreeObserver(Observer* observer) { | |
| 20 intents_observer_list_.AddObserver(observer); | |
| 21 // Call super so that TreeNodeModel can notify, too. | |
| 22 ui::TreeNodeModel<IntentsTreeNode>::AddObserver(observer); | |
| 23 } | |
| 24 | |
| 25 void IntentsModel::RemoveIntentsTreeObserver(Observer* observer) { | |
| 26 intents_observer_list_.RemoveObserver(observer); | |
| 27 // Call super so that TreeNodeModel doesn't have dead pointers. | |
| 28 ui::TreeNodeModel<IntentsTreeNode>::RemoveObserver(observer); | |
| 29 } | |
| 30 | |
| 31 string16 IntentsModel::GetTreeNodeId(IntentsTreeNode* node) { | |
| 32 if (node->Type() == IntentsTreeNode::TYPE_ORIGIN) | |
| 33 return node->GetTitle(); | |
| 34 if (node->Type() == IntentsTreeNode::TYPE_SERVICE) { | |
|
James Hawkins
2011/08/17 21:34:15
Inconsistently using braces. I'd drop the braces f
Greg Billock
2011/08/17 22:44:02
Done.
| |
| 35 return string16(); | |
| 36 } | |
| 37 | |
| 38 return string16(); | |
| 39 } | |
| 40 | |
| 41 IntentsTreeNode* IntentsModel::GetTreeNode(std::string path_id) { | |
| 42 if (path_id == "") | |
|
James Hawkins
2011/08/17 21:34:15
path_id.empty()
Greg Billock
2011/08/17 22:44:02
Done.
| |
| 43 return GetRoot(); | |
| 44 | |
| 45 std::vector<std::string> node_ids; | |
| 46 base::SplitString(path_id, ',', &node_ids); | |
| 47 | |
| 48 for (int i = 0; i < GetRoot()->child_count(); ++i) { | |
| 49 IntentsTreeNode* node = GetRoot()->GetChild(i); | |
| 50 if (UTF16ToUTF8(node->GetTitle()) == node_ids[0]) { | |
| 51 if (node_ids.size() == 1) | |
| 52 return node; | |
| 53 } | |
| 54 } | |
| 55 | |
| 56 // TODO: support service nodes? | |
| 57 return NULL; | |
| 58 } | |
| 59 | |
| 60 void IntentsModel::GetChildNodeList(IntentsTreeNode* parent, | |
| 61 int start, int count, | |
| 62 base::ListValue* nodes) { | |
| 63 for (int i = 0; i < count; ++i) { | |
| 64 base::DictionaryValue* dict = new base::DictionaryValue; | |
| 65 IntentsTreeNode* child = parent->GetChild(start + i); | |
| 66 GetIntentsTreeNodeDictionary(*child, dict); | |
| 67 nodes->Append(dict); | |
| 68 } | |
| 69 } | |
| 70 | |
| 71 void IntentsModel::GetIntentsTreeNodeDictionary(const IntentsTreeNode& node, | |
| 72 base::DictionaryValue* dict) { | |
| 73 if (node.Type() == IntentsTreeNode::TYPE_ROOT) { | |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 if (node.Type() == IntentsTreeNode::TYPE_ORIGIN) { | |
| 78 dict->SetString("site", node.GetTitle()); | |
| 79 dict->SetBoolean("hasChildren", node.child_count() > 0); | |
| 80 return; | |
| 81 } | |
| 82 | |
| 83 if (node.Type() == IntentsTreeNode::TYPE_SERVICE) { | |
| 84 const ServiceTreeNode* snode = static_cast<const ServiceTreeNode*>(&node); | |
| 85 dict->SetString("site", snode->GetTitle()); | |
| 86 dict->SetString("name", snode->ServiceName()); | |
| 87 dict->SetString("url", snode->ServiceUrl()); | |
| 88 dict->SetString("icon", snode->IconUrl()); | |
| 89 dict->SetString("action", snode->Action()); | |
| 90 dict->Set("types", snode->Types().DeepCopy()); | |
| 91 dict->SetBoolean("blocked", snode->IsBlocked()); | |
| 92 dict->SetBoolean("disabled", snode->IsDisabled()); | |
| 93 return; | |
| 94 } | |
| 95 } | |
| 96 | |
| 97 void IntentsModel::LoadModel() { | |
| 98 NotifyObserverBeginBatch(); | |
| 99 intents_registry_->GetAllIntentProviders(this); | |
| 100 } | |
| 101 | |
| 102 void IntentsModel::OnIntentsQueryDone( | |
| 103 WebIntentsRegistry::QueryID query_id, | |
| 104 const std::vector<WebIntentData>& intents) OVERRIDE { | |
| 105 for (size_t i = 0; i < intents.size(); ++i) { | |
| 106 // Eventually do some awesome sorting, grouping, clustering stuff here. | |
| 107 // For now, just stick it in the model flat. | |
| 108 IntentsTreeNode* n = new IntentsTreeNode(ASCIIToUTF16( | |
| 109 intents[i].service_url.host())); | |
| 110 ServiceTreeNode* ns = new ServiceTreeNode(ASCIIToUTF16( | |
| 111 intents[i].service_url.host())); | |
| 112 ns->SetServiceName(intents[i].title); | |
| 113 ns->SetServiceUrl(ASCIIToUTF16(intents[i].service_url.spec())); | |
| 114 GURL icon_url = intents[i].service_url.GetOrigin().Resolve("/favicon.ico"); | |
| 115 ns->SetIconUrl(ASCIIToUTF16(icon_url.spec())); | |
| 116 ns->SetAction(intents[i].action); | |
| 117 ns->AddType(intents[i].type); | |
| 118 // Won't generate a notification. OK for now as the next line will. | |
| 119 n->Add(ns, 0); | |
| 120 Add(GetRoot(), n, GetRoot()->child_count()); | |
| 121 } | |
| 122 | |
| 123 NotifyObserverEndBatch(); | |
| 124 } | |
| 125 | |
| 126 void IntentsModel::NotifyObserverBeginBatch() { | |
| 127 // Only notify the model once if we're batching in a nested manner. | |
| 128 if (batch_update_++ == 0) { | |
| 129 FOR_EACH_OBSERVER(Observer, | |
| 130 intents_observer_list_, | |
| 131 TreeModelBeginBatch(this)); | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 void IntentsModel::NotifyObserverEndBatch() { | |
| 136 // Only notify the observers if this is the outermost call to EndBatch() if | |
| 137 // called in a nested manner. | |
| 138 if (--batch_update_ == 0) { | |
| 139 FOR_EACH_OBSERVER(Observer, | |
| 140 intents_observer_list_, | |
| 141 TreeModelEndBatch(this)); | |
| 142 } | |
| 143 } | |
| OLD | NEW |