1// Copyright 2014 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/devtools/chrome_devtools_manager_delegate.h"
6
7#include "base/values.h"
8#include "chrome/browser/devtools/devtools_network_conditions.h"
9#include "chrome/browser/devtools/devtools_network_controller.h"
10#include "chrome/browser/devtools/devtools_protocol.h"
11#include "chrome/browser/devtools/devtools_protocol_constants.h"
12#include "chrome/browser/devtools/devtools_window.h"
13#include "chrome/browser/profiles/profile.h"
14#include "content/public/browser/browser_thread.h"
15#include "content/public/browser/devtools_agent_host.h"
16#include "content/public/browser/devtools_manager.h"
17#include "content/public/browser/render_process_host.h"
18#include "content/public/browser/render_view_host.h"
19#include "content/public/browser/site_instance.h"
20
21ChromeDevToolsManagerDelegate::ChromeDevToolsManagerDelegate() {
22}
23
24ChromeDevToolsManagerDelegate::~ChromeDevToolsManagerDelegate() {
25}
26
27void ChromeDevToolsManagerDelegate::Inspect(
28    content::BrowserContext* browser_context,
29    content::DevToolsAgentHost* agent_host) {
30  if (!agent_host->IsWorker()) {
31    // TODO(horo): Support other types of DevToolsAgentHost when necessary.
32    NOTREACHED() << "Inspect() only supports workers.";
33  }
34#if !defined(OS_ANDROID)
35  if (Profile* profile = Profile::FromBrowserContext(browser_context))
36    DevToolsWindow::OpenDevToolsWindowForWorker(profile, agent_host);
37#endif
38}
39
40base::DictionaryValue* ChromeDevToolsManagerDelegate::HandleCommand(
41    content::DevToolsAgentHost* agent_host,
42    base::DictionaryValue* command_dict) {
43  scoped_ptr<DevToolsProtocol::Command> command(
44       DevToolsProtocol::ParseCommand(command_dict));
45  if (!command)
46    return NULL;
47
48  const std::string method = command->method();
49  scoped_ptr<DevToolsProtocol::Response> response;
50
51  if (method == chrome::devtools::Network::emulateNetworkConditions::kName)
52    response = EmulateNetworkConditions(agent_host, command.get());
53
54  if (response)
55    return response->Serialize();
56  return NULL;
57}
58
59Profile* ChromeDevToolsManagerDelegate::GetProfile(
60    content::DevToolsAgentHost* agent_host) {
61  content::RenderViewHost* host = agent_host->GetRenderViewHost();
62  if (!host)
63    return NULL;
64  return Profile::FromBrowserContext(host->GetSiteInstance()->GetProcess()->
65      GetBrowserContext());
66}
67
68scoped_ptr<DevToolsProtocol::Response>
69ChromeDevToolsManagerDelegate::EmulateNetworkConditions(
70    content::DevToolsAgentHost* agent_host,
71    DevToolsProtocol::Command* command) {
72  base::DictionaryValue* params = command->params();
73  namespace names = ::chrome::devtools::Network::emulateNetworkConditions;
74
75  bool offline = false;
76  if (!params || !params->GetBoolean(names::kParamOffline, &offline))
77    return command->InvalidParamResponse(names::kParamOffline);
78
79  double latency = 0.0;
80  if (!params->GetDouble(names::kParamLatency, &latency))
81    return command->InvalidParamResponse(names::kParamLatency);
82  if (latency < 0.0)
83    latency = 0.0;
84
85  double download_throughput = 0.0;
86  if (!params->GetDouble(names::kParamDownloadThroughput, &download_throughput))
87    return command->InvalidParamResponse(names::kParamDownloadThroughput);
88  if (download_throughput < 0.0)
89    download_throughput = 0.0;
90
91  double upload_throughput = 0.0;
92  if (!params->GetDouble(names::kParamUploadThroughput, &upload_throughput))
93    return command->InvalidParamResponse(names::kParamUploadThroughput);
94  if (upload_throughput < 0.0)
95    upload_throughput = 0.0;
96
97  scoped_ptr<DevToolsNetworkConditions> conditions(
98      new DevToolsNetworkConditions(
99          offline, latency, download_throughput, upload_throughput));
100
101  UpdateNetworkState(agent_host, conditions.Pass());
102  return command->SuccessResponse(NULL);
103}
104
105void ChromeDevToolsManagerDelegate::UpdateNetworkState(
106    content::DevToolsAgentHost* agent_host,
107    scoped_ptr<DevToolsNetworkConditions> conditions) {
108  Profile* profile = GetProfile(agent_host);
109  if (!profile)
110    return;
111  profile->GetDevToolsNetworkController()->SetNetworkState(
112      agent_host->GetId(), conditions.Pass());
113}
114
115void ChromeDevToolsManagerDelegate::DevToolsAgentStateChanged(
116    content::DevToolsAgentHost* agent_host,
117    bool attached) {
118  scoped_ptr<DevToolsNetworkConditions> conditions;
119  if (attached)
120    conditions.reset(new DevToolsNetworkConditions());
121  UpdateNetworkState(agent_host, conditions.Pass());
122}
123