Chromium Code Reviews| Index: chrome/browser/chromeos/arc/arc_voice_interaction_service.cc |
| diff --git a/chrome/browser/chromeos/arc/arc_voice_interaction_service.cc b/chrome/browser/chromeos/arc/arc_voice_interaction_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..59dd535f92309f72e2b231d0f33ac2cfb1bd28c1 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_voice_interaction_service.cc |
| @@ -0,0 +1,160 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/arc/arc_voice_interaction_service.h" |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "ash/common/accelerators/accelerator_controller.h" |
| +#include "ash/common/wm_shell.h" |
| +#include "ash/shell.h" |
| +#include "base/command_line.h" |
| +#include "base/logging.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "components/arc/instance_holder.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +constexpr char kEnableVoiceInteraction[] = "enable-voice-interaction"; |
|
Luis Héctor Chávez
2017/03/13 17:39:34
This should be in chromeos/chromeos_switches.h
Muyuan
2017/03/14 01:38:14
Done.
|
| + |
| +} // namespace |
| + |
| +class ArcVoiceInteractionService::ArcHomeObserver |
| + : public InstanceHolder<mojom::VoiceInteractionArcHomeInstance>::Observer { |
| + public: |
| + explicit ArcHomeObserver(ArcVoiceInteractionService* service) |
| + : service_(service) { |
| + service_->arc_bridge_service()->voice_interaction_home()->AddObserver(this); |
| + } |
| + ~ArcHomeObserver() { |
| + service_->arc_bridge_service()->voice_interaction_home()->RemoveObserver( |
| + this); |
| + } |
| + void OnInstanceReady() override { service_->OnArcHomeInstanceReady(); } |
| + void OnInstanceClosed() override { service_->OnArcHomeInstanceClosed(); } |
| + |
| + private: |
| + ArcVoiceInteractionService* service_; |
|
Luis Héctor Chávez
2017/03/13 17:39:34
Please make a note that ArcVoiceInteractionService
Muyuan
2017/03/14 01:38:14
Done.
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcHomeObserver); |
| +}; |
| + |
| +class ArcVoiceInteractionService::CaptureObserver |
| + : public InstanceHolder<mojom::VoiceInteractionCaptureInstance>::Observer { |
| + public: |
| + explicit CaptureObserver(ArcVoiceInteractionService* service) |
| + : service_(service) { |
| + service_->arc_bridge_service()->voice_interaction_capture()->AddObserver( |
| + this); |
| + } |
| + ~CaptureObserver() { |
| + service_->arc_bridge_service()->voice_interaction_capture()->RemoveObserver( |
| + this); |
| + } |
| + void OnInstanceReady() override { service_->OnCaptureInstanceReady(); } |
| + void OnInstanceClosed() override { service_->OnCaptureInstanceClosed(); } |
| + |
| + private: |
| + ArcVoiceInteractionService* service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(CaptureObserver); |
| +}; |
| + |
| +ArcVoiceInteractionService::ArcVoiceInteractionService( |
| + ArcBridgeService* bridge_service) |
| + : ArcService(bridge_service), |
| + home_observer_(base::MakeUnique<ArcHomeObserver>(this)), |
| + capture_observer_(base::MakeUnique<CaptureObserver>(this)), |
| + home_binding_(this), |
| + capture_binding_(this) {} |
| + |
| +ArcVoiceInteractionService::~ArcVoiceInteractionService() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| +} |
| + |
| +void ArcVoiceInteractionService::OnArcHomeInstanceReady() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + mojom::VoiceInteractionArcHomeInstance* arc_home_instance = |
| + ARC_GET_INSTANCE_FOR_METHOD( |
| + arc_bridge_service()->voice_interaction_home(), Init); |
| + DCHECK(arc_home_instance); |
| + arc_home_instance->Init(home_binding_.CreateInterfacePtrAndBind()); |
| +} |
| + |
| +void ArcVoiceInteractionService::OnArcHomeInstanceClosed() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| +} |
| + |
| +void ArcVoiceInteractionService::OnCaptureInstanceReady() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + mojom::VoiceInteractionCaptureInstance* arc_capture_instance = |
| + ARC_GET_INSTANCE_FOR_METHOD( |
| + arc_bridge_service()->voice_interaction_capture(), Init); |
| + DCHECK(arc_capture_instance); |
| + arc_capture_instance->Init(capture_binding_.CreateInterfacePtrAndBind()); |
| + |
| + LOG(ERROR) << "Registering key: " |
|
Luis Héctor Chávez
2017/03/13 17:39:34
this is not an error. use VLOG(1) or DVLOG(1).
Muyuan
2017/03/14 01:38:14
removed.
|
| + << base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + kEnableVoiceInteraction); |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
|
Luis Héctor Chávez
2017/03/13 17:39:34
nit: use the guard clause pattern:
if (!base::Com
Muyuan
2017/03/14 01:38:14
Done.
|
| + kEnableVoiceInteraction)) { |
| + ash::WmShell::Get()->accelerator_controller()->Register( |
| + {ui::Accelerator(ui::VKEY_A, ui::EF_COMMAND_DOWN)}, this); |
| + } |
| +} |
| + |
| +void ArcVoiceInteractionService::OnCaptureInstanceClosed() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
|
Luis Héctor Chávez
2017/03/13 17:39:34
Same as above.
Muyuan
2017/03/14 01:38:14
Done.
|
| + kEnableVoiceInteraction)) { |
| + ash::WmShell::Get()->accelerator_controller()->Unregister( |
| + ui::Accelerator(ui::VKEY_A, ui::EF_COMMAND_DOWN), this); |
| + } |
| +} |
| + |
| +bool ArcVoiceInteractionService::AcceleratorPressed( |
| + const ui::Accelerator& accelerator) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + mojom::VoiceInteractionCaptureInstance* capture_instance = |
| + ARC_GET_INSTANCE_FOR_METHOD( |
| + arc_bridge_service()->voice_interaction_capture(), |
| + StartVoiceInteractionSession); |
| + DCHECK(capture_instance); |
| + capture_instance->StartVoiceInteractionSession(); |
| + return true; |
| +} |
| + |
| +bool ArcVoiceInteractionService::CanHandleAccelerators() const { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + return true; |
| +} |
| + |
| +void ArcVoiceInteractionService::GetVoiceInteractionStructure( |
| + const GetVoiceInteractionStructureCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + LOG(ERROR) << "ArcVoiceInteractionSerivce::GetVoiceInteractionStructure " |
| + << "not implemented."; |
| + // TODO(muyuanli): The logic below is only there to prevent blocking. |
| + // details needs to be implemented in the future. |
| + mojom::VoiceInteractionStructurePtr structure = |
| + mojom::VoiceInteractionStructure::New(); |
| + structure->success = false; |
| + callback.Run(std::move(structure)); |
| +} |
| + |
| +void ArcVoiceInteractionService::CaptureFocusedWindow( |
| + const CaptureFocusedWindowCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + LOG(ERROR) |
| + << "ArcVoiceInteractionService::CaptureFocusedWindow not implemented."; |
| + // TODO(muyuanli): The logic below is only there to prevent blocking. |
| + // details needs to be implemented in the future. |
| + callback.Run({}); |
| +} |
| + |
| +} // namespace arc |