OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/arc/intent_helper/link_handler_model_impl.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "components/arc/arc_bridge_service.h" |
| 11 #include "url/gurl.h" |
| 12 |
| 13 namespace arc { |
| 14 |
| 15 namespace { |
| 16 |
| 17 const int kMinInstanceVersion = 2; // see intent_helper.mojom |
| 18 |
| 19 } // namespace |
| 20 |
| 21 LinkHandlerModelImpl::LinkHandlerModelImpl() : weak_ptr_factory_(this) {} |
| 22 |
| 23 LinkHandlerModelImpl::~LinkHandlerModelImpl() {} |
| 24 |
| 25 bool LinkHandlerModelImpl::Init(const GURL& url) { |
| 26 mojom::IntentHelperInstance* intent_helper_instance = GetIntentHelper(); |
| 27 if (!intent_helper_instance) |
| 28 return false; |
| 29 |
| 30 // Check if ARC apps can handle the |url|. Since the information is held in |
| 31 // a different (ARC) process, issue a mojo IPC request. Usually, the |
| 32 // callback function, OnUrlHandlerList, is called within a few milliseconds |
| 33 // even on a slowest Chromebook we support. |
| 34 intent_helper_instance->RequestUrlHandlerList( |
| 35 url.spec(), base::Bind(&LinkHandlerModelImpl::OnUrlHandlerList, |
| 36 weak_ptr_factory_.GetWeakPtr())); |
| 37 return true; |
| 38 } |
| 39 |
| 40 void LinkHandlerModelImpl::AddObserver(Observer* observer) { |
| 41 observer_ = observer; |
| 42 } |
| 43 |
| 44 void LinkHandlerModelImpl::OpenLinkWithHandler(const GURL& url, |
| 45 uint32_t handler_id) { |
| 46 mojom::IntentHelperInstance* intent_helper_instance = GetIntentHelper(); |
| 47 if (!intent_helper_instance) |
| 48 return; |
| 49 if (handler_id >= handlers_.size()) |
| 50 return; |
| 51 intent_helper_instance->HandleUrl(url.spec(), |
| 52 handlers_[handler_id]->package_name); |
| 53 } |
| 54 |
| 55 mojom::IntentHelperInstance* LinkHandlerModelImpl::GetIntentHelper() { |
| 56 ArcBridgeService* bridge_service = arc::ArcBridgeService::Get(); |
| 57 if (!bridge_service) { |
| 58 DLOG(WARNING) << "ARC bridge is not ready."; |
| 59 return nullptr; |
| 60 } |
| 61 mojom::IntentHelperInstance* intent_helper_instance = |
| 62 bridge_service->intent_helper_instance(); |
| 63 if (!intent_helper_instance) { |
| 64 DLOG(WARNING) << "ARC intent helper instance is not ready."; |
| 65 return nullptr; |
| 66 } |
| 67 if (bridge_service->intent_helper_version() < kMinInstanceVersion) { |
| 68 DLOG(WARNING) << "ARC intent helper instance is too old."; |
| 69 return nullptr; |
| 70 } |
| 71 return intent_helper_instance; |
| 72 } |
| 73 |
| 74 void LinkHandlerModelImpl::OnUrlHandlerList( |
| 75 mojo::Array<mojom::UrlHandlerInfoPtr> handlers) { |
| 76 handlers_ = std::move(handlers); |
| 77 NotifyObserver(); |
| 78 } |
| 79 |
| 80 void LinkHandlerModelImpl::NotifyObserver() { |
| 81 if (!observer_) |
| 82 return; |
| 83 std::vector<ash::LinkHandlerInfo> handlers; |
| 84 for (size_t i = 0; i < handlers_.size(); ++i) { |
| 85 // Use the handler's index as an ID. |
| 86 ash::LinkHandlerInfo handler = {handlers_[i]->name.get(), gfx::Image(), i}; |
| 87 handlers.push_back(handler); |
| 88 } |
| 89 observer_->ModelChanged(handlers); |
| 90 } |
| 91 |
| 92 } // namespace arc |
OLD | NEW |