OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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_CHROME_TO_MOBILE_SERVICE_H_ |
| 6 #define CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <map> |
| 10 #include <string> |
| 11 #include <vector> |
| 12 |
| 13 #include "base/file_util.h" |
| 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/scoped_temp_dir.h" |
| 16 #include "base/string16.h" |
| 17 #include "base/timer.h" |
| 18 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 19 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" |
| 20 #include "content/public/common/url_fetcher_delegate.h" |
| 21 #include "googleurl/src/gurl.h" |
| 22 |
| 23 class OAuth2AccessTokenFetcher; |
| 24 class CloudPrintURL; |
| 25 class Profile; |
| 26 |
| 27 // ChromeToMobileService connects to the cloud print service to enumerate |
| 28 // compatible mobiles owned by its profile and send URLs and MHTML snapshots. |
| 29 // The mobile list updates regularly, and explicitly by RequestMobileListUpdate. |
| 30 class ChromeToMobileService : public ProfileKeyedService, |
| 31 public content::URLFetcherDelegate, |
| 32 public OAuth2AccessTokenConsumer { |
| 33 public: |
| 34 class Observer { |
| 35 public: |
| 36 virtual ~Observer(); |
| 37 |
| 38 // Called on generation of the page's MHTML snapshot. |
| 39 virtual void SnapshotGenerated(const FilePath& path, int64 bytes) = 0; |
| 40 |
| 41 // Called after URLFetcher responses from sending the URL (and snapshot). |
| 42 virtual void OnSendComplete(bool success) = 0; |
| 43 }; |
| 44 |
| 45 // The URLFetcher request types. |
| 46 enum RequestType { |
| 47 SEARCH, |
| 48 URL, |
| 49 DELAYED_SNAPSHOT, |
| 50 SNAPSHOT, |
| 51 }; |
| 52 |
| 53 // The aggregated URLFetcher submission data. |
| 54 struct RequestData { |
| 55 RequestData(); |
| 56 ~RequestData(); |
| 57 |
| 58 string16 mobile_id; |
| 59 GURL url; |
| 60 string16 title; |
| 61 FilePath snapshot_path; |
| 62 std::string snapshot_id; |
| 63 RequestType type; |
| 64 }; |
| 65 |
| 66 explicit ChromeToMobileService(Profile* profile); |
| 67 virtual ~ChromeToMobileService(); |
| 68 |
| 69 // content::URLFetcherDelegate methods. |
| 70 virtual void OnURLFetchComplete(const content::URLFetcher* source) OVERRIDE; |
| 71 |
| 72 // OAuth2AccessTokenConsumer methods. |
| 73 virtual void OnGetTokenSuccess(const std::string& access_token) OVERRIDE; |
| 74 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; |
| 75 |
| 76 // Get the list of mobile devices. |
| 77 const std::vector<base::DictionaryValue*>& mobiles() { return mobiles_; } |
| 78 |
| 79 // Request an updated mobile device list, request auth first if needed. |
| 80 void RequestMobileListUpdate(); |
| 81 |
| 82 // Callback with an MHTML snapshot of the profile's selected WebContents. |
| 83 void GenerateSnapshot(base::WeakPtr<Observer> observer); |
| 84 |
| 85 // Send the profile's selected WebContents to the specified mobile device. |
| 86 void SendToMobile(const string16& mobile_id, |
| 87 const FilePath& snapshot, |
| 88 base::WeakPtr<Observer> observer); |
| 89 |
| 90 private: |
| 91 // Utility function to initialize the ScopedTempDir. |
| 92 void CreateUniqueTempDir(); |
| 93 |
| 94 // Utility function to create URLFetcher requests. |
| 95 content::URLFetcher* CreateRequest(const RequestData& data); |
| 96 |
| 97 void RequestAuth(); |
| 98 void RequestSearch(); |
| 99 |
| 100 void HandleSearchResponse(); |
| 101 void HandleSubmitResponse(const content::URLFetcher* source); |
| 102 |
| 103 Profile* profile_; |
| 104 |
| 105 // A utility class for accessing the cloud print service. |
| 106 scoped_ptr<CloudPrintURL> cloud_print_url_; |
| 107 |
| 108 // The list of mobile devices retrieved from the cloud print service. |
| 109 std::vector<base::DictionaryValue*> mobiles_; |
| 110 |
| 111 // The temporary directory for MHTML snapshot files. |
| 112 ScopedTempDir temp_dir_; |
| 113 |
| 114 // Map URLFetchers to observers for reporting OnSendComplete. |
| 115 typedef std::map<const content::URLFetcher*, base::WeakPtr<Observer> > |
| 116 RequestObserverMap; |
| 117 RequestObserverMap request_observer_map_; |
| 118 |
| 119 // The OAuth2 token and retry count. |
| 120 std::string oauth2_token_; |
| 121 size_t oauth2_retry_count_; |
| 122 |
| 123 // The pending URL requests. |
| 124 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_request_; |
| 125 scoped_ptr<content::URLFetcher> search_request_; |
| 126 |
| 127 // A timer for authentication retries and mobile device list updates. |
| 128 base::OneShotTimer<ChromeToMobileService> request_timer_; |
| 129 |
| 130 DISALLOW_COPY_AND_ASSIGN(ChromeToMobileService); |
| 131 }; |
| 132 |
| 133 #endif // CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ |
OLD | NEW |