| 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 "components/devtools_discovery/devtools_discovery_manager.h" | |
| 6 | |
| 7 #include "base/stl_util.h" | |
| 8 | |
| 9 using content::DevToolsAgentHost; | |
| 10 | |
| 11 namespace devtools_discovery { | |
| 12 | |
| 13 // static | |
| 14 DevToolsDiscoveryManager* DevToolsDiscoveryManager::GetInstance() { | |
| 15 return base::Singleton<DevToolsDiscoveryManager>::get(); | |
| 16 } | |
| 17 | |
| 18 DevToolsDiscoveryManager::DevToolsDiscoveryManager() { | |
| 19 } | |
| 20 | |
| 21 DevToolsDiscoveryManager::~DevToolsDiscoveryManager() { | |
| 22 base::STLDeleteElements(&providers_); | |
| 23 } | |
| 24 | |
| 25 void DevToolsDiscoveryManager::AddProvider(std::unique_ptr<Provider> provider) { | |
| 26 providers_.push_back(provider.release()); | |
| 27 } | |
| 28 | |
| 29 content::DevToolsAgentHost::List DevToolsDiscoveryManager::GetDescriptors() { | |
| 30 if (providers_.size()) | |
| 31 return GetDescriptorsFromProviders(); | |
| 32 | |
| 33 return DevToolsAgentHost::GetOrCreateAll(); | |
| 34 } | |
| 35 | |
| 36 void DevToolsDiscoveryManager::SetCreateCallback( | |
| 37 const CreateCallback& callback) { | |
| 38 create_callback_ = callback; | |
| 39 } | |
| 40 | |
| 41 scoped_refptr<content::DevToolsAgentHost> DevToolsDiscoveryManager::CreateNew( | |
| 42 const GURL& url) { | |
| 43 if (create_callback_.is_null()) | |
| 44 return nullptr; | |
| 45 return create_callback_.Run(url); | |
| 46 } | |
| 47 | |
| 48 content::DevToolsAgentHost::List | |
| 49 DevToolsDiscoveryManager::GetDescriptorsFromProviders() { | |
| 50 content::DevToolsAgentHost::List result; | |
| 51 for (auto* provider : providers_) { | |
| 52 content::DevToolsAgentHost::List partial = provider->GetDescriptors(); | |
| 53 result.insert(result.begin(), partial.begin(), partial.end()); | |
| 54 } | |
| 55 return result; | |
| 56 } | |
| 57 | |
| 58 std::unique_ptr<base::DictionaryValue> | |
| 59 DevToolsDiscoveryManager::HandleCreateTargetCommand( | |
| 60 base::DictionaryValue* command_dict) { | |
| 61 int id; | |
| 62 std::string method; | |
| 63 std::string url; | |
| 64 const base::DictionaryValue* params_dict = nullptr; | |
| 65 if (command_dict->GetInteger("id", &id) && | |
| 66 command_dict->GetString("method", &method) && | |
| 67 method == "Browser.createTarget" && | |
| 68 command_dict->GetDictionary("params", ¶ms_dict) && | |
| 69 params_dict->GetString("url", &url)) { | |
| 70 scoped_refptr<content::DevToolsAgentHost> host = CreateNew(GURL(url)); | |
| 71 if (!host) | |
| 72 return nullptr; | |
| 73 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue()); | |
| 74 result->SetInteger("id", id); | |
| 75 std::unique_ptr<base::DictionaryValue> cmd_result( | |
| 76 new base::DictionaryValue()); | |
| 77 cmd_result->SetString("targetId", host->GetId()); | |
| 78 result->Set("result", std::move(cmd_result)); | |
| 79 return result; | |
| 80 } | |
| 81 return nullptr; | |
| 82 } | |
| 83 | |
| 84 } // namespace devtools_discovery | |
| OLD | NEW |