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" |
13 #include "chrome/browser/intents/api_key.h" | 15 #include "chrome/browser/intents/api_key.h" |
14 #include "chrome/browser/net/chrome_url_request_context.h" | 16 #include "chrome/browser/net/chrome_url_request_context.h" |
15 #include "chrome/browser/webdata/web_data_service.h" | 17 #include "chrome/browser/webdata/web_data_service.h" |
16 #include "chrome/common/net/url_util.h" | 18 #include "chrome/common/net/url_util.h" |
17 #include "net/base/load_flags.h" | 19 #include "net/base/load_flags.h" |
18 #include "net/base/mime_util.h" | 20 #include "net/base/mime_util.h" |
19 #include "net/url_request/url_fetcher.h" | 21 #include "net/url_request/url_fetcher.h" |
20 | 22 |
21 namespace { | 23 namespace { |
22 | 24 |
23 // Limit for the number of suggestions we fix from CWS. Ideally, the registry | 25 // Limit for the number of suggestions we fix from CWS. Ideally, the registry |
24 // simply get all of them, but there is a) chunking on the CWS side, and b) | 26 // simply get all of them, but there is a) chunking on the CWS side, and b) |
25 // there is a cost with suggestions fetched. (Network overhead for favicons, | 27 // there is a cost with suggestions fetched. (Network overhead for favicons, |
26 // roundtrips to registry to check if installed). | 28 // roundtrips to registry to check if installed). |
27 // | 29 // |
28 // Since the picker limits the number of suggestions displayed to 5, 15 means | 30 // Since the picker limits the number of suggestions displayed to 5, 15 means |
29 // the suggestion list only has the potential to be shorter than that once the | 31 // the suggestion list only has the potential to be shorter than that once the |
30 // user has at least 10 installed handlers for the particular action/type. | 32 // user has at least 10 installed handlers for the particular action/type. |
31 // | 33 // |
32 // TODO(groby): Adopt number of suggestions dynamically so the picker can | 34 // TODO(groby): Adopt number of suggestions dynamically so the picker can |
33 // always display 5 suggestions unless there are less than 5 viable extensions | 35 // always display 5 suggestions unless there are less than 5 viable extensions |
34 // in the CWS. | 36 // in the CWS. |
35 const char kMaxSuggestions[] = "15"; | 37 const char kMaxSuggestions[] = "15"; |
36 | 38 |
37 // URL for CWS intents API. | 39 // URL for CWS intents API. |
38 const char kCWSIntentServiceURL[] = | 40 const char kCWSIntentServiceURL[] = |
39 "https://www.googleapis.com/chromewebstore/v1.1b/items/intent"; | 41 "https://www.googleapis.com/chromewebstore/v1.1b/items/intent"; |
40 | 42 |
| 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 |
41 // Parses a JSON |response| from the CWS into a list of suggested extensions, | 54 // Parses a JSON |response| from the CWS into a list of suggested extensions, |
42 // stored in |intents|. |intents| must not be NULL. | 55 // stored in |intents|. |intents| must not be NULL. |
43 void ParseResponse(const std::string& response, | 56 void ParseResponse(const std::string& response, |
44 CWSIntentsRegistry::IntentExtensionList* intents) { | 57 CWSIntentsRegistry::IntentExtensionList* intents) { |
45 std::string error; | 58 std::string error; |
46 scoped_ptr<Value> parsed_response; | 59 scoped_ptr<Value> parsed_response; |
47 JSONStringValueSerializer serializer(response); | 60 JSONStringValueSerializer serializer(response); |
48 parsed_response.reset(serializer.Deserialize(NULL, &error)); | 61 parsed_response.reset(serializer.Deserialize(NULL, &error)); |
49 if (parsed_response.get() == NULL) | 62 if (parsed_response.get() == NULL) |
50 return; | 63 return; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
85 DictionaryValue* manifest_dict; | 98 DictionaryValue* manifest_dict; |
86 if (!manifest_value->GetAsDictionary(&manifest_dict) || | 99 if (!manifest_value->GetAsDictionary(&manifest_dict) || |
87 !manifest_dict->GetString("name", &info.name)) | 100 !manifest_dict->GetString("name", &info.name)) |
88 continue; | 101 continue; |
89 | 102 |
90 string16 url_string; | 103 string16 url_string; |
91 if (!item->GetString("icon_url", &url_string)) | 104 if (!item->GetString("icon_url", &url_string)) |
92 continue; | 105 continue; |
93 info.icon_url = GURL(url_string); | 106 info.icon_url = GURL(url_string); |
94 | 107 |
| 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 |
95 intents->push_back(info); | 140 intents->push_back(info); |
96 } | 141 } |
97 } | 142 } |
98 | 143 |
99 } // namespace | 144 } // namespace |
100 | 145 |
101 // Internal object representing all data associated with a single query. | 146 // Internal object representing all data associated with a single query. |
102 struct CWSIntentsRegistry::IntentsQuery { | 147 struct CWSIntentsRegistry::IntentsQuery { |
103 IntentsQuery(); | 148 IntentsQuery(); |
104 ~IntentsQuery(); | 149 ~IntentsQuery(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 "0"); | 233 "0"); |
189 request = chrome_common_net::AppendQueryParameter(request, "num_results", | 234 request = chrome_common_net::AppendQueryParameter(request, "num_results", |
190 kMaxSuggestions); | 235 kMaxSuggestions); |
191 if (web_intents::kApiKey[0]) { | 236 if (web_intents::kApiKey[0]) { |
192 request = chrome_common_net::AppendQueryParameter(request, "key", | 237 request = chrome_common_net::AppendQueryParameter(request, "key", |
193 web_intents::kApiKey); | 238 web_intents::kApiKey); |
194 } | 239 } |
195 | 240 |
196 return request; | 241 return request; |
197 } | 242 } |
OLD | NEW |