Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9094f2d

Browse files
committed
Override User-Agent from package.json
Fix nwjs#263
1 parent 75926a5 commit 9094f2d

5 files changed

Lines changed: 34 additions & 2 deletions

File tree

src/common/shell_switches.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,5 @@ const char kmPlugin[] = "plugin";
8686
// Whether to enable page caches.
8787
const char kmPageCache[] = "page-cache";
8888

89+
const char kmUserAgent[] = "user-agent";
8990
} // namespace switches

src/common/shell_switches.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ extern const char kmWebgl[];
4949
extern const char kmJava[];
5050
extern const char kmPlugin[];
5151
extern const char kmPageCache[];
52+
extern const char kmUserAgent[];
5253

5354
} // namespace switches
5455

src/nw_shell.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,12 @@ Shell* Shell::Create(BrowserContext* browser_context,
7171
base_web_contents);
7272

7373
Shell* shell = new Shell(web_contents, GetPackage()->window());
74-
web_contents->GetController().LoadURL(
75-
url, Referrer(), PAGE_TRANSITION_TYPED, std::string());
74+
NavigationController::LoadURLParams params(url);
75+
params.transition_type = PAGE_TRANSITION_TYPED;
76+
params.override_user_agent = NavigationController::UA_OVERRIDE_TRUE;
77+
78+
web_contents->GetController().LoadURLWithParams(params);
79+
7680
return shell;
7781
}
7882

src/shell_content_browser_client.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,21 @@
2323
#include "base/command_line.h"
2424
#include "base/file_path.h"
2525
#include "base/file_util.h"
26+
#include "base/string_util.h"
2627
#include "base/threading/thread_restrictions.h"
2728
#include "base/values.h"
2829
#include "content/public/browser/browser_url_handler.h"
2930
#include "content/public/browser/resource_dispatcher_host.h"
31+
#include "content/public/browser/web_contents.h"
32+
#include "content/public/common/renderer_preferences.h"
3033
#include "content/nw/src/api/dispatcher_host.h"
3134
#include "content/nw/src/common/shell_switches.h"
3235
#include "content/nw/src/browser/shell_devtools_delegate.h"
3336
#include "content/nw/src/browser/shell_resource_dispatcher_host_delegate.h"
3437
#include "content/nw/src/media/media_internals.h"
3538
#include "content/nw/src/nw_package.h"
3639
#include "content/nw/src/nw_shell.h"
40+
#include "content/nw/src/nw_version.h"
3741
#include "content/nw/src/shell_browser_context.h"
3842
#include "content/nw/src/shell_browser_main_parts.h"
3943
#include "geolocation/shell_access_token_store.h"
@@ -57,6 +61,25 @@ BrowserMainParts* ShellContentBrowserClient::CreateBrowserMainParts(
5761
return shell_browser_main_parts_;
5862
}
5963

64+
WebContentsView* ShellContentBrowserClient::OverrideCreateWebContentsView(
65+
WebContents* web_contents,
66+
RenderViewHostDelegateView** render_view_host_delegate_view) {
67+
std::string user_agent;
68+
nw::Package* package = shell_browser_main_parts()->package();
69+
if (package->root()->GetString(switches::kmUserAgent, &user_agent)) {
70+
std::string name, version;
71+
package->root()->GetString(switches::kmName, &name);
72+
package->root()->GetString("version", &version);
73+
ReplaceSubstringsAfterOffset(&user_agent, 0, "%name", name);
74+
ReplaceSubstringsAfterOffset(&user_agent, 0, "%ver", version);
75+
ReplaceSubstringsAfterOffset(&user_agent, 0, "%nwver", NW_VERSION_STRING);
76+
content::RendererPreferences* prefs =
77+
web_contents->GetMutableRendererPrefs();
78+
prefs->user_agent_override = user_agent;
79+
}
80+
return NULL;
81+
}
82+
6083
void ShellContentBrowserClient::RenderViewHostCreated(
6184
RenderViewHost* render_view_host) {
6285
new api::DispatcherHost(render_view_host);

src/shell_content_browser_client.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ class ShellContentBrowserClient : public ContentBrowserClient {
2828
const MainFunctionParams& parameters) OVERRIDE;
2929
virtual void RenderViewHostCreated(
3030
RenderViewHost* render_view_host) OVERRIDE;
31+
virtual WebContentsView* OverrideCreateWebContentsView(
32+
WebContents* web_contents,
33+
RenderViewHostDelegateView** render_view_host_delegate_view) OVERRIDE;
3134
virtual std::string GetApplicationLocale() OVERRIDE;
3235
virtual void AppendExtraCommandLineSwitches(CommandLine* command_line,
3336
int child_process_id) OVERRIDE;

0 commit comments

Comments
 (0)