OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/intents/cws_intents_registry.h" | 5 #include "chrome/browser/intents/cws_intents_registry.h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
11 #include "base/string16.h" | 11 #include "base/string16.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "chrome/common/extensions/extension_l10n_util.h" | |
14 #include "chrome/common/extensions/message_bundle.h" | |
15 #include "chrome/browser/intents/api_key.h" | 13 #include "chrome/browser/intents/api_key.h" |
16 #include "chrome/browser/net/chrome_url_request_context.h" | 14 #include "chrome/browser/net/chrome_url_request_context.h" |
17 #include "chrome/browser/webdata/web_data_service.h" | 15 #include "chrome/browser/webdata/web_data_service.h" |
18 #include "chrome/common/net/url_util.h" | 16 #include "chrome/common/net/url_util.h" |
19 #include "net/base/load_flags.h" | 17 #include "net/base/load_flags.h" |
20 #include "net/base/mime_util.h" | 18 #include "net/base/mime_util.h" |
21 #include "net/url_request/url_fetcher.h" | 19 #include "net/url_request/url_fetcher.h" |
22 | 20 |
23 namespace { | 21 namespace { |
24 | 22 |
25 // Limit for the number of suggestions we fix from CWS. Ideally, the registry | 23 // Limit for the number of suggestions we fix from CWS. Ideally, the registry |
26 // simply get all of them, but there is a) chunking on the CWS side, and b) | 24 // simply get all of them, but there is a) chunking on the CWS side, and b) |
27 // there is a cost with suggestions fetched. (Network overhead for favicons, | 25 // there is a cost with suggestions fetched. (Network overhead for favicons, |
28 // roundtrips to registry to check if installed). | 26 // roundtrips to registry to check if installed). |
29 // | 27 // |
30 // Since the picker limits the number of suggestions displayed to 5, 15 means | 28 // Since the picker limits the number of suggestions displayed to 5, 15 means |
31 // the suggestion list only has the potential to be shorter than that once the | 29 // the suggestion list only has the potential to be shorter than that once the |
32 // user has at least 10 installed handlers for the particular action/type. | 30 // user has at least 10 installed handlers for the particular action/type. |
33 // | 31 // |
34 // TODO(groby): Adopt number of suggestions dynamically so the picker can | 32 // TODO(groby): Adopt number of suggestions dynamically so the picker can |
35 // always display 5 suggestions unless there are less than 5 viable extensions | 33 // always display 5 suggestions unless there are less than 5 viable extensions |
36 // in the CWS. | 34 // in the CWS. |
37 const char kMaxSuggestions[] = "15"; | 35 const char kMaxSuggestions[] = "15"; |
38 | 36 |
39 // URL for CWS intents API. | 37 // URL for CWS intents API. |
40 const char kCWSIntentServiceURL[] = | 38 const char kCWSIntentServiceURL[] = |
41 "https://www.googleapis.com/chromewebstore/v1.1b/items/intent"; | 39 "https://www.googleapis.com/chromewebstore/v1.1b/items/intent"; |
42 | 40 |
43 // Determines if a string is a candidate for localization. | |
44 bool ShouldLocalize(const std::string& value) { | |
45 std::string::size_type index = 0; | |
46 index = value.find(extensions::MessageBundle::kMessageBegin); | |
47 if (index == std::string::npos) | |
48 return false; | |
49 | |
50 index = value.find(extensions::MessageBundle::kMessageEnd, index); | |
51 return (index != std::string::npos); | |
52 } | |
53 | |
54 // Parses a JSON |response| from the CWS into a list of suggested extensions, | 41 // Parses a JSON |response| from the CWS into a list of suggested extensions, |
55 // stored in |intents|. |intents| must not be NULL. | 42 // stored in |intents|. |intents| must not be NULL. |
56 void ParseResponse(const std::string& response, | 43 void ParseResponse(const std::string& response, |
57 CWSIntentsRegistry::IntentExtensionList* intents) { | 44 CWSIntentsRegistry::IntentExtensionList* intents) { |
58 std::string error; | 45 std::string error; |
59 scoped_ptr<Value> parsed_response; | 46 scoped_ptr<Value> parsed_response; |
60 JSONStringValueSerializer serializer(response); | 47 JSONStringValueSerializer serializer(response); |
61 parsed_response.reset(serializer.Deserialize(NULL, &error)); | 48 parsed_response.reset(serializer.Deserialize(NULL, &error)); |
62 if (parsed_response.get() == NULL) | 49 if (parsed_response.get() == NULL) |
63 return; | 50 return; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
98 DictionaryValue* manifest_dict; | 85 DictionaryValue* manifest_dict; |
99 if (!manifest_value->GetAsDictionary(&manifest_dict) || | 86 if (!manifest_value->GetAsDictionary(&manifest_dict) || |
100 !manifest_dict->GetString("name", &info.name)) | 87 !manifest_dict->GetString("name", &info.name)) |
101 continue; | 88 continue; |
102 | 89 |
103 string16 url_string; | 90 string16 url_string; |
104 if (!item->GetString("icon_url", &url_string)) | 91 if (!item->GetString("icon_url", &url_string)) |
105 continue; | 92 continue; |
106 info.icon_url = GURL(url_string); | 93 info.icon_url = GURL(url_string); |
107 | 94 |
108 // Need to parse CWS reply, since it is not pre-l10n'd. | |
109 ListValue* all_locales = NULL; | |
110 if (ShouldLocalize(UTF16ToUTF8(info.name)) && | |
111 item->GetList("locale_data", &all_locales)) { | |
112 std::map<std::string, std::string> localized_title; | |
113 | |
114 for (ListValue::const_iterator locale_iter(all_locales->begin()); | |
115 locale_iter != all_locales->end(); ++locale_iter) { | |
116 DictionaryValue* locale = static_cast<DictionaryValue*>(*locale_iter); | |
117 | |
118 std::string locale_id, title; | |
119 if (!locale->GetString("locale_string", &locale_id) || | |
120 !locale->GetString("title", &title)) | |
121 continue; | |
122 | |
123 localized_title[locale_id] = title; | |
124 } | |
125 | |
126 std::vector<std::string> valid_locales; | |
127 extension_l10n_util::GetAllFallbackLocales( | |
128 extension_l10n_util::CurrentLocaleOrDefault(), | |
129 "all", | |
130 &valid_locales); | |
131 for (std::vector<std::string>::iterator iter = valid_locales.begin(); | |
132 iter != valid_locales.end(); ++iter) { | |
133 if (localized_title.count(*iter)) { | |
134 info.name = UTF8ToUTF16(localized_title[*iter]); | |
135 break; | |
136 } | |
137 } | |
138 } | |
139 | |
140 intents->push_back(info); | 95 intents->push_back(info); |
141 } | 96 } |
142 } | 97 } |
143 | 98 |
144 } // namespace | 99 } // namespace |
145 | 100 |
146 // Internal object representing all data associated with a single query. | 101 // Internal object representing all data associated with a single query. |
147 struct CWSIntentsRegistry::IntentsQuery { | 102 struct CWSIntentsRegistry::IntentsQuery { |
148 IntentsQuery(); | 103 IntentsQuery(); |
149 ~IntentsQuery(); | 104 ~IntentsQuery(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 "0"); | 188 "0"); |
234 request = chrome_common_net::AppendQueryParameter(request, "num_results", | 189 request = chrome_common_net::AppendQueryParameter(request, "num_results", |
235 kMaxSuggestions); | 190 kMaxSuggestions); |
236 if (web_intents::kApiKey[0]) { | 191 if (web_intents::kApiKey[0]) { |
237 request = chrome_common_net::AppendQueryParameter(request, "key", | 192 request = chrome_common_net::AppendQueryParameter(request, "key", |
238 web_intents::kApiKey); | 193 web_intents::kApiKey); |
239 } | 194 } |
240 | 195 |
241 return request; | 196 return request; |
242 } | 197 } |
OLD | NEW |