webContents: set http useragent settings when overriding user agent
This commit is contained in:
parent
967c273ddb
commit
cb4309bbd9
2 changed files with 36 additions and 23 deletions
|
@ -6,12 +6,15 @@
|
||||||
|
|
||||||
#include <set>
|
#include <set>
|
||||||
|
|
||||||
|
#include "atom/browser/browser.h"
|
||||||
#include "atom/browser/api/atom_api_session.h"
|
#include "atom/browser/api/atom_api_session.h"
|
||||||
#include "atom/browser/atom_browser_client.h"
|
#include "atom/browser/atom_browser_client.h"
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
#include "atom/browser/native_window.h"
|
#include "atom/browser/native_window.h"
|
||||||
#include "atom/browser/web_view_guest_delegate.h"
|
#include "atom/browser/web_view_guest_delegate.h"
|
||||||
|
#include "atom/common/atom_version.h"
|
||||||
|
#include "atom/common/chrome_version.h"
|
||||||
#include "atom/common/api/api_messages.h"
|
#include "atom/common/api/api_messages.h"
|
||||||
#include "atom/common/event_emitter_caller.h"
|
#include "atom/common/event_emitter_caller.h"
|
||||||
#include "atom/common/native_mate_converters/gfx_converter.h"
|
#include "atom/common/native_mate_converters/gfx_converter.h"
|
||||||
|
@ -20,6 +23,7 @@
|
||||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||||
#include "atom/common/native_mate_converters/value_converter.h"
|
#include "atom/common/native_mate_converters/value_converter.h"
|
||||||
#include "base/strings/string_util.h"
|
#include "base/strings/string_util.h"
|
||||||
|
#include "base/strings/stringprintf.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
#include "brightray/browser/inspectable_web_contents.h"
|
#include "brightray/browser/inspectable_web_contents.h"
|
||||||
#include "chrome/browser/printing/print_view_manager_basic.h"
|
#include "chrome/browser/printing/print_view_manager_basic.h"
|
||||||
|
@ -36,10 +40,12 @@
|
||||||
#include "content/public/browser/storage_partition.h"
|
#include "content/public/browser/storage_partition.h"
|
||||||
#include "content/public/browser/site_instance.h"
|
#include "content/public/browser/site_instance.h"
|
||||||
#include "content/public/browser/web_contents.h"
|
#include "content/public/browser/web_contents.h"
|
||||||
|
#include "content/public/common/user_agent.h"
|
||||||
#include "native_mate/callback.h"
|
#include "native_mate/callback.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
#include "native_mate/object_template_builder.h"
|
#include "native_mate/object_template_builder.h"
|
||||||
#include "net/http/http_response_headers.h"
|
#include "net/http/http_response_headers.h"
|
||||||
|
#include "net/url_request/static_http_user_agent_settings.h"
|
||||||
#include "net/url_request/url_request_context.h"
|
#include "net/url_request/url_request_context.h"
|
||||||
|
|
||||||
#include "atom/common/node_includes.h"
|
#include "atom/common/node_includes.h"
|
||||||
|
@ -51,6 +57,20 @@ struct PrintSettings {
|
||||||
bool print_background;
|
bool print_background;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
std::string RemoveWhitespace(const std::string& str) {
|
||||||
|
std::string trimmed;
|
||||||
|
if (base::RemoveChars(str, " ", &trimmed))
|
||||||
|
return trimmed;
|
||||||
|
else
|
||||||
|
return str;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SetUserAgentInIO(scoped_refptr<net::URLRequestContextGetter> getter,
|
||||||
|
std::string user_agent) {
|
||||||
|
getter->GetURLRequestContext()->set_http_user_agent_settings(
|
||||||
|
new net::StaticHttpUserAgentSettings("en-us,en", user_agent));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
namespace mate {
|
namespace mate {
|
||||||
|
@ -167,6 +187,7 @@ WebContents::WebContents(const mate::Dictionary& options) {
|
||||||
Observe(web_contents);
|
Observe(web_contents);
|
||||||
AttachAsUserData(web_contents);
|
AttachAsUserData(web_contents);
|
||||||
InitWithWebContents(web_contents);
|
InitWithWebContents(web_contents);
|
||||||
|
SetUserAgent(std::string());
|
||||||
|
|
||||||
if (is_guest) {
|
if (is_guest) {
|
||||||
guest_delegate_->Initialize(this);
|
guest_delegate_->Initialize(this);
|
||||||
|
@ -546,7 +567,22 @@ bool WebContents::IsCrashed() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::SetUserAgent(const std::string& user_agent) {
|
void WebContents::SetUserAgent(const std::string& user_agent) {
|
||||||
|
if (user_agent.empty()) {
|
||||||
|
// Default User Agent.
|
||||||
|
Browser* browser = Browser::Get();
|
||||||
|
std::string product_name = base::StringPrintf(
|
||||||
|
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
|
||||||
|
RemoveWhitespace(browser->GetName()).c_str(),
|
||||||
|
browser->GetVersion().c_str(),
|
||||||
|
CHROME_VERSION_STRING);
|
||||||
|
const_cast<std::string&>(user_agent) =
|
||||||
|
content::BuildUserAgentFromProduct(product_name);
|
||||||
|
}
|
||||||
web_contents()->SetUserAgentOverride(user_agent);
|
web_contents()->SetUserAgentOverride(user_agent);
|
||||||
|
scoped_refptr<net::URLRequestContextGetter> getter =
|
||||||
|
web_contents()->GetBrowserContext()->GetRequestContext();
|
||||||
|
getter->GetNetworkTaskRunner()->PostTask(FROM_HERE,
|
||||||
|
base::Bind(&SetUserAgentInIO, getter, user_agent));
|
||||||
}
|
}
|
||||||
|
|
||||||
void WebContents::InsertCSS(const std::string& css) {
|
void WebContents::InsertCSS(const std::string& css) {
|
||||||
|
|
|
@ -10,11 +10,8 @@
|
||||||
|
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
#include "atom/browser/atom_browser_main_parts.h"
|
#include "atom/browser/atom_browser_main_parts.h"
|
||||||
#include "atom/browser/browser.h"
|
|
||||||
#include "atom/browser/window_list.h"
|
#include "atom/browser/window_list.h"
|
||||||
#include "atom/common/api/api_messages.h"
|
#include "atom/common/api/api_messages.h"
|
||||||
#include "atom/common/atom_version.h"
|
|
||||||
#include "atom/common/chrome_version.h"
|
|
||||||
#include "atom/common/native_mate_converters/image_converter.h"
|
#include "atom/common/native_mate_converters/image_converter.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||||
#include "atom/common/options_switches.h"
|
#include "atom/common/options_switches.h"
|
||||||
|
@ -24,7 +21,6 @@
|
||||||
#include "base/prefs/pref_service.h"
|
#include "base/prefs/pref_service.h"
|
||||||
#include "base/message_loop/message_loop.h"
|
#include "base/message_loop/message_loop.h"
|
||||||
#include "base/strings/string_number_conversions.h"
|
#include "base/strings/string_number_conversions.h"
|
||||||
#include "base/strings/stringprintf.h"
|
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
#include "brightray/browser/inspectable_web_contents.h"
|
#include "brightray/browser/inspectable_web_contents.h"
|
||||||
#include "brightray/browser/inspectable_web_contents_view.h"
|
#include "brightray/browser/inspectable_web_contents_view.h"
|
||||||
|
@ -36,7 +32,6 @@
|
||||||
#include "content/public/browser/render_widget_host_view.h"
|
#include "content/public/browser/render_widget_host_view.h"
|
||||||
#include "content/public/common/content_switches.h"
|
#include "content/public/common/content_switches.h"
|
||||||
#include "content/public/common/renderer_preferences.h"
|
#include "content/public/common/renderer_preferences.h"
|
||||||
#include "content/public/common/user_agent.h"
|
|
||||||
#include "content/public/common/web_preferences.h"
|
#include "content/public/common/web_preferences.h"
|
||||||
#include "ipc/ipc_message_macros.h"
|
#include "ipc/ipc_message_macros.h"
|
||||||
#include "native_mate/dictionary.h"
|
#include "native_mate/dictionary.h"
|
||||||
|
@ -73,14 +68,6 @@ const char* kWebRuntimeFeatures[] = {
|
||||||
switches::kPageVisibility,
|
switches::kPageVisibility,
|
||||||
};
|
};
|
||||||
|
|
||||||
std::string RemoveWhitespace(const std::string& str) {
|
|
||||||
std::string trimmed;
|
|
||||||
if (base::RemoveChars(str, " ", &trimmed))
|
|
||||||
return trimmed;
|
|
||||||
else
|
|
||||||
return str;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
NativeWindow::NativeWindow(
|
NativeWindow::NativeWindow(
|
||||||
|
@ -130,16 +117,6 @@ NativeWindow::NativeWindow(
|
||||||
options.Get(switches::kZoomFactor, &zoom_factor_);
|
options.Get(switches::kZoomFactor, &zoom_factor_);
|
||||||
|
|
||||||
WindowList::AddWindow(this);
|
WindowList::AddWindow(this);
|
||||||
|
|
||||||
// Override the user agent to contain application and atom-shell's version.
|
|
||||||
Browser* browser = Browser::Get();
|
|
||||||
std::string product_name = base::StringPrintf(
|
|
||||||
"%s/%s Chrome/%s " ATOM_PRODUCT_NAME "/" ATOM_VERSION_STRING,
|
|
||||||
RemoveWhitespace(browser->GetName()).c_str(),
|
|
||||||
browser->GetVersion().c_str(),
|
|
||||||
CHROME_VERSION_STRING);
|
|
||||||
web_contents()->GetMutableRendererPrefs()->user_agent_override =
|
|
||||||
content::BuildUserAgentFromProduct(product_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
NativeWindow::~NativeWindow() {
|
NativeWindow::~NativeWindow() {
|
||||||
|
|
Loading…
Reference in a new issue