Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(136)

Side by Side Diff: chrome/browser/history/web_history_service.cc

Issue 10392084: WIP for integration between Chrome and Google Web History. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: More work. Created 8 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/history/web_history_service.h ('k') | chrome/browser/ui/webui/history_ui.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "chrome/browser/history/web_history_service.h"
6
7 #include "chrome/browser/content_settings/cookie_settings.h"
8 #include "chrome/browser/profiles/profile_dependency_manager.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/token_service.h"
11 #include "chrome/browser/signin/token_service_factory.h"
12 #include "chrome/common/chrome_notification_types.h"
13 #include "chrome/common/net/gaia/gaia_constants.h"
14 #include "chrome/common/net/gaia/gaia_urls.h"
15 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_source.h"
18 #include "net/base/load_flags.h"
19
20 namespace {
21
22 const char kWebHistoryOAuthScope[] = "https://www.googleapis.com/auth/userinfo.p rofile";
23
24 // The maximum number of retries for the URLFetcher requests.
25 const size_t kMaxRetries = 1;
26
27 // The number of hours to delay before retrying authentication on failure.
28 // FIXME: This value was just copied from ChromeToMobileService.
29 const size_t kAuthRetryDelayHours = 6;
30
31 // Used for creating and fetching a per-profile instance of the
32 // WebHistoryService.
33 class WebHistoryServiceFactory : public ProfileKeyedServiceFactory {
34 public:
35 // Get the singleton instance of the factory.
36 static WebHistoryServiceFactory* GetInstance() {
37 return Singleton<WebHistoryServiceFactory>::get();
38 }
39
40 // Get the WebHistoryService for |profile|, creating one if needed.
41 static history::WebHistoryService* GetForProfile(Profile* profile) {
42 return static_cast<history::WebHistoryService*>(
43 GetInstance()->GetServiceForProfile(profile, true));
44 }
45
46 protected:
47 // Overridden from ProfileKeyedServiceFactory.
48 virtual ProfileKeyedService* BuildServiceInstanceFor(
49 Profile* profile) const OVERRIDE {
50 // Ensure that the service is not instantiated or used if it is disabled.
51 // if (!ChromeToMobileService::IsChromeToMobileEnabled())
52 // return NULL;
53
54 return new history::WebHistoryService(profile);
55 }
56
57 private:
58 friend struct DefaultSingletonTraits<WebHistoryServiceFactory>;
59
60 explicit WebHistoryServiceFactory()
61 : ProfileKeyedServiceFactory("WebHistoryServiceFactory",
62 ProfileDependencyManager::GetInstance()) {
63 DependsOn(TokenServiceFactory::GetInstance());
64 DependsOn(CookieSettings::Factory::GetInstance());
65 }
66
67 virtual ~WebHistoryServiceFactory() {};
68
69 DISALLOW_COPY_AND_ASSIGN(WebHistoryServiceFactory);
70 };
71
72 } // namespace
73
74 namespace history {
75
76 // static
77 WebHistoryService* WebHistoryService::GetForProfile(Profile* profile) {
78 return WebHistoryServiceFactory::GetForProfile(profile);
79 }
80
81 WebHistoryService::WebHistoryService(Profile* profile)
82 : profile_(profile) {
83 // Get an access token as soon as the Gaia login refresh token is available.
84 TokenService* service = TokenServiceFactory::GetForProfile(profile_);
85 registrar_.Add(this, chrome::NOTIFICATION_TOKEN_AVAILABLE,
86 content::Source<TokenService>(service));
87 if (service->HasOAuthLoginToken())
88 RefreshAccessToken();
89 }
90
91 WebHistoryService::~WebHistoryService() {
92 }
93
94 // FIXME: This is copied from ChromeToMobileService. Could it be shared somehow?
95 void WebHistoryService::RefreshAccessToken() {
96 if (access_token_fetcher_.get())
97 return;
98
99 std::string token = TokenServiceFactory::GetForProfile(profile_)->
100 GetOAuth2LoginRefreshToken();
101 if (token.empty())
102 return;
103
104 auth_retry_timer_.Stop();
105 access_token_fetcher_.reset(
106 new OAuth2AccessTokenFetcher(this, profile_->GetRequestContext()));
107 std::vector<std::string> scopes(1, kWebHistoryOAuthScope);
108 GaiaUrls* gaia_urls = GaiaUrls::GetInstance();
109 access_token_fetcher_->Start(gaia_urls->oauth2_chrome_client_id(),
110 gaia_urls->oauth2_chrome_client_secret(), token, scopes);
111 }
112
113 void WebHistoryService::Observe(
114 int type,
115 const content::NotificationSource& source,
116 const content::NotificationDetails& details) {
117 DCHECK_EQ(type, chrome::NOTIFICATION_TOKEN_AVAILABLE);
118 TokenService::TokenAvailableDetails* token_details =
119 content::Details<TokenService::TokenAvailableDetails>(details).ptr();
120 if (token_details->service() == GaiaConstants::kGaiaOAuth2LoginRefreshToken)
121 RefreshAccessToken();
122 }
123
124 void WebHistoryService::OnURLFetchComplete(const net::URLFetcher* source) {
125 std::string data;
126 url_fetcher_->GetResponseAsString(&data);
127 url_fetcher_.reset();
128
129 query_history_callback_.Run();
130 }
131
132 void WebHistoryService::OnGetTokenSuccess(
133 const std::string& access_token) {
134 DCHECK(!access_token.empty());
135 access_token_fetcher_.reset();
136 auth_retry_timer_.Stop();
137 access_token_ = access_token;
138 }
139
140 void WebHistoryService::OnGetTokenFailure(
141 const GoogleServiceAuthError& error) {
142 access_token_fetcher_.reset();
143 auth_retry_timer_.Stop();
144 auth_retry_timer_.Start(
145 FROM_HERE, base::TimeDelta::FromHours(kAuthRetryDelayHours),
146 this, &WebHistoryService::RefreshAccessToken);
147 }
148
149 void WebHistoryService::QueryHistory(
150 const string16& text_query,
151 const QueryOptions& options,
152 CancelableRequestConsumerBase* consumer,
153 const QueryWebHistoryCallback& callback) {
154 DCHECK(!access_token_.empty());
155 GURL url("https://www.googleapis.com/oauth2/v2/userinfo");
156
157 url_fetcher_.reset(
158 content::URLFetcher::Create(url, content::URLFetcher::GET, this));
159 url_fetcher_->SetRequestContext(profile_->GetRequestContext());
160 url_fetcher_->SetMaxRetries(kMaxRetries);
161
162 url_fetcher_->SetExtraRequestHeaders("Authorization: OAuth " + access_token_ + "\r\n");
163 LOG(WARNING) << "Authorization: OAuth " + access_token_;
164 /*
165 url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SEND_COOKIES |
166 net::LOAD_DO_NOT_SAVE_COOKIES);
167 */
168
169 // url_fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES);
170 query_history_callback_ = callback;
171 url_fetcher_->Start();
172 }
173
174 } // namespace history
OLDNEW
« no previous file with comments | « chrome/browser/history/web_history_service.h ('k') | chrome/browser/ui/webui/history_ui.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698