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 #ifndef CHROME_BROWSER_UI_INTENTS_INTENTS_MODEL_H_ | |
6 #define CHROME_BROWSER_UI_INTENTS_INTENTS_MODEL_H_ | |
7 #pragma once | |
8 | |
9 #include "base/values.h" | |
10 #include "chrome/browser/intents/web_intents_registry.h" | |
11 #include "ui/base/models/tree_node_model.h" | |
12 | |
13 class WebIntentsRegistry; | |
14 | |
15 // The tree structure is a TYPE_ROOT node with title="", | |
16 // children are TYPE_ORIGIN nodes with title=origin, whose | |
17 // children are TYPE_SERVICE nodes with title=origin, and | |
18 // will be of type ServiceTreeNode with data on individual | |
19 // services. | |
20 class IntentsTreeNode : public ui::TreeNode<IntentsTreeNode> { | |
21 public: | |
22 IntentsTreeNode() | |
23 : ui::TreeNode<IntentsTreeNode>(string16()), | |
24 type_(TYPE_ROOT) {} | |
25 | |
26 explicit IntentsTreeNode(const string16& title) | |
27 : ui::TreeNode<IntentsTreeNode>(title), | |
28 type_(TYPE_ORIGIN) {} | |
29 | |
30 virtual ~IntentsTreeNode() {} | |
31 | |
32 enum NodeType { | |
33 TYPE_ROOT, | |
34 TYPE_ORIGIN, | |
35 TYPE_SERVICE, | |
36 }; | |
37 | |
38 NodeType Type() const { return type_; } | |
39 | |
40 protected: | |
41 IntentsTreeNode(const string16& title, NodeType type) | |
42 : ui::TreeNode<IntentsTreeNode>(title), | |
43 type_(type) {} | |
44 | |
45 private: | |
46 NodeType type_; | |
47 }; | |
48 | |
49 // Tree node representing particular services presented by an origin. | |
50 class ServiceTreeNode : public IntentsTreeNode { | |
51 public: | |
52 explicit ServiceTreeNode(const string16& title); | |
53 virtual ~ServiceTreeNode(); | |
54 | |
55 const string16& ServiceName() const { return service_name_; } | |
56 const string16& ServiceUrl() const { return service_url_; } | |
57 const string16& IconUrl() const { return icon_url_; } | |
58 const string16& Action() const { return action_; } | |
59 const base::ListValue& Types() const { return types_; } | |
60 bool IsBlocked() const { return blocked_; } | |
61 bool IsDisabled() const { return disabled_; } | |
62 | |
63 void SetServiceName(string16 name) { service_name_ = name; } | |
64 void SetServiceUrl(string16 url) { service_url_ = url; } | |
65 void SetIconUrl(string16 url) { icon_url_ = url; } | |
66 void SetAction(string16 action) { action_ = action; } | |
67 void AddType(string16 type) { types_.Append(Value::CreateStringValue(type)); } | |
68 void SetBlocked(bool blocked) { blocked_ = blocked; } | |
69 void SetDisabled(bool disabled) { disabled_ = disabled; } | |
70 | |
71 private: | |
72 string16 service_name_; | |
73 string16 icon_url_; | |
74 string16 service_url_; | |
75 string16 action_; | |
76 base::ListValue types_; | |
77 | |
78 // TODO(gbillock): these are kind of a placeholder for exceptions data. | |
79 bool blocked_; | |
80 bool disabled_; | |
81 }; | |
82 | |
83 // UI-backing tree model of the data in the WebIntentsRegistry. | |
84 class IntentsModel : public ui::TreeNodeModel<IntentsTreeNode>, | |
85 public WebIntentsRegistry::Consumer { | |
86 public: | |
87 // Because nodes are fetched in a background thread, they are not | |
88 // present at the time the Model is created. The Model then notifies its | |
89 // observers for every item added. | |
90 class Observer : public ui::TreeModelObserver { | |
91 public: | |
92 virtual void TreeModelBeginBatch(IntentsModel* model) {} | |
93 virtual void TreeModelEndBatch(IntentsModel* model) {} | |
94 }; | |
95 | |
96 explicit IntentsModel(WebIntentsRegistry* intents_registry); | |
97 virtual ~IntentsModel(); | |
98 | |
99 void AddIntentsTreeObserver(Observer* observer); | |
100 void RemoveIntentsTreeObserver(Observer* observer); | |
101 | |
102 string16 GetTreeNodeId(IntentsTreeNode* node); | |
103 IntentsTreeNode* GetTreeNode(std::string path_id); | |
104 void GetChildNodeList(IntentsTreeNode* parent, int start, int count, | |
105 base::ListValue* nodes); | |
106 void GetIntentsTreeNodeDictionary(const IntentsTreeNode& node, | |
107 base::DictionaryValue* dict); | |
108 | |
109 virtual void OnIntentsQueryDone( | |
110 WebIntentsRegistry::QueryID query_id, | |
111 const std::vector<WebIntentData>& intents) OVERRIDE; | |
112 | |
113 private: | |
114 // Loads the data model from the WebIntentsRegistry. | |
115 // TODO(gbillock): need an observer on that to absorb async updates? | |
116 void LoadModel(); | |
117 | |
118 // Do batch-specific notifies for updates coming from the LoadModel. | |
119 void NotifyObserverBeginBatch(); | |
120 void NotifyObserverEndBatch(); | |
121 | |
122 // The backing registry. Weak pointer. | |
123 WebIntentsRegistry* intents_registry_; | |
124 | |
125 // Separate list of observers that'll get batch updates. | |
126 ObserverList<Observer> intents_observer_list_; | |
127 | |
128 // Batch update nesting level. Incremented to indicate that we're in | |
129 // the middle of a batch update. | |
130 int batch_update_; | |
131 }; | |
132 | |
133 #endif // CHROME_BROWSER_UI_INTENTS_INTENTS_MODEL_H_ | |
OLD | NEW |