1 // Copyright 2020 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/browser/video_tutorials/video_tutorial_service_factory.h"
6 
7 #include "base/memory/singleton.h"
8 #include "chrome/browser/browser_process.h"
9 #include "chrome/browser/image_fetcher/image_fetcher_service_factory.h"
10 #include "chrome/browser/net/system_network_context_manager.h"
11 #include "chrome/browser/profiles/profile_key.h"
12 #include "chrome/browser/video_tutorials/tutorial_factory_helper.h"
13 #include "chrome/common/channel_info.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "components/background_task_scheduler/background_task_scheduler_factory.h"
16 #include "components/keyed_service/core/simple_dependency_manager.h"
17 #include "components/language/core/browser/locale_util.h"
18 #include "components/language/core/browser/pref_names.h"
19 #include "components/prefs/pref_service.h"
20 #include "components/variations/service/variations_service.h"
21 #include "components/version_info/version_info.h"
22 #include "google_apis/google_api_keys.h"
23 #include "services/network/public/cpp/shared_url_loader_factory.h"
24 
25 namespace video_tutorials {
26 namespace {
27 
GetCountryCode()28 std::string GetCountryCode() {
29   std::string country_code;
30   auto* variations_service = g_browser_process->variations_service();
31   if (variations_service) {
32     country_code = variations_service->GetStoredPermanentCountry();
33     if (!country_code.empty())
34       return country_code;
35     country_code = variations_service->GetLatestCountry();
36   }
37   return country_code;
38 }
39 
GetGoogleAPIKey()40 std::string GetGoogleAPIKey() {
41   bool is_stable_channel =
42       chrome::GetChannel() == version_info::Channel::STABLE;
43   return is_stable_channel ? google_apis::GetAPIKey()
44                            : google_apis::GetNonStableAPIKey();
45 }
46 
47 }  // namespace
48 
49 // static
GetInstance()50 VideoTutorialServiceFactory* VideoTutorialServiceFactory::GetInstance() {
51   return base::Singleton<VideoTutorialServiceFactory>::get();
52 }
53 
54 // static
GetForKey(SimpleFactoryKey * key)55 VideoTutorialService* VideoTutorialServiceFactory::GetForKey(
56     SimpleFactoryKey* key) {
57   return static_cast<VideoTutorialService*>(
58       GetInstance()->GetServiceForKey(key, /*create=*/true));
59 }
60 
VideoTutorialServiceFactory()61 VideoTutorialServiceFactory::VideoTutorialServiceFactory()
62     : SimpleKeyedServiceFactory("VideoTutorialService",
63                                 SimpleDependencyManager::GetInstance()) {
64   DependsOn(ImageFetcherServiceFactory::GetInstance());
65   DependsOn(background_task::BackgroundTaskSchedulerFactory::GetInstance());
66 }
67 
68 std::unique_ptr<KeyedService>
BuildServiceInstanceFor(SimpleFactoryKey * key) const69 VideoTutorialServiceFactory::BuildServiceInstanceFor(
70     SimpleFactoryKey* key) const {
71   auto* db_provider =
72       ProfileKey::FromSimpleFactoryKey(key)->GetProtoDatabaseProvider();
73   // |storage_dir| is not actually used since we are using the shared leveldb.
74   base::FilePath storage_dir =
75       ProfileKey::FromSimpleFactoryKey(key)->GetPath().Append(
76           chrome::kVideoTutorialsStorageDirname);
77 
78   std::string accept_languanges =
79       ProfileKey::FromSimpleFactoryKey(key)->GetPrefs()->GetString(
80           language::prefs::kAcceptLanguages);
81 
82   auto url_loader_factory =
83       SystemNetworkContextManager::GetInstance()->GetSharedURLLoaderFactory();
84 
85   base::Version version = version_info::GetVersion();
86   std::string channel_name = chrome::GetChannelName();
87   std::string client_version =
88       base::StringPrintf("%d.%d.%d.%s.chrome",
89                          version.components()[0],  // Major
90                          version.components()[2],  // Build
91                          version.components()[3],  // Patch
92                          channel_name.c_str());
93 
94   return CreateVideoTutorialService(
95       db_provider, storage_dir, accept_languanges, GetCountryCode(),
96       GetGoogleAPIKey(), client_version, url_loader_factory,
97       ProfileKey::FromSimpleFactoryKey(key)->GetPrefs());
98 }
99 
100 }  // namespace video_tutorials
101