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/options2/web_intents_defaults_handler.h" | |
6 | |
7 #include <map> | |
8 #include <string> | |
9 #include <vector> | |
10 #include "base/bind.h" | |
11 #include "base/string16.h" | |
12 #include "base/string_util.h" | |
13 #include "base/utf_string_conversions.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/common/chrome_notification_types.h" | |
18 #include "content/public/browser/notification_details.h" | |
19 #include "content/public/browser/user_metrics.h" | |
20 #include "content/public/browser/web_contents.h" | |
21 #include "content/public/browser/web_contents_view.h" | |
22 #include "content/public/browser/web_ui.h" | |
23 #include "grit/generated_resources.h" | |
24 | |
25 namespace { | |
26 | |
27 typedef std::vector<string16> Vector16; | |
28 typedef std::map<std::string, Vector16> ServiceMap; | |
29 | |
30 void ServicesToListValues( | |
31 const std::vector<DefaultWebIntentService>& services, | |
32 ListValue* values) { | |
33 | |
34 // map of urls to list of "matchers" (action + type) | |
35 ServiceMap service_map; | |
36 WebIntentsRegistry::DefaultIntentServiceList::const_iterator service; | |
37 for (service = services.begin(); service != services.end(); ++service) { | |
38 string16 matcher = service->action; | |
39 matcher += ASCIIToUTF16(" "); | |
40 matcher += service->type; | |
41 service_map[service->service_url].push_back(matcher); | |
42 } | |
43 | |
44 // showing contents: | |
45 ServiceMap::iterator it; | |
46 for (it = service_map.begin(); it != service_map.end(); ++it) { | |
47 ListValue* value = new ListValue; | |
48 std::string service_url = (*it).first; | |
49 std::vector<string16> matchers = (*it).second; | |
50 // TODO(smckay): add join method that works with strings | |
51 // so we can join on ", ". | |
52 string16 joined_matchers = JoinString(matchers, ','); | |
53 value->Append(Value::CreateStringValue(service_url)); | |
54 value->Append(Value::CreateStringValue(joined_matchers)); | |
55 values->Append(value); | |
56 } | |
57 } | |
58 } // namespace | |
59 | |
60 namespace options2 { | |
61 | |
62 WebIntentsDefaultsHandler::WebIntentsDefaultsHandler() { | |
63 } | |
64 | |
65 WebIntentsDefaultsHandler::~WebIntentsDefaultsHandler() { | |
66 } | |
67 | |
68 void WebIntentsDefaultsHandler::GetLocalizedValues(DictionaryValue* values) { | |
69 DCHECK(values); | |
70 static OptionsStringResource resources[] = { | |
71 { "web_intents_defaults_button_title", | |
72 IDS_WEB_INTENTS_DEFAULTS_BUTTON_TITLE }, | |
73 {"web_intents_defaults_overlay_title", | |
74 IDS_WEB_INTENTS_DEFAULTS_OVERLAY_TITLE }, | |
75 {"web_intents_defaults_matcher_column", | |
76 IDS_WEB_INTENTS_DEFAULTS_MATCHER_COLUMN }, | |
77 {"web_intents_defaults_title_column", | |
78 IDS_WEB_INTENTS_DEFAULTS_TITLE_COLUMN }, | |
csilv
2012/08/09 18:18:04
per previous comment, camelCase for i18n keys.
| |
79 }; | |
80 | |
81 RegisterStrings(values, resources, arraysize(resources)); | |
82 RegisterTitle(values, "web_intents_defaults_overlay_title", | |
83 IDS_WEB_INTENTS_DEFAULTS_OVERLAY_TITLE); | |
84 } | |
85 | |
86 void WebIntentsDefaultsHandler::InitializeHandler() { | |
87 // TODO: plug into notifications to monitor registry changes. | |
csilv
2012/08/09 18:18:04
nit: TODO(smckay)
| |
88 } | |
89 | |
90 void WebIntentsDefaultsHandler::InitializePage() { | |
91 UpdateServiceList(); | |
92 } | |
93 | |
94 void WebIntentsDefaultsHandler::RegisterMessages() { | |
95 web_ui()->RegisterMessageCallback( | |
96 "removeServiceDefaults", | |
97 base::Bind(&WebIntentsDefaultsHandler::HandleRemoveServiceDefaults, | |
98 base::Unretained(this))); | |
99 } | |
100 | |
101 void WebIntentsDefaultsHandler::UpdateServiceList() { | |
102 GetRegistry()->GetAllDefaultIntentServices( | |
103 base::Bind(&WebIntentsDefaultsHandler::OnServicesLoaded, | |
104 base::Unretained(this))); | |
105 } | |
106 | |
107 void WebIntentsDefaultsHandler::OnServicesLoaded( | |
108 const std::vector<DefaultWebIntentService>& services) { | |
109 | |
csilv
2012/08/09 18:18:04
nit: bikesheding, i'm not crazy about these newlin
| |
110 ListValue values; | |
111 ServicesToListValues(services, &values); | |
112 web_ui()->CallJavascriptFunction("WebIntentsDefaults.setServices", values); | |
113 } | |
114 | |
115 void WebIntentsDefaultsHandler::HandleRemoveServiceDefaults( | |
116 const base::ListValue* args) { | |
117 | |
118 content::RecordAction( | |
119 content::UserMetricsAction("RemoveWebIntentServiceDefaults")); | |
120 std::string url; | |
121 bool ok = args->GetString(0, &url); | |
122 DCHECK(ok); | |
123 | |
124 const GURL service_url(url); | |
125 // GetRegistry()->UnregisterServiceDefaults(service_url); | |
csilv
2012/08/09 18:18:04
why is this commented out..?
| |
126 | |
127 // TODO(smckay): dependon on registry change notifications to do this. | |
128 UpdateServiceList(); | |
129 } | |
130 | |
131 WebIntentsRegistry* WebIntentsDefaultsHandler::GetRegistry() { | |
132 Profile* profile = Profile::FromWebUI(web_ui()); | |
133 return WebIntentsRegistryFactory::GetForProfile(profile); | |
134 } | |
135 | |
136 void WebIntentsDefaultsHandler::Observe( | |
137 int type, | |
138 const content::NotificationSource& source, | |
139 const content::NotificationDetails& details) { | |
140 } | |
141 | |
142 } // namespace options2 | |
OLD | NEW |