OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/devtools_discovery/devtools_discovery_manager.h" | 5 #include "components/devtools_discovery/devtools_discovery_manager.h" |
6 | 6 |
7 #include "base/stl_util.h" | 7 #include "base/stl_util.h" |
8 #include "components/devtools_discovery/basic_target_descriptor.h" | |
9 #include "content/public/browser/devtools_agent_host.h" | |
10 | 8 |
11 using content::DevToolsAgentHost; | 9 using content::DevToolsAgentHost; |
12 | 10 |
13 namespace devtools_discovery { | 11 namespace devtools_discovery { |
14 | 12 |
15 // static | 13 // static |
16 DevToolsDiscoveryManager* DevToolsDiscoveryManager::GetInstance() { | 14 DevToolsDiscoveryManager* DevToolsDiscoveryManager::GetInstance() { |
17 return base::Singleton<DevToolsDiscoveryManager>::get(); | 15 return base::Singleton<DevToolsDiscoveryManager>::get(); |
18 } | 16 } |
19 | 17 |
20 DevToolsDiscoveryManager::DevToolsDiscoveryManager() { | 18 DevToolsDiscoveryManager::DevToolsDiscoveryManager() { |
21 } | 19 } |
22 | 20 |
23 DevToolsDiscoveryManager::~DevToolsDiscoveryManager() { | 21 DevToolsDiscoveryManager::~DevToolsDiscoveryManager() { |
24 base::STLDeleteElements(&providers_); | 22 base::STLDeleteElements(&providers_); |
25 } | 23 } |
26 | 24 |
27 void DevToolsDiscoveryManager::AddProvider(std::unique_ptr<Provider> provider) { | 25 void DevToolsDiscoveryManager::AddProvider(std::unique_ptr<Provider> provider) { |
28 providers_.push_back(provider.release()); | 26 providers_.push_back(provider.release()); |
29 } | 27 } |
30 | 28 |
31 DevToolsTargetDescriptor::List DevToolsDiscoveryManager::GetDescriptors() { | 29 content::DevToolsAgentHost::List DevToolsDiscoveryManager::GetDescriptors() { |
32 if (providers_.size()) | 30 if (providers_.size()) |
33 return GetDescriptorsFromProviders(); | 31 return GetDescriptorsFromProviders(); |
34 | 32 |
35 DevToolsAgentHost::List agent_hosts = DevToolsAgentHost::GetOrCreateAll(); | 33 return DevToolsAgentHost::GetOrCreateAll(); |
36 DevToolsTargetDescriptor::List result; | |
37 result.reserve(agent_hosts.size()); | |
38 for (const auto& agent_host : agent_hosts) | |
39 result.push_back(new BasicTargetDescriptor(agent_host)); | |
40 return result; | |
41 } | 34 } |
42 | 35 |
43 void DevToolsDiscoveryManager::SetCreateCallback( | 36 void DevToolsDiscoveryManager::SetCreateCallback( |
44 const CreateCallback& callback) { | 37 const CreateCallback& callback) { |
45 create_callback_ = callback; | 38 create_callback_ = callback; |
46 } | 39 } |
47 | 40 |
48 std::unique_ptr<DevToolsTargetDescriptor> DevToolsDiscoveryManager::CreateNew( | 41 scoped_refptr<content::DevToolsAgentHost> DevToolsDiscoveryManager::CreateNew( |
49 const GURL& url) { | 42 const GURL& url) { |
50 if (create_callback_.is_null()) | 43 if (create_callback_.is_null()) |
51 return nullptr; | 44 return nullptr; |
52 return create_callback_.Run(url); | 45 return create_callback_.Run(url); |
53 } | 46 } |
54 | 47 |
55 DevToolsTargetDescriptor::List | 48 content::DevToolsAgentHost::List |
56 DevToolsDiscoveryManager::GetDescriptorsFromProviders() { | 49 DevToolsDiscoveryManager::GetDescriptorsFromProviders() { |
57 DevToolsTargetDescriptor::List result; | 50 content::DevToolsAgentHost::List result; |
58 for (auto* provider : providers_) { | 51 for (auto* provider : providers_) { |
59 DevToolsTargetDescriptor::List partial = provider->GetDescriptors(); | 52 content::DevToolsAgentHost::List partial = provider->GetDescriptors(); |
60 result.insert(result.begin(), partial.begin(), partial.end()); | 53 result.insert(result.begin(), partial.begin(), partial.end()); |
61 } | 54 } |
62 return result; | 55 return result; |
63 } | 56 } |
64 | 57 |
65 std::unique_ptr<base::DictionaryValue> | 58 std::unique_ptr<base::DictionaryValue> |
66 DevToolsDiscoveryManager::HandleCreateTargetCommand( | 59 DevToolsDiscoveryManager::HandleCreateTargetCommand( |
67 base::DictionaryValue* command_dict) { | 60 base::DictionaryValue* command_dict) { |
68 int id; | 61 int id; |
69 std::string method; | 62 std::string method; |
70 std::string url; | 63 std::string url; |
71 const base::DictionaryValue* params_dict = nullptr; | 64 const base::DictionaryValue* params_dict = nullptr; |
72 if (command_dict->GetInteger("id", &id) && | 65 if (command_dict->GetInteger("id", &id) && |
73 command_dict->GetString("method", &method) && | 66 command_dict->GetString("method", &method) && |
74 method == "Browser.createTarget" && | 67 method == "Browser.createTarget" && |
75 command_dict->GetDictionary("params", ¶ms_dict) && | 68 command_dict->GetDictionary("params", ¶ms_dict) && |
76 params_dict->GetString("url", &url)) { | 69 params_dict->GetString("url", &url)) { |
77 std::unique_ptr<devtools_discovery::DevToolsTargetDescriptor> descriptor = | 70 scoped_refptr<content::DevToolsAgentHost> host = CreateNew(GURL(url)); |
78 CreateNew(GURL(url)); | 71 if (!host) |
79 if (!descriptor) | |
80 return nullptr; | 72 return nullptr; |
81 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | 73 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); |
82 result->SetInteger("id", id); | 74 result->SetInteger("id", id); |
83 std::unique_ptr<base::DictionaryValue> cmd_result( | 75 std::unique_ptr<base::DictionaryValue> cmd_result( |
84 new base::DictionaryValue()); | 76 new base::DictionaryValue()); |
85 cmd_result->SetString("targetId", descriptor->GetAgentHost()->GetId()); | 77 cmd_result->SetString("targetId", host->GetId()); |
86 result->Set("result", std::move(cmd_result)); | 78 result->Set("result", std::move(cmd_result)); |
87 return result; | 79 return result; |
88 } | 80 } |
89 return nullptr; | 81 return nullptr; |
90 } | 82 } |
91 | 83 |
92 } // namespace devtools_discovery | 84 } // namespace devtools_discovery |
OLD | NEW |