1 // Copyright 2018 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/common/initialize_extensions_client.h"
6 
7 #include <memory>
8 
9 #include "base/no_destructor.h"
10 #include "chrome/common/apps/platform_apps/chrome_apps_api_provider.h"
11 #include "chrome/common/extensions/chrome_extensions_client.h"
12 #include "extensions/common/extensions_client.h"
13 
EnsureExtensionsClientInitialized()14 void EnsureExtensionsClientInitialized() {
15   static bool initialized = false;
16 
17   static base::NoDestructor<extensions::ChromeExtensionsClient>
18       extensions_client;
19 
20   if (!initialized) {
21     initialized = true;
22     extensions_client->AddAPIProvider(
23         std::make_unique<chrome_apps::ChromeAppsAPIProvider>());
24     extensions::ExtensionsClient::Set(extensions_client.get());
25   }
26 
27   // ExtensionsClient::Set() will early-out if the client was already set, so
28   // this allows us to check that this was the only site setting it.
29   DCHECK_EQ(extensions_client.get(), extensions::ExtensionsClient::Get())
30       << "ExtensionsClient should only be initialized through "
31       << "EnsureExtensionsClientInitialized() when using "
32       << "ChromeExtensionsClient.";
33 }
34