1 // Copyright 2006, Google Inc.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
6 // 1. Redistributions of source code must retain the above copyright notice,
7 // this list of conditions and the following disclaimer.
8 // 2. Redistributions in binary form must reproduce the above copyright notice,
9 // this list of conditions and the following disclaimer in the documentation
10 // and/or other materials provided with the distribution.
11 // 3. Neither the name of Google Inc. nor the names of its contributors may be
12 // used to endorse or promote products derived from this software without
13 // specific prior written permission.
15 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
18 // EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef GEARS_LOCALSERVER_COMMON_RESOURCE_STORE_H__
27 #define GEARS_LOCALSERVER_COMMON_RESOURCE_STORE_H__
29 #include "gears/base/common/common.h"
30 #include "gears/base/common/sqlite_wrapper.h"
31 #include "gears/base/common/string16.h"
32 #ifndef OFFICIAL_BUILD
33 // The blob API has not been finalized for official builds
34 #include "gears/blob/blob_interface.h"
36 #include "gears/localserver/common/localserver.h"
37 #include "gears/localserver/common/localserver_db.h"
39 //------------------------------------------------------------------------------
41 // TODO(michaeln): some documentation goes here
42 //------------------------------------------------------------------------------
43 class ResourceStore
: public LocalServer
{
45 // Returns true if the store exists in the DB
46 static bool ExistsInDB(const SecurityOrigin
&security_origin
,
48 const char16
*required_cookie
,
49 int64
*server_id_out
);
51 // Represents an item in the store
53 WebCacheDB::EntryInfo entry
;
54 WebCacheDB::PayloadInfo payload
;
57 #ifndef OFFICIAL_BUILD
58 // The blob API has not been finalized for official builds
59 // Converts a Blob to an Item that could be served by ResourceStore
60 static bool BlobToItem(const BlobInterface
*blob
,
61 const char16
*full_url
,
68 // Initializes an instance and inserts rows in the Servers and Versions
69 // table of the DB if needed
70 virtual bool CreateOrOpen(const SecurityOrigin
&security_origin
,
72 const char16
*required_cookie
);
74 // Initializes an instance from its server_id. Will not insert rows into
75 // the Servers or Versions table of the DB. If the expected rows are not
76 // present in the Servers and Versions table, this method fails and returns
78 virtual bool Open(int64 server_id
);
80 // Initialzes one instance from another. Will not insert rows in the DB.
81 bool Clone(ResourceStore
*store
) {
82 if (!LocalServer::Clone(store
)) {
85 version_id_
= store
->version_id_
;
86 is_initialized_
= store
->is_initialized_
;
87 return is_initialized_
;
90 // Gets an item from the store including the response body
91 bool GetItem(const char16
*url
, Item
*item
) {
92 return GetItem(url
, item
, false);
95 // Gets an item from the store without retrieving the response body
96 bool GetItemInfo(const char16
*url
, Item
*item
) {
97 return GetItem(url
, item
, true);
100 // Puts an item in the store. If an item already exists for this url, it is
102 bool PutItem(Item
*item
);
104 // Deletes all items in the store.
107 // Deletes a single item in the store.
108 bool Delete(const char16
*url
);
110 // Renames an item in the store. If an item already exists for new_url,
111 // the pre-existing item is deleted.
112 bool Rename(const char16
*orig_url
, const char16
*new_url
);
114 // Copies an item in the store. If an item already exists for dst_url,
115 // the pre-existing item is deleted.
116 bool Copy(const char16
*src_url
, const char16
*dst_url
);
118 // Returns true if an item is captured for url.
119 bool IsCaptured(const char16
*url
);
121 // Returns the filename of a captured local file.
122 bool GetCapturedFileName(const char16
*url
, std::string16
*file_name
);
124 // Returns the named http header value for url.
125 bool GetHeader(const char16
*url
, const char16
*header
, std::string16
*value
);
127 // Returns all http headers for url.
128 bool GetAllHeaders(const char16
*url
, std::string16
*headers
);
131 // Gets an item from the store. If info_only is true, the response body
132 // of is not retrieved.
133 bool GetItem(const char16
*url
, Item
*item
, bool info_only
);
135 // Retrieves from the DB the server info for this store based our
136 // identifying domain/name/required_cookie tuple.
137 static bool FindServer(const SecurityOrigin
&security_origin
,
139 const char16
*required_cookie
,
140 WebCacheDB::ServerInfo
*server
);
142 // The rowid of the version record in the DB associated with this
143 // ResourceStore. Each ResourceStore has exactly one related
144 // version record. The schema requires that Servers contain Versions
145 // contain Entries in order to support the other class of Server, the
146 // ManagedResourceStore. To satisfy the schema, this class manages
147 // the lifecyle of this record in the version table. When the Server
148 // record is inserted and delete, the Version record goes in lock step.
152 #endif // GEARS_LOCALSERVER_COMMON_RESOURCE_STORE_H__