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..f7af192222ac64286075f9bd2eae01a91abf1bce |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/arc_voice_interaction_service.cc |
| @@ -0,0 +1,152 @@ |
| +// 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 <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/arc_features.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +namespace arc { |
| + |
| +namespace { |
| + |
| +constexpr char kEnableVoiceInteraction[] = "enable-voice-interaction"; |
| + |
| +const ui::Accelerator kVoiceInteractionAccelerator(ui::VKEY_A, |
| + ui::EF_COMMAND_DOWN); |
| + |
| +} // namespace |
| + |
| +class ArcVoiceInteractionService::ArcHomeObserver |
| + : public InstanceHolder<mojom::VoiceInteractionArcHomeInstance>::Observer { |
| + public: |
| + explicit ArcHomeObserver(ArcVoiceInteractionService* service) |
| + : service_(service) {} |
| + void OnInstanceReady() override { service_->OnArcHomeInstanceReady(); } |
| + void OnInstanceClosed() override { service_->OnArcHomeInstanceClosed(); } |
| + |
| + private: |
| + ArcVoiceInteractionService* service_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ArcHomeObserver); |
| +}; |
| + |
| +class ArcVoiceInteractionService::CaptureObserver |
| + : public InstanceHolder<mojom::VoiceInteractionCaptureInstance>::Observer { |
| + public: |
| + explicit CaptureObserver(ArcVoiceInteractionService* service) |
| + : service_(service) {} |
| + 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) { |
| + arc_bridge_service()->voice_interaction_capture()->AddObserver( |
| + capture_observer_.get()); |
| + arc_bridge_service()->voice_interaction_home()->AddObserver( |
| + home_observer_.get()); |
| +} |
| + |
| +ArcVoiceInteractionService::~ArcVoiceInteractionService() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + arc_bridge_service()->voice_interaction_home()->RemoveObserver( |
| + home_observer_.get()); |
| + arc_bridge_service()->voice_interaction_capture()->RemoveObserver( |
| + capture_observer_.get()); |
| +} |
| + |
| +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()); |
| + |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + kEnableVoiceInteraction)) { |
| + ash::WmShell::Get()->accelerator_controller()->Register( |
| + {kVoiceInteractionAccelerator}, this); |
| + } |
| +} |
| + |
| +void ArcVoiceInteractionService::OnCaptureInstanceClosed() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| + kEnableVoiceInteraction)) { |
| + ash::WmShell::Get()->accelerator_controller()->Unregister( |
| + kVoiceInteractionAccelerator, this); |
| + } |
| +} |
| + |
| +bool ArcVoiceInteractionService::AcceleratorPressed( |
| + const ui::Accelerator& accelerator) { |
| + mojom::VoiceInteractionCaptureInstance* capture_instance_ = |
|
xc
2017/03/09 05:05:47
no surfix _ for local variable
Muyuan
2017/03/09 20:44:44
Done.
|
| + ARC_GET_INSTANCE_FOR_METHOD( |
| + arc_bridge_service()->voice_interaction_capture(), |
| + StartVoiceInteractionSession); |
| + DCHECK(capture_instance_); |
| + capture_instance_->StartVoiceInteractionSession(); |
|
xc
2017/03/09 05:05:47
I think we need to explicitly check null here. ot
Muyuan
2017/03/09 20:44:44
The key is registered only after the Arc instance
|
| + return true; |
| +} |
| + |
| +bool ArcVoiceInteractionService::CanHandleAccelerators() const { |
| + return true; |
| +} |
| + |
| +void ArcVoiceInteractionService::GetVoiceInteractionStructure( |
| + const GetVoiceInteractionStructureCallback& callback) { |
| + 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) { |
| + 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. |
| + std::vector<uint8_t> result; |
| + callback.Run(result); |
| +} |
| + |
| +} // namespace arc |