 Chromium Code Reviews
 Chromium Code Reviews Issue 2769113002:
  [Sync] Stop accessing BrowserContextKeyedServiceFactory on non-UI thread.  (Closed)
    
  
    Issue 2769113002:
  [Sync] Stop accessing BrowserContextKeyedServiceFactory on non-UI thread.  (Closed) 
  | OLD | NEW | 
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 5 #include "chrome/browser/extensions/api/storage/settings_sync_util.h" | 
| 6 | 6 | 
| 7 #include "base/bind.h" | |
| 7 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" | 
| 8 #include "base/values.h" | 9 #include "base/values.h" | 
| 9 #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h" | 10 #include "chrome/browser/extensions/api/storage/sync_value_store_cache.h" | 
| 10 #include "components/sync/protocol/app_setting_specifics.pb.h" | 11 #include "components/sync/protocol/app_setting_specifics.pb.h" | 
| 11 #include "components/sync/protocol/extension_setting_specifics.pb.h" | 12 #include "components/sync/protocol/extension_setting_specifics.pb.h" | 
| 12 #include "components/sync/protocol/sync.pb.h" | 13 #include "components/sync/protocol/sync.pb.h" | 
| 13 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" | 
| 14 #include "extensions/browser/api/storage/storage_frontend.h" | 15 #include "extensions/browser/api/storage/storage_frontend.h" | 
| 15 | 16 | 
| 17 using base::WeakPtr; | |
| 16 using content::BrowserThread; | 18 using content::BrowserThread; | 
| 19 using syncer::ModelType; | |
| 20 using syncer::SyncableService; | |
| 17 | 21 | 
| 18 namespace extensions { | 22 namespace extensions { | 
| 19 | 23 | 
| 20 namespace settings_sync_util { | 24 namespace settings_sync_util { | 
| 21 | 25 | 
| 22 namespace { | 26 namespace { | 
| 23 | 27 | 
| 24 void PopulateExtensionSettingSpecifics( | 28 void PopulateExtensionSettingSpecifics( | 
| 25 const std::string& extension_id, | 29 const std::string& extension_id, | 
| 26 const std::string& key, | 30 const std::string& key, | 
| (...skipping 10 matching lines...) Expand all Loading... | |
| 37 | 41 | 
| 38 void PopulateAppSettingSpecifics( | 42 void PopulateAppSettingSpecifics( | 
| 39 const std::string& extension_id, | 43 const std::string& extension_id, | 
| 40 const std::string& key, | 44 const std::string& key, | 
| 41 const base::Value& value, | 45 const base::Value& value, | 
| 42 sync_pb::AppSettingSpecifics* specifics) { | 46 sync_pb::AppSettingSpecifics* specifics) { | 
| 43 PopulateExtensionSettingSpecifics( | 47 PopulateExtensionSettingSpecifics( | 
| 44 extension_id, key, value, specifics->mutable_extension_setting()); | 48 extension_id, key, value, specifics->mutable_extension_setting()); | 
| 45 } | 49 } | 
| 46 | 50 | 
| 51 WeakPtr<SyncableService> GetSyncableService(WeakPtr<SyncValueStoreCache> cache, | |
| 52 ModelType type) { | |
| 53 // WeakPtr must be explicitly checked instead of using Bind magic because we | |
| 
pavely
2017/03/27 19:38:25
I don't think there is any Bind magic here. If cac
 
skym
2017/03/27 21:45:26
Removed the confusing comment.
 | |
| 54 // need to return a default value if the WeakPtr doesn't hold anything. | |
| 55 return cache.get() ? cache->GetSyncableService(type)->AsWeakPtr() | |
| 56 : WeakPtr<SyncableService>(); | |
| 57 } | |
| 58 | |
| 47 } // namespace | 59 } // namespace | 
| 48 | 60 | 
| 49 syncer::SyncData CreateData( | 61 syncer::SyncData CreateData(const std::string& extension_id, | 
| 50 const std::string& extension_id, | 62 const std::string& key, | 
| 51 const std::string& key, | 63 const base::Value& value, | 
| 52 const base::Value& value, | 64 ModelType type) { | 
| 53 syncer::ModelType type) { | |
| 54 sync_pb::EntitySpecifics specifics; | 65 sync_pb::EntitySpecifics specifics; | 
| 55 switch (type) { | 66 switch (type) { | 
| 56 case syncer::EXTENSION_SETTINGS: | 67 case syncer::EXTENSION_SETTINGS: | 
| 57 PopulateExtensionSettingSpecifics( | 68 PopulateExtensionSettingSpecifics( | 
| 58 extension_id, | 69 extension_id, | 
| 59 key, | 70 key, | 
| 60 value, | 71 value, | 
| 61 specifics.mutable_extension_setting()); | 72 specifics.mutable_extension_setting()); | 
| 62 break; | 73 break; | 
| 63 | 74 | 
| 64 case syncer::APP_SETTINGS: | 75 case syncer::APP_SETTINGS: | 
| 65 PopulateAppSettingSpecifics( | 76 PopulateAppSettingSpecifics( | 
| 66 extension_id, | 77 extension_id, | 
| 67 key, | 78 key, | 
| 68 value, | 79 value, | 
| 69 specifics.mutable_app_setting()); | 80 specifics.mutable_app_setting()); | 
| 70 break; | 81 break; | 
| 71 | 82 | 
| 72 default: | 83 default: | 
| 73 NOTREACHED(); | 84 NOTREACHED(); | 
| 74 } | 85 } | 
| 75 | 86 | 
| 76 return syncer::SyncData::CreateLocalData( | 87 return syncer::SyncData::CreateLocalData( | 
| 77 extension_id + "/" + key, key, specifics); | 88 extension_id + "/" + key, key, specifics); | 
| 78 } | 89 } | 
| 79 | 90 | 
| 80 syncer::SyncChange CreateAdd( | 91 syncer::SyncChange CreateAdd(const std::string& extension_id, | 
| 81 const std::string& extension_id, | 92 const std::string& key, | 
| 82 const std::string& key, | 93 const base::Value& value, | 
| 83 const base::Value& value, | 94 ModelType type) { | 
| 84 syncer::ModelType type) { | |
| 85 return syncer::SyncChange( | 95 return syncer::SyncChange( | 
| 86 FROM_HERE, | 96 FROM_HERE, | 
| 87 syncer::SyncChange::ACTION_ADD, | 97 syncer::SyncChange::ACTION_ADD, | 
| 88 CreateData(extension_id, key, value, type)); | 98 CreateData(extension_id, key, value, type)); | 
| 89 } | 99 } | 
| 90 | 100 | 
| 91 syncer::SyncChange CreateUpdate( | 101 syncer::SyncChange CreateUpdate(const std::string& extension_id, | 
| 92 const std::string& extension_id, | 102 const std::string& key, | 
| 93 const std::string& key, | 103 const base::Value& value, | 
| 94 const base::Value& value, | 104 ModelType type) { | 
| 95 syncer::ModelType type) { | |
| 96 return syncer::SyncChange( | 105 return syncer::SyncChange( | 
| 97 FROM_HERE, | 106 FROM_HERE, | 
| 98 syncer::SyncChange::ACTION_UPDATE, | 107 syncer::SyncChange::ACTION_UPDATE, | 
| 99 CreateData(extension_id, key, value, type)); | 108 CreateData(extension_id, key, value, type)); | 
| 100 } | 109 } | 
| 101 | 110 | 
| 102 syncer::SyncChange CreateDelete( | 111 syncer::SyncChange CreateDelete(const std::string& extension_id, | 
| 103 const std::string& extension_id, | 112 const std::string& key, | 
| 104 const std::string& key, | 113 ModelType type) { | 
| 105 syncer::ModelType type) { | |
| 106 base::DictionaryValue no_value; | 114 base::DictionaryValue no_value; | 
| 107 return syncer::SyncChange( | 115 return syncer::SyncChange( | 
| 108 FROM_HERE, | 116 FROM_HERE, | 
| 109 syncer::SyncChange::ACTION_DELETE, | 117 syncer::SyncChange::ACTION_DELETE, | 
| 110 CreateData(extension_id, key, no_value, type)); | 118 CreateData(extension_id, key, no_value, type)); | 
| 111 } | 119 } | 
| 112 | 120 | 
| 113 syncer::SyncableService* GetSyncableService(content::BrowserContext* context, | 121 syncer::SyncClient::ServiceProvider GetSyncableServiceProvider( | 
| 114 syncer::ModelType type) { | 122 content::BrowserContext* context, | 
| 115 DCHECK_CURRENTLY_ON(BrowserThread::FILE); | 123 ModelType type) { | 
| 124 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 116 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); | 125 DCHECK(type == syncer::APP_SETTINGS || type == syncer::EXTENSION_SETTINGS); | 
| 117 StorageFrontend* frontend = StorageFrontend::Get(context); | 126 StorageFrontend* frontend = StorageFrontend::Get(context); | 
| 118 SyncValueStoreCache* sync_cache = static_cast<SyncValueStoreCache*>( | 127 SyncValueStoreCache* sync_cache = static_cast<SyncValueStoreCache*>( | 
| 119 frontend->GetValueStoreCache(settings_namespace::SYNC)); | 128 frontend->GetValueStoreCache(settings_namespace::SYNC)); | 
| 120 return sync_cache->GetSyncableService(type); | 129 return base::Bind(&GetSyncableService, sync_cache->AsWeakPtr(), type); | 
| 121 } | 130 } | 
| 122 | 131 | 
| 123 } // namespace settings_sync_util | 132 } // namespace settings_sync_util | 
| 124 | 133 | 
| 125 } // namespace extensions | 134 } // namespace extensions | 
| OLD | NEW |