Setting cache path at runtime [OSX / MacOS]

Having problems with building or using CEF's C/C++ APIs? This forum is here to help. Please do not post bug reports or feature requests here.

Setting cache path at runtime [OSX / MacOS]

Postby coder678 » Tue Jan 16, 2018 7:43 pm

Hello,

I have an application for OSX with a multi tabbed browser view. It needs to set a cache path separately for each tab. Here is the code.
Code: Select all

-(void)addTab
{
    CefRefPtr<SimpleHandler> handler(cHandler);
    CefBrowserSettings browser_settings;
   
    browser_settings.application_cache = STATE_ENABLED;
   
    CefRequestContextSettings contextSettings;
    mContextHandler = new ContextHandler("/Users/directory/for/cookies");
    CefRefPtr<CefRequestContext> request_context2 = CefRequestContext::CreateContext(contextSettings, mContextHandler);
   
    // !!! here it needs to set a custom cache_path. How???
   
    NSRect f = self.view.frame;
    NSView *cefView = [[NSView alloc] initWithFrame:(NSMakeRect(0, 70, f.size.width - 10, f.size.height - 70))];
    CefWindowInfo window_info;
    window_info.SetAsChild(cefView, 0, 0, [cefView frame].size.width, [cefView frame].size.height);

    CefBrowserHost::CreateBrowser(window_info, handler, "some url here", browser_settings, request_context2);
}

// a custom request context to set directory for cookies at runtime
class ContextHandler : public CefRequestContextHandler
{
public:
    ContextHandler(const std::string cookieStorageDirectory)
    {
        bool persist_session_cookies = true; // was false
        CefRefPtr<CefCompletionCallback> callback = nullptr;
       
        mCookieManager = CefCookieManager::CreateManager(CefString(cookieStorageDirectory), persist_session_cookies, callback);
    }
   
    CefRefPtr<CefCookieManager> GetCookieManager() override
    {
        return mCookieManager;
    }
   
private:
    CefRefPtr<CefCookieManager> mCookieManager;
   
    IMPLEMENT_REFCOUNTING(ContextHandler);
};


I've searched in the forum but couldn't find a working solution. Please help.
coder678
Techie
 
Posts: 35
Joined: Sun Jun 26, 2016 10:03 pm

Re: Setting cache path at runtime [OSX / MacOS]

Postby coder678 » Tue Jan 16, 2018 9:34 pm

At last I found a solution.
Code: Select all
    CefRefPtr<SimpleHandler> handler(cHandler);
    CefBrowserSettings browser_settings;
   
    browser_settings.application_cache = STATE_ENABLED;
   
    std::string strCachePath("/Users/user1/Public/test_cache");
   
    CefRequestContextSettings contextSettings;
    CefString(&contextSettings.cache_path).FromASCII(strCachePath.c_str());
   
    mContextHandler = new ContextHandler("/Users/directory/for/cookies");
    CefRefPtr<CefRequestContext> request_context2 = CefRequestContext::CreateContext(contextSettings, mContextHandler);
       
    NSRect f = self.view.frame;
    NSView *cefView = [[NSView alloc] initWithFrame:(NSMakeRect(0, 70, f.size.width - 10, f.size.height - 70))];
    CefWindowInfo window_info;
    window_info.SetAsChild(cefView, 0, 0, [cefView frame].size.width, [cefView frame].size.height);

    CefBrowserHost::CreateBrowser(window_info, handler, "some url here", browser_settings, request_context2);
coder678
Techie
 
Posts: 35
Joined: Sun Jun 26, 2016 10:03 pm

Re: Setting cache path at runtime [OSX / MacOS]

Postby coder678 » Tue Jan 16, 2018 10:25 pm

Couple of useful settings for remembering websites credentials:

Code: Select all
CefRequestContextSettings contextSettings;
    CefString(&contextSettings.cache_path).FromASCII(strCachePath.c_str());
    contextSettings.persist_session_cookies = 1;
    contextSettings.persist_user_preferences = 1;
coder678
Techie
 
Posts: 35
Joined: Sun Jun 26, 2016 10:03 pm


Return to Support Forum

Who is online

Users browsing this forum: No registered users and 28 guests