File: dev_tools_manager_delegate_android.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (347 lines) | stat: -rw-r--r-- 10,885 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2014 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/android/dev_tools_manager_delegate_android.h"

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/android/tab_android.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/history/top_sites.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/android/tab_model/tab_model.h"
#include "chrome/browser/ui/android/tab_model/tab_model_list.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/browser/devtools_target.h"
#include "content/public/browser/favicon_status.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/web_contents.h"

using content::DevToolsAgentHost;
using content::WebContents;

namespace {

const char kTargetTypePage[] = "page";
const char kTargetTypeServiceWorker[] = "service_worker";
const char kTargetTypeOther[] = "other";

GURL GetFaviconURLForContents(WebContents* web_contents) {
  content::NavigationController& controller = web_contents->GetController();
  content::NavigationEntry* entry = controller.GetActiveEntry();
  if (entry != NULL && entry->GetURL().is_valid())
    return entry->GetFavicon().url;
  return GURL();
}

GURL GetFaviconURLForAgentHost(
    scoped_refptr<DevToolsAgentHost> agent_host) {
  if (WebContents* web_contents = agent_host->GetWebContents())
    return GetFaviconURLForContents(web_contents);
  return GURL();
}

base::TimeTicks GetLastActiveTimeForAgentHost(
    scoped_refptr<DevToolsAgentHost> agent_host) {
  if (WebContents* web_contents = agent_host->GetWebContents())
    return web_contents->GetLastActiveTime();
  return base::TimeTicks();
}

class TargetBase : public content::DevToolsTarget {
 public:
  // content::DevToolsTarget implementation:
  virtual std::string GetParentId() const override { return std::string(); }

  virtual std::string GetTitle() const override { return title_; }

  virtual std::string GetDescription() const override { return std::string(); }

  virtual GURL GetURL() const override { return url_; }

  virtual GURL GetFaviconURL() const override { return favicon_url_; }

  virtual base::TimeTicks GetLastActivityTime() const override {
    return last_activity_time_;
  }

 protected:
  explicit TargetBase(WebContents* web_contents)
      : title_(base::UTF16ToUTF8(web_contents->GetTitle())),
        url_(web_contents->GetURL()),
        favicon_url_(GetFaviconURLForContents(web_contents)),
        last_activity_time_(web_contents->GetLastActiveTime()) {
  }

  explicit TargetBase(scoped_refptr<DevToolsAgentHost> agent_host)
      : title_(agent_host->GetTitle()),
        url_(agent_host->GetURL()),
        favicon_url_(GetFaviconURLForAgentHost(agent_host)),
        last_activity_time_(GetLastActiveTimeForAgentHost(agent_host)) {
  }

  TargetBase(const std::string& title, const GURL& url)
      : title_(title),
        url_(url) {
  }

 private:
  const std::string title_;
  const GURL url_;
  const GURL favicon_url_;
  const base::TimeTicks last_activity_time_;
};

class TabTarget : public TargetBase {
 public:
  static TabTarget* CreateForWebContents(int tab_id,
                                         WebContents* web_contents) {
    return new TabTarget(tab_id, web_contents);
  }

  static TabTarget* CreateForUnloadedTab(int tab_id,
                                         const base::string16& title,
                                         const GURL& url) {
    return new TabTarget(tab_id, title, url);
  }

  // content::DevToolsTarget implementation:
  virtual std::string GetId() const override {
    return base::IntToString(tab_id_);
  }

  virtual std::string GetType() const override {
    return kTargetTypePage;
  }

  virtual bool IsAttached() const override {
    TabModel* model;
    int index;
    if (!FindTab(&model, &index))
      return false;
    WebContents* web_contents = model->GetWebContentsAt(index);
    if (!web_contents)
      return false;
    return DevToolsAgentHost::IsDebuggerAttached(web_contents);
  }

  virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const override {
    TabModel* model;
    int index;
    if (!FindTab(&model, &index))
      return NULL;
    WebContents* web_contents = model->GetWebContentsAt(index);
    if (!web_contents) {
      // The tab has been pushed out of memory, pull it back.
      TabAndroid* tab = model->GetTabAt(index);
      if (!tab)
        return NULL;

      if (!tab->LoadIfNeeded())
        return NULL;

      web_contents = model->GetWebContentsAt(index);
      if (!web_contents)
        return NULL;
    }
    return DevToolsAgentHost::GetOrCreateFor(web_contents);
  }

  virtual bool Activate() const override {
    TabModel* model;
    int index;
    if (!FindTab(&model, &index))
      return false;
    model->SetActiveIndex(index);
    return true;
  }

  virtual bool Close() const override {
    TabModel* model;
    int index;
    if (!FindTab(&model, &index))
      return false;
    model->CloseTabAt(index);
    return true;
  }

 private:
  TabTarget(int tab_id, WebContents* web_contents)
      : TargetBase(web_contents),
        tab_id_(tab_id) {
  }

  TabTarget(int tab_id, const base::string16& title, const GURL& url)
      : TargetBase(base::UTF16ToUTF8(title), url),
        tab_id_(tab_id) {
  }

  bool FindTab(TabModel** model_result, int* index_result) const {
    for (TabModelList::const_iterator iter = TabModelList::begin();
        iter != TabModelList::end(); ++iter) {
      TabModel* model = *iter;
      for (int i = 0; i < model->GetTabCount(); ++i) {
        TabAndroid* tab = model->GetTabAt(i);
        if (tab && tab->GetAndroidId() == tab_id_) {
          *model_result = model;
          *index_result = i;
          return true;
        }
      }
    }
    return false;
  }

  const int tab_id_;
};

class NonTabTarget : public TargetBase {
 public:
  explicit NonTabTarget(scoped_refptr<DevToolsAgentHost> agent_host)
      : TargetBase(agent_host),
        agent_host_(agent_host) {
  }

  // content::DevToolsTarget implementation:
  virtual std::string GetId() const override {
    return agent_host_->GetId();
  }

  virtual std::string GetType() const override {
    switch (agent_host_->GetType()) {
      case DevToolsAgentHost::TYPE_WEB_CONTENTS:
        if (TabModelList::begin() == TabModelList::end()) {
          // If there are no tab models we must be running in ChromeShell.
          // Return the 'page' target type for backwards compatibility.
          return kTargetTypePage;
        }
        break;
      case DevToolsAgentHost::TYPE_SERVICE_WORKER:
        return kTargetTypeServiceWorker;
      default:
        break;
    }
    return kTargetTypeOther;
  }

  virtual bool IsAttached() const override {
    return agent_host_->IsAttached();
  }

  virtual scoped_refptr<DevToolsAgentHost> GetAgentHost() const override {
    return agent_host_;
  }

  virtual bool Activate() const override {
    return agent_host_->Activate();
  }

  virtual bool Close() const override {
    return agent_host_->Close();
  }

 private:
  scoped_refptr<DevToolsAgentHost> agent_host_;
};

}  // namespace

DevToolsManagerDelegateAndroid::DevToolsManagerDelegateAndroid()
    : network_protocol_handler_(new DevToolsNetworkProtocolHandler()) {
}

DevToolsManagerDelegateAndroid::~DevToolsManagerDelegateAndroid() {
}

void DevToolsManagerDelegateAndroid::Inspect(
    content::BrowserContext* browser_context,
    content::DevToolsAgentHost* agent_host) {
}

base::DictionaryValue* DevToolsManagerDelegateAndroid::HandleCommand(
    content::DevToolsAgentHost* agent_host,
    base::DictionaryValue* command_dict) {
  return network_protocol_handler_->HandleCommand(agent_host, command_dict);
}

void DevToolsManagerDelegateAndroid::DevToolsAgentStateChanged(
    content::DevToolsAgentHost* agent_host,
    bool attached) {
  network_protocol_handler_->DevToolsAgentStateChanged(agent_host, attached);
}

scoped_ptr<content::DevToolsTarget>
    DevToolsManagerDelegateAndroid::CreateNewTarget(const GURL& url) {
  if (TabModelList::empty())
    return scoped_ptr<content::DevToolsTarget>();

  TabModel* tab_model = TabModelList::get(0);
  if (!tab_model)
    return scoped_ptr<content::DevToolsTarget>();

  WebContents* web_contents = tab_model->CreateNewTabForDevTools(url);
  if (!web_contents)
    return scoped_ptr<content::DevToolsTarget>();

  TabAndroid* tab = TabAndroid::FromWebContents(web_contents);
  if (!tab)
    return scoped_ptr<content::DevToolsTarget>();

  return scoped_ptr<content::DevToolsTarget>(
      TabTarget::CreateForWebContents(tab->GetAndroidId(), web_contents));
}

void DevToolsManagerDelegateAndroid::EnumerateTargets(TargetCallback callback) {
  TargetList targets;

  // Enumerate existing tabs, including the ones with no WebContents.
  std::set<WebContents*> tab_web_contents;
  for (TabModelList::const_iterator iter = TabModelList::begin();
      iter != TabModelList::end(); ++iter) {
    TabModel* model = *iter;
    for (int i = 0; i < model->GetTabCount(); ++i) {
      TabAndroid* tab = model->GetTabAt(i);
      if (!tab)
        continue;

      WebContents* web_contents = model->GetWebContentsAt(i);
      if (web_contents) {
        tab_web_contents.insert(web_contents);
        targets.push_back(TabTarget::CreateForWebContents(tab->GetAndroidId(),
                                                          web_contents));
      } else {
        targets.push_back(TabTarget::CreateForUnloadedTab(tab->GetAndroidId(),
                                                          tab->GetTitle(),
                                                          tab->GetURL()));
      }
    }
  }

  // Add targets for WebContents not associated with any tabs.
  DevToolsAgentHost::List agents = DevToolsAgentHost::GetOrCreateAll();
  for (DevToolsAgentHost::List::iterator it = agents.begin();
       it != agents.end(); ++it) {
    if (WebContents* web_contents = (*it)->GetWebContents()) {
      if (tab_web_contents.find(web_contents) != tab_web_contents.end())
        continue;
    }
    targets.push_back(new NonTabTarget(*it));
  }

  callback.Run(targets);
}

std::string DevToolsManagerDelegateAndroid::GetPageThumbnailData(
    const GURL& url) {
  Profile* profile = ProfileManager::GetLastUsedProfile()->GetOriginalProfile();
  history::TopSites* top_sites = profile->GetTopSites();
  if (top_sites) {
    scoped_refptr<base::RefCountedMemory> data;
    if (top_sites->GetPageThumbnail(url, false, &data))
      return std::string(data->front_as<char>(), data->size());
  }
  return std::string();
}