• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2016 The Chromium Embedded Framework Authors.
2 // Portions copyright (c) 2012 The Chromium Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 
6 #include "libcef/browser/alloy/chrome_profile_manager_alloy.h"
7 
8 #include "libcef/browser/browser_context.h"
9 #include "libcef/browser/request_context_impl.h"
10 #include "libcef/common/app_manager.h"
11 
12 namespace {
13 
14 // Return the active browser context. This is primarily called from Chrome code
15 // that handles WebUI views and wishes to associate the view's data with a
16 // particular context (profile). Chrome stores multiple profiles in sub-
17 // directories of |user_data_dir| and then uses ProfileManager to track which
18 // profile (sub-directory name) was last active.
19 //
20 // TODO(cef): To most closely match Chrome behavior this should return the
21 // context for the currently active browser (e.g. the browser with input focus).
22 // Return the main context for now since we don't currently have a good way to
23 // determine that.
GetActiveBrowserContext()24 CefBrowserContext* GetActiveBrowserContext() {
25   auto request_context = static_cast<CefRequestContextImpl*>(
26       CefAppManager::Get()->GetGlobalRequestContext().get());
27   return request_context->GetBrowserContext();
28 }
29 
30 }  // namespace
31 
ChromeProfileManagerAlloy()32 ChromeProfileManagerAlloy::ChromeProfileManagerAlloy()
33     : ProfileManager(base::FilePath()) {}
34 
~ChromeProfileManagerAlloy()35 ChromeProfileManagerAlloy::~ChromeProfileManagerAlloy() {}
36 
GetProfile(const base::FilePath & profile_dir)37 Profile* ChromeProfileManagerAlloy::GetProfile(
38     const base::FilePath& profile_dir) {
39   CefBrowserContext* browser_context =
40       CefBrowserContext::FromCachePath(profile_dir);
41   if (!browser_context) {
42     // ProfileManager makes assumptions about profile directory paths that do
43     // not match CEF usage. For example, the default Chrome profile name is
44     // "Default" so it will append that sub-directory name to an empty
45     // |user_data_dir| value and then call this method. Use the active context
46     // in cases such as this where we don't understand what ProfileManager is
47     // asking for.
48     browser_context = GetActiveBrowserContext();
49   }
50   return browser_context->AsProfile();
51 }
52 
IsValidProfile(const void * profile)53 bool ChromeProfileManagerAlloy::IsValidProfile(const void* profile) {
54   if (!profile)
55     return false;
56   return !!CefBrowserContext::FromBrowserContext(
57       static_cast<const content::BrowserContext*>(profile));
58 }
59