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