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 "content/browser/speech/speech_recognition_engine.h" | 5 #include "content/browser/speech/speech_recognition_engine.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/big_endian.h" | 10 #include "base/big_endian.h" |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
17 #include "content/browser/speech/audio_buffer.h" | 17 #include "content/browser/speech/audio_buffer.h" |
18 #include "content/browser/speech/proto/google_streaming_api.pb.h" | 18 #include "content/browser/speech/proto/google_streaming_api.pb.h" |
19 #include "content/public/common/speech_recognition_error.h" | 19 #include "content/public/common/speech_recognition_error.h" |
20 #include "content/public/common/speech_recognition_result.h" | 20 #include "content/public/common/speech_recognition_result.h" |
21 #include "google_apis/google_api_keys.h" | 21 #include "google_apis/google_api_keys.h" |
22 #include "net/base/escape.h" | 22 #include "net/base/escape.h" |
23 #include "net/base/load_flags.h" | 23 #include "net/base/load_flags.h" |
| 24 #include "net/traffic_annotation/network_traffic_annotation.h" |
24 #include "net/url_request/http_user_agent_settings.h" | 25 #include "net/url_request/http_user_agent_settings.h" |
25 #include "net/url_request/url_fetcher.h" | 26 #include "net/url_request/url_fetcher.h" |
26 #include "net/url_request/url_request_context.h" | 27 #include "net/url_request/url_request_context.h" |
27 #include "net/url_request/url_request_context_getter.h" | 28 #include "net/url_request/url_request_context_getter.h" |
28 #include "net/url_request/url_request_status.h" | 29 #include "net/url_request/url_request_status.h" |
29 | 30 |
30 using net::URLFetcher; | 31 using net::URLFetcher; |
31 | 32 |
32 namespace content { | 33 namespace content { |
33 namespace { | 34 namespace { |
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 // Setup downstream fetcher. | 331 // Setup downstream fetcher. |
331 std::vector<std::string> downstream_args; | 332 std::vector<std::string> downstream_args; |
332 downstream_args.push_back( | 333 downstream_args.push_back( |
333 "key=" + net::EscapeQueryParamValue(google_apis::GetAPIKey(), true)); | 334 "key=" + net::EscapeQueryParamValue(google_apis::GetAPIKey(), true)); |
334 downstream_args.push_back("pair=" + request_key); | 335 downstream_args.push_back("pair=" + request_key); |
335 downstream_args.push_back("output=pb"); | 336 downstream_args.push_back("output=pb"); |
336 GURL downstream_url(std::string(kWebServiceBaseUrl) + | 337 GURL downstream_url(std::string(kWebServiceBaseUrl) + |
337 std::string(kDownstreamUrl) + | 338 std::string(kDownstreamUrl) + |
338 base::JoinString(downstream_args, "&")); | 339 base::JoinString(downstream_args, "&")); |
339 | 340 |
340 downstream_fetcher_ = URLFetcher::Create( | 341 net::NetworkTrafficAnnotationTag downstream_traffic_annotation = |
341 kDownstreamUrlFetcherIdForTesting, downstream_url, URLFetcher::GET, this); | 342 net::DefineNetworkTrafficAnnotation("speech_recognition_downstream", R"( |
| 343 semantics { |
| 344 sender: "Speech Recognition" |
| 345 description: |
| 346 "Chrome provides translation from speech audio recorded with a " |
| 347 "microphone to text, by using the Google speech recognition web " |
| 348 "service. Audio is sent to Google's servers (upstream) and text is " |
| 349 "returned (downstream). This network request (downstream) sends an " |
| 350 "id for getting the text response. Then the (upstream) request " |
| 351 "sends the audio data along with the id. When the server has " |
| 352 "finished processing the audio data and produced a text response, " |
| 353 "it replies to this request." |
| 354 trigger: |
| 355 "The user chooses to start the recognition by clicking the " |
| 356 "microphone icon in the Google search field." |
| 357 data: "A unique random id for this speech recognition request." |
| 358 destination: GOOGLE_OWNED_SERVICE |
| 359 } |
| 360 policy { |
| 361 cookies_allowed: false |
| 362 setting: |
| 363 "The user must allow the browser to access the microphone in a " |
| 364 "permission prompt. This is set per site (hostname pattern). In " |
| 365 "the content settings menu, microphone access can be turned off " |
| 366 "for all sites and site specific settings can be changed." |
| 367 chrome_policy { |
| 368 AudioCaptureAllowed { |
| 369 policy_options {mode: MANDATORY} |
| 370 AudioCaptureAllowed: false |
| 371 } |
| 372 } |
| 373 chrome_policy { |
| 374 AudioCaptureAllowedUrls { |
| 375 policy_options {mode: MANDATORY} |
| 376 AudioCaptureAllowedUrls: {} |
| 377 } |
| 378 } |
| 379 })"); |
| 380 downstream_fetcher_ = |
| 381 URLFetcher::Create(kDownstreamUrlFetcherIdForTesting, downstream_url, |
| 382 URLFetcher::GET, this, downstream_traffic_annotation); |
342 downstream_fetcher_->SetRequestContext(url_context_.get()); | 383 downstream_fetcher_->SetRequestContext(url_context_.get()); |
343 downstream_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 384 downstream_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
344 net::LOAD_DO_NOT_SEND_COOKIES | | 385 net::LOAD_DO_NOT_SEND_COOKIES | |
345 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 386 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
346 downstream_fetcher_->Start(); | 387 downstream_fetcher_->Start(); |
347 | 388 |
348 // Setup upstream fetcher. | 389 // Setup upstream fetcher. |
349 // TODO(hans): Support for user-selected grammars. | 390 // TODO(hans): Support for user-selected grammars. |
350 std::vector<std::string> upstream_args; | 391 std::vector<std::string> upstream_args; |
351 upstream_args.push_back("key=" + | 392 upstream_args.push_back("key=" + |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 if (preamble_encoder_) | 427 if (preamble_encoder_) |
387 audio_format = preamble_encoder_->GetMimeType() + ","; | 428 audio_format = preamble_encoder_->GetMimeType() + ","; |
388 audio_format += encoder_->GetMimeType(); | 429 audio_format += encoder_->GetMimeType(); |
389 upstream_args.push_back( | 430 upstream_args.push_back( |
390 "audioFormat=" + net::EscapeQueryParamValue(audio_format, true)); | 431 "audioFormat=" + net::EscapeQueryParamValue(audio_format, true)); |
391 } | 432 } |
392 GURL upstream_url(std::string(kWebServiceBaseUrl) + | 433 GURL upstream_url(std::string(kWebServiceBaseUrl) + |
393 std::string(kUpstreamUrl) + | 434 std::string(kUpstreamUrl) + |
394 base::JoinString(upstream_args, "&")); | 435 base::JoinString(upstream_args, "&")); |
395 | 436 |
396 upstream_fetcher_ = URLFetcher::Create(kUpstreamUrlFetcherIdForTesting, | 437 net::NetworkTrafficAnnotationTag upstream_traffic_annotation = |
397 upstream_url, URLFetcher::POST, this); | 438 net::DefineNetworkTrafficAnnotation("speech_recognition_upstream", R"( |
| 439 semantics { |
| 440 sender: "Speech Recognition" |
| 441 description: |
| 442 "Chrome provides translation from speech audio recorded with a " |
| 443 "microphone to text, by using the Google speech recognition web " |
| 444 "service. Audio is sent to Google's servers (upstream) and text is " |
| 445 "returned (downstream)." |
| 446 trigger: |
| 447 "The user chooses to start the recognition by clicking the " |
| 448 "microphone icon in the Google search field." |
| 449 data: |
| 450 "Audio recorded with the microphone, and the unique id of " |
| 451 "downstream speech recognition request." |
| 452 destination: GOOGLE_OWNED_SERVICE |
| 453 } |
| 454 policy { |
| 455 cookies_allowed: false |
| 456 setting: |
| 457 "The user must allow the browser to access the microphone in a " |
| 458 "permission prompt. This is set per site (hostname pattern). In " |
| 459 "the content settings menu, microphone access can be turned off " |
| 460 "for all sites and site specific settings can be changed." |
| 461 chrome_policy { |
| 462 AudioCaptureAllowed { |
| 463 policy_options {mode: MANDATORY} |
| 464 AudioCaptureAllowed: false |
| 465 } |
| 466 } |
| 467 chrome_policy { |
| 468 AudioCaptureAllowedUrls { |
| 469 policy_options {mode: MANDATORY} |
| 470 AudioCaptureAllowedUrls: {} |
| 471 } |
| 472 } |
| 473 })"); |
| 474 upstream_fetcher_ = |
| 475 URLFetcher::Create(kUpstreamUrlFetcherIdForTesting, upstream_url, |
| 476 URLFetcher::POST, this, upstream_traffic_annotation); |
398 if (use_framed_post_data_) | 477 if (use_framed_post_data_) |
399 upstream_fetcher_->SetChunkedUpload("application/octet-stream"); | 478 upstream_fetcher_->SetChunkedUpload("application/octet-stream"); |
400 else | 479 else |
401 upstream_fetcher_->SetChunkedUpload(encoder_->GetMimeType()); | 480 upstream_fetcher_->SetChunkedUpload(encoder_->GetMimeType()); |
402 upstream_fetcher_->SetRequestContext(url_context_.get()); | 481 upstream_fetcher_->SetRequestContext(url_context_.get()); |
403 upstream_fetcher_->SetReferrer(config_.origin_url); | 482 upstream_fetcher_->SetReferrer(config_.origin_url); |
404 upstream_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | | 483 upstream_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES | |
405 net::LOAD_DO_NOT_SEND_COOKIES | | 484 net::LOAD_DO_NOT_SEND_COOKIES | |
406 net::LOAD_DO_NOT_SEND_AUTH_DATA); | 485 net::LOAD_DO_NOT_SEND_AUTH_DATA); |
407 upstream_fetcher_->Start(); | 486 upstream_fetcher_->Start(); |
(...skipping 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
643 } | 722 } |
644 | 723 |
645 SpeechRecognitionEngine::FSMEventArgs::FSMEventArgs(FSMEvent event_value) | 724 SpeechRecognitionEngine::FSMEventArgs::FSMEventArgs(FSMEvent event_value) |
646 : event(event_value) { | 725 : event(event_value) { |
647 } | 726 } |
648 | 727 |
649 SpeechRecognitionEngine::FSMEventArgs::~FSMEventArgs() { | 728 SpeechRecognitionEngine::FSMEventArgs::~FSMEventArgs() { |
650 } | 729 } |
651 | 730 |
652 } // namespace content | 731 } // namespace content |
OLD | NEW |