首先,chromium除了默认主题外,其它都是以chrome拓展的方式安装的,所以对于一个新的主题,需要定一个主题id,这个主题id可以先自己定一个,只要长度一致就可以了,但是主题id会有字母范围的限制,所以可以找一个现成的id,然后直接交换几个字母的位置就可以,后继这个id并不会真正用到。
src\chrome\browser\themes\theme_service.cc const char* ThemeService::kDefaultThemeID = ""; const char* ThemeService::kNewDefaultThemeID = "ndoippcjlipfddffdcimakhebaemnmgn";
在theme_service_factory.cc中设置默认注册的主题id为新的主题id。
src\chrome\browser\themes\theme_service_factory.cc registry->RegisterStringPref( prefs::kCurrentThemeID, ThemeService::kNewDefaultThemeID, user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
在chrome启动后,会调用ThemeService::Init函数,进而进入LoadThemePrefs函数,但是进入LoadThemePrefs后,这里是不载入主题的,因为当前current_id等于kNewDefaultThemeID,kCurrentThemePackFilename为空。ThemeService会监听NOTIFICATION_EXTENSIONS_READY信号,当extension系统启动完成后,调用ThemeService的OnExtensionServiceReady函数,接下来就可以通过extension系统加载主题。在OnExtensionServiceReady函数中,ready_当前的值为false,因为没有载入任何主题。调用MigrateTheme函数,将函数修改为
void ThemeService::MigrateTheme() { // TODO(erg): We need to pop up a dialog informing the user that their // theme is being migrated. ExtensionService* service = extensions::ExtensionSystem::Get(profile_)->extension_service(); const Extension* extension = service ? service->GetExtensionById(GetThemeID(), false) : NULL; if (extension) { DLOG(ERROR) << "Migrating theme"; BuildFromExtension(extension); content::RecordAction(UserMetricsAction("Themes.Migrated")); } else { if (GetThemeID() == kNewDefaultThemeID) { base::FilePath dir_exe; PathService::Get(base::DIR_EXE, &dir_exe); base::FilePath new_theme_dir(dir_exe.value() + FILE_PATH_LITERAL("/new_theme")); extensions::UnpackedInstaller::Create(service)->Load(new_theme_dir); } else { DLOG(ERROR) << "Theme is mysteriously gone."; ClearAllThemeData(); content::RecordAction(UserMetricsAction("Themes.Gone")); } } }
由于初始的主题id是kNewDefaultThemeID,会需要从给定文件夹new_theme_dir载入主题拓展。MigrateTheme运行完毕调用NotifyThemeChanged,主题加载完成。
在重置主题时,会调用ClearAllThemeData函数,该函数重新设置主题id,并重新载入主题。
void ThemeService::ClearAllThemeData() { if (!ready_) return; SwapThemeSupplier(NULL); FreePlatformCaches(); profile_->GetPrefs()->ClearPref(prefs::kCurrentThemePackFilename); SaveThemeID(kNewDefaultThemeID); base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&ThemeService::RemoveUnusedThemes, weak_ptr_factory_.GetWeakPtr(), true)); MigrateTheme(); }
时间: 2024-10-07 01:33:24