Chromium Code Reviews| 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/voicesearch_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() ? | |
| 114 ASCIIToUTF16("undefined") : files); | |
|
rpetterson
2015/01/05 03:57:30
move to the line above or indent
Anand Mistry (off Chromium)
2015/01/05 04:11:48
Done.
| |
| 115 AddLineBreak(list); | |
| 116 } | |
| 117 | |
| 118 content::BrowserThread::PostTask(content::BrowserThread::UI, | |
| 119 FROM_HERE, | |
| 120 callback); | |
| 121 } | |
| 122 | |
| 92 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
| 93 // | 124 // |
| 94 // VoiceSearchDomHandler | 125 // VoiceSearchDomHandler |
| 95 // | 126 // |
| 96 //////////////////////////////////////////////////////////////////////////////// | 127 //////////////////////////////////////////////////////////////////////////////// |
| 97 | 128 |
| 98 // The handler for Javascript messages for the about:flags page. | 129 // The handler for Javascript messages for the about:flags page. |
| 99 class VoiceSearchDomHandler : public WebUIMessageHandler { | 130 class VoiceSearchDomHandler : public WebUIMessageHandler { |
| 100 public: | 131 public: |
| 101 explicit VoiceSearchDomHandler(Profile* profile) : profile_(profile) {} | 132 explicit VoiceSearchDomHandler(Profile* profile) |
| 133 : profile_(profile), | |
| 134 weak_factory_(this) {} | |
| 102 | 135 |
| 103 ~VoiceSearchDomHandler() override {} | 136 ~VoiceSearchDomHandler() override {} |
| 104 | 137 |
| 105 // WebUIMessageHandler implementation. | 138 // WebUIMessageHandler implementation. |
| 106 void RegisterMessages() override { | 139 void RegisterMessages() override { |
| 107 web_ui()->RegisterMessageCallback( | 140 web_ui()->RegisterMessageCallback( |
| 108 "requestVoiceSearchInfo", | 141 "requestVoiceSearchInfo", |
| 109 base::Bind(&VoiceSearchDomHandler::HandleRequestVoiceSearchInfo, | 142 base::Bind(&VoiceSearchDomHandler::HandleRequestVoiceSearchInfo, |
| 110 base::Unretained(this))); | 143 base::Unretained(this))); |
| 111 } | 144 } |
| 112 | 145 |
| 113 private: | 146 private: |
| 114 // Callback for the "requestVoiceSearchInfo" message. No arguments. | 147 // Callback for the "requestVoiceSearchInfo" message. No arguments. |
| 115 void HandleRequestVoiceSearchInfo(const base::ListValue* args) { | 148 void HandleRequestVoiceSearchInfo(const base::ListValue* args) { |
| 149 PopulatePageInformation(); | |
| 150 } | |
| 151 | |
| 152 void ReturnVoiceSearchInfo(scoped_ptr<base::ListValue> info) { | |
| 153 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 154 DCHECK(info); | |
| 116 base::DictionaryValue voiceSearchInfo; | 155 base::DictionaryValue voiceSearchInfo; |
| 117 PopulatePageInformation(&voiceSearchInfo); | 156 // voiceSearchInfo will take ownership of |info|, and clean it up on |
| 118 web_ui()->CallJavascriptFunction("returnVoiceSearchInfo", | 157 // destruction. |
| 119 voiceSearchInfo); | 158 voiceSearchInfo.Set("voiceSearchInfo", info.release()); |
| 159 web_ui()->CallJavascriptFunction("returnVoiceSearchInfo", voiceSearchInfo); | |
| 120 } | 160 } |
| 121 | 161 |
| 122 // Fill in the data to be displayed on the page. | 162 // Fill in the data to be displayed on the page. |
| 123 void PopulatePageInformation(base::DictionaryValue* voiceSearchInfo) { | 163 void PopulatePageInformation() { |
| 124 // Store Key-Value pairs of about-information. | 164 // Store Key-Value pairs of about-information. |
| 125 scoped_ptr<base::ListValue> list(new base::ListValue()); | 165 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 126 | 166 |
| 127 // Populate information. | 167 // Populate information. |
| 128 AddOperatingSystemInfo(list.get()); | 168 AddOperatingSystemInfo(list.get()); |
| 129 AddAudioInfo(list.get()); | 169 AddAudioInfo(list.get()); |
| 130 AddLanguageInfo(list.get()); | 170 AddLanguageInfo(list.get()); |
| 131 AddHotwordInfo(list.get()); | 171 AddHotwordInfo(list.get()); |
| 172 AddAppListInfo(list.get()); | |
| 132 | 173 |
| 133 std::string extension_id = extension_misc::kHotwordExtensionId; | 174 std::string extension_id = extension_misc::kHotwordExtensionId; |
| 134 HotwordService* hotword_service = | 175 HotwordService* hotword_service = |
| 135 HotwordServiceFactory::GetForProfile(profile_); | 176 HotwordServiceFactory::GetForProfile(profile_); |
| 136 if (hotword_service && hotword_service->IsExperimentalHotwordingEnabled()) | 177 if (hotword_service && hotword_service->IsExperimentalHotwordingEnabled()) |
| 137 extension_id = extension_misc::kHotwordNewExtensionId; | 178 extension_id = extension_misc::kHotwordNewExtensionId; |
| 138 AddExtensionInfo(extension_id, "Extension", list.get()); | 179 AddExtensionInfo(extension_id, "Extension", list.get()); |
| 139 | 180 |
| 140 AddExtensionInfo(extension_misc::kHotwordSharedModuleId, | 181 AddExtensionInfo(extension_misc::kHotwordSharedModuleId, |
| 141 "Shared Module", | 182 "Shared Module", |
| 142 list.get()); | 183 list.get()); |
| 143 AddAppListInfo(list.get()); | |
| 144 | 184 |
| 145 // voiceSearchInfo will take ownership of list, and clean it up on | 185 base::FilePath path; |
| 146 // destruction. | 186 extensions::ExtensionSystem* extension_system = |
| 147 voiceSearchInfo->Set("voiceSearchInfo", list.release()); | 187 extensions::ExtensionSystem::Get(profile_); |
| 188 if (extension_system) { | |
| 189 ExtensionService* extension_service = | |
| 190 extension_system->extension_service(); | |
| 191 const extensions::Extension* extension = | |
| 192 extension_service->GetExtensionById( | |
| 193 extension_misc::kHotwordSharedModuleId, true); | |
| 194 if (extension) { | |
| 195 path = extension->path(); | |
| 196 } | |
| 197 } | |
| 198 // Temporary, unowned ptr to |list| to pass into | |
| 199 // AddSharedModulePlatformsOnFileThread. | |
| 200 base::ListValue* raw_list = list.get(); | |
| 201 content::BrowserThread::PostTask( | |
| 202 content::BrowserThread::FILE, | |
| 203 FROM_HERE, | |
| 204 base::Bind( | |
| 205 &AddSharedModulePlatformsOnFileThread, | |
| 206 raw_list, | |
| 207 path, | |
| 208 base::Bind(&VoiceSearchDomHandler::ReturnVoiceSearchInfo, | |
| 209 weak_factory_.GetWeakPtr(), | |
| 210 base::Passed(&list)))); | |
| 148 } | 211 } |
| 149 | 212 |
| 150 // Adds information regarding the system and chrome version info to list. | 213 // Adds information regarding the system and chrome version info to list. |
| 151 void AddOperatingSystemInfo(base::ListValue* list) { | 214 void AddOperatingSystemInfo(base::ListValue* list) { |
| 152 // Obtain the Chrome version info. | 215 // Obtain the Chrome version info. |
| 153 chrome::VersionInfo version_info; | 216 chrome::VersionInfo version_info; |
| 154 AddPair(list, | 217 AddPair(list, |
| 155 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME), | 218 l10n_util::GetStringUTF8(IDS_PRODUCT_NAME), |
| 156 version_info.Version() + " (" + | 219 version_info.Version() + " (" + |
| 157 chrome::VersionInfo::GetVersionStringModifier() + ")"); | 220 chrome::VersionInfo::GetVersionStringModifier() + ")"); |
| (...skipping 201 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 359 state = "SPEECH_RECOGNITION_STOPPING"; | 422 state = "SPEECH_RECOGNITION_STOPPING"; |
| 360 break; | 423 break; |
| 361 case app_list::SPEECH_RECOGNITION_NETWORK_ERROR: | 424 case app_list::SPEECH_RECOGNITION_NETWORK_ERROR: |
| 362 state = "SPEECH_RECOGNITION_NETWORK_ERROR"; | 425 state = "SPEECH_RECOGNITION_NETWORK_ERROR"; |
| 363 break; | 426 break; |
| 364 default: | 427 default: |
| 365 state = "undefined"; | 428 state = "undefined"; |
| 366 } | 429 } |
| 367 } | 430 } |
| 368 AddPair(list, "Start Page State", state); | 431 AddPair(list, "Start Page State", state); |
| 432 AddLineBreak(list); | |
| 369 #endif | 433 #endif |
| 370 } | 434 } |
| 371 | 435 |
| 372 Profile* profile_; | 436 Profile* profile_; |
| 437 base::WeakPtrFactory<VoiceSearchDomHandler> weak_factory_; | |
| 373 | 438 |
| 374 DISALLOW_COPY_AND_ASSIGN(VoiceSearchDomHandler); | 439 DISALLOW_COPY_AND_ASSIGN(VoiceSearchDomHandler); |
| 375 }; | 440 }; |
| 376 | 441 |
| 377 } // namespace | 442 } // namespace |
| 378 | 443 |
| 379 /////////////////////////////////////////////////////////////////////////////// | 444 /////////////////////////////////////////////////////////////////////////////// |
| 380 // | 445 // |
| 381 // VoiceSearchUI | 446 // VoiceSearchUI |
| 382 // | 447 // |
| 383 /////////////////////////////////////////////////////////////////////////////// | 448 /////////////////////////////////////////////////////////////////////////////// |
| 384 | 449 |
| 385 VoiceSearchUI::VoiceSearchUI(content::WebUI* web_ui) | 450 VoiceSearchUI::VoiceSearchUI(content::WebUI* web_ui) |
| 386 : content::WebUIController(web_ui) { | 451 : content::WebUIController(web_ui) { |
| 387 Profile* profile = Profile::FromWebUI(web_ui); | 452 Profile* profile = Profile::FromWebUI(web_ui); |
| 388 web_ui->AddMessageHandler(new VoiceSearchDomHandler(profile)); | 453 web_ui->AddMessageHandler(new VoiceSearchDomHandler(profile)); |
| 389 | 454 |
| 390 // Set up the about:voicesearch source. | 455 // Set up the about:voicesearch source. |
| 391 content::WebUIDataSource::Add(profile, CreateVoiceSearchUiHtmlSource()); | 456 content::WebUIDataSource::Add(profile, CreateVoiceSearchUiHtmlSource()); |
| 392 } | 457 } |
| 393 | 458 |
| 394 VoiceSearchUI::~VoiceSearchUI() {} | 459 VoiceSearchUI::~VoiceSearchUI() {} |
| OLD | NEW |