Skip to content

Commit d4786c2

Browse files
author
Cong Liu
committed
[win] implemented APIs for set/get App User Model ID
AUMID ([AppUserModelID](https://msdn.microsoft.com/en-us/library/windows/desktop/dd378459(v=vs.85).aspx)) is useful to group windows on task bars for multi-process applications on Windows 7 or later. With the newly added APIs, developers can get and explicitly set AUMID for each window. Or set a default AUMID for all windows opened.
1 parent 3154ede commit d4786c2

File tree

11 files changed

+155
-1
lines changed

11 files changed

+155
-1
lines changed

src/api/nw_app.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace nw.App {
2626
static DOMString[] getArgvSync();
2727
static DOMString getDataPath();
2828
static void crashBrowser();
29+
static DOMString getDefaultAppUserModelID();
30+
static void setDefaultAppUserModelID(DOMString app_id);
2931
};
3032
interface Events {
3133
static void onOpen(DOMString cmdline);

src/api/nw_app_api.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
#include "base/command_line.h"
44
#include "base/memory/ptr_util.h"
5+
#include "base/strings/utf_string_conversions.h"
56
#include "chrome/browser/browsing_data/browsing_data_appcache_helper.h"
67
#include "chrome/browser/browsing_data/browsing_data_helper.h"
78
#include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
89
#include "chrome/browser/devtools/devtools_window.h"
910
#include "chrome/browser/extensions/devtools_util.h"
1011
#include "chrome/browser/extensions/extension_service.h"
12+
#include "chrome/browser/profiles/profile.h"
13+
#include "chrome/browser/web_applications/web_app.h"
14+
#include "content/nw/src/common/shell_switches.h"
1115
#include "content/nw/src/nw_base.h"
1216
#include "content/public/browser/render_process_host.h"
1317
#include "content/public/browser/render_view_host.h"
@@ -24,6 +28,10 @@
2428
#include "net/url_request/url_request_context.h"
2529
#include "net/url_request/url_request_context_getter.h"
2630

31+
#if defined(OS_WIN)
32+
#include "chrome/browser/shell_integration_win.h"
33+
#endif
34+
2735
namespace {
2836
void SetProxyConfigCallback(
2937
base::WaitableEvent* done,
@@ -182,5 +190,35 @@ bool NwAppCrashBrowserFunction::RunAsync() {
182190
return true;
183191
}
184192

193+
bool NwAppGetDefaultAppUserModelIDFunction::RunNWSync(base::ListValue* response, std::string* error) {
194+
#if defined(OS_WIN)
195+
base::string16 app_id;
196+
nw::Package* package = nw::package();
197+
if (package->root()->GetString(::switches::kmAppUserModelID, &app_id)) {
198+
response->AppendString(app_id);
199+
} else {
200+
std::string app_name =
201+
web_app::GenerateApplicationNameFromExtensionId(extension_id());
202+
base::string16 app_name_wide = base::UTF8ToWide(app_name);
203+
Profile* profile =
204+
Profile::FromBrowserContext(browser_context());
205+
app_id = shell_integration::win::GetAppModelIdForProfile(
206+
app_name_wide, profile->GetPath());
207+
response->AppendString(app_id);
208+
}
209+
#endif
210+
return true;
211+
}
212+
213+
bool NwAppSetDefaultAppUserModelIDFunction::RunNWSync(base::ListValue* response, std::string* error) {
214+
#if defined(OS_WIN)
215+
std::string app_id;
216+
EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &app_id));
217+
218+
nw::Package* package = nw::package();
219+
package->root()->SetString(::switches::kmAppUserModelID, app_id);
220+
#endif
221+
return true;
222+
}
185223

186224
} // namespace extensions

src/api/nw_app_api.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,31 @@ class NwAppCrashBrowserFunction : public AsyncExtensionFunction {
117117
DECLARE_EXTENSION_FUNCTION("nw.App.crashBrowser", UNKNOWN)
118118
};
119119

120+
class NwAppGetDefaultAppUserModelIDFunction : public NWSyncExtensionFunction {
121+
public:
122+
NwAppGetDefaultAppUserModelIDFunction(){}
123+
bool RunNWSync(base::ListValue* response, std::string* error) override;
124+
125+
protected:
126+
~NwAppGetDefaultAppUserModelIDFunction() override {}
127+
128+
DECLARE_EXTENSION_FUNCTION("nw.App.getDefaultAppUserModelID", UNKNOWN)
129+
private:
130+
DISALLOW_COPY_AND_ASSIGN(NwAppGetDefaultAppUserModelIDFunction);
131+
};
132+
133+
class NwAppSetDefaultAppUserModelIDFunction : public NWSyncExtensionFunction {
134+
public:
135+
NwAppSetDefaultAppUserModelIDFunction(){}
136+
bool RunNWSync(base::ListValue* response, std::string* error) override;
137+
138+
protected:
139+
~NwAppSetDefaultAppUserModelIDFunction() override {}
140+
141+
DECLARE_EXTENSION_FUNCTION("nw.App.setDefaultAppUserModelID", UNKNOWN)
142+
private:
143+
DISALLOW_COPY_AND_ASSIGN(NwAppSetDefaultAppUserModelIDFunction);
144+
};
145+
120146
} // namespace extensions
121147
#endif

src/api/nw_current_window_internal.idl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,7 @@ namespace nw.currentWindowInternal {
4444
static object getWinParamInternal();
4545
static void getPrinters(GetPrintersCallback callback);
4646
static void setPrintSettingsInternal(optional object options);
47+
static DOMString getAppUserModelID();
48+
static void setAppUserModelID(DOMString id);
4749
};
4850
};

src/api/nw_window.idl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ namespace nw.Window {
3939
[nodoc] DOMString? icon;
4040
[nodoc] DOMString? inject_js_start;
4141
[nodoc] DOMString? inject_js_end;
42+
[nodoc] DOMString? app_user_model_id;
4243
};
4344

4445
[noinline_doc] dictionary NWWindow {
@@ -72,6 +73,8 @@ namespace nw.Window {
7273
static void removeAllListeners(DOMString event);
7374
static void reload();
7475
static void reloadIgnoringCache();
76+
static DOMString getAppUserModelID();
77+
static void setAppUserModelID(DOMString id);
7578

7679
static void eval(object frame, DOMString script);
7780
static void evalNWBin(object frame, DOMString path);

src/api/nw_window_api.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "ui/gfx/platform_font.h"
3939
#include "ui/display/win/dpi.h"
4040
#include "ui/views/win/hwnd_util.h"
41+
#include "chrome/browser/ui/views/apps/chrome_native_app_window_views_win.h"
4142
#endif
4243

4344
#if defined(OS_LINUX)
@@ -746,5 +747,46 @@ bool NwCurrentWindowInternalSetShowInTaskbarFunction::RunAsync() {
746747
return true;
747748
}
748749

750+
bool NwCurrentWindowInternalGetAppUserModelIDFunction::RunNWSync(base::ListValue* response, std::string* error) {
751+
#if defined(OS_WIN)
752+
AppWindow* app_window = getAppWindow(this);
753+
754+
if (!app_window) {
755+
*error = "cannot get current window; are you in background page/node context?";
756+
return false;
757+
}
758+
759+
ChromeNativeAppWindowViewsWin* native_app_window = static_cast<ChromeNativeAppWindowViewsWin*>(app_window->GetBaseWindow());
760+
response->AppendString(native_app_window->app_model_id());
761+
#endif
762+
return true;
763+
}
764+
765+
bool NwCurrentWindowInternalSetAppUserModelIDFunction::RunNWSync(base::ListValue* response, std::string* error) {
766+
#if defined(OS_WIN)
767+
EXTENSION_FUNCTION_VALIDATE(args_);
768+
769+
if (!args_->GetSize())
770+
return false;
771+
base::string16 app_id;
772+
if (!args_->GetString(0, &app_id))
773+
return false;
774+
if (app_id.empty())
775+
*error = "AppUserModelID cannot set to empty string";
776+
return false;
777+
778+
AppWindow* app_window = getAppWindow(this);
779+
780+
if (!app_window) {
781+
*error = "cannot get current window; are you in background page/node context?";
782+
return false;
783+
}
784+
785+
ChromeNativeAppWindowViewsWin* native_app_window = static_cast<ChromeNativeAppWindowViewsWin*>(app_window->GetBaseWindow());
786+
native_app_window->SetAppModelId(app_id);
787+
#endif
788+
return true;
789+
}
790+
749791
} // namespace extensions
750792

src/api/nw_window_api.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,5 +300,25 @@ class NwCurrentWindowInternalSetPrintSettingsInternalFunction : public NWSyncExt
300300
~NwCurrentWindowInternalSetPrintSettingsInternalFunction() override {}
301301
DECLARE_EXTENSION_FUNCTION("nw.currentWindowInternal.setPrintSettingsInternal", UNKNOWN)
302302
};
303+
304+
class NwCurrentWindowInternalGetAppUserModelIDFunction : public NWSyncExtensionFunction {
305+
public:
306+
NwCurrentWindowInternalGetAppUserModelIDFunction() {}
307+
bool RunNWSync(base::ListValue* response, std::string* error) override;
308+
309+
protected:
310+
~NwCurrentWindowInternalGetAppUserModelIDFunction() override {}
311+
DECLARE_EXTENSION_FUNCTION("nw.currentWindowInternal.getAppUserModelID", UNKNOWN)
312+
};
313+
314+
class NwCurrentWindowInternalSetAppUserModelIDFunction : public NWSyncExtensionFunction {
315+
public:
316+
NwCurrentWindowInternalSetAppUserModelIDFunction() {}
317+
bool RunNWSync(base::ListValue* response, std::string* error) override;
318+
319+
protected:
320+
~NwCurrentWindowInternalSetAppUserModelIDFunction() override {}
321+
DECLARE_EXTENSION_FUNCTION("nw.currentWindowInternal.setAppUserModelID", UNKNOWN)
322+
};
303323
} // namespace extensions
304324
#endif

src/common/shell_switches.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const char kmNewInstance[] = "new-instance";
112112
const char kmInjectJSDocStart[] = "inject_js_start";
113113
const char kmInjectJSDocEnd[] = "inject_js_end";
114114
const char kmInjectCSS[] = "inject-css";
115+
const char kmAppUserModelID[] = "app_user_model_id";
115116

116117
#if defined(OS_WIN)
117118
// Enable conversion from vector to raster for any page.

src/common/shell_switches.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ extern NW_EXPORT const char kmWebkit[];
3535
extern NW_EXPORT const char kmWindow[];
3636
extern NW_EXPORT const char kmChromiumArgs[];
3737
extern NW_EXPORT const char kmJsFlags[];
38+
extern NW_EXPORT const char kmAppUserModelID[];
3839

3940
extern NW_EXPORT const char kmSingleInstance[];
4041

src/resources/api_nw_app.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ nw_binding.registerCustomHook(function(bindingsAPI) {
7171
apiFunctions.setHandleRequest('removeOriginAccessWhitelistEntry', function() {
7272
nwNatives.removeOriginAccessWhitelistEntry.apply(this, arguments);
7373
});
74+
apiFunctions.setHandleRequest('getDefaultAppUserModelID', function() {
75+
return sendRequest.sendRequestSync('nw.App.getDefaultAppUserModelID', arguments, this.definition.parameters, {})[0];
76+
});
77+
apiFunctions.setHandleRequest('setDefaultAppUserModelID', function() {
78+
sendRequest.sendRequestSync('nw.App.setDefaultAppUserModelID', arguments, this.definition.parameters, {});
79+
});
7480
apiFunctions.setHandleRequest('once', function(event, listener) { //FIXME: unify with nw.Window
7581
if (typeof listener !== 'function')
7682
throw new TypeError('listener must be a function');
@@ -119,7 +125,6 @@ nw_binding.registerCustomHook(function(bindingsAPI) {
119125
bindingsAPI.compiledApi.unregisterGlobalHotKey = function() {
120126
return nw.Shortcut.unregisterGlobalHotKey.apply(nw.Shortcut, arguments);
121127
};
122-
123128
});
124129

125130
exports.binding = nw_binding.generate();

0 commit comments

Comments
 (0)