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/extension_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(ExtensionMessageBundle::kMessageBegin); | |
47 if (index == std::string::npos) | |
48 return false; | |
49 | |
50 index = value.find(ExtensionMessageBundle::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 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
84 | 97 |
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 |
Greg Billock
2012/07/20 15:58:30
Can you add a comment about why we need this block
groby-ooo-7-16
2012/07/20 22:59:54
Done.
| |
108 ListValue* all_locales = NULL; | |
109 if (ShouldLocalize(UTF16ToUTF8(info.name)) && | |
110 item->GetList("locale_data", &all_locales)) { | |
111 std::map<std::string, std::string> localized_title; | |
112 | |
113 for (ListValue::const_iterator locale_iter(all_locales->begin()); | |
114 locale_iter != all_locales->end(); ++locale_iter) { | |
115 DictionaryValue* locale = static_cast<DictionaryValue*>(*locale_iter); | |
116 | |
117 std::string locale_id, title; | |
118 if (!locale->GetString("locale_string", &locale_id) || | |
119 !locale->GetString("title", &title)) | |
Greg Billock
2012/07/20 15:58:30
So this isn't the normal place, so the extension l
groby-ooo-7-16
2012/07/20 22:59:54
That is correct. There's also the point that the C
| |
120 continue; | |
121 | |
122 localized_title[locale_id] = title; | |
123 } | |
124 | |
125 std::vector<std::string> valid_locales; | |
Greg Billock
2012/07/20 15:58:30
Can you just use vector<string> here and below? Th
groby-ooo-7-16
2012/07/20 22:59:54
As far as I can tell, we always prefix.
On 2012/0
| |
126 extension_l10n_util::GetAllFallbackLocales( | |
127 extension_l10n_util::CurrentLocaleOrDefault(), | |
Greg Billock
2012/07/20 15:58:30
Shouldn't the util class compose these default/cur
groby-ooo-7-16
2012/07/20 22:59:54
No, because the util class is shared with extensio
| |
128 "all", | |
129 &valid_locales); | |
130 for (std::vector<std::string>::iterator iter = valid_locales.begin(); | |
131 iter != valid_locales.end(); ++iter) { | |
132 if (localized_title.count(*iter)) { | |
133 info.name = UTF8ToUTF16(localized_title[*iter]); | |
134 break; | |
135 } | |
136 } | |
137 } | |
138 | |
95 intents->push_back(info); | 139 intents->push_back(info); |
96 } | 140 } |
97 } | 141 } |
98 | 142 |
99 } // namespace | 143 } // namespace |
100 | 144 |
101 // Internal object representing all data associated with a single query. | 145 // Internal object representing all data associated with a single query. |
102 struct CWSIntentsRegistry::IntentsQuery { | 146 struct CWSIntentsRegistry::IntentsQuery { |
103 IntentsQuery(); | 147 IntentsQuery(); |
104 ~IntentsQuery(); | 148 ~IntentsQuery(); |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 "0"); | 232 "0"); |
189 request = chrome_common_net::AppendQueryParameter(request, "num_results", | 233 request = chrome_common_net::AppendQueryParameter(request, "num_results", |
190 kMaxSuggestions); | 234 kMaxSuggestions); |
191 if (web_intents::kApiKey[0]) { | 235 if (web_intents::kApiKey[0]) { |
192 request = chrome_common_net::AppendQueryParameter(request, "key", | 236 request = chrome_common_net::AppendQueryParameter(request, "key", |
193 web_intents::kApiKey); | 237 web_intents::kApiKey); |
194 } | 238 } |
195 | 239 |
196 return request; | 240 return request; |
197 } | 241 } |
OLD | NEW |