Commit 08f4508d authored by yileili's avatar yileili Committed by Commit Bot

Add cast http apis for assistant services.

Bug: b:118193766
Test: Manual
Change-Id: I6da9d8458a59701ae7c9ed642d16094e2f36782b
Reviewed-on: https://chromium-review.googlesource.com/c/1304489Reviewed-by: default avatarXiaohui Chen <xiaohuic@chromium.org>
Commit-Queue: Yilei Li <yileili@google.com>
Cr-Commit-Position: refs/heads/master@{#605395}
parent 93358e14
......@@ -47,6 +47,12 @@ source_set("lib") {
"assistant_manager_service_impl.h",
"assistant_settings_manager_impl.cc",
"assistant_settings_manager_impl.h",
"chromium_api_delegate.cc",
"chromium_api_delegate.h",
"chromium_http_connection.cc",
"chromium_http_connection.h",
"default_url_request_context_getter.cc",
"default_url_request_context_getter.h",
"platform/audio_input_provider_impl.cc",
"platform/audio_input_provider_impl.h",
"platform/audio_media_data_source.cc",
......@@ -78,9 +84,11 @@ source_set("lib") {
"//libassistant/shared/internal_api/c:api_wrappers_entrypoint",
"//libassistant/shared/public",
"//libassistant/shared/public:export",
"//net",
"//services/network/public/cpp",
"//services/network/public/mojom",
"//ui/base",
"//url",
]
libs = [ "$root_out_dir/libassistant.so" ]
......
// Copyright 2018 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 "chromeos/services/assistant/chromium_api_delegate.h"
#include "chromeos/services/assistant/default_url_request_context_getter.h"
namespace chromeos {
namespace assistant {
ChromiumApiDelegate::ChromiumApiDelegate()
: http_connection_factory_(
base::MakeRefCounted<DefaultURLRequestContextGetter>(
"chromium_http_connection")) {}
ChromiumApiDelegate::~ChromiumApiDelegate() = default;
assistant_client::HttpConnectionFactory*
ChromiumApiDelegate::GetHttpConnectionFactory() {
return &http_connection_factory_;
}
} // namespace assistant
} // namespace chromeos
// Copyright 2018 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.
#ifndef CHROMEOS_SERVICES_ASSISTANT_CHROMIUM_API_DELEGATE_H_
#define CHROMEOS_SERVICES_ASSISTANT_CHROMIUM_API_DELEGATE_H_
#include "chromeos/services/assistant/chromium_http_connection.h"
#include <memory>
#include "base/macros.h"
#include "libassistant/shared/internal_api/fuchsia_api_helper.h"
namespace chromeos {
namespace assistant {
class ChromiumHttpConnectionFactory;
class ChromiumApiDelegate : public assistant_client::FuchsiaApiDelegate {
public:
ChromiumApiDelegate();
~ChromiumApiDelegate() override;
// assistant_client::FuchsiaApiDelegate overrides:
assistant_client::HttpConnectionFactory* GetHttpConnectionFactory() override;
private:
ChromiumHttpConnectionFactory http_connection_factory_;
DISALLOW_COPY_AND_ASSIGN(ChromiumApiDelegate);
};
} // namespace assistant
} // namespace chromeos
#endif // CHROMEOS_SERVICES_ASSISTANT_CHROMIUM_API_DELEGATE_H_
This diff is collapsed.
// Copyright 2018 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.
// The file comes from Google Home(cast) implementation.
#ifndef CHROMEOS_SERVICES_ASSISTANT_CHROMIUM_HTTP_CONNECTION_H_
#define CHROMEOS_SERVICES_ASSISTANT_CHROMIUM_HTTP_CONNECTION_H_
#include <stdint.h>
#include <memory>
#include <string>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/single_thread_task_runner.h"
#include "libassistant/shared/internal_api/http_connection.h"
#include "net/http/http_request_headers.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"
namespace chromeos {
namespace assistant {
// Implements libassistant's HttpConnection using Chromium //net library.
class ChromiumHttpConnection
: public assistant_client::HttpConnection,
public ::net::URLFetcherDelegate,
public base::RefCountedThreadSafe<ChromiumHttpConnection> {
public:
ChromiumHttpConnection(
scoped_refptr<::net::URLRequestContextGetter> url_request_context_getter,
Delegate* delegate);
// assistant_client::HttpConnection implementation:
void SetRequest(const std::string& url, Method method) override;
void AddHeader(const std::string& name, const std::string& value) override;
void SetUploadContent(const std::string& content,
const std::string& content_type) override;
void SetChunkedUploadContentType(const std::string& content_type) override;
void EnableHeaderResponse() override;
void EnablePartialResults() override;
void Start() override;
void Pause() override;
void Resume() override;
void Close() override;
void UploadData(const std::string& data, bool is_last_chunk) override;
protected:
~ChromiumHttpConnection() override;
private:
friend class base::RefCountedThreadSafe<ChromiumHttpConnection>;
enum class State {
NEW,
STARTED,
COMPLETED,
DESTROYED,
};
// HttpConnection methods, re-scheduled on network thread:
void SetRequestOnThread(const std::string& url, Method method);
void AddHeaderOnThread(const std::string& name, const std::string& value);
void SetUploadContentOnThread(const std::string& content,
const std::string& content_type);
void SetChunkedUploadContentTypeOnThread(const std::string& content_type);
void EnablePartialResultsOnThread();
void StartOnThread();
void CloseOnThread();
void UploadDataOnThread(const std::string& data, bool is_last_chunk);
// URLFetcherDelegate implementation:
void OnURLFetchComplete(const ::net::URLFetcher* source) override;
scoped_refptr<::net::URLRequestContextGetter> url_request_context_getter_;
Delegate* const delegate_;
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
State state_ = State::NEW;
// Parameters to be set before Start() call.
GURL url_;
Method method_ = Method::GET;
::net::HttpRequestHeaders headers_;
std::string upload_content_;
std::string upload_content_type_;
std::string chunked_upload_content_type_;
bool handle_partial_response_ = false;
// |url_fetcher_| has to be accessed by network thread only. Some methods
// of URLFetcher (especially GetResponseAsString()) are not safe to access
// from other threads even under lock, since chromium accesses it as well.
std::unique_ptr<::net::URLFetcher> url_fetcher_;
DISALLOW_COPY_AND_ASSIGN(ChromiumHttpConnection);
};
class ChromiumHttpConnectionFactory
: public assistant_client::HttpConnectionFactory {
public:
ChromiumHttpConnectionFactory(
scoped_refptr<::net::URLRequestContextGetter> url_request_context_getter);
~ChromiumHttpConnectionFactory() override;
// assistant_client::HttpConnectionFactory implementation:
assistant_client::HttpConnection* Create(
assistant_client::HttpConnection::Delegate* delegate) override;
private:
scoped_refptr<::net::URLRequestContextGetter> url_request_context_getter_;
DISALLOW_COPY_AND_ASSIGN(ChromiumHttpConnectionFactory);
};
} // namespace assistant
} // namespace chromeos
#endif // CHROMEOS_SERVICES_ASSISTANT_CHROMIUM_HTTP_CONNECTION_H_
// Copyright 2018 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.
// The file comes from Google Home(cast) implementation.
#include "chromeos/services/assistant/default_url_request_context_getter.h"
#include <utility>
#include "base/logging.h"
#include "base/message_loop/message_loop.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread.h"
#include "net/proxy_resolution/proxy_config_service.h"
#include "net/proxy_resolution/proxy_config_service_fixed.h"
#include "net/proxy_resolution/proxy_resolution_service.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_builder.h"
namespace {
std::unique_ptr<::net::ProxyConfigServiceFixed> GetProxyConfigurationFromParams(
const std::string& proxy_server,
const std::string& bypass_list) {
::net::ProxyConfig proxy_config;
proxy_config.proxy_rules().ParseFromString(proxy_server);
proxy_config.proxy_rules().bypass_rules.ParseFromString(bypass_list);
::net::ProxyConfigWithAnnotation annotated_config(proxy_config,
NO_TRAFFIC_ANNOTATION_YET);
return std::make_unique<::net::ProxyConfigServiceFixed>(
::net::ProxyConfigServiceFixed(annotated_config));
}
} // namespace
namespace chromeos {
namespace assistant {
DefaultURLRequestContextGetter::DefaultURLRequestContextGetter(
const std::string& network_thread_name)
: thread_(new base::Thread(network_thread_name)) {
thread_->StartWithOptions(
base::Thread::Options(base::MessageLoop::TYPE_IO, 0));
network_task_runner_ = thread_->task_runner();
DCHECK(network_task_runner_);
}
DefaultURLRequestContextGetter::DefaultURLRequestContextGetter(
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner)
: network_task_runner_(network_task_runner) {
DCHECK(network_task_runner_);
}
DefaultURLRequestContextGetter::~DefaultURLRequestContextGetter() {
if (request_context_) {
// The context should be destroyed on the network thread.
network_task_runner_->DeleteSoon(FROM_HERE, request_context_.release());
}
if (thread_)
thread_->Stop();
}
void DefaultURLRequestContextGetter::CreateContext() {
// Context must be created on network thread since its internal objects
// create and enforce thread checkers.
DCHECK(network_task_runner_->BelongsToCurrentThread());
::net::URLRequestContextBuilder builder;
// Set direct proxy configuration.
builder.set_proxy_config_service(GetProxyConfigurationFromParams("", ""));
builder.DisableHttpCache();
request_context_ = builder.Build();
CHECK(request_context_);
}
::net::URLRequestContext*
DefaultURLRequestContextGetter::GetURLRequestContext() {
DCHECK(network_task_runner_->BelongsToCurrentThread());
if (!request_context_)
CreateContext();
return request_context_.get();
}
scoped_refptr<base::SingleThreadTaskRunner>
DefaultURLRequestContextGetter::GetNetworkTaskRunner() const {
return network_task_runner_;
}
void DefaultURLRequestContextGetter::SetProxyConfiguration(
const std::string& proxy_server,
const std::string& bypass_list) {
network_task_runner_->PostTask(
FROM_HERE,
base::Bind(&DefaultURLRequestContextGetter::SetProxyConfigurationInternal,
this, proxy_server, bypass_list));
}
void DefaultURLRequestContextGetter::SetProxyConfigurationInternal(
const std::string& proxy_server,
const std::string& bypass_list) {
DCHECK(network_task_runner_->BelongsToCurrentThread());
GetURLRequestContext()->proxy_resolution_service()->ResetConfigService(
GetProxyConfigurationFromParams(proxy_server, bypass_list));
}
} // namespace assistant
} // namespace chromeos
// Copyright 2018 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.
// The file comes from Google Home(cast) implementation.
#ifndef CHROMEOS_SERVICES_ASSISTANT_DEFAULT_URL_REQUEST_CONTEXT_GETTER_H_
#define CHROMEOS_SERVICES_ASSISTANT_DEFAULT_URL_REQUEST_CONTEXT_GETTER_H_
#include <memory>
#include <string>
#include "base/memory/ref_counted.h"
#include "net/url_request/url_request_context_getter.h"
namespace base {
class Thread;
} // namespace base
namespace chromeos {
namespace assistant {
// A default URLRequestContextGetter implementation for creating a URL request
// context for HTTP-related communications in the voice UI client.
//
// URL request context will have no HTTP caching.
//
// Instance of this class should be kept and reused for as long as possible.
// Some internal objects, such as HostResolver are not always safe to destroy
// and may cause random crashes (b/30282661).
class DefaultURLRequestContextGetter : public ::net::URLRequestContextGetter {
public:
// Creates a new task runner thread with the given name.
explicit DefaultURLRequestContextGetter(
const std::string& network_thread_name);
// Uses the provided |network_task_runner| as the network task runner.
explicit DefaultURLRequestContextGetter(
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner);
// net::URLRequestContextGetter implementation:
::net::URLRequestContext* GetURLRequestContext() override;
scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner()
const override;
void SetProxyConfiguration(const std::string& proxy_server,
const std::string& bypass_list);
private:
~DefaultURLRequestContextGetter() override;
void CreateContext();
void SetProxyConfigurationInternal(const std::string& proxy_server,
const std::string& bypass_list);
// |thread_| is non-null if created by this class.
std::unique_ptr<base::Thread> thread_;
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_;
std::unique_ptr<::net::URLRequestContext> request_context_;
DISALLOW_COPY_AND_ASSIGN(DefaultURLRequestContextGetter);
};
} // namespace assistant
} // namespace chromeos
#endif // CHROMEOS_SERVICES_ASSISTANT_DEFAULT_URL_REQUEST_CONTEXT_GETTER_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment