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