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(ArcBridgeService* bridge_service) | |
22 : bridge_service_(bridge_service), | |
23 has_results_(false), | |
24 weak_ptr_factory_(this) { | |
25 DCHECK(bridge_service_); | |
26 } | |
27 | |
28 LinkHandlerModelImpl::~LinkHandlerModelImpl() {} | |
29 | |
30 bool LinkHandlerModelImpl::Init(const GURL& url) { | |
31 IntentHelperInstance* intent_helper_instance = GetIntentHelper(); | |
32 if (!intent_helper_instance) | |
33 return false; | |
34 | |
35 // Check if ARC apps can handle the |url|. Since the information is held in | |
36 // a different (ARC) process, issue a mojo IPC request. Usually, the | |
37 // callback function, OnUrlHandlerList, is called within a few milliseconds | |
38 // even on a slowest Chromebook we support. | |
39 intent_helper_instance->RequestUrlHandlerList( | |
40 url.spec(), base::Bind(&LinkHandlerModelImpl::OnUrlHandlerList, | |
41 weak_ptr_factory_.GetWeakPtr())); | |
42 return true; | |
43 } | |
44 | |
45 void LinkHandlerModelImpl::AddObserver(Observer* observer) { | |
46 observer_ = observer; | |
47 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.
| |
48 } | |
49 | |
50 void LinkHandlerModelImpl::OpenLinkWithHandler(const GURL& url, | |
51 uint32_t handler_id) { | |
52 IntentHelperInstance* intent_helper_instance = GetIntentHelper(); | |
53 if (!intent_helper_instance) | |
54 return; | |
55 if (handler_id >= handlers_.size()) | |
56 return; | |
57 intent_helper_instance->HandleUrl(url.spec(), | |
58 handlers_[handler_id]->package_name); | |
59 } | |
60 | |
61 IntentHelperInstance* LinkHandlerModelImpl::GetIntentHelper() { | |
62 if (!bridge_service_) { | |
63 DLOG(WARNING) << "ARC bridge is not ready."; | |
64 return nullptr; | |
65 } | |
66 IntentHelperInstance* intent_helper_instance = | |
67 bridge_service_->intent_helper_instance(); | |
68 if (!intent_helper_instance) { | |
69 DLOG(WARNING) << "ARC intent helper instance is not ready."; | |
70 return nullptr; | |
71 } | |
72 if (bridge_service_->intent_helper_version() < kMinInstanceVersion) { | |
73 DLOG(WARNING) << "ARC intent helper instance is too old."; | |
74 return nullptr; | |
75 } | |
76 return intent_helper_instance; | |
77 } | |
78 | |
79 void LinkHandlerModelImpl::OnUrlHandlerList( | |
80 mojo::Array<UrlHandlerInfoPtr> handlers) { | |
81 handlers_ = std::move(handlers); | |
82 has_results_ = true; | |
83 NotifyObserver(); | |
84 } | |
85 | |
86 void LinkHandlerModelImpl::NotifyObserver() { | |
87 if (!observer_ || !has_results_) | |
88 return; | |
89 std::vector<ash::LinkHandlerInfo> handlers; | |
90 for (size_t i = 0; i < handlers_.size(); ++i) { | |
91 // Use the handler's index as an ID. | |
92 ash::LinkHandlerInfo handler = {handlers_[i]->name.get(), gfx::Image(), i}; | |
93 handlers.push_back(handler); | |
94 } | |
95 observer_->ModelChanged(handlers); | |
96 } | |
97 | |
98 } // namespace arc | |
OLD | NEW |