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.
5 #include "google_apis/drive/gdata_wapi_requests.h"
7 #include "base/location.h"
8 #include "base/sequenced_task_runner.h"
9 #include "base/task_runner_util.h"
10 #include "base/values.h"
11 #include "google_apis/drive/gdata_wapi_parser.h"
12 #include "google_apis/drive/gdata_wapi_url_generator.h"
14 namespace google_apis
{
18 scoped_ptr
<ResourceEntry
> ParseResourceEntry(const std::string
& json
) {
19 scoped_ptr
<base::Value
> value
= ParseJson(json
);
20 return value
? ResourceEntry::ExtractAndParse(*value
) :
21 scoped_ptr
<ResourceEntry
>();
26 GetResourceEntryRequest::GetResourceEntryRequest(
27 RequestSender
* sender
,
28 const GDataWapiUrlGenerator
& url_generator
,
29 const std::string
& resource_id
,
30 const GURL
& embed_origin
,
31 const GetResourceEntryCallback
& callback
)
32 : UrlFetchRequestBase(sender
),
33 url_generator_(url_generator
),
34 resource_id_(resource_id
),
35 embed_origin_(embed_origin
),
37 weak_ptr_factory_(this) {
38 DCHECK(!callback
.is_null());
41 GetResourceEntryRequest::~GetResourceEntryRequest() {}
43 GURL
GetResourceEntryRequest::GetURL() const {
44 return url_generator_
.GenerateEditUrlWithEmbedOrigin(
45 resource_id_
, embed_origin_
);
48 void GetResourceEntryRequest::ProcessURLFetchResults(
49 const net::URLFetcher
* source
) {
50 GDataErrorCode error
= GetErrorCode();
54 base::PostTaskAndReplyWithResult(
55 blocking_task_runner(),
57 base::Bind(&ParseResourceEntry
, response_writer()->data()),
58 base::Bind(&GetResourceEntryRequest::OnDataParsed
,
59 weak_ptr_factory_
.GetWeakPtr(), error
));
62 RunCallbackOnPrematureFailure(error
);
63 OnProcessURLFetchResultsComplete();
68 void GetResourceEntryRequest::RunCallbackOnPrematureFailure(
69 GDataErrorCode error
) {
70 callback_
.Run(error
, scoped_ptr
<ResourceEntry
>());
73 void GetResourceEntryRequest::OnDataParsed(GDataErrorCode error
,
74 scoped_ptr
<ResourceEntry
> entry
) {
76 error
= GDATA_PARSE_ERROR
;
77 callback_
.Run(error
, entry
.Pass());
78 OnProcessURLFetchResultsComplete();
81 } // namespace google_apis