1 // Copyright 2008, Google Inc. All rights reserved.
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are met:
5 //
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.
14 //
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.
25 
26 // This file contains the implementation of the KmlCache class.
27 
28 #include "kml/engine/kml_cache.h"
29 #include "boost/scoped_ptr.hpp"
30 #include "kml/engine/kml_file.h"
31 #include "kml/engine/kml_uri_internal.h"
32 #include "kml/engine/kmz_cache.h"
33 
34 namespace kmlengine {
35 
KmlCache(kmlbase::NetFetcher * net_fetcher,size_t max_size)36 KmlCache::KmlCache(kmlbase::NetFetcher* net_fetcher, size_t max_size) {
37   kml_file_cache_.reset(new KmlFileNetCache(net_fetcher, max_size));
38   kmz_file_cache_.reset(new KmzCache(net_fetcher, max_size));
39 }
40 
FetchKmlRelative(const string & base,const string & target)41 KmlFilePtr KmlCache::FetchKmlRelative(const string& base,
42                                       const string& target) {
43   boost::scoped_ptr<KmlUri> kml_uri(KmlUri::CreateRelative(base, target));
44   if (!kml_uri.get()) {
45     // Failed to create KmlUri likely due to bad url or href.
46     return NULL;
47   }
48   string url = kml_uri->get_url();
49   // If there's a KmlFile cached for this URL just return it and we're done.
50   if (KmlFilePtr kml_file = kml_file_cache_->LookUp(url)) {
51     return kml_file;
52   }
53   // No KmlFile cached for this URL.  Fetch the KML through the KMZ cache.
54   string content;
55   if (kmz_file_cache_->DoFetchAndReturnUrl(kml_uri.get(), &content, &url)) {
56     // The KML content was found within in a fetched and/or cached KMZ.
57     // Parse it into a KmlFile for it and cache it.
58     KmlFilePtr kml_file = KmlFile::CreateFromStringWithUrl(content, url, this);
59     if (kml_file) {
60       // Parsed fine so save in KmlFile cache and return.
61       kml_file_cache_->Save(url, kml_file);
62       return kml_file;
63     }
64   }
65   return NULL;
66 }
67 
68 // TODO teach KmlUri about the concept of absolute...
FetchKmlAbsolute(const string & kml_uri)69 KmlFilePtr KmlCache::FetchKmlAbsolute(const string& kml_uri) {
70   // The base url must be a valid absolute URL even if the target is
71   // absolute.  See the above TODO w.r.t KmlUri and absolute.
72   // FetchXxxRelative is the most common use case.
73   return FetchKmlRelative(kml_uri, kml_uri);
74 }
75 
FetchDataRelative(const string & base,const string & target,string * data)76 bool KmlCache::FetchDataRelative(const string& base,
77                                  const string& target,
78                                  string* data) {
79   boost::scoped_ptr<KmlUri> kml_uri(KmlUri::CreateRelative(base, target));
80   // KmzCache::Fetch has NULL pointer check.
81   if (kmz_file_cache_->DoFetch(kml_uri.get(), data)) {
82     return true;
83   }
84   return false;
85 }
86 
87 // TODO is a FetchDataAbsolute necessary?
88 
89 }  // end namespace kmlengine
90