OLD | NEW |
| (Empty) |
1 // Copyright 2015 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 "chrome/browser/android/dev_tools_discovery_provider_android.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/macros.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/strings/string_number_conversions.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/android/tab_android.h" | |
14 #include "chrome/browser/browser_process.h" | |
15 #include "chrome/browser/ui/android/tab_model/tab_model.h" | |
16 #include "chrome/browser/ui/android/tab_model/tab_model_list.h" | |
17 #include "components/devtools_discovery/devtools_discovery_manager.h" | |
18 #include "content/public/browser/devtools_agent_host.h" | |
19 #include "content/public/browser/devtools_agent_host_client.h" | |
20 #include "content/public/browser/devtools_external_agent_proxy.h" | |
21 #include "content/public/browser/devtools_external_agent_proxy_delegate.h" | |
22 #include "content/public/browser/favicon_status.h" | |
23 #include "content/public/browser/navigation_entry.h" | |
24 #include "content/public/browser/web_contents.h" | |
25 | |
26 using content::DevToolsAgentHost; | |
27 using content::WebContents; | |
28 | |
29 namespace { | |
30 | |
31 class TabProxyDelegate : public content::DevToolsExternalAgentProxyDelegate, | |
32 public content::DevToolsAgentHostClient { | |
33 public: | |
34 explicit TabProxyDelegate(TabAndroid* tab) | |
35 : tab_id_(tab->GetAndroidId()), | |
36 title_(base::UTF16ToUTF8(tab->GetTitle())), | |
37 url_(tab->GetURL()), | |
38 agent_host_(tab->web_contents() ? | |
39 DevToolsAgentHost::GetOrCreateFor(tab->web_contents()) : nullptr) { | |
40 } | |
41 | |
42 ~TabProxyDelegate() override { | |
43 } | |
44 | |
45 void DispatchProtocolMessage(DevToolsAgentHost* agent_host, | |
46 const std::string& message) override { | |
47 proxy_->DispatchOnClientHost(message); | |
48 } | |
49 | |
50 void AgentHostClosed(DevToolsAgentHost* agent_host, | |
51 bool replaced_with_another_client) override { | |
52 proxy_->ConnectionClosed(); | |
53 } | |
54 | |
55 void Attach(content::DevToolsExternalAgentProxy* proxy) override { | |
56 proxy_ = proxy; | |
57 MaterializeAgentHost(); | |
58 } | |
59 | |
60 void Detach() override { | |
61 if (agent_host_) | |
62 agent_host_->DetachClient(this); | |
63 agent_host_ = nullptr; | |
64 proxy_ = nullptr; | |
65 } | |
66 | |
67 std::string GetId() override { | |
68 return base::IntToString(tab_id_); | |
69 } | |
70 | |
71 std::string GetType() override { | |
72 return agent_host_ ? agent_host_->GetType() : DevToolsAgentHost::kTypePage; | |
73 } | |
74 | |
75 std::string GetTitle() override { | |
76 return agent_host_ ? agent_host_->GetTitle() : title_; | |
77 } | |
78 | |
79 std::string GetDescription() override { | |
80 return agent_host_ ? agent_host_->GetDescription() : ""; | |
81 } | |
82 | |
83 GURL GetURL() override { | |
84 return agent_host_ ? agent_host_->GetURL() : url_; | |
85 } | |
86 | |
87 GURL GetFaviconURL() override { | |
88 return agent_host_ ? agent_host_->GetFaviconURL() : GURL(); | |
89 } | |
90 | |
91 bool Activate() override { | |
92 TabModel* model; | |
93 int index; | |
94 if (!FindTab(&model, &index)) | |
95 return false; | |
96 model->SetActiveIndex(index); | |
97 return true; | |
98 } | |
99 | |
100 bool Inspect() override { | |
101 MaterializeAgentHost(); | |
102 if (agent_host_) | |
103 return agent_host_->Inspect(); | |
104 return false; | |
105 } | |
106 | |
107 void Reload() override { | |
108 MaterializeAgentHost(); | |
109 if (agent_host_) | |
110 agent_host_->Reload(); | |
111 } | |
112 | |
113 bool Close() override { | |
114 TabModel* model; | |
115 int index; | |
116 if (!FindTab(&model, &index)) | |
117 return false; | |
118 model->CloseTabAt(index); | |
119 return true; | |
120 } | |
121 | |
122 void SendMessageToBackend(const std::string& message) override { | |
123 if (agent_host_) | |
124 agent_host_->DispatchProtocolMessage(this, message); | |
125 } | |
126 | |
127 private: | |
128 void MaterializeAgentHost() { | |
129 if (agent_host_) | |
130 return; | |
131 TabModel* model; | |
132 int index; | |
133 if (!FindTab(&model, &index)) | |
134 return; | |
135 WebContents* web_contents = model->GetWebContentsAt(index); | |
136 if (!web_contents) | |
137 return; | |
138 agent_host_ = DevToolsAgentHost::GetOrCreateFor(web_contents); | |
139 } | |
140 | |
141 bool FindTab(TabModel** model_result, int* index_result) const { | |
142 for (TabModelList::const_iterator iter = TabModelList::begin(); | |
143 iter != TabModelList::end(); ++iter) { | |
144 TabModel* model = *iter; | |
145 for (int i = 0; i < model->GetTabCount(); ++i) { | |
146 TabAndroid* tab = model->GetTabAt(i); | |
147 if (tab && tab->GetAndroidId() == tab_id_) { | |
148 *model_result = model; | |
149 *index_result = i; | |
150 return true; | |
151 } | |
152 } | |
153 } | |
154 return false; | |
155 } | |
156 | |
157 const int tab_id_; | |
158 const std::string title_; | |
159 const GURL url_; | |
160 scoped_refptr<content::DevToolsAgentHost> agent_host_; | |
161 content::DevToolsExternalAgentProxy* proxy_; | |
162 DISALLOW_COPY_AND_ASSIGN(TabProxyDelegate); | |
163 }; | |
164 | |
165 scoped_refptr<content::DevToolsAgentHost> CreateNewAndroidTab(const GURL& url) { | |
166 if (TabModelList::empty()) | |
167 return nullptr; | |
168 | |
169 TabModel* tab_model = TabModelList::get(0); | |
170 if (!tab_model) | |
171 return nullptr; | |
172 | |
173 WebContents* web_contents = tab_model->CreateNewTabForDevTools(url); | |
174 if (!web_contents) | |
175 return nullptr; | |
176 | |
177 TabAndroid* tab = TabAndroid::FromWebContents(web_contents); | |
178 if (!tab) | |
179 return nullptr; | |
180 | |
181 return content::DevToolsAgentHost::Create(new TabProxyDelegate(tab)); | |
182 } | |
183 | |
184 } // namespace | |
185 | |
186 DevToolsDiscoveryProviderAndroid::DevToolsDiscoveryProviderAndroid() { | |
187 } | |
188 | |
189 DevToolsDiscoveryProviderAndroid::~DevToolsDiscoveryProviderAndroid() { | |
190 } | |
191 | |
192 content::DevToolsAgentHost::List | |
193 DevToolsDiscoveryProviderAndroid::GetDescriptors() { | |
194 content::DevToolsAgentHost::List result; | |
195 | |
196 // Enumerate existing tabs, including the ones with no WebContents. | |
197 std::set<WebContents*> tab_web_contents; | |
198 for (TabModelList::const_iterator iter = TabModelList::begin(); | |
199 iter != TabModelList::end(); ++iter) { | |
200 TabModel* model = *iter; | |
201 for (int i = 0; i < model->GetTabCount(); ++i) { | |
202 TabAndroid* tab = model->GetTabAt(i); | |
203 if (!tab) | |
204 continue; | |
205 | |
206 scoped_refptr<content::DevToolsAgentHost> host = | |
207 DevToolsAgentHost::Create(new TabProxyDelegate(tab)); | |
208 } | |
209 } | |
210 | |
211 // Add descriptors for targets not associated with any tabs. | |
212 DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll(); | |
213 for (DevToolsAgentHost::List::iterator it = agents.begin(); | |
214 it != agents.end(); ++it) { | |
215 if (WebContents* web_contents = (*it)->GetWebContents()) { | |
216 if (tab_web_contents.find(web_contents) != tab_web_contents.end()) | |
217 continue; | |
218 } | |
219 result.push_back(*it); | |
220 } | |
221 | |
222 return result; | |
223 } | |
224 | |
225 // static | |
226 void DevToolsDiscoveryProviderAndroid::Install() { | |
227 devtools_discovery::DevToolsDiscoveryManager* discovery_manager = | |
228 devtools_discovery::DevToolsDiscoveryManager::GetInstance(); | |
229 discovery_manager->AddProvider( | |
230 base::WrapUnique(new DevToolsDiscoveryProviderAndroid())); | |
231 discovery_manager->SetCreateCallback(base::Bind(&CreateNewAndroidTab)); | |
232 } | |
OLD | NEW |