]> khamidou — GitHub mirrors - electron.git/commitdiff
win: Add app.addUserTasks API
authorCheng Zhao <zcbenz@gmail.com>
Mon, 17 Nov 2014 09:19:41 +0000 (17:19 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Mon, 17 Nov 2014 09:19:41 +0000 (17:19 +0800)
atom/browser/api/atom_api_app.cc
atom/browser/browser.h
atom/browser/browser_win.cc

index 8305b6a320ed05ab4ff9039c3f6c60cbe15f83b3..12668dbf344ca6b41f5b78792fb3129746f31b96 100644 (file)
 
 using atom::Browser;
 
+namespace mate {
+
+template<>
+struct Converter<Browser::UserTask> {
+  static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
+                     Browser::UserTask* out) {
+    mate::Dictionary dict;
+    if (!ConvertFromV8(isolate, val, &dict))
+      return false;
+    return dict.Get("program", &(out->program)) &&
+           dict.Get("arguments", &(out->arguments)) &&
+           dict.Get("title", &(out->title)) &&
+           dict.Get("description", &(out->description));
+  }
+};
+
+}  // namespace mate
+
+
 namespace atom {
 
 namespace api {
@@ -162,6 +181,10 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
                  base::Bind(&Browser::AddRecentDocument, browser))
       .SetMethod("clearRecentDocuments",
                  base::Bind(&Browser::ClearRecentDocuments, browser))
+#if defined(OS_WIN)
+      .SetMethod("addUserTasks",
+                 base::Bind(&Browser::AddUserTasks, browser))
+#endif
       .SetMethod("getDataPath", &App::GetDataPath)
       .SetMethod("resolveProxy", &App::ResolveProxy)
       .SetMethod("setDesktopName", &App::SetDesktopName);
index 6fed3eec0c650a5a4ea917452bf58d3c9bdf84d9..69f61da84f2c5b5e9baf12b53742666ad80de922 100644 (file)
@@ -6,6 +6,7 @@
 #define ATOM_BROWSER_BROWSER_H_
 
 #include <string>
+#include <vector>
 
 #include "base/basictypes.h"
 #include "base/compiler_specific.h"
@@ -14,6 +15,7 @@
 #include "atom/browser/window_list_observer.h"
 
 #if defined(OS_WIN)
+#include "base/files/file_path.h"
 #include "base/strings/string16.h"
 #endif
 
@@ -84,6 +86,16 @@ class Browser : public WindowListObserver {
 #endif  // defined(OS_MACOSX)
 
 #if defined(OS_WIN)
+  struct UserTask {
+    base::FilePath program;
+    base::string16 arguments;
+    base::string16 title;
+    base::string16 description;
+  };
+
+  // Add a custom task to jump list.
+  void AddUserTasks(const std::vector<UserTask>& tasks);
+
   // Set the application user model ID, called when "SetName" is called.
   void SetAppUserModelID(const std::string& name);
 #endif
index c5e3aadc8be3a09d18b93412fae64a1ad5839b21..9e4db535b2322b17aba21ae85dec19dcf35075de 100644 (file)
@@ -5,6 +5,7 @@
 #include "atom/browser/browser.h"
 
 #include <atlbase.h>
+#include <propkey.h>
 #include <windows.h>
 #include <shlobj.h>
 #include <shobjidl.h>
@@ -16,6 +17,7 @@
 #include "base/path_service.h"
 #include "base/strings/stringprintf.h"
 #include "base/strings/utf_string_conversions.h"
+#include "base/win/win_util.h"
 #include "base/win/windows_version.h"
 #include "atom/common/atom_version.h"
 
@@ -69,6 +71,47 @@ void Browser::ClearRecentDocuments() {
   destinations->RemoveAllDestinations();
 }
 
+void Browser::AddUserTasks(const std::vector<UserTask>& tasks) {
+  CComPtr<ICustomDestinationList> destinations;
+  if (FAILED(destinations.CoCreateInstance(CLSID_DestinationList)))
+    return;
+  if (FAILED(destinations->SetAppID(app_user_model_id_.c_str())))
+    return;
+
+  // Start a transaction that updates the JumpList of this application.
+  UINT max_slots;
+  CComPtr<IObjectArray> removed;
+  if (FAILED(destinations->BeginList(&max_slots, IID_PPV_ARGS(&removed))))
+    return;
+
+  CComPtr<IObjectCollection> collection;
+  if (FAILED(collection.CoCreateInstance(CLSID_EnumerableObjectCollection)))
+    return;
+
+  for (auto& task : tasks) {
+    CComPtr<IShellLink> link;
+    if (FAILED(link.CoCreateInstance(CLSID_ShellLink)) ||
+        FAILED(link->SetPath(task.program.value().c_str())) ||
+        FAILED(link->SetArguments(task.arguments.c_str())) ||
+        FAILED(link->SetDescription(task.description.c_str())))
+      return;
+
+    CComQIPtr<IPropertyStore> property_store = link;
+    if (!base::win::SetStringValueForPropertyStore(property_store, PKEY_Title,
+                                                   task.title.c_str()))
+      return;
+
+    if (FAILED(collection->AddObject(link)))
+      return;
+  }
+
+  // When the list is empty "AddUserTasks" could fail, so we don't check return
+  // value for it.
+  CComQIPtr<IObjectArray> task_array = collection;
+  destinations->AddUserTasks(task_array);
+  destinations->CommitList();
+}
+
 void Browser::SetAppUserModelID(const std::string& name) {
   app_user_model_id_ = base::UTF8ToUTF16(
       base::StringPrintf("atom-shell.app.%s", name));