Chromium Code Reviews| Index: chrome/browser/chromeos/arc/voice_interaction/arc_voice_interaction_arc_home_service.cc |
| diff --git a/chrome/browser/chromeos/arc/voice_interaction/arc_voice_interaction_arc_home_service.cc b/chrome/browser/chromeos/arc/voice_interaction/arc_voice_interaction_arc_home_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..976e21bde0f40c9acb7cad2fa70a2cf4078dfc99 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/arc/voice_interaction/arc_voice_interaction_arc_home_service.cc |
| @@ -0,0 +1,117 @@ |
| +// 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/voice_interaction/arc_voice_interaction_arc_home_service.h" |
| + |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "ash/shell.h" |
| +#include "base/logging.h" |
| +#include "base/strings/string16.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "chrome/browser/ui/ash/launcher/chrome_launcher_controller.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/browser/ui/browser_window.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| +#include "components/arc/arc_bridge_service.h" |
| +#include "components/arc/instance_holder.h" |
| +#include "content/public/browser/browser_thread.h" |
| +#include "content/public/browser/web_contents.h" |
| +#include "ui/accessibility/platform/ax_snapshot_node_android_platform.h" |
| +#include "ui/aura/window.h" |
| +#include "ui/snapshot/snapshot.h" |
| +#include "ui/wm/public/activation_client.h" |
| + |
| +namespace arc { |
| + |
| +ArcVoiceInteractionArcHomeService::ArcVoiceInteractionArcHomeService( |
| + ArcBridgeService* bridge_service) |
| + : ArcService(bridge_service), binding_(this) { |
| + arc_bridge_service()->voice_interaction_arc_home()->AddObserver(this); |
| +} |
| + |
| +ArcVoiceInteractionArcHomeService::~ArcVoiceInteractionArcHomeService() { |
| + arc_bridge_service()->voice_interaction_arc_home()->RemoveObserver(this); |
| +} |
| + |
| +void ArcVoiceInteractionArcHomeService::OnInstanceReady() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + mojom::VoiceInteractionArcHomeInstance* home_instance = |
| + ARC_GET_INSTANCE_FOR_METHOD( |
| + arc_bridge_service()->voice_interaction_arc_home(), Init); |
| + DCHECK(home_instance); |
| + home_instance->Init(binding_.CreateInterfacePtrAndBind()); |
| +} |
| + |
| +void ArcVoiceInteractionArcHomeService::GetVoiceInteractionStructure( |
| + const GetVoiceInteractionStructureCallback& callback) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + Browser* browser = BrowserList::GetInstance()->GetLastActive(); |
| + if (!browser || !browser->window()->IsActive()) { |
| + // TODO(muyuanli): retrieve context for apps. |
| + LOG(ERROR) << "Retrieving context from apps is not implemented."; |
| + callback.Run(mojom::VoiceInteractionStructure::New()); |
| + return; |
| + } |
| + |
| + content::WebContents* web_contents = |
| + browser->tab_strip_model()->GetActiveWebContents(); |
| + // Do not process incognito tab. |
| + if (web_contents->GetBrowserContext()->IsOffTheRecord()) { |
| + callback.Run(mojom::VoiceInteractionStructure::New()); |
| + return; |
| + } |
| + |
| + web_contents->RequestAXTreeSnapshot( |
| + base::Bind(&ArcVoiceInteractionArcHomeService:: |
|
Luis Héctor Chávez
2017/04/25 18:22:36
iwyu: #include "base/bind.h"
Muyuan
2017/04/25 19:38:13
Done.
|
| + RequestVoiceInteractionStructureCallback, |
| + callback)); |
| +} |
| + |
| +// static |
| +mojom::VoiceInteractionStructurePtr |
| +ArcVoiceInteractionArcHomeService::CreateVoiceInteractionStructure( |
| + const ui::AXSnapshotNodeAndroid& view_structure) { |
| + auto structure = mojom::VoiceInteractionStructure::New(); |
| + structure->text = view_structure.text; |
| + structure->text_size = view_structure.text_size; |
| + |
| + structure->bold = view_structure.bold; |
| + structure->italic = view_structure.italic; |
| + structure->underline = view_structure.underline; |
| + structure->line_through = view_structure.line_through; |
| + structure->color = view_structure.color; |
| + structure->bgcolor = view_structure.bgcolor; |
| + |
| + structure->class_name = view_structure.class_name; |
| + structure->rect = view_structure.rect; |
| + |
| + if (view_structure.has_selection) { |
| + auto selection = mojom::TextSelection::New(); |
| + selection->start_selection = view_structure.start_selection; |
| + selection->end_selection = view_structure.end_selection; |
| + structure->selection = std::move(selection); |
| + } |
| + |
| + for (auto& child : view_structure.children) |
| + structure->children.push_back(CreateVoiceInteractionStructure(*child)); |
| + |
| + return structure; |
| +} |
| + |
| +// static |
| +void ArcVoiceInteractionArcHomeService:: |
| + RequestVoiceInteractionStructureCallback( |
| + const base::Callback<void(mojom::VoiceInteractionStructurePtr)>& |
| + callback, |
| + const ui::AXTreeUpdate& update) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + callback.Run( |
| + ArcVoiceInteractionArcHomeService::CreateVoiceInteractionStructure( |
| + *ui::AXSnapshotNodeAndroid::Create(update))); |
| +} |
| + |
| +} // namespace arc |