Chromium Code Reviews| Index: components/arc/intent_helper/link_handler_model_impl.cc | 
| diff --git a/components/arc/intent_helper/link_handler_model_impl.cc b/components/arc/intent_helper/link_handler_model_impl.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..b281915677972dce36fe77c0a960a9979a5b36ee | 
| --- /dev/null | 
| +++ b/components/arc/intent_helper/link_handler_model_impl.cc | 
| @@ -0,0 +1,98 @@ | 
| +// Copyright 2016 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 "components/arc/intent_helper/link_handler_model_impl.h" | 
| + | 
| +#include <vector> | 
| + | 
| +#include "base/bind.h" | 
| +#include "components/arc/arc_bridge_service.h" | 
| +#include "url/gurl.h" | 
| + | 
| +namespace arc { | 
| + | 
| +namespace { | 
| + | 
| +const int kMinInstanceVersion = 2; // see intent_helper.mojom | 
| + | 
| +} // namespace | 
| + | 
| +LinkHandlerModelImpl::LinkHandlerModelImpl(ArcBridgeService* bridge_service) | 
| + : bridge_service_(bridge_service), | 
| + has_results_(false), | 
| + weak_ptr_factory_(this) { | 
| + DCHECK(bridge_service_); | 
| +} | 
| + | 
| +LinkHandlerModelImpl::~LinkHandlerModelImpl() {} | 
| + | 
| +bool LinkHandlerModelImpl::Init(const GURL& url) { | 
| + IntentHelperInstance* intent_helper_instance = GetIntentHelper(); | 
| + if (!intent_helper_instance) | 
| + return false; | 
| + | 
| + // Check if ARC apps can handle the |url|. Since the information is held in | 
| + // a different (ARC) process, issue a mojo IPC request. Usually, the | 
| + // callback function, OnUrlHandlerList, is called within a few milliseconds | 
| + // even on a slowest Chromebook we support. | 
| + intent_helper_instance->RequestUrlHandlerList( | 
| + url.spec(), base::Bind(&LinkHandlerModelImpl::OnUrlHandlerList, | 
| + weak_ptr_factory_.GetWeakPtr())); | 
| + return true; | 
| +} | 
| + | 
| +void LinkHandlerModelImpl::AddObserver(Observer* observer) { | 
| + observer_ = observer; | 
| + NotifyObserver(); | 
| 
 
dcheng
2016/04/21 04:20:45
Btw, it seems weird that we have to call NotifyObs
 
Yusuke Sato
2016/04/22 05:27:48
Done, deleted.
 
 | 
| +} | 
| + | 
| +void LinkHandlerModelImpl::OpenLinkWithHandler(const GURL& url, | 
| + uint32_t handler_id) { | 
| + IntentHelperInstance* intent_helper_instance = GetIntentHelper(); | 
| + if (!intent_helper_instance) | 
| + return; | 
| + if (handler_id >= handlers_.size()) | 
| + return; | 
| + intent_helper_instance->HandleUrl(url.spec(), | 
| + handlers_[handler_id]->package_name); | 
| +} | 
| + | 
| +IntentHelperInstance* LinkHandlerModelImpl::GetIntentHelper() { | 
| + if (!bridge_service_) { | 
| + DLOG(WARNING) << "ARC bridge is not ready."; | 
| + return nullptr; | 
| + } | 
| + IntentHelperInstance* intent_helper_instance = | 
| + bridge_service_->intent_helper_instance(); | 
| + if (!intent_helper_instance) { | 
| + DLOG(WARNING) << "ARC intent helper instance is not ready."; | 
| + return nullptr; | 
| + } | 
| + if (bridge_service_->intent_helper_version() < kMinInstanceVersion) { | 
| + DLOG(WARNING) << "ARC intent helper instance is too old."; | 
| + return nullptr; | 
| + } | 
| + return intent_helper_instance; | 
| +} | 
| + | 
| +void LinkHandlerModelImpl::OnUrlHandlerList( | 
| + mojo::Array<UrlHandlerInfoPtr> handlers) { | 
| + handlers_ = std::move(handlers); | 
| + has_results_ = true; | 
| + NotifyObserver(); | 
| +} | 
| + | 
| +void LinkHandlerModelImpl::NotifyObserver() { | 
| + if (!observer_ || !has_results_) | 
| + return; | 
| + std::vector<ash::LinkHandlerInfo> handlers; | 
| + for (size_t i = 0; i < handlers_.size(); ++i) { | 
| + // Use the handler's index as an ID. | 
| + ash::LinkHandlerInfo handler = {handlers_[i]->name.get(), gfx::Image(), i}; | 
| + handlers.push_back(handler); | 
| + } | 
| + observer_->ModelChanged(handlers); | 
| +} | 
| + | 
| +} // namespace arc |