1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_BROWSING_DATA_LOCAL_STORAGE_HELPER_H_ 6 #define CHROME_BROWSER_BROWSING_DATA_LOCAL_STORAGE_HELPER_H_ 7 #pragma once 8 9 #include <string> 10 #include <vector> 11 12 #include "base/callback.h" 13 #include "base/file_path.h" 14 #include "base/memory/scoped_ptr.h" 15 #include "base/synchronization/lock.h" 16 #include "base/time.h" 17 #include "chrome/common/url_constants.h" 18 #include "googleurl/src/gurl.h" 19 20 class Profile; 21 22 // This class fetches local storage information in the WebKit thread, and 23 // notifies the UI thread upon completion. 24 // A client of this class need to call StartFetching from the UI thread to 25 // initiate the flow, and it'll be notified by the callback in its UI 26 // thread at some later point. 27 // The client must call CancelNotification() if it's destroyed before the 28 // callback is notified. 29 class BrowsingDataLocalStorageHelper 30 : public base::RefCountedThreadSafe<BrowsingDataLocalStorageHelper> { 31 public: 32 // Contains detailed information about local storage. 33 struct LocalStorageInfo { 34 LocalStorageInfo(); 35 LocalStorageInfo( 36 const std::string& protocol, 37 const std::string& host, 38 unsigned short port, 39 const std::string& database_identifier, 40 const std::string& origin, 41 const FilePath& file_path, 42 int64 size, 43 base::Time last_modified); 44 ~LocalStorageInfo(); 45 IsFileSchemeDataLocalStorageInfo46 bool IsFileSchemeData() { 47 return protocol == chrome::kFileScheme; 48 } 49 50 std::string protocol; 51 std::string host; 52 unsigned short port; 53 std::string database_identifier; 54 std::string origin; 55 FilePath file_path; 56 int64 size; 57 base::Time last_modified; 58 }; 59 60 explicit BrowsingDataLocalStorageHelper(Profile* profile); 61 62 // Starts the fetching process, which will notify its completion via 63 // callback. 64 // This must be called only in the UI thread. 65 virtual void StartFetching( 66 Callback1<const std::vector<LocalStorageInfo>& >::Type* callback); 67 // Cancels the notification callback (i.e., the window that created it no 68 // longer exists). 69 // This must be called only in the UI thread. 70 virtual void CancelNotification(); 71 // Requests a single local storage file to be deleted in the WEBKIT thread. 72 virtual void DeleteLocalStorageFile(const FilePath& file_path); 73 74 protected: 75 friend class base::RefCountedThreadSafe<BrowsingDataLocalStorageHelper>; 76 virtual ~BrowsingDataLocalStorageHelper(); 77 78 // Notifies the completion callback in the UI thread. 79 void NotifyInUIThread(); 80 81 Profile* profile_; 82 83 // This only mutates on the UI thread. 84 scoped_ptr<Callback1<const std::vector<LocalStorageInfo>& >::Type > 85 completion_callback_; 86 87 // Indicates whether or not we're currently fetching information: 88 // it's true when StartFetching() is called in the UI thread, and it's reset 89 // after we notified the callback in the UI thread. 90 // This only mutates on the UI thread. 91 bool is_fetching_; 92 93 // This only mutates in the WEBKIT thread. 94 std::vector<LocalStorageInfo> local_storage_info_; 95 96 private: 97 // Enumerates all local storage files in the WEBKIT thread. 98 void FetchLocalStorageInfoInWebKitThread(); 99 // Delete a single local storage file in the WEBKIT thread. 100 void DeleteLocalStorageFileInWebKitThread(const FilePath& file_path); 101 102 DISALLOW_COPY_AND_ASSIGN(BrowsingDataLocalStorageHelper); 103 }; 104 105 // This class is a thin wrapper around BrowsingDataLocalStorageHelper that does 106 // not fetch its information from the local storage tracker, but gets them 107 // passed as a parameter during construction. 108 class CannedBrowsingDataLocalStorageHelper 109 : public BrowsingDataLocalStorageHelper { 110 public: 111 explicit CannedBrowsingDataLocalStorageHelper(Profile* profile); 112 113 // Return a copy of the local storage helper. Only one consumer can use the 114 // StartFetching method at a time, so we need to create a copy of the helper 115 // everytime we instantiate a cookies tree model for it. 116 CannedBrowsingDataLocalStorageHelper* Clone(); 117 118 // Add a local storage to the set of canned local storages that is returned 119 // by this helper. 120 void AddLocalStorage(const GURL& origin); 121 122 // Clear the list of canned local storages. 123 void Reset(); 124 125 // True if no local storages are currently stored. 126 bool empty() const; 127 128 // BrowsingDataLocalStorageHelper methods. 129 virtual void StartFetching( 130 Callback1<const std::vector<LocalStorageInfo>& >::Type* callback); CancelNotification()131 virtual void CancelNotification() {} 132 133 private: 134 virtual ~CannedBrowsingDataLocalStorageHelper(); 135 136 // Convert the pending local storage info to local storage info objects. 137 void ConvertPendingInfoInWebKitThread(); 138 139 // Used to protect access to pending_local_storage_info_. 140 mutable base::Lock lock_; 141 142 // May mutate on WEBKIT and UI threads. 143 std::vector<GURL> pending_local_storage_info_; 144 145 Profile* profile_; 146 147 DISALLOW_COPY_AND_ASSIGN(CannedBrowsingDataLocalStorageHelper); 148 }; 149 150 #endif // CHROME_BROWSER_BROWSING_DATA_LOCAL_STORAGE_HELPER_H_ 151