1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef EXTENSIONS_BROWSER_API_STORAGE_STORAGE_API_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define EXTENSIONS_BROWSER_API_STORAGE_STORAGE_API_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h" 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/storage/settings_namespace.h" 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/api/storage/settings_observer.h" 12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/extension_function.h" 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "extensions/browser/value_store/value_store.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace extensions { 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Superclass of all settings functions. 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class SettingsFunction : public UIThreadExtensionFunction { 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) SettingsFunction(); 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~SettingsFunction(); 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ExtensionFunction: 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool ShouldSkipQuotaLimiting() const OVERRIDE; 25010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) virtual ResponseAction Run() OVERRIDE; 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Extension settings function implementations should do their work here. 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // The StorageFrontend makes sure this is posted to the appropriate thread. 295c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual ResponseValue RunWithStorage(ValueStore* storage) = 0; 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Handles the |result| of a read function. 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // - If the result succeeded, this will set |result_| and return. 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // - If |result| failed with a ValueStore::CORRUPTION error, this will call 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // RestoreStorageAndRetry(), and return that result. 35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // - If the |result| failed with a different error, this will set |error_| 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // and return. 375c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ResponseValue UseReadResult(ValueStore::ReadResult result, 385c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ValueStore* storage); 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Handles the |result| of a write function. 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // - If the result succeeded, this will set |result_| and return. 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // - If |result| failed with a ValueStore::CORRUPTION error, this will call 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // RestoreStorageAndRetry(), and return that result. 44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // - If the |result| failed with a different error, this will set |error_| 45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // and return. 46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // This will also send out a change notification, if appropriate. 475c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ResponseValue UseWriteResult(ValueStore::WriteResult result, 485c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ValueStore* storage); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 51010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Called via PostTask from Run. Calls RunWithStorage and then 522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) // SendResponse with its success value. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void AsyncRunWithStorage(ValueStore* storage); 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Called if we encounter a ValueStore error. If the error is due to 56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // corruption, tries to restore the ValueStore and re-run the API function. 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // If the storage cannot be restored or was due to some other error, then sets 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // error and returns. This also sets the |tried_restoring_storage_| flag to 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // ensure we don't enter a loop. 605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ResponseValue HandleError(const ValueStore::Error& error, 615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu ValueStore* storage); 62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The settings namespace the call was for. For example, SYNC if the API 645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // call was chrome.settings.experimental.sync..., LOCAL if .local, etc. 655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) settings_namespace::Namespace settings_namespace_; 665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // A flag indicating whether or not we have tried to restore storage. We 68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // should only ever try once (per API call) in order to avoid entering a loop. 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) bool tried_restoring_storage_; 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Observers, cached so that it's only grabbed from the UI thread. 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_refptr<SettingsObserverList> observers_; 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StorageStorageAreaGetFunction : public SettingsFunction { 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DECLARE_EXTENSION_FUNCTION("storage.get", STORAGE_GET) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 802a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~StorageStorageAreaGetFunction() {} 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // SettingsFunction: 835c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual ResponseValue RunWithStorage(ValueStore* storage) OVERRIDE; 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StorageStorageAreaSetFunction : public SettingsFunction { 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DECLARE_EXTENSION_FUNCTION("storage.set", STORAGE_SET) 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~StorageStorageAreaSetFunction() {} 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // SettingsFunction: 945c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual ResponseValue RunWithStorage(ValueStore* storage) OVERRIDE; 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ExtensionFunction: 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void GetQuotaLimitHeuristics( 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) QuotaLimitHeuristics* heuristics) const OVERRIDE; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StorageStorageAreaRemoveFunction : public SettingsFunction { 1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DECLARE_EXTENSION_FUNCTION("storage.remove", STORAGE_REMOVE) 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~StorageStorageAreaRemoveFunction() {} 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // SettingsFunction: 1095c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual ResponseValue RunWithStorage(ValueStore* storage) OVERRIDE; 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ExtensionFunction: 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void GetQuotaLimitHeuristics( 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) QuotaLimitHeuristics* heuristics) const OVERRIDE; 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StorageStorageAreaClearFunction : public SettingsFunction { 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 1182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DECLARE_EXTENSION_FUNCTION("storage.clear", STORAGE_CLEAR) 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~StorageStorageAreaClearFunction() {} 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // SettingsFunction: 1245c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual ResponseValue RunWithStorage(ValueStore* storage) OVERRIDE; 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ExtensionFunction: 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void GetQuotaLimitHeuristics( 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) QuotaLimitHeuristics* heuristics) const OVERRIDE; 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)class StorageStorageAreaGetBytesInUseFunction : public SettingsFunction { 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 1332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) DECLARE_EXTENSION_FUNCTION("storage.getBytesInUse", STORAGE_GETBYTESINUSE) 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) protected: 1362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) virtual ~StorageStorageAreaGetBytesInUseFunction() {} 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // SettingsFunction: 1395c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual ResponseValue RunWithStorage(ValueStore* storage) OVERRIDE; 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace extensions 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 144a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // EXTENSIONS_BROWSER_API_STORAGE_STORAGE_API_H_ 145