OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/dom_distiller/webui/dom_distiller_handler.h" | 5 #include "components/dom_distiller/webui/dom_distiller_handler.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 web_ui()->RegisterMessageCallback( | 29 web_ui()->RegisterMessageCallback( |
30 "addArticle", | 30 "addArticle", |
31 base::Bind(&DomDistillerHandler::HandleAddArticle, | 31 base::Bind(&DomDistillerHandler::HandleAddArticle, |
32 base::Unretained(this))); | 32 base::Unretained(this))); |
33 web_ui()->RegisterMessageCallback( | 33 web_ui()->RegisterMessageCallback( |
34 "selectArticle", | 34 "selectArticle", |
35 base::Bind(&DomDistillerHandler::HandleSelectArticle, | 35 base::Bind(&DomDistillerHandler::HandleSelectArticle, |
36 base::Unretained(this))); | 36 base::Unretained(this))); |
37 } | 37 } |
38 | 38 |
39 void DomDistillerHandler::HandleAddArticle(const ListValue* args) { | 39 void DomDistillerHandler::HandleAddArticle(const base::ListValue* args) { |
40 std::string url; | 40 std::string url; |
41 args->GetString(0, &url); | 41 args->GetString(0, &url); |
42 GURL gurl(url); | 42 GURL gurl(url); |
43 if (gurl.is_valid()) { | 43 if (gurl.is_valid()) { |
44 service_->AddToList( | 44 service_->AddToList( |
45 gurl, | 45 gurl, |
46 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, | 46 base::Bind(base::Bind(&DomDistillerHandler::OnArticleAdded, |
47 base::Unretained(this)))); | 47 base::Unretained(this)))); |
48 } else { | 48 } else { |
49 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 49 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
50 } | 50 } |
51 } | 51 } |
52 | 52 |
53 void DomDistillerHandler::HandleSelectArticle(const ListValue* args) { | 53 void DomDistillerHandler::HandleSelectArticle(const base::ListValue* args) { |
54 std::string entry_id; | 54 std::string entry_id; |
55 args->GetString(0, &entry_id); | 55 args->GetString(0, &entry_id); |
56 | 56 |
57 // TODO(nyquist): Do something here. | 57 // TODO(nyquist): Do something here. |
58 } | 58 } |
59 | 59 |
60 void DomDistillerHandler::HandleRequestEntries(const ListValue* args) { | 60 void DomDistillerHandler::HandleRequestEntries(const base::ListValue* args) { |
61 base::ListValue entries; | 61 base::ListValue entries; |
62 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); | 62 const std::vector<ArticleEntry>& entries_specifics = service_->GetEntries(); |
63 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); | 63 for (std::vector<ArticleEntry>::const_iterator it = entries_specifics.begin(); |
64 it != entries_specifics.end(); | 64 it != entries_specifics.end(); |
65 ++it) { | 65 ++it) { |
66 const ArticleEntry& article = *it; | 66 const ArticleEntry& article = *it; |
67 DCHECK(IsEntryValid(article)); | 67 DCHECK(IsEntryValid(article)); |
68 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); | 68 scoped_ptr<base::DictionaryValue> entry(new base::DictionaryValue()); |
69 entry->SetString("entry_id", article.entry_id()); | 69 entry->SetString("entry_id", article.entry_id()); |
70 std::string title = (!article.has_title() || article.title().empty()) | 70 std::string title = (!article.has_title() || article.title().empty()) |
71 ? article.entry_id() | 71 ? article.entry_id() |
72 : article.title(); | 72 : article.title(); |
73 entry->SetString("title", title); | 73 entry->SetString("title", title); |
74 entries.Append(entry.release()); | 74 entries.Append(entry.release()); |
75 } | 75 } |
76 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); | 76 web_ui()->CallJavascriptFunction("domDistiller.onReceivedEntries", entries); |
77 } | 77 } |
78 | 78 |
79 void DomDistillerHandler::OnArticleAdded(bool article_available) { | 79 void DomDistillerHandler::OnArticleAdded(bool article_available) { |
80 // TODO(nyquist): Update this function. | 80 // TODO(nyquist): Update this function. |
81 if (article_available) { | 81 if (article_available) { |
82 HandleRequestEntries(NULL); | 82 HandleRequestEntries(NULL); |
83 } else { | 83 } else { |
84 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); | 84 web_ui()->CallJavascriptFunction("domDistiller.onArticleAddFailed"); |
85 } | 85 } |
86 } | 86 } |
87 | 87 |
88 } // namespace dom_distiller | 88 } // namespace dom_distiller |
OLD | NEW |