| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #ifndef CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_KEY_UTILITY_CLIENT_H_ | 5 #ifndef CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_KEY_UTILITY_CLIENT_H_ |
| 6 #define CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_KEY_UTILITY_CLIENT_H_ | 6 #define CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_KEY_UTILITY_CLIENT_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <vector> |
| 10 |
| 9 #include "base/ref_counted.h" | 11 #include "base/ref_counted.h" |
| 10 #include "base/synchronization/waitable_event.h" | 12 #include "base/string16.h" |
| 11 #include "chrome/browser/utility_process_host.h" | |
| 12 | 13 |
| 13 class IndexedDBKey; | 14 class IndexedDBKey; |
| 14 class SerializedScriptValue; | 15 class SerializedScriptValue; |
| 16 class KeyUtilityClientImpl; |
| 15 | 17 |
| 16 // This class is responsible to obtain IndexedDBKeys from the | 18 namespace base { |
| 17 // SerializedScriptValues given an IDBKeyPath. It uses UtilityProcess to do this | 19 template <typename T> |
| 18 // inside a sandbox (a V8 lock is required there). At this level, all methods | 20 struct DefaultLazyInstanceTraits; |
| 19 // are synchronous as required by the caller. The public API is used on | 21 } // namespace base |
| 20 // WEBKIT thread, but internally it moves around to UI and IO as needed. | 22 |
| 21 class IndexedDBKeyUtilityClient | 23 // Class for obtaining IndexedDBKeys from the SerializedScriptValues given |
| 22 : public base::RefCountedThreadSafe<IndexedDBKeyUtilityClient> { | 24 // an IDBKeyPath. This class is a thin singleton wrapper around the |
| 25 // KeyUtilityClientImpl, which does the real work. |
| 26 class IndexedDBKeyUtilityClient { |
| 23 public: | 27 public: |
| 24 IndexedDBKeyUtilityClient(); | |
| 25 | |
| 26 // Starts the UtilityProcess. Must be called before any other method. | |
| 27 void StartUtilityProcess(); | |
| 28 | |
| 29 // Ends the UtilityProcess. Must be called after StartUtilityProcess() and | |
| 30 // before destruction. | |
| 31 // TODO(bulach): figure out an appropriate hook so that we can keep the | |
| 32 // UtilityProcess running for a longer period of time and avoid spinning it | |
| 33 // on every IDBObjectStore::Put call. | |
| 34 void EndUtilityProcess(); | |
| 35 | |
| 36 // Synchronously obtain the |keys| from |values| for the given |key_path|. | 28 // Synchronously obtain the |keys| from |values| for the given |key_path|. |
| 37 void CreateIDBKeysFromSerializedValuesAndKeyPath( | 29 static void CreateIDBKeysFromSerializedValuesAndKeyPath( |
| 38 const std::vector<SerializedScriptValue>& values, | 30 const std::vector<SerializedScriptValue>& values, |
| 39 const string16& key_path, | 31 const string16& key_path, |
| 40 std::vector<IndexedDBKey>* keys); | 32 std::vector<IndexedDBKey>* keys); |
| 41 | 33 |
| 34 // Shut down the underlying implementation. Must be called on the IO thread. |
| 35 static void Shutdown(); |
| 36 |
| 42 private: | 37 private: |
| 43 class Client : public UtilityProcessHost::Client { | 38 friend struct base::DefaultLazyInstanceTraits<IndexedDBKeyUtilityClient>; |
| 44 public: | 39 IndexedDBKeyUtilityClient(); |
| 45 explicit Client(IndexedDBKeyUtilityClient* parent); | |
| 46 | |
| 47 // UtilityProcessHost::Client | |
| 48 virtual void OnProcessCrashed(int exit_code); | |
| 49 virtual void OnIDBKeysFromValuesAndKeyPathSucceeded( | |
| 50 int id, const std::vector<IndexedDBKey>& keys); | |
| 51 virtual void OnIDBKeysFromValuesAndKeyPathFailed(int id); | |
| 52 | |
| 53 private: | |
| 54 IndexedDBKeyUtilityClient* parent_; | |
| 55 | |
| 56 DISALLOW_COPY_AND_ASSIGN(Client); | |
| 57 }; | |
| 58 | |
| 59 friend class base::RefCountedThreadSafe<IndexedDBKeyUtilityClient>; | |
| 60 ~IndexedDBKeyUtilityClient(); | 40 ~IndexedDBKeyUtilityClient(); |
| 61 | 41 |
| 62 void GetRDHAndStartUtilityProcess(); | 42 bool is_shutdown_; |
| 63 void StartUtilityProcessInternal(ResourceDispatcherHost* rdh); | |
| 64 void EndUtilityProcessInternal(); | |
| 65 void CallStartIDBKeyFromValueAndKeyPathFromIOThread( | |
| 66 const std::vector<SerializedScriptValue>& values, | |
| 67 const string16& key_path); | |
| 68 | 43 |
| 69 void SetKeys(const std::vector<IndexedDBKey>& keys); | 44 // The real client; laziliy instantiated. |
| 70 void FinishCreatingKeys(); | 45 scoped_refptr<KeyUtilityClientImpl> impl_; |
| 71 | |
| 72 base::WaitableEvent waitable_event_; | |
| 73 | |
| 74 // Used in both IO and WEBKIT threads, but guarded by WaitableEvent, i.e., | |
| 75 // these members are only set / read when the other thread is blocked. | |
| 76 enum State { | |
| 77 STATE_UNINITIALIZED, | |
| 78 STATE_INITIALIZED, | |
| 79 STATE_CREATING_KEYS, | |
| 80 STATE_SHUTDOWN, | |
| 81 }; | |
| 82 State state_; | |
| 83 std::vector<IndexedDBKey> keys_; | |
| 84 | |
| 85 // Used in the IO thread. | |
| 86 UtilityProcessHost* utility_process_host_; | |
| 87 scoped_refptr<Client> client_; | |
| 88 | |
| 89 DISALLOW_COPY_AND_ASSIGN(IndexedDBKeyUtilityClient); | |
| 90 }; | 46 }; |
| 91 | 47 |
| 92 #endif // CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_KEY_UTILITY_CLIENT_H_ | 48 #endif // CHROME_BROWSER_IN_PROCESS_WEBKIT_INDEXED_DB_KEY_UTILITY_CLIENT_H_ |
| OLD | NEW |