Chromium Code Reviews| 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 "remoting/test/hostlist_fetcher.h" | |
| 6 | |
| 7 #include <iostream> | |
|
joedow
2015/07/01 03:39:35
You should not be using iostream, use printf or DV
tonychun
2015/07/06 20:11:51
I put it in because I wanted to output the hostlis
| |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/callback_helpers.h" | |
| 11 #include "base/json/json_reader.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/message_loop/message_loop.h" | |
|
joedow
2015/07/01 03:39:34
Where is message_loop used? Remove it if not used
tonychun
2015/07/06 20:11:50
Done.
| |
| 14 #include "base/strings/stringprintf.h" | |
|
joedow
2015/07/01 03:39:35
remove header and StringPrintf call since it isn't
tonychun
2015/07/06 20:11:51
Done.
| |
| 15 #include "base/thread_task_runner_handle.h" | |
| 16 #include "base/values.h" | |
| 17 #include "net/http/http_response_headers.h" | |
| 18 #include "net/http/http_status_code.h" | |
| 19 #include "net/url_request/url_fetcher.h" | |
| 20 #include "remoting/base/url_request_context_getter.h" | |
| 21 | |
| 22 namespace remoting { | |
| 23 namespace test { | |
| 24 | |
| 25 HostlistFetcher::HostlistFetcher() { | |
| 26 } | |
| 27 | |
| 28 HostlistFetcher::~HostlistFetcher() { | |
| 29 } | |
| 30 | |
| 31 bool HostlistFetcher::RetrieveHostlist(const std::string& access_token, | |
| 32 ServiceEnvironment service_environment, | |
| 33 const HostlistCallback& callback) { | |
| 34 // Message the Directory service for the hostlist | |
|
joedow
2015/07/01 03:39:35
I don't think this comment is useful, please remov
tonychun
2015/07/06 20:11:51
Done.
| |
| 35 DVLOG(2) << "HostlistFetcher::RetrieveHostlist() called"; | |
| 36 | |
|
joedow
2015/07/01 03:39:35
Please add some additional DCHECKS here. You shou
tonychun
2015/07/06 20:11:51
I removed service_environment because it is only t
| |
| 37 | |
|
joedow
2015/07/01 03:39:35
Why so many empty newlines? Please reduce to one.
tonychun
2015/07/06 20:11:51
Done.
| |
| 38 | |
| 39 std::string service_url; | |
| 40 switch (service_environment) { | |
| 41 case kProductionEnvironment: | |
| 42 DVLOG(1) << "Configuring service request for prod environment"; | |
| 43 service_url = base::StringPrintf(kProdServiceEnvironmentUrlFormat); | |
|
joedow
2015/07/01 03:39:35
Why are you using StringPrintf here? You aren't r
tonychun
2015/07/06 20:11:50
Done.
| |
| 44 break; | |
| 45 | |
| 46 default: | |
| 47 LOG(ERROR) << "Unrecognized service type: " << service_environment; | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 hostlist_callback_ = callback; | |
| 52 | |
| 53 request_context_getter_ = new remoting::URLRequestContextGetter( | |
| 54 base::ThreadTaskRunnerHandle::Get(), // network_runner | |
| 55 base::ThreadTaskRunnerHandle::Get()); // file_runner | |
| 56 | |
| 57 request_ = | |
| 58 net::URLFetcher::Create(GURL(service_url), net::URLFetcher::GET, this); | |
| 59 request_->SetRequestContext(request_context_getter_.get()); | |
| 60 request_->AddExtraRequestHeader("Authorization: OAuth " + access_token); | |
| 61 request_->SetUploadData("application/json; charset=UTF-8", "{}"); | |
|
joedow
2015/07/01 03:39:35
Is this line needed for this call? I'm not sure t
tonychun
2015/07/06 20:11:51
Removed.
| |
| 62 request_->Start(); | |
| 63 | |
| 64 return true; | |
| 65 } | |
| 66 | |
| 67 void HostlistFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | |
| 68 DCHECK(source); | |
| 69 DVLOG(2) << "URL Fetch Completed for: " << source->GetOriginalURL(); | |
| 70 | |
| 71 std::vector<ChromotingHostInfo> hostlist; | |
| 72 int response_code = request_->GetResponseCode(); | |
| 73 if (response_code != net::HTTP_OK) { | |
| 74 LOG(ERROR) << "Hostlist request failed with error code: " | |
| 75 << response_code; | |
| 76 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
|
joedow
2015/07/01 03:39:35
I think a ScopedClosureRunner() would make the mul
tonychun
2015/07/06 20:11:51
Done.
| |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 std::string response_string; | |
| 81 if (!request_->GetResponseAsString(&response_string)) { | |
| 82 LOG(ERROR) << "Failed to retrieve Hostlist response data"; | |
| 83 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
| 84 return; | |
| 85 } | |
| 86 | |
| 87 scoped_ptr<base::Value> response_value( | |
| 88 base::JSONReader::Read(response_string)); | |
| 89 if (!response_value || | |
| 90 !response_value->IsType(base::Value::TYPE_DICTIONARY)) { | |
| 91 LOG(ERROR) << "Failed to parse response string to JSON"; | |
| 92 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 const base::DictionaryValue* response; | |
| 97 if (!response_value->GetAsDictionary(&response)) { | |
| 98 LOG(ERROR) << "Failed to convert parsed JSON to a dictionary object"; | |
| 99 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
| 100 return; | |
| 101 } | |
| 102 | |
| 103 // add hosts to hostlist | |
|
joedow
2015/07/01 03:39:35
Is this comment useful? Please remove it or updat
tonychun
2015/07/06 20:11:51
Done.
| |
| 104 const base::ListValue* items = nullptr; | |
| 105 response->GetList("data.items", &items); | |
| 106 | |
| 107 int size = items->GetSize(); | |
|
joedow
2015/07/01 03:39:35
What if items is a nullptr, you should make sure i
tonychun
2015/07/06 20:11:51
Done.
| |
| 108 DVLOG(1) << "There are " << size << " hosts in the hostlist"; | |
| 109 const base::DictionaryValue* item; | |
|
joedow
2015/07/01 03:39:35
it's a good idea to initialize pointers to nullptr
tonychun
2015/07/06 20:11:51
Done.
| |
| 110 for (int i = 0; i < size; ++i) { | |
| 111 items->GetDictionary(i,&item); | |
|
joedow
2015/07/01 03:39:35
add space between comma and ampersand (and run cpp
tonychun
2015/07/06 20:11:51
Done.
| |
| 112 hostlist.push_back(ChromotingHostInfo(*item)); | |
| 113 } | |
| 114 | |
| 115 base::ResetAndReturn(&hostlist_callback_).Run(hostlist); | |
| 116 } | |
| 117 | |
| 118 } // namespace test | |
| 119 } // namespace remoting | |
| OLD | NEW |