1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved. 2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file. 4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SOURCE_H_ 6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SOURCE_H_ 7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 8cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include <map> 9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <string> 10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/basictypes.h" 12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "base/callback.h" 13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/weak_ptr.h" 145f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/suggestions/proto/suggestions.pb.h" 15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "content/public/browser/url_data_source.h" 16cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "url/gurl.h" 17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class Profile; 19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)class SkBitmap; 20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace base { 22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class RefCountedMemory; 23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace base 24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace suggestions { 26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// SuggestionsSource renders a webpage to list SuggestionsService data. 28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class SuggestionsSource : public content::URLDataSource { 29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public: 30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) explicit SuggestionsSource(Profile* profile); 31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // content::URLDataSource implementation. 33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual std::string GetSource() const OVERRIDE; 34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void StartDataRequest( 35cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const std::string& path, int render_process_id, int render_frame_id, 36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const content::URLDataSource::GotDataCallback& callback) OVERRIDE; 37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual std::string GetMimeType(const std::string& path) const OVERRIDE; 38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual base::MessageLoop* MessageLoopForRequestPath( 39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const std::string& path) const OVERRIDE; 40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private: 42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual ~SuggestionsSource(); 43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 44cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Container for the state of a request. 45cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) struct RequestContext { 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) RequestContext( 47cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const suggestions::SuggestionsProfile& suggestions_profile_in, 48cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const content::URLDataSource::GotDataCallback& callback_in); 49cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) ~RequestContext(); 50cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 51cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const suggestions::SuggestionsProfile suggestions_profile; 52cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const content::URLDataSource::GotDataCallback callback; 53cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) std::map<GURL, std::string> base64_encoded_pngs; 54cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) }; 55cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Callback for suggestions from SuggestionsService. 57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) void OnSuggestionsAvailable( 58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const content::URLDataSource::GotDataCallback& callback, 59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) const SuggestionsProfile& suggestions_profile); 60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 616e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Callback for responses from each Thumbnail request. 62cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) void OnThumbnailAvailable(RequestContext* context, base::Closure barrier, 63cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) const GURL& url, const SkBitmap* bitmap); 64cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 65cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // Callback for when all requests are complete. Renders the output webpage and 66cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) // passes the result to the original caller. 67cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) void OnThumbnailsFetched(RequestContext* context); 68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) 69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Only used when servicing requests on the UI thread. 70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) Profile* const profile_; 71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // For callbacks may be run after destruction. 73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) base::WeakPtrFactory<SuggestionsSource> weak_ptr_factory_; 74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(SuggestionsSource); 76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}; 77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)} // namespace suggestions 79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif // CHROME_BROWSER_SEARCH_SUGGESTIONS_SUGGESTIONS_SOURCE_H_ 81