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