]> khamidou — GitHub mirrors - electron.git/commitdiff
Add app.resolveProxy API, fixes #545.
authorCheng Zhao <zcbenz@gmail.com>
Tue, 19 Aug 2014 13:26:45 +0000 (21:26 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Tue, 19 Aug 2014 13:26:45 +0000 (21:26 +0800)
atom/browser/api/atom_api_app.cc
atom/browser/api/atom_api_app.h
docs/api/app.md

index ca8166136f15e817ccff058539b7f9f24454b5cd..9424853970818455fb752e6e0434151e29b026c6 100644 (file)
@@ -7,14 +7,21 @@
 #include <string>
 #include <vector>
 
+#include "atom/browser/atom_browser_context.h"
+#include "atom/browser/browser.h"
+#include "atom/common/native_mate_converters/file_path_converter.h"
+#include "atom/common/native_mate_converters/gurl_converter.h"
 #include "base/values.h"
 #include "base/command_line.h"
 #include "base/environment.h"
 #include "base/files/file_path.h"
 #include "base/path_service.h"
-#include "atom/browser/browser.h"
+#include "native_mate/callback.h"
 #include "native_mate/dictionary.h"
 #include "native_mate/object_template_builder.h"
+#include "net/proxy/proxy_service.h"
+#include "net/url_request/url_request_context.h"
+#include "net/url_request/url_request_context_getter.h"
 
 #include "atom/common/node_includes.h"
 
@@ -28,6 +35,46 @@ namespace atom {
 
 namespace api {
 
+namespace {
+
+class ResolveProxyHelper {
+ public:
+  ResolveProxyHelper(const GURL& url, App::ResolveProxyCallback callback)
+      : callback_(callback) {
+    net::ProxyService* proxy_service = AtomBrowserContext::Get()->
+        url_request_context_getter()->GetURLRequestContext()->proxy_service();
+
+    // Start the request.
+    int result = proxy_service->ResolveProxy(
+        url, &proxy_info_,
+        base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
+                   base::Unretained(this)),
+        &pac_req_, net::BoundNetLog());
+
+    // Completed synchronously.
+    if (result != net::ERR_IO_PENDING)
+      OnResolveProxyCompleted(result);
+  }
+
+  void OnResolveProxyCompleted(int result) {
+    std::string proxy;
+    if (result == net::OK)
+      proxy = proxy_info_.ToPacString();
+    callback_.Run(proxy);
+
+    delete this;
+  }
+
+ private:
+  App::ResolveProxyCallback callback_;
+  net::ProxyInfo proxy_info_;
+  net::ProxyService::PacRequest* pac_req_;
+
+  DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
+};
+
+}  // namespace
+
 App::App() {
   Browser::Get()->AddObserver(this);
 }
@@ -76,13 +123,17 @@ base::FilePath App::GetDataPath() {
                                     base::nix::kXdgConfigHomeEnvVar,
                                     base::nix::kDotConfigDir);
 #else
-  CHECK(PathService::Get(base::DIR_APP_DATA, &path));
+  PathService::Get(base::DIR_APP_DATA, &path);
 #endif
 
   return path.Append(base::FilePath::FromUTF8Unsafe(
       Browser::Get()->GetName()));
 }
 
+void App::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
+  new ResolveProxyHelper(url, callback);
+}
+
 mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
     v8::Isolate* isolate) {
   Browser* browser = Browser::Get();
@@ -99,7 +150,8 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
                                        base::Unretained(browser)))
       .SetMethod("setName", base::Bind(&Browser::SetName,
                                        base::Unretained(browser)))
-      .SetMethod("getDataPath", &App::GetDataPath);
+      .SetMethod("getDataPath", &App::GetDataPath)
+      .SetMethod("resolveProxy", &App::ResolveProxy);
 }
 
 // static
index d0cbddaadff5a27cdeb427246420b8b3554bbed4..91aa16b8207aa5af99a9bde262960bb8935b8baf 100644 (file)
@@ -7,12 +7,17 @@
 
 #include <string>
 
-#include "base/compiler_specific.h"
 #include "atom/browser/api/event_emitter.h"
 #include "atom/browser/browser_observer.h"
-#include "atom/common/native_mate_converters/file_path_converter.h"
+#include "base/callback.h"
 #include "native_mate/handle.h"
 
+class GURL;
+
+namespace base {
+class FilePath;
+}
+
 namespace atom {
 
 namespace api {
@@ -20,6 +25,8 @@ namespace api {
 class App : public mate::EventEmitter,
             public BrowserObserver {
  public:
+  typedef base::Callback<void(std::string)> ResolveProxyCallback;
+
   static mate::Handle<App> Create(v8::Isolate* isolate);
 
  protected:
@@ -35,13 +42,15 @@ class App : public mate::EventEmitter,
   virtual void OnActivateWithNoOpenWindows() OVERRIDE;
   virtual void OnWillFinishLaunching() OVERRIDE;
   virtual void OnFinishLaunching() OVERRIDE;
-  virtual base::FilePath GetDataPath();
 
   // mate::Wrappable implementations:
   virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
       v8::Isolate* isolate);
 
  private:
+  base::FilePath GetDataPath();
+  void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
+
   DISALLOW_COPY_AND_ASSIGN(App);
 };
 
index f2f19f5268da461307dc75388d7ff244c5ce3068..0f585f5e521f6cadf44becfee4b7b488803c3684 100644 (file)
@@ -111,6 +111,14 @@ to the spec of npm modules. So usually you should also specify a `productName`
 field, which is your application's full capitalized name, and it will be
 preferred over `name` by atom-shell.
 
+## app.resolveProxy(url, callback)
+
+* `url` URL
+* `callback` Function
+
+Resolves the proxy information for `url`, the `callback` would be called with
+`callback(proxy)` when the request is done.
+
 ## app.commandLine.appendSwitch(switch, [value])
 
 Append a switch [with optional value] to Chromium's command line.