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