OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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/options/web_intents_settings_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/utf_string_conversions.h" | |
9 #include "base/values.h" | |
10 #include "chrome/browser/browsing_data/browsing_data_appcache_helper.h" | |
11 #include "chrome/browser/browsing_data/browsing_data_database_helper.h" | |
12 #include "chrome/browser/browsing_data/browsing_data_file_system_helper.h" | |
13 #include "chrome/browser/browsing_data/browsing_data_indexed_db_helper.h" | |
14 #include "chrome/browser/browsing_data/browsing_data_local_storage_helper.h" | |
15 #include "chrome/browser/intents/web_intents_registry.h" | |
16 #include "chrome/browser/intents/web_intents_registry_factory.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/webdata/web_data_service.h" | |
19 #include "content/public/browser/web_ui.h" | |
20 #include "grit/generated_resources.h" | |
21 #include "net/url_request/url_request_context_getter.h" | |
22 #include "ui/base/l10n/l10n_util.h" | |
23 | |
24 namespace options { | |
25 | |
26 WebIntentsSettingsHandler::WebIntentsSettingsHandler() | |
27 : web_intents_registry_(NULL), | |
28 batch_update_(false) { | |
29 } | |
30 | |
31 WebIntentsSettingsHandler::~WebIntentsSettingsHandler() { | |
32 } | |
33 | |
34 void WebIntentsSettingsHandler::GetLocalizedValues( | |
35 DictionaryValue* localized_strings) { | |
36 DCHECK(localized_strings); | |
37 | |
38 static OptionsStringResource resources[] = { | |
39 { "intentsDomain", IDS_INTENTS_DOMAIN_COLUMN_HEADER }, | |
40 { "intentsServiceData", IDS_INTENTS_SERVICE_DATA_COLUMN_HEADER }, | |
41 { "manageIntents", IDS_INTENTS_MANAGE_BUTTON }, | |
42 { "removeIntent", IDS_INTENTS_REMOVE_INTENT_BUTTON }, | |
43 }; | |
44 | |
45 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
46 RegisterTitle(localized_strings, "intentsViewPage", | |
47 IDS_INTENTS_MANAGER_WINDOW_TITLE); | |
48 } | |
49 | |
50 void WebIntentsSettingsHandler::RegisterMessages() { | |
51 web_ui()->RegisterMessageCallback("removeIntent", | |
52 base::Bind(&WebIntentsSettingsHandler::RemoveIntent, | |
53 base::Unretained(this))); | |
54 web_ui()->RegisterMessageCallback("loadIntents", | |
55 base::Bind(&WebIntentsSettingsHandler::LoadChildren, | |
56 base::Unretained(this))); | |
57 } | |
58 | |
59 void WebIntentsSettingsHandler::TreeNodesAdded(ui::TreeModel* model, | |
60 ui::TreeModelNode* parent, | |
61 int start, | |
62 int count) { | |
63 SendChildren(intents_tree_model_->GetRoot()); | |
64 } | |
65 | |
66 void WebIntentsSettingsHandler::TreeNodesRemoved(ui::TreeModel* model, | |
67 ui::TreeModelNode* parent, | |
68 int start, | |
69 int count) { | |
70 SendChildren(intents_tree_model_->GetRoot()); | |
71 } | |
72 | |
73 void WebIntentsSettingsHandler::TreeModelBeginBatch(WebIntentsModel* model) { | |
74 batch_update_ = true; | |
75 } | |
76 | |
77 void WebIntentsSettingsHandler::TreeModelEndBatch(WebIntentsModel* model) { | |
78 batch_update_ = false; | |
79 | |
80 SendChildren(intents_tree_model_->GetRoot()); | |
81 } | |
82 | |
83 void WebIntentsSettingsHandler::EnsureWebIntentsModelCreated() { | |
84 if (intents_tree_model_.get()) return; | |
85 | |
86 Profile* profile = Profile::FromWebUI(web_ui()); | |
87 web_intents_registry_ = WebIntentsRegistryFactory::GetForProfile(profile); | |
88 intents_tree_model_.reset(new WebIntentsModel(web_intents_registry_)); | |
89 intents_tree_model_->AddWebIntentsTreeObserver(this); | |
90 } | |
91 | |
92 void WebIntentsSettingsHandler::RemoveIntent(const base::ListValue* args) { | |
93 std::string node_path; | |
94 if (!args->GetString(0, &node_path)) { | |
95 return; | |
96 } | |
97 | |
98 EnsureWebIntentsModelCreated(); | |
99 | |
100 WebIntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); | |
101 if (node->Type() == WebIntentsTreeNode::TYPE_ORIGIN) { | |
102 RemoveOrigin(node); | |
103 } else if (node->Type() == WebIntentsTreeNode::TYPE_SERVICE) { | |
104 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(node); | |
105 RemoveService(snode); | |
106 } | |
107 } | |
108 | |
109 void WebIntentsSettingsHandler::RemoveOrigin(WebIntentsTreeNode* node) { | |
110 // TODO(gbillock): This is a known batch update. Worth optimizing? | |
111 while (!node->empty()) { | |
112 WebIntentsTreeNode* cnode = node->GetChild(0); | |
113 CHECK(cnode->Type() == WebIntentsTreeNode::TYPE_SERVICE); | |
114 ServiceTreeNode* snode = static_cast<ServiceTreeNode*>(cnode); | |
115 RemoveService(snode); | |
116 } | |
117 delete intents_tree_model_->Remove(node->parent(), node); | |
118 } | |
119 | |
120 void WebIntentsSettingsHandler::RemoveService(ServiceTreeNode* snode) { | |
121 webkit_glue::WebIntentServiceData service; | |
122 service.service_url = GURL(snode->ServiceUrl()); | |
123 service.action = snode->Action(); | |
124 string16 stype; | |
125 if (snode->Types().GetString(0, &stype)) { | |
126 service.type = stype; // Really need to iterate here. | |
127 } | |
128 service.title = snode->ServiceName(); | |
129 web_intents_registry_->UnregisterIntentService(service); | |
130 delete intents_tree_model_->Remove(snode->parent(), snode); | |
131 } | |
132 | |
133 void WebIntentsSettingsHandler::LoadChildren(const base::ListValue* args) { | |
134 EnsureWebIntentsModelCreated(); | |
135 | |
136 std::string node_path; | |
137 if (!args->GetString(0, &node_path)) { | |
138 SendChildren(intents_tree_model_->GetRoot()); | |
139 return; | |
140 } | |
141 | |
142 WebIntentsTreeNode* node = intents_tree_model_->GetTreeNode(node_path); | |
143 SendChildren(node); | |
144 } | |
145 | |
146 void WebIntentsSettingsHandler::SendChildren(WebIntentsTreeNode* parent) { | |
147 // Early bailout during batch updates. We'll get one after the batch concludes | |
148 // with batch_update_ set false. | |
149 if (batch_update_) return; | |
150 | |
151 ListValue* children = new ListValue; | |
152 intents_tree_model_->GetChildNodeList(parent, 0, parent->child_count(), | |
153 children); | |
154 | |
155 ListValue args; | |
156 args.Append(parent == intents_tree_model_->GetRoot() ? | |
157 Value::CreateNullValue() : | |
158 Value::CreateStringValue(intents_tree_model_->GetTreeNodeId(parent))); | |
159 args.Append(children); | |
160 | |
161 web_ui()->CallJavascriptFunction("IntentsView.loadChildren", args); | |
162 } | |
163 | |
164 } // namespace options | |
OLD | NEW |