OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/ui/webui/voicesearch_ui.h" | 5 #include "chrome/browser/ui/webui/voice_search_ui.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/files/file_enumerator.h" |
| 11 #include "base/memory/weak_ptr.h" |
10 #include "base/metrics/field_trial.h" | 12 #include "base/metrics/field_trial.h" |
11 #include "base/path_service.h" | 13 #include "base/path_service.h" |
12 #include "base/prefs/pref_service.h" | 14 #include "base/prefs/pref_service.h" |
13 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
15 #include "chrome/browser/browser_process.h" | 17 #include "chrome/browser/browser_process.h" |
16 #include "chrome/browser/extensions/extension_service.h" | 18 #include "chrome/browser/extensions/extension_service.h" |
17 #include "chrome/browser/plugins/plugin_prefs.h" | 19 #include "chrome/browser/plugins/plugin_prefs.h" |
18 #include "chrome/browser/profiles/profile.h" | 20 #include "chrome/browser/profiles/profile.h" |
19 #include "chrome/browser/search/hotword_service.h" | 21 #include "chrome/browser/search/hotword_service.h" |
20 #include "chrome/browser/search/hotword_service_factory.h" | 22 #include "chrome/browser/search/hotword_service_factory.h" |
21 #include "chrome/browser/ui/app_list/start_page_service.h" | 23 #include "chrome/browser/ui/app_list/start_page_service.h" |
22 #include "chrome/browser/ui/webui/version_handler.h" | 24 #include "chrome/browser/ui/webui/version_handler.h" |
23 #include "chrome/common/chrome_content_client.h" | 25 #include "chrome/common/chrome_content_client.h" |
24 #include "chrome/common/chrome_paths.h" | 26 #include "chrome/common/chrome_paths.h" |
25 #include "chrome/common/chrome_version_info.h" | 27 #include "chrome/common/chrome_version_info.h" |
26 #include "chrome/common/extensions/extension_constants.h" | 28 #include "chrome/common/extensions/extension_constants.h" |
27 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
28 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
29 #include "chrome/grit/chromium_strings.h" | 31 #include "chrome/grit/chromium_strings.h" |
30 #include "chrome/grit/generated_resources.h" | 32 #include "chrome/grit/generated_resources.h" |
31 #include "chrome/grit/google_chrome_strings.h" | 33 #include "chrome/grit/google_chrome_strings.h" |
| 34 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/plugin_service.h" | 35 #include "content/public/browser/plugin_service.h" |
33 #include "content/public/browser/url_data_source.h" | 36 #include "content/public/browser/url_data_source.h" |
34 #include "content/public/browser/web_ui.h" | 37 #include "content/public/browser/web_ui.h" |
35 #include "content/public/browser/web_ui_data_source.h" | 38 #include "content/public/browser/web_ui_data_source.h" |
36 #include "content/public/browser/web_ui_message_handler.h" | 39 #include "content/public/browser/web_ui_message_handler.h" |
37 #include "content/public/common/user_agent.h" | 40 #include "content/public/common/user_agent.h" |
38 #include "extensions/browser/extension_prefs.h" | 41 #include "extensions/browser/extension_prefs.h" |
39 #include "extensions/browser/extension_system.h" | 42 #include "extensions/browser/extension_system.h" |
40 #include "extensions/common/extension.h" | 43 #include "extensions/common/extension.h" |
41 #include "grit/browser_resources.h" | 44 #include "grit/browser_resources.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 const base::StringPiece& key, | 85 const base::StringPiece& key, |
83 const base::StringPiece& value) { | 86 const base::StringPiece& value) { |
84 AddPair16(list, UTF8ToUTF16(key), UTF8ToUTF16(value)); | 87 AddPair16(list, UTF8ToUTF16(key), UTF8ToUTF16(value)); |
85 } | 88 } |
86 | 89 |
87 // Generate an empty data-pair which acts as a line break. | 90 // Generate an empty data-pair which acts as a line break. |
88 void AddLineBreak(base::ListValue* list) { | 91 void AddLineBreak(base::ListValue* list) { |
89 AddPair(list, "", ""); | 92 AddPair(list, "", ""); |
90 } | 93 } |
91 | 94 |
| 95 void AddSharedModulePlatformsOnFileThread(base::ListValue* list, |
| 96 const base::FilePath& path, |
| 97 base::Closure callback) { |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::FILE); |
| 99 |
| 100 if (!path.empty()) { |
| 101 // Display available platforms for shared module. |
| 102 base::FilePath platforms_path = path.AppendASCII("_platform_specific"); |
| 103 base::FileEnumerator enumerator( |
| 104 platforms_path, false, base::FileEnumerator::DIRECTORIES); |
| 105 base::string16 files; |
| 106 for (base::FilePath name = enumerator.Next(); |
| 107 !name.empty(); |
| 108 name = enumerator.Next()) { |
| 109 files += name.BaseName().LossyDisplayName() + ASCIIToUTF16(" "); |
| 110 } |
| 111 AddPair16(list, |
| 112 ASCIIToUTF16("Shared Module Platforms"), |
| 113 files.empty() ? ASCIIToUTF16("undefined") : files); |
| 114 AddLineBreak(list); |
| 115 } |
| 116 |
| 117 content::BrowserThread::PostTask(content::BrowserThread::UI, |
| 118 FROM_HERE, |
| 119 callback); |
| 120 } |
| 121 |
92 //////////////////////////////////////////////////////////////////////////////// | 122 //////////////////////////////////////////////////////////////////////////////// |
93 // | 123 // |
94 // VoiceSearchDomHandler | 124 // VoiceSearchDomHandler |
95 // | 125 // |
96 //////////////////////////////////////////////////////////////////////////////// | 126 //////////////////////////////////////////////////////////////////////////////// |
97 | 127 |
98 // The handler for Javascript messages for the about:flags page. | 128 // The handler for Javascript messages for the about:flags page. |
99 class VoiceSearchDomHandler : public WebUIMessageHandler { | 129 class VoiceSearchDomHandler : public WebUIMessageHandler { |
100 public: | 130 public: |
101 explicit VoiceSearchDomHandler(Profile* profile) : profile_(profile) {} | 131 explicit VoiceSearchDomHandler(Profile* profile) |
| 132 : profile_(profile), |
| 133 weak_factory_(this) {} |
102 | 134 |
103 ~VoiceSearchDomHandler() override {} | 135 ~VoiceSearchDomHandler() override {} |
104 | 136 |
105 // WebUIMessageHandler implementation. | 137 // WebUIMessageHandler implementation. |
106 void RegisterMessages() override { | 138 void RegisterMessages() override { |
107 web_ui()->RegisterMessageCallback( | 139 web_ui()->RegisterMessageCallback( |
108 "requestVoiceSearchInfo", | 140 "requestVoiceSearchInfo", |
109 base::Bind(&VoiceSearchDomHandler::HandleRequestVoiceSearchInfo, | 141 base::Bind(&VoiceSearchDomHandler::HandleRequestVoiceSearchInfo, |
110 base::Unretained(this))); | 142 base::Unretained(this))); |
111 } | 143 } |
112 | 144 |
113 private: | 145 private: |
114 // Callback for the "requestVoiceSearchInfo" message. No arguments. | 146 // Callback for the "requestVoiceSearchInfo" message. No arguments. |
115 void HandleRequestVoiceSearchInfo(const base::ListValue* args) { | 147 void HandleRequestVoiceSearchInfo(const base::ListValue* args) { |
| 148 PopulatePageInformation(); |
| 149 } |
| 150 |
| 151 void ReturnVoiceSearchInfo(scoped_ptr<base::ListValue> info) { |
| 152 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 153 DCHECK(info); |
116 base::DictionaryValue voiceSearchInfo; | 154 base::DictionaryValue voiceSearchInfo; |
117 PopulatePageInformation(&voiceSearchInfo); | 155 voiceSearchInfo.Set("voiceSearchInfo", info.release()); |
118 web_ui()->CallJavascriptFunction("returnVoiceSearchInfo", | 156 web_ui()->CallJavascriptFunction("returnVoiceSearchInfo", voiceSearchInfo); |
119 voiceSearchInfo); | |
120 } | 157 } |
121 | 158 |
122 // Fill in the data to be displayed on the page. | 159 // Fill in the data to be displayed on the page. |
123 void PopulatePageInformation(base::DictionaryValue* voiceSearchInfo) { | 160 void PopulatePageInformation() { |
124 // Store Key-Value pairs of about-information. | 161 // Store Key-Value pairs of about-information. |
125 scoped_ptr<base::ListValue> list(new base::ListValue()); | 162 scoped_ptr<base::ListValue> list(new base::ListValue()); |
126 | 163 |
127 // Populate information. | 164 // Populate information. |
128 AddOperatingSystemInfo(list.get()); | 165 AddOperatingSystemInfo(list.get()); |
129 AddAudioInfo(list.get()); | 166 AddAudioInfo(list.get()); |
130 AddLanguageInfo(list.get()); | 167 AddLanguageInfo(list.get()); |
131 AddHotwordInfo(list.get()); | 168 AddHotwordInfo(list.get()); |
| 169 AddAppListInfo(list.get()); |
132 | 170 |
133 std::string extension_id = extension_misc::kHotwordExtensionId; | 171 std::string extension_id = extension_misc::kHotwordExtensionId; |
134 HotwordService* hotword_service = | 172 HotwordService* hotword_service = |
135 HotwordServiceFactory::GetForProfile(profile_); | 173 HotwordServiceFactory::GetForProfile(profile_); |
136 if (hotword_service && hotword_service->IsExperimentalHotwordingEnabled()) | 174 if (hotword_service && hotword_service->IsExperimentalHotwordingEnabled()) |
137 extension_id = extension_misc::kHotwordNewExtensionId; | 175 extension_id = extension_misc::kHotwordNewExtensionId; |
138 AddExtensionInfo(extension_id, "Extension", list.get()); | 176 AddExtensionInfo(extension_id, "Extension", list.get()); |
139 | 177 |
140 AddExtensionInfo(extension_misc::kHotwordSharedModuleId, | 178 AddExtensionInfo(extension_misc::kHotwordSharedModuleId, |
141 "Shared Module", | 179 "Shared Module", |
142 list.get()); | 180 list.get()); |
143 AddAppListInfo(list.get()); | |
144 | 181 |
145 // voiceSearchInfo will take ownership of list, and clean it up on | 182 base::FilePath path; |
146 // destruction. | 183 extensions::ExtensionSystem* extension_system = |
147 voiceSearchInfo->Set("voiceSearchInfo", list.release()); | 184 extensions::ExtensionSystem::Get(profile_); |
| 185 if (extension_system) { |
| 186 ExtensionService* extension_service = |
| 187 extension_system->extension_service(); |
| 188 const extensions::Extension* extension = |
| 189 extension_service->GetExtensionById( |
| 190 extension_misc::kHotwordSharedModuleId, true); |
| 191 if (extension) |
| 192 path = extension->path(); |
| 193 } |
| 194 base::ListValue* raw_list = list.get(); |
| 195 content::BrowserThread::PostTask( |
| 196 content::BrowserThread::FILE, |
| 197 FROM_HERE, |
| 198 base::Bind( |
| 199 &AddSharedModulePlatformsOnFileThread, |
| 200 raw_list, |
| 201 path, |
| 202 base::Bind(&VoiceSearchDomHandler::ReturnVoiceSearchInfo, |
| 203 weak_factory_.GetWeakPtr(), |
| 204 base::Passed(list.Pass())))); |
148 } | 205 } |
149 | 206 |
150 // Adds information regarding the system and chrome version info to list. | 207 // Adds information regarding the system and chrome version info to list. |
151 void AddOperatingSystemInfo(base::ListValue* list) { | 208 void AddOperatingSystemInfo(base::ListValue* list) { |
152 // Obtain the Chrome version info. | 209 // Obtain the Chrome version info. |
153 chrome::VersionInfo version_info; | 210 chrome::VersionInfo version_info; |
154 AddPair(list, | 211 AddPair(list, |
155 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME), | 212 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME), |
156 version_info.Version() + " (" + | 213 version_info.Version() + " (" + |
157 chrome::VersionInfo::GetVersionStringModifier() + ")"); | 214 chrome::VersionInfo::GetVersionStringModifier() + ")"); |
(...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
359 state = "SPEECH_RECOGNITION_STOPPING"; | 416 state = "SPEECH_RECOGNITION_STOPPING"; |
360 break; | 417 break; |
361 case app_list::SPEECH_RECOGNITION_NETWORK_ERROR: | 418 case app_list::SPEECH_RECOGNITION_NETWORK_ERROR: |
362 state = "SPEECH_RECOGNITION_NETWORK_ERROR"; | 419 state = "SPEECH_RECOGNITION_NETWORK_ERROR"; |
363 break; | 420 break; |
364 default: | 421 default: |
365 state = "undefined"; | 422 state = "undefined"; |
366 } | 423 } |
367 } | 424 } |
368 AddPair(list, "Start Page State", state); | 425 AddPair(list, "Start Page State", state); |
| 426 AddLineBreak(list); |
369 #endif | 427 #endif |
370 } | 428 } |
371 | 429 |
372 Profile* profile_; | 430 Profile* profile_; |
| 431 base::WeakPtrFactory<VoiceSearchDomHandler> weak_factory_; |
373 | 432 |
374 DISALLOW_COPY_AND_ASSIGN(VoiceSearchDomHandler); | 433 DISALLOW_COPY_AND_ASSIGN(VoiceSearchDomHandler); |
375 }; | 434 }; |
376 | 435 |
377 } // namespace | 436 } // namespace |
378 | 437 |
379 /////////////////////////////////////////////////////////////////////////////// | 438 /////////////////////////////////////////////////////////////////////////////// |
380 // | 439 // |
381 // VoiceSearchUI | 440 // VoiceSearchUI |
382 // | 441 // |
383 /////////////////////////////////////////////////////////////////////////////// | 442 /////////////////////////////////////////////////////////////////////////////// |
384 | 443 |
385 VoiceSearchUI::VoiceSearchUI(content::WebUI* web_ui) | 444 VoiceSearchUI::VoiceSearchUI(content::WebUI* web_ui) |
386 : content::WebUIController(web_ui) { | 445 : content::WebUIController(web_ui) { |
387 Profile* profile = Profile::FromWebUI(web_ui); | 446 Profile* profile = Profile::FromWebUI(web_ui); |
388 web_ui->AddMessageHandler(new VoiceSearchDomHandler(profile)); | 447 web_ui->AddMessageHandler(new VoiceSearchDomHandler(profile)); |
389 | 448 |
390 // Set up the about:voicesearch source. | 449 // Set up the about:voicesearch source. |
391 content::WebUIDataSource::Add(profile, CreateVoiceSearchUiHtmlSource()); | 450 content::WebUIDataSource::Add(profile, CreateVoiceSearchUiHtmlSource()); |
392 } | 451 } |
393 | 452 |
394 VoiceSearchUI::~VoiceSearchUI() {} | 453 VoiceSearchUI::~VoiceSearchUI() {} |
OLD | NEW |